-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.s.php
120 lines (87 loc) · 3.05 KB
/
mysql.s.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
<?php
declare(strict_types=1);
class HomegearNode extends HomegearNodeBase
{
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)
{
$nodeId = $nodeInfoLocal['id'];
$configNodeId = $this->nodeInfo['info']['server'];
try {
if (empty($message['topic'])) {
HomegearNodeBase::log(2, 'Empty topic, thus no query');
throw new \RuntimeException('empty query');
}
if (empty($message['payload'])) {
$message['payload'] = [];
}
if ((strpos($message['topic'], '?') !== false) && empty($message['payload'])) {
HomegearNodeBase::log(2, 'no data for placeholders in query');
throw new \RuntimeException('not enough data');
}
if (!is_array($message['payload']) && !empty($message['payload'])) {
$message['payload'] = [$message['payload']];
}
HomegearNodeBase::nodeEvent('statusBottom/' . $nodeId,
['text' => 'query sent', 'fill' => 'green', 'shape' => 'ring']);
$message['payload'] = $this->invokeNodeMethod(
$configNodeId,
'query',
[
$message['topic'],
$message['payload']
],
true //wait for return
);
if (!$message['payload']) {
throw new \RuntimeException('sql error, see log');
}
HomegearNodeBase::nodeEvent('statusBottom/' . $nodeId,
['text' => 'query executed', 'fill' => 'green', 'shape' => 'dot']);
$this->output(0, $message);
} catch (\RuntimeException $e) {
HomegearNodeBase::nodeEvent('statusBottom/' . $nodeId,
['text' => $e->getMessage(), 'fill' => 'red', 'shape' => 'dot']);
}
}
public function stop()
{
$this->invokeNodeMethod($this->nodeInfo['info']['server'], 'unregisterNode', [$this->nodeInfo], false);
}
public function waitForStop()
{
//cloud block max 30s
}
}