-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.php
executable file
·265 lines (237 loc) · 11.3 KB
/
action.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/*
@nom: action
@auteur: piwebpool ([email protected])
@description: execute all actions from the web GUI
*/
//
// invoke with
// action.php?action=changeState&pin=11&state=1
// action.php?action=updateSetting&id=actionTableCollapse&value=1
// action.php?action=resetSchedule
//etc..
require_once('common.php');
require_once('luaContext.php');
$result = array('state'=>'undef', 'answer'=>'OK');
//appendlog("graph.php",$params,"");
if(isset($_['action'])){
switch($_['action']){
// ACTION FOR WEBAPP and PLCLINK (JSON QUERY UNDER BRACKET)
case 'calibrate':
//Cal,mid,n
//Cal,low,n
//Cal,high,n
//Cal,clear
//Cal,?
switch($_['id']){
case 'Ph':
if (!isset($_['value'])) $_['value']="7.00";
readSensor(getDevice("ph"),"Cal,clear\n");
temperatureCompensation();
// we don't exploit the answer of the clearance (should be *ok)
$frame="Cal,mid,".$_['value']."\n";
$result['state'] = readSensor(getDevice("ph"),$frame);
appendlog("CALIBRATE",$frame,json_encode($result));
break;
case 'ORP':
if (!isset($_['value'])) $_['value']="650";
readSensor(getDevice("orp"),"Cal,clear\n");
// we don't exploit the answer of the clearance (should be *ok)
$frame ="Cal,".$_['value']."\n";
$result['state'] = readSensor(getDevice("orp"),$frame);
appendlog("CALIBRATE",$frame,json_encode($result));
break;
case 'Temp':
if (isset($_['value'])){
$deltaTemp = $_['value'] - getTemperature() ;
mysql_connect($options["database"]["host"],$options["database"]["username"],$options["database"]["password"]) or die('error connection');
mysql_select_db($options["database"]["name"]) or die('error database selection');
// in case of duplicate, simply erase
$query="INSERT INTO `settings` (`id`, `value`, `userSetting`, `description`) VALUES ('tempOffset', ".$deltaTemp.", 1, 'Temperature calibration offset') ON DUPLICATE KEY UPDATE id='tempOffset',value=".$deltaTemp." ,userSetting=1,description='Temperature calibration offset'";
$outcome = mysql_query($query);
//appendlog("CalibrateLog",$query,$outcome);
if (!$outcome){
$result['answer'] = "ERROR";
$result['state'] = mysql_error();
}else
$result['answer']="Ecart retenu";
$result['state']=$deltaTemp;
}
else{
$result['state'] = "Valeur incorrecte ou indéfinie";
$result['answer'] = "ERROR";
}
appendlog("CALIBRATE","Temp",json_encode($result));
break;
default:
$result['answer']="ERROR";
$result['state']="unknown device ".$_['id'];
break;
}
break;
case 'sms':
if (!sms($_['message'])){
$result['answer']="ERROR";
}
break;
case 'getTemperature':
//case 'getTemperaturePLC':
$result['state'] = getTemperature();
if ($result['state']==false){
$result['answer']="ERROR";
$result['state']="indispo ";
}
break;
//case 'getORPPLC':
case 'getORP':
$result['state'] = getORP();
if ($result['state']==false){
$result['answer']="ERROR";
$result['state']="indispo ";
}
break;
case 'getPh':
//case 'getPhPLC':
$result['state'] = getPh();
if ($result['state']==false){
$result['answer']="ERROR";
$result['state']="indispo ";
}
break;
case 'getLog':
$result['state'] = getLog($logfilename);
break;
case 'sql':
if(isset($_['script'])){
$result['state'] = "OK";
$dbms_schema="scripts/".$_['script'];
mysql_connect($options["database"]["host"],$options["database"]["username"],$options["database"]["password"]) or die('error connection');
mysql_select_db($options["database"]["name"]) or die('error database selection');
$sql_query = @fread(@fopen($dbms_schema, 'r'), @filesize($dbms_schema)) or die("could not process ".$dbms_schema);
$sql_query = remove_remarks($sql_query);
$sql_query = split_sql_file($sql_query, ';');
foreach($sql_query as $sql){
$outcome=mysql_query($sql);
if (!$outcome){
$result['answer'] = "ERROR";
$result['state'] = mysql_error();
}
}
}else{
$result['answer'] = "ERROR";
$result['state'] = "missing script parameter";
}
break;
// restore the code from db to blockly thru ajax call
case 'getScript':
mysql_connect($options["database"]["host"],$options["database"]["username"],$options["database"]["password"]) or die('error connection');
mysql_select_db($options["database"]["name"]) or die('error database selection');
$query="select `".$_['code']."` from `scripts` WHERE `id`='".$_['id']."'";
$outcome = mysql_query($query);
if (!$outcome) {
$result['answer'] = "ERROR";
$result['state'] = $query; /* mysql_error();*/
}else{
while ($row = mysql_fetch_assoc($outcome)) {
// decode special caharacter like accent so they are sent back in the correct text format
$result['state']=html_entity_decode(($row[$_['code']]));
}
// result return "undef" in state in case no data match
mysql_free_result($outcome);
}
mysql_free_result($outcome);
break;
// store the xml and lua code generated by blockly
// we use htmlspecialchars_decode to avoid encoding html special char like < > to < and >
// which would avoid the xml script being interpreted correctly when sent back to blockly via DOM
case 'updateScript':
mysql_connect($options["database"]["host"],$options["database"]["username"],$options["database"]["password"]) or die('error connection');
mysql_select_db($options["database"]["name"]) or die('error database selection');
$cleanLuaCode=mysql_real_escape_string(htmlspecialchars_decode($_['lua']));
$cleanXMLCode=mysql_real_escape_string(htmlspecialchars_decode($_['xml']));
$query="UPDATE `scripts` SET `xml` = '".$cleanXMLCode."',`lua`='".$cleanLuaCode."' WHERE `id`='".$_['id']."'";
//appendlualog($cleanLuaCode);
$outcome = mysql_query($query);
if (!$outcome) {
$result['answer'] = "ERROR";
$result['state'] = mysql_error(); /* mysql_error();*/
}else{
$result['state'] = "Sauvegarde effectuée";
}
mysql_free_result($outcome);
break;
case 'updateSetting':
mysql_connect($options["database"]["host"],$options["database"]["username"],$options["database"]["password"]) or die('error connection');
mysql_select_db($options["database"]["name"]) or die('error database selection');
$query="UPDATE `settings` SET `value` = '".$_['value']."' WHERE `id`='".$_['id']."'";
$outcome = mysql_query($query);
if (!$outcome) {
$result['answer'] = "ERROR";
$result['state'] = mysql_error(); /* mysql_error();*/
}else{
$result['state'] = $outcome;
}
//mysql_free_result($outcome);
break;
case 'getSetting':
mysql_connect($options["database"]["host"],$options["database"]["username"],$options["database"]["password"]) or die('error connection');
mysql_select_db($options["database"]["name"]) or die('error database selection');
$query="SELECT value FROM `settings` WHERE id='".$_['id']."'";
$outcome = mysql_query($query);
if (!$outcome) {
$result['answer'] = "ERROR";
$result['state'] = mysql_error();
}else{
while ($row = mysql_fetch_assoc($outcome)) {
$result['state']=($row['value']);
}
// result return "undef" in state in case no data match
mysql_free_result($outcome);
}
break;
case 'forceCron':
$result['state'] = shell_exec('./scripts/hourlypiwebpool.sh');
// not a good solution as it will be executed by apache with no write access to the logs
// for strange reason this also return {|main:Runtime error|custom:Runtime error}] in the log
// we'd better call the code of cronaction.php here
break;
case 'run':
break;
case 'lua':
$result['state']='';
if (!goLua("function run() return 'lua works!'; end",$materials,$pins,$result['state'])) $result['answer'] = "ERROR";
break;
default:
$result['answer'] = "ERROR";
$result['state'] = "undefined action: ".$_['action'];
break;
}
}else if (isset($_['getState'])){
$material=$_['getState'];
if (array_key_exists($material, $materials)){
$result['state'] = getPin($pins[$materials[$material]]);
}else{
$result['state'] = "ERROR";
$result['state'] = "undefined material:".$material;
}
}else{
// finally, do we have to execute a material type command
$result['answer'] = "ERROR";
$result['state'] = 'Undefined call '.$params;
foreach($materials as $material=>$pin){
if (isset($_[$material])){
setPinState($pins[$materials[$material]],$_[$material]);
$result['state'] = getPin($pins[$materials[$material]]);
$result['answer'] = "OK";
break;
}
}
}
$returnValue = json_encode($result);
// in case the action include extendedJson it means we expect a standard json answer, without the ( )
if (isset($_['extendedJson'])) {
$returnValue = '('.$returnValue.')';
}
//header('Content-Type: application/json');
echo $returnValue;
?>