Fix MQTT to Redis to python backend
This commit is contained in:
parent
ffafa47a95
commit
b7bb0cdaf9
1 changed files with 21 additions and 6 deletions
|
|
@ -4,30 +4,45 @@ namespace App\Service;
|
||||||
|
|
||||||
use Mosquitto\Client as MosquittoClient;
|
use Mosquitto\Client as MosquittoClient;
|
||||||
use App\Entity\JobOrder;
|
use App\Entity\JobOrder;
|
||||||
|
use Redis;
|
||||||
|
|
||||||
class MQTTClient
|
class MQTTClient
|
||||||
{
|
{
|
||||||
const PREFIX = 'motolite.control.';
|
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)
|
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 = new MosquittoClient();
|
||||||
$this->mclient->setTlsCertificates($cert);
|
$this->mclient->setTlsCertificates($cert);
|
||||||
$this->mclient->setTlsOptions(MosquittoClient::SSL_VERIFY_NONE, 'tlsv1');
|
$this->mclient->setTlsOptions(MosquittoClient::SSL_VERIFY_NONE, 'tlsv1');
|
||||||
$this->mclient->connect($ip_address, $port);
|
$this->mclient->connect($ip_address, $port);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
$this->mclient->disconnect();
|
// $this->mclient->disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function publish($channel, $message)
|
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)
|
public function sendEvent(JobOrder $job_order, $payload)
|
||||||
|
|
@ -40,7 +55,7 @@ class MQTTClient
|
||||||
{
|
{
|
||||||
error_log("no sessions to send mqtt event to");
|
error_log("no sessions to send mqtt event to");
|
||||||
return;
|
return;
|
||||||
{
|
}
|
||||||
|
|
||||||
// send to every customer session
|
// send to every customer session
|
||||||
foreach ($sessions as $sess)
|
foreach ($sessions as $sess)
|
||||||
|
|
@ -49,7 +64,7 @@ class MQTTClient
|
||||||
$channel = self::PREFIX . $phone_num;
|
$channel = self::PREFIX . $phone_num;
|
||||||
$this->publish($channel, json_encode($payload));
|
$this->publish($channel, json_encode($payload));
|
||||||
|
|
||||||
error_log("sent to $channel");
|
error_log('sent to ' . $channel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue