forked from BrewPi/brewpi-www
-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketmessage.php
116 lines (104 loc) · 3.05 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?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));
}
function readFromSocket($sock){
$msg = socket_read($sock, 4096);
if($msg == false){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't read from socket: [$errorcode] $errormsg" . "\nIs the script running?");
}
else{
return $msg;
}
}
function writeToSocket($sock, $msg){
$bytesWritten = socket_write($sock, $msg, 4096);
if($bytesWritten == false){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't write to socket: [$errorcode] $errormsg" . "\nIs the script running?");
}
}
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){
case "setActiveProfile":
case "startNewBrew":
writeToSocket($sock, $messageType . "=" . $data);
echo readFromSocket($sock);
break;
default:
writeToSocket($sock, $messageType . "=" . $data);
break;
}
}
else{
// message without a data argument
switch($messageType){
case "checkScript":
writeToSocket($sock, "ack");
$answer = readFromSocket($sock);
if($answer == "ack"){
echo 1;
}
else{
echo 0;
}
break;
case "lcd":
writeToSocket($sock, "lcd");
$lcdText = readFromSocket($sock);
echo str_replace(chr(0xB0), "°", $lcdText); // replace degree sign with &Deg
break;
default:
// just pass the command to the socket and read the answer if needed
writeToSocket($sock, $messageType);
if(startsWith($messageType, "get") or $messageType == "stopLogging" or
$messageType == "pauseLogging" or $messageType == "resumeLogging"){
// return data expected, read from socket
echo readFromSocket($sock);
}
}
}
socket_close($sock);
}
else{
die("Cannot open socket to script");
}