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.
54 lines
1.6 KiB
54 lines
1.6 KiB
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use AlibabaCloud\Client\AlibabaCloud;
|
|
use AlibabaCloud\Client\Exception\ClientException;
|
|
use AlibabaCloud\Client\Exception\ServerException;
|
|
use App\Commons\Log;
|
|
use App\Constants\LogLabel;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class IOTAliService implements IOTServiceInterface
|
|
{
|
|
|
|
/**
|
|
* @Inject
|
|
* @var Log
|
|
*/
|
|
protected $log;
|
|
|
|
public function pub($device_name, $msg)
|
|
{
|
|
AlibabaCloud::accessKeyClient(env('ALI_IOT_KEY'), env('ALI_IOT_SECRET'))
|
|
->regionId(env('ALI_IOT_REGION'))
|
|
->asDefaultClient();
|
|
|
|
try {
|
|
AlibabaCloud::rpc()
|
|
->product('Iot')
|
|
->version('2018-01-20')
|
|
->action('Pub')
|
|
->method('POST')
|
|
->host(env('ALI_IOT_HOST'))
|
|
->options([
|
|
'query' => [
|
|
'RegionId' => "cn-shanghai",
|
|
'TopicFullName' => "/a1ZSurIJmO0/".$device_name."/user/get",
|
|
'MessageContent' => base64_encode($msg),
|
|
'ProductKey' => env('ALI_IOT_PRODUCT_KEY'),
|
|
],
|
|
])
|
|
->request();
|
|
} catch (ClientException $e) {
|
|
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => 'ClientException发布失败:'.$e->getErrorMessage()]);
|
|
return false;
|
|
} catch (ServerException $e) {
|
|
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => 'ServerException发布失败:'.$e->getErrorMessage()]);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|