forked from tsawyer/allmon2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvoterserver.php
181 lines (157 loc) · 5.01 KB
/
voterserver.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
<?php
#error_reporting(E_ALL);
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');
date_default_timezone_set('America/Los_Angeles');
#print date("Y-m-d H:i:s") . "<br/>\n";
#print microtime() . "<br/>New voter app under development.<br/>\n";
include('allmon.inc.php');
// Sanity check
if (empty($_GET['node'])) {
echo "data: Unknown voter request!\n\n";
ob_flush();
flush();
exit;
}
// Read parameters passed to us
$node = trim(strip_tags($_GET['node']));
// Get Allstar database file
$db = "astdb.txt";
$astdb = array();
if (file_exists($db)) {
$fh = fopen($db, "r");
if (flock($fh, LOCK_SH)){
while(($line = fgets($fh)) !== FALSE) {
$arr = explode("|", trim($line));
$astdb[$arr[0]] = $arr;
}
}
flock($fh, LOCK_UN);
fclose($fh);
}
#print_r($astdb);
// Read config INI file
if (!file_exists('allmon.ini.php')) {
die("data: Couldn't load allmon ini file.\n\n");
}
$config = parse_ini_file('allmon.ini.php', true);
#print "<pre>"; print_r($config[$node]); print "</pre>";
// Open a socket to Asterisk Manager
echo "data: Connecting...\n\n";
ob_flush();
flush();
$fp = AMIconnect($config[$node]['host']);
if (FALSE === $fp) {
die("Could not connect to Asterisk Manager.\n\n");
}
if (FALSE === AMIlogin($fp, $config[$node]['user'], $config[$node]['passwd'])) {
die("Could not login to Asterisk Manager.");
}
#print "data: Connected and logged in.\n\n";
#ob_flush();
#flush();
$ticToc = '*';
$actionID = "voter$node" . mt_rand(); # Asterisk Manger Interface an actionID so we can find our own response
while(TRUE) {
// Get voter response
$response = get_voter($fp, $actionID);
if ($response === FALSE) {
die ("data: Bad voter response!<br/>");
}
// Build an array of nodes containing client, rssi and IP
$lines = preg_split("/\n/", $response);
$voted = array();
$nodes=array();
foreach ($lines as $line) {
$line = trim($line);
if (strlen($line) == 0) {
continue;
}
list($key, $value) = explode(": ", $line);
$$key = $value;
if ($key == "Node") {
$nodes[$Node]=array();
}
if ($key == "RSSI") {
$nodes[$Node][$Client]['rssi']=$RSSI;
$nodes[$Node][$Client]['ip']=$IP;
}
if ($key == 'Voted') {
$voted[$Node] = $value;
}
}
#print "\$nodes: "; print_r($nodes[2532]);
// Print tables for each node
# foreach($nodes as $nodeNum => $clients) {
# print_r($clients);
# }
$message = printNode($node, $nodes, $voted, $config[$node]);
// Make a stupid blinky
if ($ticToc == '*') {
$ticToc = '<br/>';
} else {
$ticToc = '*';
}
print "data: $message\n";
print "data: $ticToc\n\n";
ob_flush();
flush();
usleep(100000);
}
exit;
function printNode($nodeNum, $nodes, $voted, $config) {
#print '<pre>'; print_r($config); print '</pre>';
$message = '';
$info = getAstInfo($nodeNum);
if (@$config['hideNodeURL'] == 1) {
$message .= "<table class='rtcm table table-sm table-bordered'><tr><th colspan=2>Node $nodeNum - $info</i></th></tr>";
} else {
$nodeURL = "http://stats.allstarlink.org/nodeinfo.cgi?node=$nodeNum";
$message .= "<table class='rtcm table table-sm table-bordered'><tr><th colspan=2>Node <a href=\"$nodeURL\" target=\"_blank\">$nodeNum</a> - $info</i></th></tr>";
}
$message .= "<tr><th>Client</th><th>RSSI</th></tr>";
if (isset($clients) && count($clients) == 0) {
$message .= "<td><div style='width: 120px;'> </div></td>";
$message .= "<td><div style='width: 339px;'> </div></td>";
}
$clients = $nodes[$nodeNum];
foreach($clients as $clientName => $client) {
$rssi = $client['rssi'];
$percent = ($rssi/255)*100;
if ($percent == 0) {
$percent = 1;
}
// find voted if any
if (@$voted[$nodeNum] != 'none' && strstr($clientName, @$voted[$nodeNum])) {
$barcolor = 'bg-success';
$textcolor = 'black';
} elseif (strstr($clientName, 'Mix')) {
$barcolor = 'bg-info';
$textcolor = 'black';
} else {
$barcolor = "bg-primary";
$textcolor = 'white';
}
// print table rows
$message .= "<tr>";
# $message .= "<td><div style='width: 150px;'>$clientName</div></td>";
$message .= "<td>$clientName</td>";
$message .= "<td><div class=\"progress\" style=\"height: 20px\">";
$message .= "<div class=\"progress-bar $barcolor\" style=\"width: $percent%\" role=\progressbar\" aria-valuenow=\"$percent\" aria-valuemin=\"0\" aria-valuemax=\"100\">$rssi</div>";
$message .= "</div></td></tr>";
}
$message .= "<tr><td colspan=2> </td></tr>";
$message .= "</table><br/>";
return $message;
}
function get_voter($fp, $actionID) {
// Voter status
if ((@fwrite($fp,"ACTION: VoterStatus\r\nActionID: $actionID\r\n\r\n")) > 0) {
// Get Voter Status
return get_response($fp, $actionID);
} else {
return FALSE;
}
}
?>