Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- define('BROKER', '192.168.0.250');
- define('PORT', 1883);
- define('CLIENT_ID', "pubclient_" + getmypid());
- $client = new Mosquitto\Client(CLIENT_ID);
- $client->onConnect('connect');
- $client->onDisconnect('disconnect');
- $client->onSubscribe('subscribe');
- $client->onMessage('message');
- $client->connect(BROKER, PORT, 60);
- $client->subscribe('Garagentuere/#', 1); // Subscribe to all messages
- $client->loopForever();
- function publishvalue($prefix, $param, $message) {
- $topic = $prefix.$param;
- $client = new Mosquitto\Client();
- //$client->setCredentials('S1','123456');
- $client->connect("192.168.0.250", 1883, 5);
- $client->publish($topic, $message, 1, 0);
- $client->disconnect();
- unset($client);
- }
- function connect($r) {
- echo "Received response code {$r}\n";
- }
- function subscribe() {
- echo "Successful subscribed to a topic.\n";
- }
- function message($message) {
- //echo getDatetimeNow().": ";
- printf("Msg received on %s payload: >>>>>%s<<<<<\n", $message->topic, $message->payload);
- if ($message->topic == 'Garagentuere/status/set') {
- doorcontrol($message->payload);
- }
- return;
- }
- function endsWith($haystack, $needle) {
- return substr_compare($haystack, $needle, -strlen($needle)) === 0;
- }
- function disconnect() {
- //echo "Disconnected cleanly. Attempt to reconnect in 5s.\n";
- echo "Disconnected cleanly. Exiting.\n";
- exit();
- sleep(5);
- $client = new Mosquitto\Client(CLIENT_ID);
- $client->onConnect('connect');
- $client->onDisconnect('disconnect');
- $client->onSubscribe('subscribe');
- $client->onMessage('message');
- $client->connect(BROKER, PORT, 60);
- $client->subscribe('Garagentuere/#', 1); // Subscribe to all messages
- $client->loopForever();
- }
- function doorcontrol($param) {
- $sudo = '/usr/bin/sudo ';
- $cmd = '/usr/local/bin/keyble-sendcommand --address 00:11:22:33:44:55 --user_id 1 --user_key XXXXX --command ';
- //echo getDatetimeNow().": executing doorcontrol with param >>>>>".$param."<<<<<\n";
- echo "executing doorcontrol with param >>>>>".$param."<<<<<\n";
- //$param = 'status';
- $result = exec($sudo.$cmd.$param.' 2>&1', $output, $retcode);
- if ($retcode != 0) {
- //echo getDatetimeNow().": errorcode: ".$retcode."\n";
- echo "errorcode: ".$retcode."\n";
- print_r($output);
- unset($output);
- return;
- } else {
- publishvalue('Garagentuere/', 'status', $result);
- //echo getDatetimeNow().": Changed and published new state >>>>>".$result."<<<<<\n";
- echo "Changed and published new state >>>>>".$result."<<<<<\n";
- }
- }
- function getDatetimeNow() {
- $tz_object = new DateTimeZone('Europe/Vienna');
- //date_default_timezone_set('Europe/Vienna');
- $datetime = new DateTime();
- $datetime->setTimezone($tz_object);
- return $datetime->format('Y\-m\-d\ h:i:s');
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement