suyuan-breed/app/api/controller/monitorController.php

37 lines
1.1 KiB
PHP

<?php
namespace app\api\controller;
use app\common\model\device\Device;
use app\common\model\device\MonitorAlarm;
use think\response\Json;
class monitorController extends BaseApiController
{
public array $notNeedLogin = ['alarm'];
//获取报警数据
public function alarm(): Json
{
$params = $this->request->post(['device_code','content','value']);
if(empty($params['device_code']) || empty($params['content']) || empty($params['value'])){
return $this->fail('缺少必要参数');
}
//获取设备信息
$device = Device::where('code',$params['device_code'])->findOrEmpty();
if($device->isEmpty()){
return $this->fail('设备不匹配');
}
//写入数据
$res = MonitorAlarm::create([
'user_id' => $device['user_id'],
'device_id' => $device['id'],
'type' => $device['type'],
'content' => $params['content'],
'value' => $params['value'],
'solution' => '',
'create_time' => time(),
'update_time' => time()
]);
return $res->id ? $this->success('数据添加成功') : $this->fail('数据添加失败');
}
}