<?php
namespace Trumpet;

use Mosquitto\Client;



class Szzkc{

    ##此处填写阿里云帐号 AccessKey
    private $accessKey;
    ##此处填写阿里云帐号 SecretKey
    private $secretKey;
    ## 接入点地址，购买实例后从控制台获取
    private $endPoint;
    ##实例 ID，购买后从控制台获取
    private $instanceId;
    ## MQTT 第一级 Topic 需要在 MQTT 控制台提前申请
    private $topic;
    ## MQTT 客户端ID 前缀， GroupID，需要在 MQTT 控制台申请
    private $groupId;
    ## 发送端ClientID，@@@后面字符串可以随意修改，不能与设备IMEI重复
    private $deviceId;


	public function __construct($config=[]){

		$this->accessKey=isset($config['accessKey'])?$config['accessKey']:'';

		$this->secretKey=isset($config['secretKey'])?$config['secretKey']:'';

		$this->endPoint=isset($config['endPoint'])?$config['endPoint']:'';

		$this->instanceId=isset($config['instanceId'])?$config['instanceId']:'';

		$this->topic=isset($config['topic'])?$config['topic']:'';

		$this->deviceId=isset($config['groupId'])?$config['groupId']:'';

		$this->deviceId=isset($config['deviceId'])?$config['deviceId']:'';
	}
	public function sendMsg(){
		
		$accessKey = 'LTAI4FgxuFC1auJcZNSnFSw5';

		$secretKey = 'uRYSk7pGXkGEE1uNnezShLbvBkAC6v';

		$endPoint = 'mqtt-cn-0pp1bnxar02.mqtt.aliyuncs.com';
		##实例 ID，购买后从控制台获取
		$instanceId = 'mqtt-cn-0pp1bnxar02';

		$topic = 'TID_ZKC';

		$groupId = 'GID_ZKC';
		## MQTT 客户端ID 后缀，DeviceId，业务方自由指定，需要保证全局唯一，禁止 2 个客户端连接使用同一个 ID
		## 设备IMEI号码
		$deviceId = '352736082440754';

		## QOS必须为2
		$qos = 2;
		$port = 1883;
		$keepalive = 90;
		$cleanSession = true;
		$clientId = $groupId.'@@@'.$deviceId;

		$masterClientId=$groupId . '@@@20191010master';
		echo $clientId . "\n";
		$mid = 0;
		## 初始化客户端，需要设置 clientId 和 CleanSession 参数，参考官网文档规范
		$mqttClient = new Mosquitto\Client($masterClientId, $cleanSession);
		## 设置鉴权参数，参考 MQTT 客户端鉴权代码计算 username 和 password
		$username = 'Signature|' . $accessKey . '|' . $instanceId;
		$sigStr = hash_hmac("sha1", $masterClientId, $secretKey, true);
		$password = base64_encode($sigStr);
		echo "UserName:" . $username . "  Password:" . $password . "\n";
		$mqttClient-&setCredentials($username, $password);
		## 设置连接成功回调
		$mqttClient-&onConnect(function ($rc, $message) use ($mqttClient, &$mid, $topic, $qos, $clientId) {
		    echo date('Y-m-d H:i:s')." 连接成功：Connnect to Server Code is " . $rc . " message is " . $message . "\n";
		    ## P2P消息的二级 topic 是/p2p/,三级 topic 是目标客户端的 clientId
		    $mqttP2PTopic = $topic . "/p2p/" . $clientId;
		    $ZPL = $deviceId."|1007|2019022611153312312345|566.88|2001";
		    echo date('Y-m-d H:i:s')." 推送Topic：".$mqttP2PTopic."\n";
		    echo date('Y-m-d H:i:s')." 推送内容：".$ZPL."\n";
		    $mqttClient->publish($mqttP2PTopic, $ZPL, $qos);
		});
		## 设置发送成功回调
		$mqttClient-&onPublish(function ($publishedId) use ($mqttClient, $mid) {
		    echo date('Y-m-d H:i:s')." 发送成功：publish message success " . $mid . "\n";
		});
		## 设置消息接收回调
		$mqttClient-&onMessage(function ($message) {
		    echo date('Y-m-d H:i:s')." 接收回调：Message From mqtt, topic is " . $message-&topic . "  qos is " . $message-&qos . "  messageId is " . $message-&mid . "  payload is " . $message-&payload . "\n";
		});
		$mqttClient-&connect($endPoint, $port, $keepalive);
		$mqttClient-&loopForever();
		echo "Finished";
	}

}


