HomeGenie Forum
Automation Program Plugins and Wizard Scripting => Help => Topic started by: nolio on November 23, 2014, 11:21:44 PM
-
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 ! :)
return (Program.WithName("Security Alarm System").Parameter("HomeGenie.SecurityTriggered").Value == "1");
////////////////////// 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);
-
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.
-
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
-
hi nolio,
probably because you're constantly querying modules in the trigger code with this:
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:
return true;
and by adding an event listener to the program code (Code to Run):
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.
-
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 :
var alarmTriggeredLogic = new Func<bool>(()=>{
// this code will be executed when the alarm is triggered; put your code logic here...
return true;
});
Before
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 ?
return true;
;)
Bye
-
Having said that on powerful cpus than RPi you could probably use the old code too, trigger code is also for using Program.Setup (http://www.homegenie.it/docs/doxy/de/d1f/class_home_genie_1_1_automation_1_1_scripting_1_1_program_helper.html#aba65b477efba06ac22a4f908881bbece), 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.
-
Hi,
Ok that seems more clear when use the trigger code.
Thanks again.
Bye