Observing the status of switches

Aug 25, 2014 at 9:34 AM
Hello,

I want to write a c# method to notice the status changes of switches immediately. I know that in the codes of lab of things, in every 10 seconds the ui is refreshed and the new status of switches is updated in the ui. However, I don't want it like that. I want to control these switches in my c# code so that I can manage them immediately. Is there any way to do this? Can I use event handler or something different?

Any help will be appreciated.
Thank you in advance.
Coordinator
Aug 25, 2014 at 2:06 PM

Hi

In SwitchController.cs, the OnNotification method is called to do status changes of switches (e.g. level, color). Take a look at that method and see if you can do what you want in there.

A.J.

Aug 25, 2014 at 2:41 PM
Thank you for your reply but I think I couldn't explain my situation properly.

I examine OnNotification methot. However I see that method is used for changing the status of a switch. I mean in the ui of platform, there are some buttons to change switch status and when one of these buttons are clicked then the OnNotification method is called.

What I want to do is that in case of any change in any switch(not by clicking a button to change it's status), I want to notice that change immediately. For example, when I open a switch manually, I want my code to realize that change and save the new status of that switch.
Coordinator
Aug 25, 2014 at 4:21 PM

Hi –

Can you give me more detail on what sensors you are using or want to use? It sounds like it might ones that support the switch role and the sensor role. When you say “switch” below that sounds more like a door window sensor which implements the sensor role. If so, looking at either alert app or sensor application you will see how those apps respond immediately when the sensor state changes. (again in OnNotification) You may need a new application that merge some features of switch and alert.

Another important point is that the architecture of the code keeps the UI very simple, without state. The c# code is agnostic to how many different UI points are connecting to it and does not have a way to push state changes to UI endpoints. This is why the UI in switch (index.html) is polling the c# webservice. Another approach if you want “quick” response in the UI is to make the UI poll much more frequently by changing the 10 seconds to something smaller.

A.J.

Aug 26, 2014 at 8:51 AM
Hi,

Just now, I'm just using On/Off switches(Everspring) but in the next steps I will also use sensor(EZMotion Wireless 3 in 1 Sensor). That's why I've combined the switch and sensor applications in my own application.

Another solution in my mind is that in every refresh of the ui, sending the new switch information array to c# part and keep my controls there. I've also tried to do that but I couldn't pass a parameter via MakeServiceCall. It gives an error like "Cannot find application web service endpoints" although I follow the code in codeplex. What I've followed is:
    url2 = "webapp/GetUnconfiguredDevicesForScout";
    data2 = '{"scoutName": "' + sName + '"}';
    new PlatformServiceHelper().MakeServiceCall(url2, data2, GetUnconfiguredDeviceCallback);
I've referred this code snippets but it didn't work. Is there a way to pass an array or a parameter to my c# method?
Or do you have any suggestion for my situation?
Coordinator
Aug 27, 2014 at 2:24 PM

Hi

Apologies meant to respond yesterday and the day got away from me.

I think I still need to better understand your goal. I think I understand it to be having a combination of switches and sensors and having a UI that responds to changes (e.g. with a door opens that is reflected in the UI). If this matches the goal then I think keeping all the state in the C# part of the application you have create is the easiest and having the javascript UI poll the C# webservice to get the state will work. So I think similar to switch application you want to keep the state in the c# and query it.

The error you are describing below suggest to me that your application isn’t getting installed, but might be because the method signature isn’t matching up. Can you double check the following (these are steps on https://labofthings.codeplex.com/wikipage?title=How%20to%3a%20Create%20an%20App)

1. Verify that your application is being complied and placed in the output folder: Hub\output\binaries\pipeline\AddIns. You should see an output folder for your new application. (see Set Project Properties section on “How to: Create an App)

2. Verify you added the application to your home store catalog (See “Add Dummy2 App” section on How to: Create an App)

3. Verify you can successfully install and run the application when you install devices?

Based on your previous message I think the above are likely all true, but wanted to double check. If the application is being installed and runs correctly and the error is being thrown just when you try to call a new method that you have defined then likely something is off in the method signature. I would model on the calls in the switch or sensor application. A few more questions:

4. Are you defining a new method that you want to call?

5. If so, are you including the method signature in the ServiceContract (see end of SwitchSvc.cs) and the appropriate implementation (see middle of SwitchSv.cs) and then in file that implements the module (e.g. SwitchController.cs)

6. Can you try defining your new method without any parameters and calling that to verify it is working?

If you can zip up the code for the application you are writing and share – I can take a look at the whole thing and that context would be helpful. The parameter passing can be a bit fussy and easy for something to be off.

A.J.