device_name; $topic = self::$productId . '/' . $deviceName . '/data'; $secret = base64_decode(self::$secret); list($secretId, $secretKey) = explode('|', $secret); $credential = new Credential($secretId, $secretKey); $client = new IotcloudClient($credential, self::$region); $request = new PublishMessageRequest(); $request->Topic = $topic; $request->Payload = $msg; $request->ProductId = self::$productId; $request->DeviceName = $deviceName; $req = $client->PublishMessage($request); return [ 'code' => 0, 'data' => json_decode($req->toJsonString(), true), ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 获取设备信息 * @param mixed $storeId * @return mixed * @throws \yii\base\InvalidConfigException * @author Syan mzsongyan@gmail.com * @date 2022-06-02 */ public static function getDeviceInfo($storeId) { try { $store = self::getStore($storeId); $deviceName = $store->device_name; $secret = base64_decode(self::$secret); list($secretId, $secretKey) = explode('|', $secret); $credential = new Credential($secretId, $secretKey); $client = new IotcloudClient($credential, self::$region); $request = new DescribeDeviceRequest(); $request->ProductId = self::$productId; $request->DeviceName = $deviceName; $req = $client->DescribeDevice($request); return [ 'code' => 0, 'data' => json_decode($req->toJsonString(), true), ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 获取商城 * @param mixed $storeId * @return \app\models\Store * @throws \yii\base\InvalidConfigException * @throws \Exception * @author Syan mzsongyan@gmail.com * @date 2022-06-02 */ private static function getStore($storeId) { $store = Store::findOne($storeId); if (!$store) { throw new \Exception('未找到商城'); } if (empty($store->device_name)) { throw new \Exception('该商城还未绑定设备'); } return $store; } }