Fix MQTT to Redis to python backend

This commit is contained in:
root 2018-07-19 11:54:05 +08:00
parent ffafa47a95
commit b7bb0cdaf9

View file

@ -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);
}
}