forked from BrewPi/brewpi-www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketmessage.php
92 lines (81 loc) · 2.29 KB
/
socketmessage.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
<?php
/* Copyright 2012 BrewPi/Elco Jacobs.
* This file is part of BrewPi.
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BrewPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
/* to make sockets work on windows, uncomment extension=php_sockets.dll in php.ini */
require_once('socket_open.php');
// Read config settings
if(file_exists('config.php')) {
require_once('config.php');
}
else {
die('ERROR: Unable to open required file (config.php)');
}
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
error_reporting(E_ALL ^ E_WARNING);
if(isset($_POST['messageType'])){
$messageType = $_POST['messageType'];
}
else{
die("messageType not set");
}
if(isset($_POST['message'])){
// message with data
$data = $_POST['message'];
}
$sock = open_socket();
if($sock !== false){
if(isset($data) && $data != ""){
switch($messageType){
default:
socket_write($sock, $messageType . "=" . $data, 4096);
break;
}
}
else{
// message without a data argument
switch($messageType){
case "checkScript":
socket_write($sock, "ack", 4096);
$answer = socket_read($sock, 4096);
if($answer == "ack"){
echo 1;
}
else{
echo 0;
}
break;
case "lcd":
socket_write($sock, "lcd", 4096);
$lcdText = socket_read($sock, 4096);
if($lcdText !== false){
echo str_replace(chr(0xB0), "°", $lcdText); // replace degree sign with &Deg
}
else{
echo false;
}
break;
default:
// just pass the command to the socket and read the answer if needed
socket_write($sock, $messageType, 4096);
if(startsWith($messageType, "get") || $messageType == "uploadProfile"){
// return data expected, read from socket
echo socket_read($sock, 4096);
}
}
}
socket_close($sock);
}