From b7bb0cdaf9ef82d4e50c363d85d7f134996a5d91 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 19 Jul 2018 11:54:05 +0800 Subject: [PATCH] Fix MQTT to Redis to python backend --- src/Service/MQTTClient.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Service/MQTTClient.php b/src/Service/MQTTClient.php index fe27cd64..f652ca76 100644 --- a/src/Service/MQTTClient.php +++ b/src/Service/MQTTClient.php @@ -4,30 +4,45 @@ namespace App\Service; use Mosquitto\Client as MosquittoClient; use App\Entity\JobOrder; +use Redis; class MQTTClient { const PREFIX = 'motolite.control.'; - const RIDER_PREFIX = 'motorider.'; + const RIDER_PREFIX = 'motorider_'; + const REDIS_KEY = 'events'; - protected $mclient; + // protected $mclient; + protected $redis; public function __construct($ip_address, $port, $cert) { + // we use redis now + // we have an mqtt server listening on redis and sending to mqtt + $this->redis = new Redis(); + $this->redis->connect('127.0.0.1', 6379); + + /* + error_log("connecting to mqtt server - $ip_address : $port"); + error_log("using $cert"); $this->mclient = new MosquittoClient(); $this->mclient->setTlsCertificates($cert); $this->mclient->setTlsOptions(MosquittoClient::SSL_VERIFY_NONE, 'tlsv1'); $this->mclient->connect($ip_address, $port); + */ } public function __destruct() { - $this->mclient->disconnect(); + // $this->mclient->disconnect(); } public function publish($channel, $message) { - $this->mclient->publish($channel, $message); + // $this->mclient->publish($channel, $message); + + $data = $channel . '|' . $message; + $this->redis->lpush(self::REDIS_KEY, $data); } public function sendEvent(JobOrder $job_order, $payload) @@ -40,7 +55,7 @@ class MQTTClient { error_log("no sessions to send mqtt event to"); return; - { + } // send to every customer session foreach ($sessions as $sess) @@ -49,7 +64,7 @@ class MQTTClient $channel = self::PREFIX . $phone_num; $this->publish($channel, json_encode($payload)); - error_log("sent to $channel"); + error_log('sent to ' . $channel); } }