This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
/
apc.php
119 lines (93 loc) · 2.58 KB
/
apc.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
<?php
// Configuration
$apcupsd_host = 'localhost';
$apcupsd_port = 3551;
$st_hub = '192.168.7.250';
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : 'status';
$pull = isset($_REQUEST['p']) ? true : false;
// Pull UPS Data
if ($action == 'status')
{
$data = query($apcupsd_host, $apcupsd_port);
if ($data)
{
if ($pull)
{
header('Referer: apcupsd');
header('Content-type: text/json');
echo json_encode(array('data' => array('device' => $data)));
}
else
stDataPush(array('device' => $data));
}
}
// Event notification
else if ($action == 'notify')
{
$status = isset($_REQUEST['status']) ? $_REQUEST['status'] : '';
stDataPush(array('event' => $status));
die('OK');
}
// Event notification
else if ($action == 'ping')
{
header("Referer: apcupsd\r\n");
die('OK');
}
function htons($i)
{
return pack('n', $i);
}
function nstoh($d)
{
$length = unpack('n', $d);
return $length[1];
}
function query($ip, $port)
{
$fp = fsockopen($ip, $port, $errno, $errstr);
if (!$fp)
{
return;
}
stream_set_blocking($fp, 1);
stream_set_timeout($fp, 5);
fwrite($fp, htons(6), 2);
fwrite($fp, 'status', 6);
$status = '';
while (!feof($fp))
{
$length = nstoh(fread($fp, 2));
if ($length == 0)
{
break;
}
$status .= fread($fp, $length);
}
fclose($fp);
$lines = explode("\n", $status);
foreach ($lines AS $line)
{
$rec = preg_split("/:/", $line, 2);
if (empty($rec[0])) break;
$response[strtolower(trim($rec[0]))] = trim($rec[1]);
}
return $response;
}
function stDataPush($data)
{
global $st_hub;
$url = "http://$st_hub/notify";
$data = array(
'data' => $data
);
$data = json_encode($data);
// request via cURL
$c = curl_init($url); curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_PORT, 39501);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $data );
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: text/html', 'Content-Length: ' . strlen($data), 'Referer: apcupsd'));
$response = curl_exec($c);
}
?>