2023-10-24 14:54:18 +08:00

130 lines
4.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\modules\api\components;
use PhpMqtt\Client\MQTTClient;
class Mqtt
{
public $mqtt;
public $data = [];
public function __construct()
{
try {
$redis_name = "api:cxaibc:mqtt:config";
$data = \Yii::$app->redis->get($redis_name);
if (!empty($data)) {
$data = json_decode($data, true);
$server = $data['mqtt_server'];
$port = intval($data['mqtt_port']);
$username = $data['mqtt_user'];
$password = $data['mqtt_pass'];
} else {
$server = \Yii::$app->params['mqtt']['server'];
$port = \Yii::$app->params['mqtt']['port'];
$username = \Yii::$app->params['mqtt']['username'];
$password = \Yii::$app->params['mqtt']['password'];
}
$clientId = md5(microtime(true));
$this->mqtt = new MqttClient($server, $port, $clientId);
$this->mqtt->connect($username, $password);
} catch (\Exception $e) {
$this->data = ['code' => 1, 'msg' => "链接mqtt失败"];
}
}
/**
* @ Author : Lw
* @ CreateTime : 2022-11-02
* @ Info : 启动,关闭车辆
* @param string $number 球车编号
* @param int $OPERATION_ENABLE -- 0.禁止使用1.允许使用
* @param int $OPERATION_LEVEL -- 0.助力模式1.遥杆模式2.跟随模式 -- 现在应该是传入0
* @return array 此操作仅做请求最终车辆的信息请查找redis中车辆的最新数据
*/
public function sendOpen(string $number, int $OPERATION_ENABLE, int $OPERATION_LEVEL)
{
if (!empty($this->data)) {
return $this->data;
}
// 车辆状态1.启动0.关闭
$redis_name = "api:cxaibc:sendOpen:{$number}";
try {
$time_base = time();
$str_res = pack('l', $time_base) . pack('C', $OPERATION_ENABLE) . pack('C', $OPERATION_LEVEL) . strtoupper(md5(str_pad($number, 16, "0", STR_PAD_LEFT) . '+' . $time_base));//请求启动车辆
$res = $this->x_o($str_res);
$str_res .= chr($res);
$this->mqtt->publish("$number/Permission", $str_res);
// $this->sendAck($number);
\Yii::$app->redis->set($redis_name, $OPERATION_ENABLE);
} catch (\Exception $e) {
return ['code' => 1, 'msg' => $e->getMessage()];
}
$this->getInfo($number,1);
return ['code' => 0, 'msg' => '成功'];
}
/**
* @ Author : Lw
* @ CreateTime : 2022-11-02
* @ Info : 主动获取车辆状态,请求后对方服务器会及时推送一条消息到本服务器上
* @param string $number 球车编号
* @param int $type 类型1.车辆详情2、定位信息4.告警信息7.三种信息都查
* @return array 查询的信息都会订阅中先存入redis后再存入mysql此操作只做请求
*/
public function getInfo(string $number, int $type)
{
if (!empty($this->data)) {
return $this->data;
}
try {
$time_base = time();
$str_res = pack('l', $time_base) . pack('L', $type);
$res = $this->x_o($str_res);
$str_res .= chr($res);
$this->mqtt->publish("$number/Query", $str_res);
// $this->sendAck($number);
} catch (\Exception $e) {
return ['code' => 1, 'msg' => $e->getMessage()];
}
return ['code' => 0, 'msg' => ''];
}
/**
* @ Author : Lw
* @ CreateTime : 2022-11-07
* @ Info : 发送ack消息用于同步时间戳
*/
public function sendAck(string $number)
{
if (!empty($this->data)) {
return $this->data;
}
try {
$time_base = time();
$str_res = pack('l', $time_base) . pack('l', 1);
$res = $this->x_o($str_res);
$str_res .= chr($res);
$this->mqtt->publish("$number/Ack", $str_res);
} catch (\Exception $e) {
return ['code' => 1, 'msg' => $e->getMessage()];
}
return ['code' => 0, 'msg' => ''];
}
public function x_o($data)
{
$t = 0;
for ($i = 0; $i < strlen($data); $i++) {
if ($i) {
$t ^= ord($data[$i]);
} else {
$t = ord($data[$i]) ^ 0;
}
}
return $t;
}
}