-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
204 lines (154 loc) · 4.18 KB
/
functions.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
<?php
function alert($text) {
echo " <script language=\"javascript\" type=\"text/javascript\"> ";
echo " alert('$text')";
echo " </script>";
return;
}
function AddLog($log, $time)
{
mysql_query("INSERT INTO logs (id,log,time) VALUES('','$log',now())");
}
function Succes($title,$msg)
{
echo "<table width=\"400\" align=\"center\" border=\"0\" cellpadding=\"0\" cellpadding=\"0\">";
echo " <form method=\"post\" name=\"ok\" action=\"index.php\">";
echo " <tr>";
echo " <td background=\"images/content-table-headbg.jpg\" width=\"100%\"><center>".$msg."</center></td> ";
echo " <tr>";
echo " <tr>";
echo " <td width=\"100%\"><center> <img src=\"./images/done.gif\"> </center></td> ";
echo " <tr>";
echo " <tr>";
echo " <td width=\"100%\"><center>".$msg."</center></td> ";
echo " <tr>";
echo " <tr>";
echo " <td width=\"100%\"><center> <input type=\"submit\" name=\"ok\" value=\"Ok\"> </center></td> ";
echo " <tr>";
echo " </form>";
echo " </table>";
}
/** This is for GPIO , changed 0 to ON / 1 to OFF otherwise the relays will be on when device disabled ***/
function to_state($id)
{
switch($id)
{
case 0 : return "On"; break;
case 1 : return "Off"; break;
}
}
function to_state_menu($id)
{
switch($id)
{
case 0 : return "Off"; break;
case 1 : return "On"; break;
}
}
function yes_no($id)
{
switch($id)
{
case 0 : return "No"; break;
case 1 : return "Yes"; break;
}
}
function to_function($rank)
{
switch($rank)
{
case 0 : return "None"; break;
case 1 : return "User"; break;
case 2 : return "Administrator"; break;
}
}
function WritePin($pin,$state)
{
$output = shell_exec('gpio mode '.$pin.' out');
$output = shell_exec('gpio write '.$pin.' '.$state);
return $output;
}
function ReadPin($pin)
{
$output = shell_exec('gpio read '.$pin.'');
return $output;
}
function PinToName($pin)
{
$sql = "SELECT * FROM relays WHERE pin='$pin' LIMIT 1";
$query = mysql_query($sql);
$pins = mysql_fetch_assoc($query);
return $pins['name'];
}
function RelayToPin($id)
{
$sql = "SELECT * FROM relays WHERE id='$id' LIMIT 1";
$query = mysql_query($sql);
$pins = mysql_fetch_assoc($query);
return $pins['pin'];
}
function sensor_name($id)
{
$sql = "SELECT * FROM sensors WHERE id='$id' LIMIT 1";
$query = mysql_query($sql);
$sensor = mysql_fetch_assoc($query);
return $sensor['name'];
}
function sensor_address_name($address)
{
$sql = "SELECT * FROM sensors WHERE address='$address' LIMIT 1";
$query = mysql_query($sql);
$sensor = mysql_fetch_assoc($query);
return $sensor['name'];
}
function sensor_id_address($id)
{
$sql = "SELECT * FROM sensors WHERE id='$id' LIMIT 1";
$query = mysql_query($sql);
$sensor = mysql_fetch_assoc($query);
return $sensor['address'];
}
function FindAddress($address)
{
$sql = "SELECT * FROM sensors WHERE address='$address' LIMIT 1";
$query = mysql_query($sql);
$sensor = mysql_fetch_assoc($query);
if(mysql_num_rows($query) == "1") {
$name = 1;
} else {
$name = 0;
}
return $name;
}
// This function is also in the cron files.
function GetTemp($address)
{
$sql = "SELECT * FROM sensors WHERE address='$address' LIMIT 1";
$query = mysql_query($sql);
$sensor = mysql_fetch_assoc($query);
//File to read
$file = '/var/log/sensors/'.$address.'/sonoff_th';
if (file_exists($file)) {
//Read the file line by line
$lines = file($file);
//Get the temp from second line
$temp = ($lines[1]);
}
return $temp + $sensor['calibration_value'];
$file = '/sys/bus/w1/devices/'.$address.'/w1_slave';
if (file_exists($file)) {
//Read the file line by line
$lines = file($file);
//Get the temp from second line
$temp = explode('=', $lines[1]);
//$temp = ($lines[1]);
//Setup some nice formatting (i.e. 21,3)
$temp = number_format($temp[1] / 1000, 1, '.', '');
$temp = $temp;
/// Dont change 9999 because cronjob 10 minutes
} else {
$temp = "9999";
}
return $temp + $sensor['calibration_value'];
}
?>