The Serial I/O program example is already available from Programs->Devices and Things group.
After testing the communication you can go further by:
- adding virtual modules for your lights
- adding the handler for standard commands (on, off, level)
for example:
// create 30 dimmer light modules (from 1 to 30)
Program.AddVirtualModules("HomeAutomation.MyLights", "Dimmer", "homegenie/generic/dimmer", 1, 30);
var myModules = Modules.InDomain("HomeAutomation.MyLights");
// handle standard API calls
When.WebServiceCallReceived("HomeAutomation.MyLights", ( args ) => {
string[] reqs = ((string)args).Split('/');
try
{
string lightnumber = reqs[1];
string command = reqs[2];
string parameter = ""; if (reqs.Length > 3) parameter = Uri.UnescapeDataString(reqs[3]);
//
var module = myModules.WithAddress(lightnumber).Get();
//
switch(command)
{
// eg. http://<hg_address>/api/HomeAutomation.MyLights/<light_number>/Control.On
case "Control.On":
SerialPort.SendMessage("switch on light " + lightnumber);
Program.RaiseEvent(module, "Status.Level", 1, "My Light");
break;
case "Control.Off":
SerialPort.SendMessage ..... // switch off
Program.RaiseEvent(module, "Status.Level", 1, "My Light");
break;
case "Control.Toggle":
if (module.Parameter("Status.Level").DecimalValue == 0)
{
SerialPort.SendMessage ..... // switch on
Program.RaiseEvent(module, "Status.Level", 1, "My Light");
}
else
{
SerialPort.SendMessage ..... // switch off
Program.RaiseEvent(module, "Status.Level", 0, "My Light");
}
break;
case "Control.Level":
SerialPort.SendMessage ..... // set level
Program.RaiseEvent(module, "Status.Level", parameter, "My Light");
break;
}
//
return "{ 'ResponseValue' : 'OK' }";
} catch (Exception e) {
Program.Notify("My Light ERROR!", e.Message);
}
return "{ 'ResponseValue' : 'ERROR' }";
});
g.