Home AMX User Forum Duet/Cafe Duet

Why is my Duet initiated channel event firing twice

I have written a module that turns on/off channels for monitoring by the NetLinx program. The channel events are getting fired twice. Why? How can I prevent the duplicate?

Duet Code:
String dvVirtualStr = this.getProperty("Duet-Device");
DPS dps = new DPS(dvVirtualStr);
dvVirtual = new NetLinxDevice(dps, false);


NetLinx Code:

ON: {
integer chan;
chan = channel.channel
send_string 0, "'Virtual Projector Channel ', itoa(chan), ' ON'"
OFF: {
integer chan;
chan = channel.channel;
send_string 0, "'Virtual Projector Channel ', itoa(chan), ' OFF'"

Line 10 (11:13:57):: Virtual Projector Channel 842 ON
Line 13 (11:13:57):: Virtual Projector Channel 842 ON
Line 14 (11:13:57):: Virtual Projector Channel 843 OFF
Line 15 (11:13:57):: Virtual Projector Channel 843 OFF


  • Options
    AMXJeffAMXJeff Posts: 450
    I would say you are causing it by turning on the channels via the netlinxDevice you created. Once via the device and the second via the snapiRouter, which is managing the device. For sending messages from your duet module through the snapiRouter you should be using code like this for channel feedback.
    private void sendAdvancedChannelFeedback(int channel, int port, boolean b)
    	if (this.isDataInitialized() == false) return;
        	Event e = new Event();
        	e.type = b ? Event.E_ON : Event.E_OFF;
        	e.idx = channel;                                                
        	AdvancedEvent advEv = new AdvancedEvent(new Boolean(b), null);
        	processAdvancedEvent(new ModuleComponentEvent(this,advEv,e,port));
    sendAdvancedChannelFeedback(842, 1, true); // channel on
    sendAdvancedChannelFeedback(842, 1, false); // channel off

    Good Luck!

  • Options
    PhreaKPhreaK Posts: 966
    ^^ what he said.

    For any interaction you have with the virtual device associated with your module build an AdvancedEvent and process that. It will allow any listeners to be alerted and make sure everything is handled neatly with the interaction between Duet and NetLinx.
  • Options
    Thank you very much for your help. That is just what I needed.

    I have not been through the Duet training yet, but we intend to set up a class soon. That said, due to the minimal documentation, it's all been trial and error for me. This solution brought up another question.

    Rather than just returning true from the respective methods, is there a way to test for isDeviceOnline and isDataInitialized?

  • Options
    PhreaKPhreaK Posts: 966
    Handling whether the module is reporting online and/or initialised is entirely up to you as these values should reflect your module state (isDeviceOnline being that the device is talking and isDataInitialized reflecting your module's synchronisation with the device state). That's why you must provide the implementation for those methods.

    One thing to note is that rather than just using a couple of boolean's and flicking these as your state changes you will need to process the appropriate online / initialised event for the channels to flick on the virtual device. Here's a little helper class that might help out - https://gist.github.com/4506054.
  • Options
    AMXJeffAMXJeff Posts: 450
    jimboat wrote: »
    Rather than just returning true from the respective methods, is there a way to test for isDeviceOnline and isDataInitialized?

    Like Phreak said...

    Your isDeviceOnline method should only return true when you are getting responses from the device you are controlling. When you get three failed responses, your method should return false.

    Your isDataInitialized method should return true when you have successfully got the complete status from the controlled device for every function you are controlling. Reset it to false when you lose communication with the controlled device.

    You must use the "processDeviceOnLineEvent" and "processDataInitializedEvent" message handlers to set the channels (251, 252) through SNAPI. Call them when the state actually changes.
    this.processDataInitializedEvent(new ModuleComponentEvent(this, state, 1));
    this.processDeviceOnLineEvent(new ModuleComponentEvent(this, state, 1))
Sign In or Register to comment.