more G-Labs products

Author Topic: Script and module become "Module not available"  (Read 2382 times)

November 23, 2014, 11:21:44 PM
Read 2382 times

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
Hi,

I try the following script and aleatory on the web interface become "module not available". I don't know why and it's very weird. So i try to activate and desactivate several times this script and it "work" all the time ...
If someone as a idea why, it will be super !  :)

  • Trigger
Code: [Select]
return (Program.WithName("Security Alarm System").Parameter("HomeGenie.SecurityTriggered").Value == "1");
  • Program
Code: [Select]
////////////////////// Send SMS //////////////////////
var remoteserversms = "x.x.x.x"; //@IP of your server with python script
var tel1 = "xxxxx"; // Your phone number
var tel2 = "xxxxx"; // Your phone number
//////////////////// 1st SMS /////////////////////
TcpClient
   .Service( remoteserversms ) // server
   .Connect( xxxx ); // port of python script
TcpClient.SendMessage(tel1+"::// Alarm // "+Program.WithName("Security Alarm System").Parameter("HomeGenie.SecurityTriggerSource").Value+"//");
Pause(1);
TcpClient.Disconnect();
Pause(1);
//////////////////// 2nd SMS /////////////////////
TcpClient
   .Service( remoteserversms ) // server
   .Connect( xxxx ); // port of python script
TcpClient.SendMessage(tel2+"::// Alarm // "+Program.WithName("Security Alarm System").Parameter("HomeGenie.SecurityTriggerSource").Value+"//");
Pause(1);
TcpClient.Disconnect();
Pause(1);
Net.WebService("http://xxx/xxx").Call();
Pause(1);

    November 25, 2014, 10:51:59 PM
    Reply #1

    Johnny H

    • **
    • Information
    • Jr. Member
    • Posts: 47
    Hi,

    I've noticed the same issue also, sometimes my light modules will be unavailable, and have to either restart HG or go back/forth from configure then they will re-appear

    I suspect this is a bug.

    November 29, 2014, 03:33:11 PM
    Reply #2

    nolio

    • *****
    • Information
    • Global Moderator
    • Posts: 544
    Hi,

    I check the ressource of HG server.
    With the script disable : the usage of cpu arround 10%
    With the script enable : the usage of cpu arround 30%

    But i don't really understand why, because the script shouldn't be launch (except if alarm is triggered). So why does it use so much CPU ...

    Bye

    November 29, 2014, 03:59:13 PM
    Reply #3

    Gene

    • *****
    • Information
    • Administrator
    • Posts: 1472
    • Tangible is the future!
      • Yet Another Programmer
    hi nolio,

    probably because you're constantly querying modules in the trigger code with this:
    Code: [Select]
    return (Program.WithName("Security Alarm System").Parameter("HomeGenie.SecurityTriggered").Value == "1");

    Instead, it is strongly recommended that you use an event based model by changing your trigger code to:
    Code: [Select]
    return true;

    and by adding an event listener to the  program code (Code to Run):
    Code: [Select]
    When.ModuleParameterChange( (module, parameter) =>
    {
        if (module.Is("Security Alarm System") && parameter.Is("HomeGenie.SecurityTriggered") && parameter.Value == "1")
        {
            // alarm was triggered...
            alarmTriggeredLogic();
            return false;
        }
        return true;
    });

    var alarmTriggeredLogic = new Func<bool>(()=>{
        // this code will be executed when the alarm is triggered; put your code logic here...
        return true;
    });
    Program.GoBackground();

    Cheers,
    g.
    « Last Edit: November 29, 2014, 04:00:58 PM by Gene »

    November 29, 2014, 09:55:44 PM
    Reply #4

    nolio

    • *****
    • Information
    • Global Moderator
    • Posts: 544
    Thanks Gene for the advise. I try and it work fine. I only do a little modification on program code (because a compilation error about "alarmTriggeredLogic" variable and declaration). This part :
    Code: [Select]
    var alarmTriggeredLogic = new Func<bool>(()=>{
        // this code will be executed when the alarm is triggered; put your code logic here...
        return true;
    });
    Before
    Code: [Select]
    When.ModuleParameterChange( (module, parameter) => ..........
    I have some other C# script that use my initial method in trigger code (test value of module) but they work fine.
    Your advise is to use this method all the times in all case ? Does that mean that the trigger code is always ?
    Code: [Select]
    return true; ;)

    Bye

    November 29, 2014, 10:29:58 PM
    Reply #5

    Gene

    • *****
    • Information
    • Administrator
    • Posts: 1472
    • Tangible is the future!
      • Yet Another Programmer
    Having said that on powerful cpus than RPi you could probably use the old code too, trigger code is also for using Program.Setup, or for any other stuff (eg. checking other programs parameters or global settings) that won't do any time-consuming task.
    It has to be quick and efficient in most cases.

    Cheers,
    g.

    November 30, 2014, 01:33:21 PM
    Reply #6

    nolio

    • *****
    • Information
    • Global Moderator
    • Posts: 544
    Hi,
    Ok that seems more clear when use the trigger code.
    Thanks again.
    Bye