You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
1.8 KiB

<?php
namespace App\Service\v3\Implementations;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use App\Commons\Log;
use App\Constants\v3\LogLabel;
use App\Service\v3\Interfaces\IOTServiceInterface;
use Hyperf\Di\Annotation\Inject;
class IOTAliService implements IOTServiceInterface
{
/**
* @Inject
* @var Log
*/
protected $log;
public function pub($device_name, $msg)
{
AlibabaCloud::accessKeyClient(config('aliiot.key'), config('aliiot.secret'))
->regionId(config('aliiot.region'))
->asDefaultClient();
try {
AlibabaCloud::rpc()
->product('Iot')
->version('2018-01-20')
->action('Pub')
->method('POST')
->host(config('aliiot.host'))
->options([
'query' => [
'RegionId' => "cn-shanghai",
'TopicFullName' => "/".config('aliiot.prod_key')."/".$device_name."/user/get",
'MessageContent' => base64_encode($msg),
'ProductKey' => config('aliiot.prod_key'),
'IotInstanceId'=> config('aliiot.inst_id'),
],
])
->request();
} catch (ClientException $e) {
$this->log->event(LogLabel::DEVICE_SEND_LOG, ['msg' => 'ClientException发布失败:'.$e->getErrorMessage()]);
return false;
} catch (ServerException $e) {
$this->log->event(LogLabel::DEVICE_SEND_LOG, ['msg' => 'ServerException发布失败:'.$e->getErrorMessage()]);
return false;
}
return true;
}
}