-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback.php
160 lines (128 loc) · 4.3 KB
/
callback.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
<?php
require_once "loxberry_system.php";
require_once "loxberry_log.php";
require_once "./phpMQTT/phpMQTT.php";
$params = [
"name" => "Callback",
"filename" => LBPLOGDIR."/callback.log",
"append" => 1,
"stdout" => 1,
"addtime" => 1,
"loglevel" => 6
];
$log = LBLog::newLog ($params);
LOGSTART("Callback log");
$SentByType = array (
1 => "callback",
2 => "cron",
3 => "manual",
254 => "testing"
);
$mqttcfgfile = LBPCONFIGDIR."/mqtt.json";
tolog("LOGINF", "Callback processing");
if( $argc > 1 ) {
$longopts = array(
"json:",
"sentbytype:",
);
$options = getopt(null, $longopts);
if( empty($options["json"]) or empty($options["sentbytype"]) ) {
echo "--json and --sentbytype parameters required\n";
exit(1);
}
$options["json"] = stripslashes ( $options["json"] );
} else {
$options["json"] = file_get_contents('php://input');
$options["sentbytype"] = 1;
}
// Read MQTT config from plugin config
$mqttcfg = json_decode(file_get_contents($mqttcfgfile));
if( empty($mqttcfg) ) {
tolog("LOGCRIT", "MQTT config $mqttcfgfile is empty or invalid. Check your MQTT settings.");
exit(1);
}
// Topic
$topic = !empty($mqttcfg->topic) ? $mqttcfg->topic : "nuki";
if( substr($topic, -1) != "/" ) {
$topic = $topic."/";
}
define( "TOPIC", $topic);
tolog("LOGDEB", "MQTT topic is " . $topic);
// Broker
if( is_enabled($mqttcfg->usemqttgateway) ) {
// Use MQTT Gateway credentials
// Check if MQTT plugin in installed
require_once "loxberry_io.php";
$mqttcred = mqtt_connectiondetails();
if( !empty($mqttcred) ) {
$brokeraddress = $mqttcred["brokeraddress"];
$brokeruser = $mqttcred["brokeruser"];
$brokerpass = $mqttcred["brokerpass"];
tolog("LOGINF", "Using MQTT Gateway settings, MQTT server " . $brokeraddress);
} else {
tolog("LOGCRIT", "Could not read MQTT Server settings from MQTT Gateway.");
exit(1);
}
} else {
// Use MQTT config from Nuki plugin
$brokeraddress = $mqttcfg->server.":".$mqttcfg->port;
$brokeruser = $mqttcfg->username;
$brokerpass = $mqttcfg->password;
}
// Broker validation
@list($host, $port) = explode(":", $brokeraddress, 2);
$port = $port ? $port : 1883;
echo "Topic: ". TOPIC . "\n";
echo "Host : $host\n";
echo "Port : $port\n";
echo "User : $brokeruser\n";
echo "Pass : " . substr($brokerpass, 0, 1) . str_repeat("*", strlen($brokerpass)-1) . "\n";
echo "Type : " . $SentByType[$options["sentbytype"]] . " (" . $options["sentbytype"] . ")\n";
echo "DATA : {$options["json"]}\n";
$nukidata = json_decode($options["json"]);
if ( !empty($nukidata) and isset($nukidata->nukiId) ) {
echo "OK: Publishing...\n";
nuki_mqtt_publish( [
$nukidata->nukiId => $options["json"],
$nukidata->nukiId."/sentBy" => $options["sentbytype"],
$nukidata->nukiId."/sentByName" => $SentByType[$options["sentbytype"]],
$nukidata->nukiId."/sentAtTimeLox" => epoch2lox(),
$nukidata->nukiId."/sentAtTimeISO" => currtime(),
] );
tolog("LOGOK", "Published: " . $nukidata->nukiId . " sentByName " . $SentByType[$options["sentbytype"]] . " Data\n" . $options["json"]);
} else {
tolog("LOGCRIT", "No valid json data");
}
####################################################
# MQTT handler
####################################################
function nuki_mqtt_publish ( $keysandvalues ) {
global $brokeraddress, $brokeruser, $brokerpass;
$broker = explode(':', $brokeraddress, 2);
$broker[1] = !empty($broker[1]) ? $broker[1] : 1883;
$client_id = uniqid(gethostname()."_nuki");
$mqtt = new Bluerhinos\phpMQTT($broker[0], $broker[1], $client_id);
if( $mqtt->connect(true, NULL, $brokeruser, $brokerpass) ) {
foreach ($keysandvalues as $key => $value) {
$mqtt->publish(TOPIC . "$key", $value, 0, 1);
}
$mqtt->close();
} else {
echo "MQTT connection failed\n";
tolog("LOGCRIT", "Connection to MQTT broker failed ($broker[0]:$broker[1] User $brokeruser)");
}
}
######################################################
# To log a single line to a LoxBerry log (for errors)
######################################################
function tolog ( $logfunction, $message ) {
// Creates a log object, automatically assigned to your plugin, with the group name "PHPLog"
$logfunction($message);
}
// Shutdown handler
register_shutdown_function('shutdown');
function shutdown()
{
LOGEND();
}
?>