-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushover.php
More file actions
150 lines (109 loc) · 4.27 KB
/
pushover.php
File metadata and controls
150 lines (109 loc) · 4.27 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
<?php
declare(strict_types=1);
class HomegearNode extends HomegearNodeBase
{
const PUSHOVER_API_URL = 'https://api.pushover.net/1/messages.json';
private $hg = null;
private $nodeInfo = null;
public function __construct()
{
$this->hg = new \Homegear\Homegear();
}
function __destruct()
{
//$this->nodeInfo
}
//Init is called when the node is created directly after the constructor
public function init(array $nodeInfo): bool
{
$this->nodeInfo = $nodeInfo;
return true;
}
public function start(): bool
{
return true; //True means: "start" was successful
}
public function configNodesStarted()
{
/**
* @param $nodeId string nodeId
* @param $method string methodName
* @param $parameters array parameter list
* @param $wait bool waitForReturn (default true)
*/
//$this->invokeNodeMethod($this->nodeInfo['info']['server'], 'registerNode', [$this->nodeInfo], true);
}
public function startUpComplete()
{
}
public function input(array $nodeInfoLocal, int $inputIndex, array $message)
{
//var_dump($nodeInfoLocal);
$nodeId = $nodeInfoLocal['id'];
try {
if (empty($nodeInfoLocal['info']['userKey']) || empty($nodeInfoLocal['info']['apiToken'])) {
HomegearNodeBase::log(2, 'Missing userKey or apiToken');
throw new \RuntimeException('check User Key/API Token');
}
if (empty($message['payload'])) {
HomegearNodeBase::log(2, 'Missing payload for pushover message');
throw new \RuntimeException('empty Payload');
}
//check for device in message or info, else let it be empty and send to all devices
$device = null;
if (!empty($message['device'])) {
$device = $message['device'];
} elseif (!empty($nodeInfoLocal['info']['device'])) {
$device = $nodeInfoLocal['info']['device'];
}
//do we have a user supplied title?
$title = 'Homegear';
if (!empty($message['topic'])) {
$title = $message['topic'];
} elseif (!empty($nodeInfoLocal['info']['title'])) {
$title = $nodeInfoLocal['info']['title'];
}
$requestData = [
'token' => filter_var($nodeInfoLocal['info']['apiToken'], FILTER_SANITIZE_STRING),
'user' => filter_var($nodeInfoLocal['info']['userKey'], FILTER_SANITIZE_STRING),
'message' => filter_var($message['payload'], FILTER_SANITIZE_STRING),
'title' => filter_var($title, FILTER_SANITIZE_STRING)
];
if (!empty($device)) {
$requestData['device'] = filter_var($device, FILTER_SANITIZE_STRING);
}
//do the curl magic
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SELF::PUSHOVER_API_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$error = curl_error($ch);
$errno = curl_errno($ch);
if (is_resource($ch)) {
curl_close($ch);
}
if (0 !== $errno) {
throw new \RuntimeException($error, $errno);
}
$response = json_decode($response, true);
if ($response['status'] === 0) {
HomegearNodeBase::log(2, 'Got error(s) from Pushover API: ' . implode(', ', $response['errors']));
throw new \RuntimeException('ERROR! See Log');
}
HomegearNodeBase::nodeEvent('statusBottom/' . $nodeId,
['text' => 'message sent (' . date('Y-m-d H:i:s') . ')', 'fill' => 'green', 'shape' => 'dot']);
} catch (\RuntimeException $e) {
HomegearNodeBase::nodeEvent('statusBottom/' . $nodeId,
['text' => $e->getMessage(), 'fill' => 'red', 'shape' => 'dot']);
}
}
public function stop()
{
}
public function waitForStop()
{
//cloud block max 30s
}
}