Callback in C#

Moderator: Telldus

Zeko
Posts: 16
Joined: Fri Mar 17, 2023 9:45 am
Contact:

Callback in C#

Post by Zeko »

I have received a Duo a couple of days ago and has performed some initial testing.
There was no problem to turn on/off Nexa devices and the physical package with the self explaning light is quite nice, but when it came to receiving signals it became a little bit trickier.

I've tried the following code:

//Import function
[DllImport("TelldusCore.dll")]
public static extern int tdRegisterRawDeviceEvent(TDRawEventCallBack eventFunction, IntPtr context);

//Create the delegate
public delegate void TDRawEventCallBack(RawEvent ev);

//Register callback
TDRawEventCallBack del = new TDRawEventCallBack(RawEvent_Occured);
tdRegisterRawDeviceEvent(del, new IntPtr(1));

void RawEvent_Occured(RawEvent e)
{
//Some code
}

When I run the code and press one of the Nexa remote buttons (self learning) nothing triggers the RawEvent_Occured. Is there something I have missed? Shouldn't the RawEventCallback be triggered when a non-configured device status is changed, for example by a remote?
moryoav
Posts: 33
Joined: Fri Mar 17, 2023 9:45 am

Post by moryoav »

Where / How do you declare RawEvent?
Zeko
Posts: 16
Joined: Fri Mar 17, 2023 9:45 am
Contact:

Post by Zeko »

Hi,
It's declared in the class "TellStick" (from project TellStickNet wrapper)

public struct RawEvent
{
public string data;
public int callbackId;
public IntPtr context;
}
moryoav
Posts: 33
Joined: Fri Mar 17, 2023 9:45 am

Post by moryoav »

Just for the record, I have also tried tdRegisterDeviceEvent and registered it to a specific device that I set-up to be "remote-sensitive".
It does work in TelldusCenter (you click on the button in the remote and the icon in TelldusCenter changes from on to off and vice versa).
It doesn't work in my C# code though...

I tried the following code:

Code: Select all

// the enum part
public struct DeviceEvent
        {
            public int deviceId;
            public TellStickMethods method;
            public string data;
            public int callbackId;
            public IntPtr context;
        }

// create the delegate
public delegate void TDDeviceEventCallBack(DeviceEvent ev);

// import function
[DllImport("TelldusCore.dll")] public static extern int tdRegisterDeviceEvent(TDDeviceEventCallBack eventFunction, IntPtr context);

// registering the callback in the constructor of my Tellstick class.
TDDeviceEventCallBack del2 = new TDDeviceEventCallBack(DeviceEvent_Occured);
tdRegisterDeviceEvent(del2, new IntPtr(1));

// the event
void DeviceEvent_Occured(DeviceEvent e)
{
        MessageBox.Show("Device Event Occured!");
} 
Zeko
Posts: 16
Joined: Fri Mar 17, 2023 9:45 am
Contact:

Post by Zeko »

It does work in TelldusCenter (you click on the button in the remote and the icon in TelldusCenter changes from on to off and vice versa).
How did you know which housecode to use in Telldus Center for this device when Telldus Duo only has support for selfLearning? eVentTool?
moryoav
Posts: 33
Joined: Fri Mar 17, 2023 9:45 am

Post by moryoav »

Zeko wrote:How did you know which housecode to use in Telldus Center for this device when Telldus Duo only has support for selfLearning? eVentTool?
0. Program your remote to turn on a device (regular coupling, without the computer).
1. You stop the Telldus service (in services.msc) [I'm still not sure why you need this step, but you do, it doesn't work otherwise].
2. You load eVentTool.
3. You press the button in the remote that you defined in step (0) - eVentTool will tell you the housecode and the unit code for each button you press.
4. You start Telldus service again and load TelldusCenter.
5. Add a new [in my case Nexa] On/Off device with the codes you written down in step (3).
6. In my case at this point TelldusCenter crashes and even if you reload it, nothing works again. I had to reboot here. And then it wanted me to reinstall the driver. Not sure if this is a bug or some other non-related problem I had.
7. That's it... it works. You press a button in the remote, the lights go on/off and the icon in telldus center changes accordingly.

The C# event, however, is not triggered... :(
naslund
Posts: 97
Joined: Fri Mar 17, 2023 9:45 am
Location: Täby

Post by naslund »

Följer tråden med intresse
Jag har inte heller fått eventet att funka, kör i dag med timer som kollar om det finns något i bufferten. Funkar bra till mina små tester men det är ju inte snyggt.

Någon som har någon bra idé om hur man snyggt kan identifiera att ett helt meddelande ligger i bufferten? Eller är det att leta <CR> som gäller?
krambriw
Posts: 654
Joined: Fri Mar 17, 2023 9:45 am

Post by krambriw »

I have similar problems (with python however). If it would work correctly, you should get the event via callbacks. Your workaround (polling) should be avoided but I understand that you use it currently. What language are you using? Sample on how to read the buffer??? I could do the same in python as a starter

I wonder if Telldus is supporting this discussion at all. So far no input...but it would be the best place so that we all can share experience on this

Walter
moryoav
Posts: 33
Joined: Fri Mar 17, 2023 9:45 am

Post by moryoav »

Are we sure about the definition of "struct DeviceEvent" ? according to the email Micke sent:
DeviceEvent:
This event is fired when the state of a device changes. This can either occur via a remote control, but can as well occur via another software on the computer.

Parameters:
int deviceId - The device id of the device that changed.
int method - The new state. Can be TELLSTICK_TURNON, TELLSTICK_TURNOFF etc.
const char *data - For some methods this contains data. For TELLSTICK_DIM this hold the current value.
Zeko
Posts: 16
Joined: Fri Mar 17, 2023 9:45 am
Contact:

Post by Zeko »

Are we sure about the definition of "struct DeviceEvent" ?
Guess not! One more thing, I cant find any info regarding struct for RawEvent in the mail.
krambriw
Posts: 654
Joined: Fri Mar 17, 2023 9:45 am

Post by krambriw »

I looked here in the trunk to (try to) understand

http://developer.telldus.se/browser/tru ... core.h#L25

Code: Select all

typedef void (WINAPI *TDDeviceEvent)(int deviceId, int method, const char *data, int callbackId, void *context);
typedef void (WINAPI *TDDeviceChangeEvent)(int deviceId, int changeEvent, int changeType, int callbackId, void *context); 
typedef void (WINAPI *TDRawDeviceEvent)(const char *data, int callbackId, void *context);
moryoav
Posts: 33
Joined: Fri Mar 17, 2023 9:45 am

Post by moryoav »

It seems fine then. I can't seem to find the problem with our implementation... Unless we figure something out, we'll be needing Telldus' help here to continue...
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Post by micke.prag »

We have discovered, with the help of krambriw, some issues when using Python. I don't know if it also applies for C#?

The problem is the communication between telldus-core.dll and TelldusService. You can recreate the problem this way:

1) Start TelldusCenter and make sure it can detect events from your remote.
2) Keep TelldusCenter running and launch your C# app which tries to register for the events.

Is TelldusCenter still able to detect events from the remote?
Micke Prag
Software
Telldus Technologies
krambriw
Posts: 654
Joined: Fri Mar 17, 2023 9:45 am

Post by krambriw »

Start of test:

To begin, Telldus Center works as expected and status changes are reflected in the GUI when using the remote (I'm using a Nexa self learning remote)

I then start my application (EventGhost/python in this case) and from EG I turn another Nexa device on or off. The command works fine but after this, Telldus Center stops showing status changes made by the remote mentioned above. My application still works fine and I can continue to control devices from there

It seems that the first communication made from the application to the dll will block the status update in Telldus Center

A short time (a couple of seconds) after I closed my application, Telldus Center started to show status changes made by the remote again but then stopped doing this again

To make Telldus Center work again, you will have to restart the Telldus Service

edit: It can happen that it is enough to close your application to make Telldus Center start working again...
Last edited by krambriw on Thu Jun 10, 2010 3:16 pm, edited 1 time in total.
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Post by micke.prag »

Yes, this is exactly the problems I have discovered.

If someone could do the same tests with an C# application it would be great to see if it is the same problem there or something else.
Micke Prag
Software
Telldus Technologies
Post Reply