Browse Source

debug mqttclient

master
weigang 6 years ago
parent
commit
8133c86dc5
  1. 9
      app/Service/MqttServiceInterface.php
  2. 19
      app/Service/MqttSpeakerService.php

9
app/Service/MqttServiceInterface.php

@ -10,15 +10,6 @@ interface MqttServiceInterface
*/ */
public function speakToStore($orderId, $isMain = true); public function speakToStore($orderId, $isMain = true);
/**
* MQTT发布消息
* @param $message
* @param $topic
* @param $toClientId
* @return mixed
*/
public function pubToMqtt($message, $topic, $toClientId);
/** /**
* @param string|number $message 消息内容 * @param string|number $message 消息内容
* @param string $topic 发布消息到主题,主题名 * @param string $topic 发布消息到主题,主题名

19
app/Service/MqttSpeakerService.php

@ -17,7 +17,7 @@ class MqttSpeakerService implements MqttServiceInterface
*/ */
public function speakToStore($orderId, $isMain = true) public function speakToStore($orderId, $isMain = true)
{ {
var_dump('speaker-orderid', $orderId);
// 获取订单 // 获取订单
$orders = Order::query()->select(['id','order_num','money', 'pay_type', 'store_id', 'type']); $orders = Order::query()->select(['id','order_num','money', 'pay_type', 'store_id', 'type']);
if ($isMain) { if ($isMain) {
@ -26,26 +26,22 @@ class MqttSpeakerService implements MqttServiceInterface
$orders = $orders->where(['id' => $orderId])->get()->toArray(); $orders = $orders->where(['id' => $orderId])->get()->toArray();
} }
var_dump('speaker-orders', $orders);
if(empty($orders)) return; if(empty($orders)) return;
// 循环发送 // 循环发送
foreach ($orders as $k => &$order) { foreach ($orders as $k => &$order) {
$order['template'] = "懒族生活支付到账".floatval($order['money'])."";
$order['template'] = "懒族支付到账".floatval($order['money'])."";
// 获取终端ID // 获取终端ID
$order['to_client_id'] = Store::query()->where(['id' => $order['store_id']])->value('loudspeaker_imei'); $order['to_client_id'] = Store::query()->where(['id' => $order['store_id']])->value('loudspeaker_imei');
var_dump('speaker-$order', $order);
// 发布订阅消息 // 发布订阅消息
$this->pubToMqtt($order['template'], self::TOPIC, $order['to_client_id']);
$res = $this->publish($order['template'], self::TOPIC, $order['to_client_id']);
var_dump('speaker-$res', $res);
} }
} }
/**
* @inheritDoc
*/
public function pubToMqtt($message, $topic, $toClientId)
{
$this->publish($message, $topic, $toClientId);
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -60,6 +56,7 @@ class MqttSpeakerService implements MqttServiceInterface
$client = new MQTTClient(env('MQTT_HOST'), env('MQTT_PORT')); $client = new MQTTClient(env('MQTT_HOST'), env('MQTT_PORT'));
$client->setAuthentication(env('MQTT_NAME'), env('MQTT_PASS')); $client->setAuthentication(env('MQTT_NAME'), env('MQTT_PASS'));
$client->setDebug(true);
if (env('MQTT_CERT')) { if (env('MQTT_CERT')) {
$client->setEncryption(env('MQTT_CERT')); $client->setEncryption(env('MQTT_CERT'));

Loading…
Cancel
Save