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.
 
 
 
 
 
 
shipin/app/services/user/IpcServices.php

557 lines
20 KiB

<?php
namespace app\services\user;
use app\services\BaseServices;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
// 设备查询
use AlibabaCloud\SDK\Iot\V20180120\Iot;
use AlibabaCloud\SDK\Iot\V20180120\Models\QueryDeviceBySQLRequest; // 通过sql查询
use AlibabaCloud\SDK\Iot\V20180120\Models\QueryDeviceRequest; // 查询设备列表
// 批量修改设备名称
use AlibabaCloud\SDK\Iot\V20180120\Models\BatchUpdateDeviceNicknameRequest\deviceNicknameInfo;
use AlibabaCloud\SDK\Iot\V20180120\Models\BatchUpdateDeviceNicknameRequest;
// 设备播放
use AlibabaCloud\SDK\Linkvisual\V20180120\Linkvisual;
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\QueryLiveStreamingRequest; // 获取播放地址
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\QueryRecordRequest; // 获取云端录像列表
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\QueryRecordUrlRequest; // 获取云端录像播放地址
// 录像计划相关
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\AddRecordPlanDeviceRequest; // IPC设备添加连续录像计划
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\DeleteRecordPlanDeviceRequest; // 删除IPC设备关联的录像计划
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\UpdateTimeTemplateRequest\timeSections; // 更新时间模版
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\UpdateTimeTemplateRequest;
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\UpdateRecordPlanRequest; // 更新录像计划
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\QueryRecordPlanDeviceByPlanRequest; // 关联录像计划的设备列表
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\QueryRecordPlanDetailRequest; // 查询录像计划详情
// 云储存相关
use AlibabaCloud\SDK\Linkvisual\V20180120\Models\SetDeviceRecordLifeCycleRequest; // 设置存储时间
use crmeb\services\FormBuilder;
use think\exception\ValidateException;
use think\facade\Log;
class IpcServices extends BaseServices
{
private const ACCESS_KEY_ID = 'LTAI5tKQxxBhWPaVMrmzy2T9';
private const ACCESS_KEY_SECRET = 'fHYmKvk0gRRjXcicVwaNxlE9HrtZjC';
private const END_POINT = 'iot.cn-shanghai.aliyuncs.com';
private const IOT_INSTANCE_ID = 'iot-060a7gpf'; // 实例ID
private const PRODUCT_KEY = 'k0b32NCJ0pp'; // 产品key-IPC设备
private const NVR_PRODUCT_KEY = 'k0b32rH7O9R'; // 产品key-NVR设备
private const TEMPLATE_ID = 'eff6627a886044ccb54874835c6ed848'; // 时间模版
private const PLAN_ID = '23829576a7aa4fddaff00339ead802ff'; // 录像计划ID
private $config = null;
public function __construct()
{
$config = new Config([
"accessKeyId" => self::ACCESS_KEY_ID,
"accessKeySecret" => self::ACCESS_KEY_SECRET,
]);
$config->endpoint = self::END_POINT;
$this->config = $config;
}
/**
* 编辑和获取表单
*/
public function getCrudForm($data)
{
$url = '/ipc/save';
$rule = [];
$rule[] = FormBuilder::input("iot_id", "设备ID", $data['iotId'])->disabled(true);
$rule[] = FormBuilder::input("class_code", "班级代码", '');
return create_form('关联班级', $rule, $url, 'POST');
}
/**
* 获取设置/更新时间模版以及录像计划表
*/
public function getRecordForm()
{
$text = '当前录像时间:';
$res = $this->QueryRecordPlanDetail();
if ($res['success'] == true) {
$data = $res['data'];
$templateInfo = $data->templateInfo;
if ($templateInfo->allDay == 1) {
$text .= '7 * 24';
} else {
$week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
$resetDataObj = reset($templateInfo->timeSectionList);
$endDataObj = end($templateInfo->timeSectionList);
$text .= $week[$resetDataObj->dayOfWeek] . '至' . $week[$endDataObj->dayOfWeek];
$text .= ' ' . $resetDataObj->begin / 3600 . ':00' . '-' . $resetDataObj->end / 3600 . ':00';
}
} else {
$text .= '未获取到当前录像时间';
}
$url = '/ipc/setRecordTime';
$rule = [];
$rule[] = FormBuilder::input("start_day", "开始天数", '')->required('请输入周几开始')->props(['placeholder' => '每周的周几(0~6代表周日到周六)']);
$rule[] = FormBuilder::input("end_day", "结束天数", '')->required('请输入周几结束')->props(['placeholder' => '每周的周几(0~6代表周日到周六)']);
$rule[] = FormBuilder::input("start_time", "开始时间", '')->required('请输入开始时间')->props(['placeholder' => '每天几时(0~23)']);
$rule[] = FormBuilder::input("end_time", "结束时间", '')->required('请输入结束时间')->props(['placeholder' => '每天几时(0~23)']);
return create_form($text, $rule, $url, 'POST');
}
/**
* 获取设备ID
* @param $nickName 备注名称
*/
public function getDeviceId($nickName)
{
$client = new Iot($this->config);
$queryDeviceBySQLRequest = new QueryDeviceBySQLRequest([
"iotInstanceId" => self::IOT_INSTANCE_ID,
"SQL" => "select iot_id, name, gmt_create, status from device where nickname = '" . $nickName . "'"
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryDeviceBySQLWithOptions($queryDeviceBySQLRequest, $runtime);
$body = $res->body;
$resultData = [];
if ($body->data == null || !is_array($body->data)) {
return $resultData;
}
foreach ($body->data as $value) {
$resultData[] = get_object_vars($value);
}
return $resultData;
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('设备ID获取失败:' . $error->message);
return $error->message;
}
}
/**
* 获取播放地址
* @param $Scheme 播放协议
* @param @IotId 设备ID
*/
public function QueryLiveStreaming($Scheme, $IotId)
{
$client = new Linkvisual($this->config);
$queryLiveStreamingRequest = new QueryLiveStreamingRequest([
'scheme' => $Scheme,
'iotInstanceId' => self::IOT_INSTANCE_ID,
'iotId' => $IotId,
'playUnLimited' => true,
'urlValidDuration' => 600,
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryLiveStreamingWithOptions($queryLiveStreamingRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('获取播放地址失败:' . $error->message);
return $error->message;
}
}
/**
* IPC设备列表-分页查询
* 设备查询上线为一万台,超出一万台设备需进行功能迭代
*/
public function IpcQueryDeviceRequest()
{
[$page, $limit] = $this->getPageValue();
$client = new Iot($this->config);
$queryDeviceRequest = new QueryDeviceRequest([
"iotInstanceId" => self::IOT_INSTANCE_ID,
"productKey" => self::PRODUCT_KEY,
"pageSize" => $limit,
"currentPage" => $page
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryDeviceWithOptions($queryDeviceRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('查询设备列表失败:' . $error->message);
return $error->message;
}
}
/**
* 修改设备备注名称
* @param $iotId 设备ID
* @param $nickname 备注名称
*/
public function DitDeviceNicknameInfo($iotId, $nickname)
{
$client = new Iot($this->config);
$deviceNicknameInfo0 = new deviceNicknameInfo([
"productKey" => self::PRODUCT_KEY,
"iotId" => $iotId,
"nickname" => $nickname
]);
if (empty($nickname)) {
$deviceNicknameInfo0 = new deviceNicknameInfo([
"productKey" => self::PRODUCT_KEY,
"iotId" => $iotId
]);
}
$batchUpdateDeviceNicknameRequest = new BatchUpdateDeviceNicknameRequest([
"deviceNicknameInfo" => [
$deviceNicknameInfo0
],
"iotInstanceId" => self::IOT_INSTANCE_ID
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->batchUpdateDeviceNicknameWithOptions($batchUpdateDeviceNicknameRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('修改备注名称失败:' . $error->message);
return $error->message;
}
}
/**
* 获取录像文件列表
* 默认时间为当天早8点到下午五点
* @param $iotId 设备ID
*/
public function QueryRecord($iotId)
{
$client = new Linkvisual($this->config);
// $startTime = strtotime('today');
$oneHouse = 60 * 60;
// $beginTime = $startTime + $oneHouse * 8; // 录像开始时间
// $endTime = $startTime + $oneHouse * 17; // 录像结束时间
$endTime = time();
$beginTime = $endTime - ($oneHouse * 24);
$queryRecordRequest = new QueryRecordRequest([
"endTime" => $endTime,
"beginTime" => $beginTime,
"needSnapshot" => true,
"iotInstanceId" => self::IOT_INSTANCE_ID,
"currentPage" => 1,
"pageSize" => 50,
"productKey" => self::PRODUCT_KEY,
"iotId" => $iotId
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryRecordWithOptions($queryRecordRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('获取录像列表失败' . $error->message);
return $error->message;
}
}
/**
* 获取录像文件播放地址
* @param $fileName 录像文件名称
* @param @iotId 设备ID
*/
public function QueryRecordUrlRequest($fileName, $iotId)
{
$client = new Linkvisual($this->config);
$queryRecordUrlRequest = new QueryRecordUrlRequest([
"fileName" => $fileName,
"productKey" => self::PRODUCT_KEY,
"iotInstanceId" => self::IOT_INSTANCE_ID,
"iotId" => $iotId,
"urlValidDuration" => 60 // 地址过期时间
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryRecordUrlWithOptions($queryRecordUrlRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('获取录像文件播放地址失败:' . $error->message);
return $error->message;
}
}
/**
* 添加IPC连续录像计划
*/
public function AddRecordPlanDevice($iotId)
{
$client = new Linkvisual($this->config);
$addRecordPlanDeviceRequest = new AddRecordPlanDeviceRequest([
"iotInstanceId" => self::IOT_INSTANCE_ID,
"productKey" => self::PRODUCT_KEY,
"iotId" => $iotId,
"planId" => self::PLAN_ID,
"streamType" => 0
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->addRecordPlanDeviceWithOptions($addRecordPlanDeviceRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('IPC添加录像计划失败:' . $error->message);
return $error->message;
}
}
/**
* 删除IPC连续录像
*/
public function DeleteRecordPlanDevice($iotId)
{
$client = new Linkvisual($this->config);
$deleteRecordPlanDeviceRequest = new DeleteRecordPlanDeviceRequest([
"iotInstanceId" => self::IOT_INSTANCE_ID,
"productKey" => self::PRODUCT_KEY,
"iotId" => $iotId,
"streamType" => 0
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->deleteRecordPlanDeviceWithOptions($deleteRecordPlanDeviceRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('删除IPC连续录像失败:' . $error->message);
return $error->message;
}
}
/**
* 更新时间模版
*/
public function UpdateTimeTemplate($startDay, $endDay, $startTime = 8, $endTime = 16)
{
$client = new Linkvisual($this->config);
$startTime = 3600 * $startTime;
$endTime = 3600 * $endTime;
$timeSections = [];
for ($i = $startDay; $i <= $endDay; $i++) {
$timeSections[] = new timeSections([
"dayOfWeek" => $i,
"begin" => $startTime,
"end" => $endTime
]);
}
// $timeSections0 = new timeSections([
// "dayOfWeek" => 1,
// "begin" => $startTime,
// "end" => $endTime
// ]);
// $timeSections = [];
$updateTimeTemplateRequest = new UpdateTimeTemplateRequest([
"timeSections" => $timeSections,
"templateId" => self::TEMPLATE_ID,
"allDay" => 0
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->updateTimeTemplateWithOptions($updateTimeTemplateRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('更新时间模版失败:' . $error->message);
return $error->message;
}
}
/**
* 更新录像计划
*/
public function UpdateRecordPlan()
{
$client = new Linkvisual($this->config);
$updateRecordPlanRequest = new UpdateRecordPlanRequest([
"planId" => self::PLAN_ID,
"name" => "text",
"templateId" => self::TEMPLATE_ID
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->updateRecordPlanWithOptions($updateRecordPlanRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('更新录像计划失败' . $error->message);
return $error->message;
}
}
/**
* 对应录像计划列表
*/
public function QueryRecordPlanDeviceByPlan($planId = self::PLAN_ID, $page = 1, $limit = 15)
{
$client = new Linkvisual($this->config);
$queryRecordPlanDeviceByPlanRequest = new QueryRecordPlanDeviceByPlanRequest([
"planId" => $planId,
"currentPage" => $page,
"pageSize" => $limit
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryRecordPlanDeviceByPlanWithOptions($queryRecordPlanDeviceByPlanRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('获取录像计划列表失败:' . $error->message);
return $error->message;
}
}
/**
* 查询录像计划详情
*/
public function QueryRecordPlanDetail($planId = self::PLAN_ID)
{
$client = new Linkvisual($this->config);
$queryRecordPlanDetailRequest = new QueryRecordPlanDetailRequest([
"planId" => $planId
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->queryRecordPlanDetailWithOptions($queryRecordPlanDetailRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('查询录像详情失败:' . $error->message);
return $error->message;
}
}
/**
* 设置云端存储天数
* @param $lotId 设备ID
* @param $day 存储天数;最大可设置3650天
*/
public function SetDeviceRecordLifeCycle(string $lotId = '', int $day = 1)
{
$client = new Linkvisual($this->config);
$setDeviceRecordLifeCycleRequest = new SetDeviceRecordLifeCycleRequest([
"iotInstanceId" => self::IOT_INSTANCE_ID,
"iotId" => $lotId,
"day" => $day
]);
$runtime = new RuntimeOptions([]);
try {
$res = $client->setDeviceRecordLifeCycleWithOptions($setDeviceRecordLifeCycleRequest, $runtime);
$body = $res->body;
return get_object_vars($body);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::error('设置云端存储天数失败:' . $error->message);
return $error->message;
}
}
/**
* 处理时间格式
*/
public function setTimeToTime(array $data)
{
foreach ($data as $key => $value) {
$dateTime = strtotime($value['gmtCreate']);
$data[$key]['gmtCreate'] = date('Y-m-d H:i:s', $dateTime);
}
return $data;
}
}