-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.inc.php
executable file
·309 lines (194 loc) · 8.93 KB
/
functions.inc.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
//create message tables
function createTables() {
global $db, $Plugin_DBName;
$createQuery = "CREATE TABLE IF NOT EXISTS messages (messageID INTEGER PRIMARY KEY AUTOINCREMENT, timestamp int(16) NOT NULL, message varchar(255), pluginName varchar(64), pluginData varchar(64));";
logEntry("CREATING Messages in db: ".$Plugin_DBName.": ".$createQuery);
$db->exec($createQuery) or die('Create Table Failed');
}
function insertMessage($DBName, $table, $message, $pluginName, $pluginData) {
global $db;
logEntry("MESSAGEQUEUE_PLUGIN: DBName: ".$DBName);
$db = new SQLite3($DBName) or die('Unable to open database');
$insertQuery = "INSERT INTO ".$table." (timestamp, message, pluginName, pluginData) VALUES ('".time()."','".urlencode($message)."','".$pluginName."','".urlencode($pluginData)."');";
logEntry("MESSAGEQUEUE_PLUGIN: INSERT query string: ".$insertQuery);
$db->exec($insertQuery) or die('could not insert into database');
}
//add message to queue
function addNewMessage($messageText,$pluginName,$pluginData="",$messageFile) {
global $messageQueueFile, $TwilioVersion, $settings, $WeatherVersion;
if($pluginName == "TwilioControl") {
$pluginVersion = $TwilioVersion;
} elseif($pluginName == "FPP-Plugin-Weather") {
$pluginVersion = "2.0";
} elseif($pluginName == "SportsTicker") {
$pluginVersion = "2.0";
} elseif($pluginName == "FPP-Plugin-EventDate") { // pjd 7/15/2019 added as temp workaround
$pluginVersion = "2.0";
}
logEntry("Message file passed: ".$messageFile);
if($messageFile == "") {
$messageFile = $messageQueueFile;
}
//logEntry("MESSAGEQUEUE_PLUGIN: Message File: ".$messageQueueFile);
logEntry("MESSAGEQUEUE_PLUGIN: Message queue file: ".$messageFile);
logEntry("MESSAGEQUEUE_PLUGIN: Adding message to message queue: ".$messageText." :".$pluginName." :".$pluginData);
logEntry("MESSAGEQUEUE_PLUGIN: Plugin Version: ".$pluginVersion); //pjd 7/15/2019
switch ($pluginVersion) {
case "2.0":
logEntry("MESSAGEQUEUE_PLUGIN: Case 2.0"); //pjd 7/15/2019
insertMessage($messageFile, "messages", $messageText, $pluginName, $pluginData);
break;
default;
logEntry("MESSAGEQUEUE_PLUGIN: Case default"); //pjd 7/15/2019
$messageLine = "";
$messageLine = time()."| ".urlencode($messageText) . " | ".$pluginName. " | ".$pluginData."\n";
//$messageLine = date('Y-m-d h:i:s A',time())."| ".$messageText . " | ".$pluginName. " | ".$pluginData."\n";
//echo "writing message line \r\n".$messageLine;
file_put_contents($messageFile, $messageLine, FILE_APPEND | LOCK_EX);
}
}
//get new messages.. write a status file with the plugin name of the last time messages were read
//only get messages from plugins that it wants to subscribe to
function getNewPluginMessages($subscriptions="") {
global $DEBUG, $messageQueuePluginPath,$messageQueueFile, $TwilioVersion, $MatrixMessageVersion;
if($DEBUG)
logEntry("MESSAGE QUEUE: Inside function ".__METHOD__,0,__FILE__,__LINE__);
$pluginVersion = $TwilioVersion;
$pluginVersion = $MatrixMessageVersion;
$pluginVersion = "2.0";
logEntry("Plugin version: ".$pluginVersion);
switch ($pluginVersion) {
case "2.0":
if($subscriptions == "") {
$pluginSubscriptions[] = $pluginName;
} else {
$pluginSubscriptions = explode(",",$subscriptions);
}
//build list of messages from the pluginSubscriptions array
$newMessages=array();
$pluginLastRead= 0;
foreach($pluginSubscriptions as $pluginName) {
//require("/opt/fpp/www/common.php");
$DB_NAME = $settings['configDirectory']."/FPP.".$pluginName.".db";
$DB_NAME = "/home/fpp/media/config"."/FPP.".$pluginName.".db";
logEntry("MESSAGE QUEUE: getting NEW ".$pluginName." messages from ".$pluginName." ".$DB_NAME." DB");
$pluginLastRead = urldecode(ReadSettingFromFile("LAST_READ",$pluginName));
logEntry("MESSAGE QUEUE: plugin ".$pluginName." last read: ".$pluginLastRead);
$db = new SQLite3($DB_NAME) or die("Unable to open ".$pluginName." database");
//using > ONLY. caused the last message to be repeated, repeated, repeated because it was = the last read!
//if TWO messages arrive with the EXACT same timestamp. this could cause one message to possibly be missed,
//highly unlikely
$messagesQuery = "SELECT * FROM messages WHERE pluginName = '".$pluginName."' AND timestamp > '".$pluginLastRead."'";
if($DEBUG) {
logEntry("MESSAGE QUEUE: New Messages query: ".$messagesQuery);
}
$messagesResult = $db->query($messagesQuery) or die('Query failed');
while ($row = $messagesResult->fetchArray()) {
logEntry("MESSAGE QUEUE: Message found: ".$row['timestamp']." ".$row['message']." ".$row['pluginName']." ".$row['pluginData']);
//add message to array
$newMessages[] = $row['timestamp']."|".$row['message']."|".$pluginName."|".$row['pluginData'];
//update the last read with each incremental read
$pluginLastRead = $row['timestamp'];
logEntry("MESSAGEQUEUE: Plugin: ".$pluginName." last read set to: ".$pluginLastRead);
}
logEntry("MESSAGE QUEUE: Writing high water mark for plugin: ".$pluginName." LAST_READ = ".$pluginLastRead);
WriteSettingToFile("LAST_READ",urlencode($pluginLastRead),$pluginName);
// $db.close();
}
if($DEBUG) {
logEntry("MESSAGE QUEUE: Wrote high water mark",0,__FILE__,__LINE__);
}
//debug to output messages to log file
if($DEBUG) {
foreach($newMessages as $tmpMsg) {
logEntry("MESSAGE QUEUE PENDING QUEUE: ".$tmpMsg);
}
}
return $newMessages;
break;
default:
break;
}
}
//only get the messages for a given plugin to help manage on the plugin's page. can pass a last read paramater to get messages SINCE the last read
//plugins using this functionality = SMS
//dec 18 2015
function getPluginMessages($subscriptions="", $pluginLastRead=0, $messageFile="") {
global $messageQueuePluginPath,$messageQueueFile, $pluginName, $TwilioVersion;
$db = new SQLite3($messageQueueFile) or die('Unable to open database');
// set up DB connection
//$DB_NAME = "/tmp/FPP." . $pluginName . ".db";
//$db = new SQLite3 ( $DB_NAME ) or die ( 'Unable to open database' );
$pluginVersion = $TwilioVersion;
switch ($pluginVersion) {
case "2.0":
$result = $db->query('SELECT * FROM messages WHERE pluginName =\'".$subscriptions."') or die('Query failed');
return $result;//->fetchArray();
break;
default;
break;
}
if(!file_exists($messageQueueFile))
{
logEntry("No message queue file exists to process: ".$messageQueueFile);
return null;
}
if($messageFile == "") {
$messageFile = $messageQueueFile;// = $messageFile;
}
logEntry("Getting plugin messages from: ".$messageFile);
$newMessages=array();
//reset the julian to empty
$pluginLastRead= 0;
$pluginSubscriptions = array();
if($subscriptions == "") {
$pluginSubscriptions[] = $pluginName;
} else {
$pluginSubscriptions = explode(",",$subscriptions);
}
//print_r($pluginSubscriptions);
//loop through all the subscriptions that this plugin reader needs to do and append all messages!!!
$i=0;
for($pluginIndex=0;$pluginIndex<=count($pluginSubscriptions)-1;$pluginIndex++) {
logEntry("MessageQueuePlugin: getting messages for plugin: ".$pluginSubscriptions[$pluginIndex]);
logEntry("plugin ".$pluginSubscriptions[$pluginIndex]." last read: ".$pluginLastRead);
if((int)$pluginLastRead == 0 || $pluginLastRead == "")
{
logEntry("last read =0 or no last read messages for plugin: ".$pluginSubscriptions[$pluginIndex]. " getting all messages");
$pluginLastRead=0;
}
$messagesTemp = file_get_contents($messageFile);
$pluginMessageQueue = explode("\n",$messagesTemp);
//print_r($pluginMessageQueue);
//print_r($pluginMessageQueue);
$pluginLatest ="0";
//get the lastest number and write it to the last read file
$i=0;
for($i=0;$i<count($pluginMessageQueue)-1;$i++)
{
// echo $i."\n";
$pluginLatest = substr($pluginMessageQueue[$i],0,10);
}
$messageIndex = 0;
for($messageIndex = 0 ;$messageIndex < count($pluginMessageQueue)-1;$messageIndex++)
{
$messageQueueParts = explode("|",$pluginMessageQueue[$messageIndex]);
// print_r($messageQueueParts);
// print_r($pluginSubscriptions);
//echo "MessageQueueParts: ".$messageQueueParts[0]. " -- ".$pluginLastRead."\n";
//echo "MessageQueueParts: ".$messageQueueParts[2]. " --".$pluginSubscriptions[$pluginIndex]."\n";
if($messageQueueParts[0] > $pluginLastRead && strtoupper(trim($messageQueueParts[2])) == strtoupper($pluginSubscriptions[$pluginIndex]))
{
//echo "we have a new message and subscriptions matches";
//add message to new queue
$newMessages[]=$pluginMessageQueue[$messageIndex];
} else {
//this is VERY verbose
//logEntry("message: ".$pluginMessageQueue[$messageIndex]. " is not newer then last read or is not a subscription");
}
}
}
return $newMessages;
}
?>