HomeGenie Forum
Automation Program Plugins and Wizard Scripting => Help => Topic started by: domotica_user on September 08, 2015, 01:18:15 PM
-
Dear all,
I have a question: i have a custom piece of hardware for switching my lights in the house. This device has a serial console and i can issue commands like: "switch on light 25". And receive information like: "light 15 has been switched on". I created this interface myself, so i can adapt this "protocol" to whatever is most convenient for HomeGenie.
I want to interface this system to HomeGenie. It is my understanding that i need to add a APP (Automation Program Plugin) for this using the Serial port helper.
Is this correct?
I suppose i have to create an APP like the "Serial Port I/O Test" http://sourceforge.net/p/homegenie/discussion/csharpscripting/thread/69ed4523/ (http://sourceforge.net/p/homegenie/discussion/csharpscripting/thread/69ed4523/)
Is this correct?
Thank you very much.
Kind regards,
Jeroen
-
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.
-
Thank you Gene for your answer. I now have the communication up and running, but there are a few questions i still have:
1) I see a lot of modules using c#. I am not bound to a specific language. I guess this is the best choice?
2) I see a "Trigger code" and a "Program code". I guess the trigger code is for initialisation stuff (like creating virtual modules). Should i initialize the serial port (i need to send a specific string to the serial port in order to make it work in the first place) in the trigger or program code?
3) I am still struggling a bit with the terminology and workflow. Let me try to put it in my own words and please correct me if i am wrong.
First i create a "program". This program is the "driver" for my serial hardware. It contains e.g. the parsing of the incoming serial strings.
Within my specific program there are two mechanisms at work:
- a serial-port receive event (updating the status of the widget). This is the interface with the serial hardware.
- When.WebServiceCallReceived for transmitting to the serial port when the switch-widget is clicked. This is the interface with the web-interface.
This module spawns, say, 5 switches when started. In mylights, groups i can now add a "switch" widget and assign this widget to one of the 5 switches.
A widget is just a GUI item (a combination of html and css and javascript).
4) Is there a good example of how to update a widget from within the "program code"? How does the program code know i needs to update "Switch 4"?
5) I see it is possible to assign configuration items to modules (in my case e.g.: serialport number). Is it also possible to assign configuration items to the spawned switches? In other words: i have a module with 5 switches, but every switch has a specific address (not related to 1,2,3,4,5). Can i add a configuration item "light address" to every switch-instance?
That is enough questions for now. I have evaluated quite some domotics suites (Pilight, Pimatic, Domoticz), but i am really enthusiastic about Homegenie. Not only does it LOOK great. It incorporates some things i value greatly (separation of GUI and logic, plugable modules, responsive gui, cross platform). This is looking good so far.
Thanks for your answers already.
-
1) You can choose your preferred language. Javascript is also a good option I guess.
2) right, the trigger code is meant to be a Setup/Initialization code but only for things regarding the program itself (like program options, features)
3-4) the widget is updated automatically when you call the Program.RaiseEvent method (see example above)
5) to assign configuration to the modules you can use "Program.AddFeature" method
g.
-
Thank you Gene. I am now starting implementing everything (i have some hopes of having my first version ready tonight). In the meantime i have two more questions:
1) In the widget editor, if i go to the homegenie/generic/switch, i can assign this widget to a module. But this widget is used for multiple switches. What is this binding to a specific module? (In the group editor i have multiple instances of the switch bound to the correct module instance)
2) I started the module with the module domain "MyLight" and i later on i renamed it to "Lightxyz". Now i have 5 virtual modules Mylight:1-5 and 5 virtual modules Lightxyz:1-5. How can i remove the old Mylight virtual modules?
Thanks again.
-
After a few hours of coding i now have the serial parsing correct. That is great, but now i need to connect the hardware to the virtual switches on the hg dashboard.
I looked at the MiLight program and i noticed a few things:
3) If the MiLight is controlled from outside hg the dashboard is not updated right? I could do that by receiving the serial signals and then calling Program.RaiseEvent() from within my serial parsing function?
4) In the MiLight source i see a "When.ModuleParameterChanged()" and a "When.WebServiceCallReceived()" routine. Both of them contain "Control.On" and "Control.Off" switch cases. When is what part used?
Thank you again for your answers!
-
Look at that topic : http://www.homegenie.it/forum/index.php?topic=975.msg5938#msg5938 (http://www.homegenie.it/forum/index.php?topic=975.msg5938#msg5938) for question 4.
Chères
Dani