more G-Labs products

Author Topic: HG interface --> widget  (Read 867 times)

November 01, 2016, 04:18:44 PM
Read 867 times

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I currently have an enum in my code which maps states to the value I expect..

For example

0 = Off
1 = On
2 = Turning On
3 = Turning Off.

Should I store these values in status .level, and then how do I map the enum structure back to text in a widget..

IE do i just do:

if x == 0 then "Label text Off"
if x == 1 then "Label text On"
if x == 2 then "Label text turning on"

More to the point, any examples?

November 02, 2016, 10:59:56 AM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
But enums would be nice in HG, not sure why they're not supported.

I prefer using a switch if there is more than two or three options, ie:

Code: [Select]
switch(x){
  case  0:
    "Label text Off"
    break;
  case  1:
    "Label text On"
    break;
  case  2:
    "Label text turning on"
    break;
  default:
    "Label text unknown value"
    break;
}

The syntax is the same for C# and Javascript.
« Last Edit: November 02, 2016, 11:02:15 AM by mvdarend »

November 02, 2016, 11:14:48 AM
Reply #2

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
thanks, yeah I know about switch, it was just a pseudo example :) enums aren't officially supported but I do wonder if they could be by moving some of the code from the appfactory.cs code into the code editor window so you can see the anonymous methods.. keep meaning to do a fork and play, that would potentially let you do includes too..

The enum I don't need in this instance, just wonder where I should be storing these values and if there are any example widgets?

IE if is for an alarm area, I have two areas's for example, 0 and 1 named house and garage.

I add these currently then the status could be:

armed
arming
part armed
unarmed

and I want to represent these states via a widget, just really need one I can look at to understand :)

November 02, 2016, 11:30:49 AM
Reply #3

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Ah OK, I think I've got it now.

If you use the Status Widget to test with you can send the values as StatusWidget.House = 0 and StatusWidget.Garage = 1 for example.

The values should show up directly in the widget.

If you then modify the widget slightly in the widget editor (here is the GitHub source code as an example) at line 27 to something like this.

replace
Code: [Select]
var displayvalue = value;
with your version of this:
Code: [Select]
switch(value){
case 0:
  displayvalue = "Armed"
  break;
case 1:
  displayvalue = "Unarmed"
  break;
etc.
}

Untested code, but you should get the idea.

Edit: In the MySensor code you can also see how I've used the parameter list to store some custom stuff.

« Last Edit: November 02, 2016, 11:33:14 AM by mvdarend »

November 02, 2016, 08:56:40 PM
Reply #4

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Thanks.. that's helped me a lot!

erm how do I change the widget.. Adding the modules in the interface like this:

Code: [Select]
            for (var i = 1; i < alarm.Areas.Count; i++)
            {
                var moduleToAdd = new InterfaceModule
                {
                    Domain = this.GetDomain(),
                    Address = $"A{alarm.Areas[i].AreaNumber}",
                    Description = $"Texecom Area {alarm.Areas[i].AreaNumber}",
                    ModuleType = ModuleTypes.Sensor,
                    //CustomData = new { ZoneText = alarm.Zones[i].ZoneName }
                    CustomData = alarm.Areas[i]
                };
                interfaceModules.Add(moduleToAdd);
            }
            OnInterfaceModulesChanged(this.GetDomain());

But I don't seem to be able to change the widget when adding...

November 02, 2016, 09:12:52 PM
Reply #5

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Also tried as ModuleType.Generic

November 03, 2016, 07:08:52 AM
Reply #6

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
I'm not familiar with interfaceModule as I've never created a MIG interface before, I've always added virtual modules as follows:

Code: [Select]
Program.AddVirtualModule(hgDomain, myNodeId, hgType, hgWidget);for example
Quote
Program.AddVirtualModule("HomeAutomation.MySensors", 1, "Generic", "homegenie/generic/status");

Not exactly what you're looking for, but hopefully it will point you in the right direction.

You can also change the widget by changing the Widget.DisplayModule parameter.

« Last Edit: November 03, 2016, 07:18:12 AM by mvdarend »

November 03, 2016, 02:28:32 PM
Reply #7

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I found some code last night in the UPNP.cs file which has this in prior to you posting this, I haven't fully tested it yet..

Really wish MIG and Homegenie were one project.. doesn't make it easy to search for things.

David

November 03, 2016, 02:28:48 PM
Reply #8

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271

November 03, 2016, 06:42:22 PM
Reply #9

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
for reference:

Code: [Select]
OnInterfacePropertyChanged(this.GetDomain(), "Source Module Here", "Description Here", "Widget.DisplayModule", "homegenie/something/mywidget");