-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcvsend.php
More file actions
executable file
·187 lines (156 loc) · 5.04 KB
/
rcvsend.php
File metadata and controls
executable file
·187 lines (156 loc) · 5.04 KB
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
#!/usr/bin/php
<?php
// Git version
// <copyright> Copyright (c) 2012 All Rights Reserved,
// Escurio
// http://www.escurio.com/
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Peter van Es</author>
// <version>2.5</version>
// <email>vanesp@escurio.com</email>
// <date>2014-02-11</date>
// <summary>rcvsend receives text from a jeenode and stores records in a redis queue
// text received over the serial line is transmitted to the jeenode</summary>
// version 2.1 -- PIR motion records are sent directly to the Redis channel
// version 2.2 -- a class added to daemonize this program
// version 2.3 -- extra exceptions caught on redis publishing
// version 2.4 -- stop filling the log with Redis exception messages, except when in debug mode
// version 2.5 -- use Redis lists to communicate with consume, drop MySQL
/**
* System_Daemon Code
*
* If you run this code successfully, a daemon will be spawned
* and stopped directly. You should find a log enty in
* /var/log/simple.log
*
*/
// Make it possible to test in source directory
// This is for PEAR developers only
ini_set('include_path', ini_get('include_path').':..');
// Include Class
error_reporting(E_ALL);
require_once "System/Daemon.php";
// No PEAR, run standalone
System_Daemon::setOption("usePEAR", false);
// Setup
$options = array(
'appName' => 'rcvsend',
'appDir' => dirname(__FILE__),
'appDescription' => 'Receives data from Arduino and stores it in a redis queue',
'authorName' => 'Peter van Es',
'authorEmail' => 'vanesp@escurio.com',
'sysMaxExecutionTime' => '0',
'sysMaxInputTime' => '0',
'sysMemoryLimit' => '1024M',
'appRunAsGID' => 20, // dialout group for /dev/ttyS1
'appRunAsUID' => 1000,
);
System_Daemon::setOptions($options);
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started so this ".
"will be written on-screen");
// Spawn Deamon!
System_Daemon::start();
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon: '".
System_Daemon::getOption("appName").
"' spawned! This will be written to ".
System_Daemon::getOption("logLocation"));
// get access credentials
include('access.php');
$device = "/dev/ttyS1";
$LEN = 128; // records are max 128 bytes
date_default_timezone_set('Europe/Amsterdam');
// DEBUG
$debug = false;
// don't timeout!
set_time_limit(0);
// include Redis pub sub functionality
include('redis.php');
// set to the correct serial parameters
exec('stty -F '.$device.' 57600');
// open the serial port (later need to set settings)
$handle = fopen ($device, "r");
if($handle === FALSE) {
$message = date('Y-m-d H:i') . " rcvsend.php: Failed to open device " .$device. "\n";
System_Daemon::notice($message);
// No point in continuing
System_Daemon::stop();
exit(1);
}
// Open the redis database
$i = 0;
sleep(30); // wait a minute... for the database to be up and running
if (!$redis->isConnected()) {
try {
$redis->connect();
$pubredis = true;
}
catch (Exception $e) {
$pubredis = false;
$message = date('Y-m-d H:i') . " Cannot connect to Redis for publishing " . $e->getMessage() . "\n";
System_Daemon::notice($message);
// No point in continuing
System_Daemon::stop();
exit(1);
}
}
if ($debug) System_Daemon::info("rcvsend.php: Ready to receive...\n");
while (($buf = fgets($handle, $LEN)) !== false) {
if (strlen($buf) > 1) {
// process another line
if ($debug) System_Daemon::info($buf);
if (strpos($buf, "[")) {
// skip this line, it tells us the program version
continue;
};
if (strpos($buf, " A")) {
// skip this line, it indicates startup of communication
continue;
};
// check if it is a motion event...
if (strpos($buf, "GNR") === 0) {
$field = explode(" ",$buf);
if ($debug) print_r ($field);
// field[0] = GNR
// field[1] = room id
$roomid = $field[1] & 0x1F; // node from the header
// Room node. PIR is on if ACK is set
$pir = (($field[1] & 0x20) == 0x20);
if ($pir) {
if ($debug) System_Daemon::info("rcvsend.php: Publishing motion event room:".$roomid."\n");
// update Redis using socketstream message
$msg = new PubMessage;
$msg->setParams('Motion', $roomid, '',1);
try {
$redis->publish('ss:event', json_encode($msg));
}
catch (Exception $e) {
$message = date('Y-m-d H:i') . " rcvsend.php: Cannot publish to Redis " . $e->getMessage() . "\n";
if ($debug) System_Daemon::notice($message);
}
}
}
// and insert into the database buffer, at the end...
$value['buf'] = $buf;
$value['tstamp'] = time();
try {
$redis->lpush ('queue',json_encode($value));
}
catch (Exception $e) {
$message = date('Y-m-d H:i') . " rcvsend.php: Cannot lpush to Redis queue" . $e->getMessage() . "\n";
if ($debug) System_Daemon::notice($message);
}
}
} // while
if (!feof($handle)) {
$message = date('Y-m-d H:i') . " rcvsend.php: fgets failed\n";
System_Daemon::notice($message);
}
fclose($handle);
System_Daemon::stop();
?>