more G-Labs products

Author Topic: Virtual Modules and parameters  (Read 3114 times)

September 19, 2015, 03:16:47 PM
Read 3114 times

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
How can i found a virtual module by parameter value?

September 19, 2015, 07:52:22 PM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Could you give an example of what you're trying to achieve? I'm not sure what you mean by finding a module by its parameter value.

September 19, 2015, 09:42:46 PM
Reply #2

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
I get some code here in the forum to get serial port and virtual modules and it works, i can add 4 modules for 4 sensors. In the moment i can update the modules but his names, with WithName method, but isn't a friendly name, in fact is a code that don't say nothing about the sensor, so i wich i can use the name to indentify the local, for instance room, garage, etc and the add a parameter that will be the sensor id.  (i manage to do that) but i can't find a way to then find the correct module to update it, because i can't get the parameter value...

September 20, 2015, 07:23:19 AM
Reply #3

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
OK, now I understand. In the first version I created for RfxCom devices I encountered the same problem, I was giving my Virtual modules the cryptic Id given by the FrxCom device as the Name. Instead you should use the Id property of the Virtual module and give the module its name later in the groups configuration.

Here's a quick list of steps of how I ended up doing it. Look at CptJacks MySensor code for a good example of checking for the existence and creating Virtual modules.

1: Create the virtual Module in your APP with the following (pseudo) code:
Code: [Select]
Program.AddVirtualModule("RfxCom", "IdGivenFromRfxCom", "Sensor", "homegenie/generic/sensor");
2: No go to Configure -> Groups .
3: Create a new group or open an existing one.
4: Add the new Module via Actions -> Add new module
5: You should be able to find the new module in the list. (Sometimes new ones take a few minutes to show up.)
6: When you've selected the module, a new window opens up giving you the option to give it a name.

This is the name you can then use to acces your module from another program/App.

eg:
Code: [Select]
When.ModuleParameterChanged( (module, parameter) =>
{                             
if (module.Is("Attic Multisensor"))
{         
if(parameter.Name == "Sensor.MotionDetect" && parameter.Value == "1")
{
// Do something
return false;
}
}
return true;
});

Hope that helps, if I've misunderstood your goal please let me know.
« Last Edit: September 20, 2015, 12:47:01 PM by mvdarend »

September 20, 2015, 12:16:44 PM
Reply #4

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Sorry but i don't understand... i talk about module id, but how can i set that module id?

The last piece of code you put, if i understand correct will run when module parameter change, but hoe can i make some change in the module if i can't find him in the first place?

September 20, 2015, 12:54:40 PM
Reply #5

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Could you show the code you are using now to create the modules? When you create one the second parameter is the ID:

http://www.homegenie.it/docs/doxy/a00007.html#ad74a2a82101dd80e17244fd03eaf181f

September 20, 2015, 01:17:50 PM
Reply #6

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Code: [Select]
Program.Setup(()=>{
 
Program.AddFeature("HomeAutomation.RFX433.th", "Sensor", "RFX433.th.DeviceId", "Device ID","text");

  Program.AddVirtualModules("HomeAutomation.RFX433.th", "Sensor", "homegenie/generic/sensor", 1, 6);

});

I want to use that field, device id to identify the module, and not the module name.
« Last Edit: September 20, 2015, 01:25:05 PM by codedmind »

September 20, 2015, 01:39:06 PM
Reply #7

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Oh, I see now. In this case, the ID's are 1,2,3,4,5,6, so to get the Module with Id 3 you can use the following code:

Code: [Select]
var module = Modules.InDomain("HomeAutomation.RFX433.th").WithAddress("3").Get();
Or you can get it by its name that you have set via Configure -> Groups . In this example the sensor is called "GardenSensor"

Code: [Select]
var module = Modules.InDomain("HomeAutomation.RFX433.th").WithName("GardenSensor").Get();
I still prefer CptJacks MySensor example for creating Virtual modules though.

September 20, 2015, 01:41:48 PM
Reply #8

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
The problem is that i get the device id from serial read, so i never know what sensor i'm reading until i get it from serial, so then i must find it by their id, i don't know if the device id XPTO will by in module 1,2 3 etc..

September 20, 2015, 01:54:54 PM
Reply #9

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
The problem is that i get the device id from serial read, so i never know what sensor i'm reading until i get it from serial
Then you should really use the code example given by CptJack in the MySensor example. In the other thread you mention the following string:

XXXXXXXXXXXXXXXXXXXXXXXXXX;Id:82;Channel:2;bate:90%;temp:17

Once you've parsed the values from this string you can use them in the following way:

Code: [Select]

var unitId= [Id from string]; // 82
var myHumidity = [Bate from string]; // 90
var myTemperatue= [Temp from string]; // 17

// Try to get the corresponding Virtual Module
var module = Modules.InDomain("HomeAutomation.RFX433.th").WithAddress(unitId).Get();
 if (module.Instance == null)
{
  // Module not found, create it
  Program.AddVirtualModule("HomeAutomation.RFX433.th", unitId, "Sensor", "homegenie/generic/sensor");
  module = Modules.InDomain("HomeAutomation.RFX433.th").WithAddress(unitId).Get();
}

 // Set the values
module.Parameter("Sensor.Temperature").Value = myTemperatue;
Program.RaiseEvent(module, "Sensor.Temperature", myTemperatue, "");
module.Parameter("Sensor.Humidity").Value = myHumidity ;
Program.RaiseEvent(module, "Sensor.Humidity", myHumidity , "");

Code is untested by the way :)

So, instead of creating 6 new modules with AddVirtualModules, just create them with  AddVirtualModule when necessary.

September 21, 2015, 09:19:13 PM
Reply #10

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Great!!! That works very well.

If you allow me, with this is possible i add more parameters to the module right?
Could be possible add batery charge to the widget? I have the value...

September 21, 2015, 09:27:09 PM
Reply #11

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Could be possible add batery charge to the widget? I have the value...
Is that what bate is for? I had no idea, so I made it a humidity level...  ;D 

Battery level can be set with Status.Battery (I think), I've never used battery level myself, but I expect you can set it like this:
Code: [Select]
module.Parameter("Status.Battery").Value = bate;
Program.RaiseEvent(module, "Status.Battery", bate, "");

September 21, 2015, 09:34:08 PM
Reply #12

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Makes sense... i missing some docs, i have searching in the helper class but don't have much info, and i struggle. I will try it, thanks.