From 058afbe306f2a287aec062e1082c2c0ae8d98904 Mon Sep 17 00:00:00 2001 From: weigang Date: Fri, 14 Aug 2020 14:36:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86MQTTClient=E7=A7=BB=E5=87=BAtask?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/MqttServiceInterface.php | 17 ++++++++ app/Service/MqttSpeakerService.php | 46 ++++++++++++++++++-- app/TaskWorker/MQTTClientTask.php | 65 ---------------------------- 3 files changed, 60 insertions(+), 68 deletions(-) delete mode 100644 app/TaskWorker/MQTTClientTask.php diff --git a/app/Service/MqttServiceInterface.php b/app/Service/MqttServiceInterface.php index 203b908..127b3cc 100644 --- a/app/Service/MqttServiceInterface.php +++ b/app/Service/MqttServiceInterface.php @@ -18,4 +18,21 @@ interface MqttServiceInterface * @return mixed */ public function pubToMqtt($message, $topic, $toClientId); + + /** + * @param string|number $message 消息内容 + * @param string $topic 发布消息到主题,主题名 + * @param string $type 消息类型,cash或tts + * @param string $payId 支付方式,如“支付宝”、“微信”等 + * @param string $toClientId 终端id,如IMEI码 + * @param string $curClientId 当前客户端id + */ + public function publish( + $message, + $topic, + $toClientId = '', + $type = '', + $payId = '', + $curClientId = '' + ); } \ No newline at end of file diff --git a/app/Service/MqttSpeakerService.php b/app/Service/MqttSpeakerService.php index 5a8b802..4eaf580 100644 --- a/app/Service/MqttSpeakerService.php +++ b/app/Service/MqttSpeakerService.php @@ -2,9 +2,9 @@ namespace App\Service; +use App\Libs\MQTTClient; use App\Model\Order; use App\Model\Store; -use App\TaskWorker\MQTTClientTask; use Hyperf\Utils\ApplicationContext; class MqttSpeakerService implements MqttServiceInterface @@ -43,7 +43,47 @@ class MqttSpeakerService implements MqttServiceInterface */ public function pubToMqtt($message, $topic, $toClientId) { - $task = ApplicationContext::getContainer()->get(MQTTClientTask::class); - $task->publish($message, $topic, $toClientId); + $this->publish($message, $topic, $toClientId); + } + + /** + * @inheritDoc + */ + public function publish( + $message, + $topic, + $toClientId = '', + $type = '', + $payId = '', + $curClientId = '' + ) { + + $client = new MQTTClient(env('MQTT_HOST'), env('MQTT_PORT')); + $client->setAuthentication(env('MQTT_NAME'), env('MQTT_PASS')); + + if (env('MQTT_CERT')) { + $client->setEncryption(env('MQTT_CERT')); + } + + $msgArr = []; + if ( (empty($type)&&is_numeric($message)) || 'cash' === $type ) { + $msgArr['cash'] = $message; + $payId AND $msgArr['payid'] = $payId; + } else { + $msgArr['message'] = $message; + } + + if (!empty($toClientId)) { + $topic .= '/'.$toClientId; + } + + $curClientId OR $curClientId = (string)rand(1,999999999); + $success = $client->sendConnect($curClientId); + + if ($success) { + $client->sendPublish($topic, json_encode($msgArr), MQTTClient::MQTT_QOS2); + $client->sendDisconnect(); + } + $client->close(); } } \ No newline at end of file diff --git a/app/TaskWorker/MQTTClientTask.php b/app/TaskWorker/MQTTClientTask.php deleted file mode 100644 index e49f562..0000000 --- a/app/TaskWorker/MQTTClientTask.php +++ /dev/null @@ -1,65 +0,0 @@ -getClient(); - - $msgArr = []; - if ( (empty($type)&&is_numeric($message)) || 'cash' === $type ) { - $msgArr['cash'] = $message; - $payId AND $msgArr['payid'] = $payId; - } else { - $msgArr['message'] = $message; - } - - if (!empty($toClientId)) { - $topic .= '/'.$toClientId; - } - - $curClientId OR $curClientId = (string)rand(1,999999999); - $success = $this->mqttClient->sendConnect($curClientId); - - if ($success) { - $this->mqttClient->sendPublish($topic, json_encode($msgArr), MQTTClient::MQTT_QOS2); - $this->mqttClient->sendDisconnect(); - } - $this->mqttClient->close(); - } - - protected function getClient() - { - $this->mqttClient = new MQTTClient(env('MQTT_HOST'), env('MQTT_PORT')); - $this->mqttClient->setAuthentication(env('MQTT_NAME'), env('MQTT_PASS')); - - if (env('MQTT_CERT')) { - $this->mqttClient->setEncryption(env('MQTT_CERT')); - } - - return $this->mqttClient; - } -} \ No newline at end of file