Equipment2.php
5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/***
* 匠星设备采集数据
*/
namespace app\api\controller\reservoir;
use app\common\controller\Api;
use app\common\helper\HttpHelper;
use think\Exception;
use think\exception\PDOException;
use think\Request;
use think\Db;
use qx\Qx;
use addons\alisms\controller\Index;
/**
* Class Equipment
* @package app\api\controller\reservoir
*/
class Equipment2 extends Api
{
protected $model = '';
protected $noNeedLogin = ['getlcc', 'getflow','getRainEquipment', 'abnormal', 'day30', 'getQxList', 'equipmentType', 'getData', 'getDisplacement', 'getAllData', 'getIsotonic', 'getRainfall', 'getWaterLevel', 'getReservoirId', 'getSeepage', 'jx_url_data'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
$this->model = new \app\admin\model\reservoir\Equipment;
}
/**
* 1. 采集流量硬件数据
*/
public function getflow()
{
$list = Db::name('reservoir_equipment')->where('type', '1')->where('company_type', '2')->select();
$url = "http://47.108.153.209:2010/api/device/data";
$upt_succ = 0;
// 启动事务
Db::startTrans();
try {
foreach ($list as $k => $v) {
$startTime = Db::name('reservoir_flow')->where(["number" => $v['deviceId'], "reservoir_id" => $v['reservoir_id']])->order('createtime desc')->value('createtime');
$startTime = $startTime ? $startTime + 1 : strtotime("2024-06-05 10:00:00");//数据是从07-14开始才有的,所以默认这个时间点
$time_arr = time_to_today_end($startTime);
if (!empty($time_arr)) {
foreach ($time_arr as $k2 => $v2) {
if (!empty($v2)) {
$s_time = $v2[0];
$e_time = $v2[1];
$url = "http://47.108.153.209:2010/api/device/data";//. "?deviceId=" .$v['apiKey']. "&startTime=" . $s_time . "&endTime=" . $e_time;
$param = ["deviceId" => $v['apiKey'], "startTime" => $s_time, "endTime" => $e_time];
$res = HttpHelper::get($url, $param);
if (!empty($res)) {
$r = json_decode($res, true);
if (!empty($r) && $r['code'] == 0) {
$instantaneous_data = $r['data']['L3_SL']['L3_SL_27'];
$cumulative_data = $r['data']['L3_SL']['L3_SL_60'];
if (!empty($instantaneous_data)) {
//$this->warning($v['deviceId'], $instantaneous_data);
foreach ($instantaneous_data as $k3 => $v3) {
$insert_data = [];
$insert_data = [
'reservoir_id' => $v['reservoir_id'],
'instantaneous_value' =>$v3['value'],
'cumulative_value' => $cumulative_data[$k3]['value'],
'createtime' => $v3['reportTime'] / 1000,
'updatetime' => $v3['reportTime'] / 1000,
'reporttime' => $v3['reportTime'],
'number' => $v['apiKey']
];
$rr = Db::name('reservoir_flow')->insertGetId($insert_data);
if ($rr) {
$upt_succ = $upt_succ + 1;
}
}
}
}
}
}
}
}
}
Db::commit();
return "流量新增成功数量:" . $upt_succ;
// $this->success("水位新增成功数量:" . $upt_succ, $upt_succ);
} catch (PDOException $e) {
Db::rollback();
return $e->getMessage();
// $this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
return $e->getMessage();
// $this->error($e->getMessage());
}
}
public function warning($deviceid, $data)
{
$type = $this->model->where('deviceId', $deviceid)->value('type');
$threshold = Db::name('reservoir_threshold')->where('deviceId', $deviceid)->field('config,reservoir_id')->find();
$config = json_decode($threshold['config'], true);
$reservoir = Db::name('reservoir_list')->where('id', $threshold['reservoir_id'])->field('user_mobile,name')->find();
if (!$config) {
return;
}
//水位
if ($type == '1') {
$arr = explode(',', $config['value']);
sort($arr);
foreach ($data as $k => $v) {
if ($v['value'] < $arr[0] || $v['value'] > $arr[1]) {
$insert = [
'value' => $v['value'],
'warning' => $arr[0] . ',' . $arr[1],
'equipment_id' => $deviceid,
'reservoir_id' => $threshold['reservoir_id'],
'createtime' => time(),
'reporttime' => $v['reportTime']
];
Db::name('reservoir_warning_waterlevel')->insert($insert);
// $sms = new Index();
// $res = $sms->send_param($reservoir['user_mobile'],'SMS_227742188','云南智慧水库安全云平台',array('password'=>$reservoir['name']));
}
}
}
}
}