-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluaContext.php
executable file
·89 lines (77 loc) · 3.18 KB
/
luaContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
@nom: luacontext
@auteur: piwebpool ([email protected])
@description: lua script execution engine
*/
//
require_once ("functions.php");
include ("configuration.php");
function goLua($luaCode,$materials,$pins,&$feedback,$link,$scriptID="emptyScriptID"){
try{
// clean up all registered listeners.
// see issue #20 listeners management thru tablegear
//$sql = "delete from listeners;";
//$result = mysql_query($sql, $link);
//$lua=new Lua($file);
$lua=new Lua();
$lua->eval($luaCode);
// record names of the command as variable and manipulate them directly phyisically in lua using set and get functions
// since the setPinState function gets the logical pin number, we have to get it from the table
foreach($materials as $material=>$pin){
$luaVariables[$pin]=getPin($pins[$pin]);
$lua->assign($material, $pins[$pin]);
//echo "{material: ".$material." pin:".$pins[$pin]."}";
}
$phValue = getPh();
$orpValue = getORP();
$temperatureValue = getTemperature();
if($phValue==null) $phValue=-99;
if($orpValue==null) $orpValue=-99;
if($temperatureValue==null) $temperatureValue=-99;
$lua->assign("temperature",$temperatureValue);
$lua->assign("ph",$phValue);
$lua->assign("orp",$orpValue);
$lua->assign("period",intval(getCurrentTimeWindow()));
$lua->assign("hour",intval(getCurrentTime()));
$lua->assign("minute",intval(getCurrentMinute()));
$lua->assign("timestamp",getCurrentTimeStamp());
$lua->assign("scriptID",$scriptID);
//db related variables
$sql = "SELECT id,value from settings where userSetting=true;";
$result = mysql_query($sql, $link);
if (!$result) {
$feedback=$feedback." ".mysql_error();
return false;
}else{
while ($row = mysql_fetch_assoc($result)) {
$id=($row['id']);
$value=($row['value']);
if (is_numeric($value))
$lua->assign($id,floatval($value));
else
$lua->assign($id,$value);
//$lua->assign("parametre['".$id."']",$value);
//appendlualog(" assign(parametre['".$id."'],".$value.") ");
//appendlualog(" assign(".$id.",".$value.") ");
}
}
mysql_free_result($result);
$lua->registerCallback("set", 'setLuaPinState');
$lua->registerCallback("get", 'getLuaPin');
$lua->registerCallback("log", 'appendlualog');
$lua->registerCallback("sms", 'sendsms');
$lua->registerCallback("email", 'sendemail');
$lua->registerCallback("web", 'weburl');
$lua->registerCallback("subscribe",'registerMaterialURLCallBack');
// execute the script
$retval = $lua->run();
if (!$retval) $feedback = $feedback."Runtime error";
else $feedback = $feedback.$retval;
} catch (LuaException $e) {
$feedback=$feedback." ".$e->getMessage();
return false;
}
return true;
}
?>