Equipment2.php
4.3 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
<?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-01 00: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) {
$water_data = $r['data']['L3_SL'][$v['deviceId']];
if (!empty($water_data)) {
//$this->warning($v['deviceId'], $water_data);
foreach ($water_data as $k3 => $v3) {
$insert_data = [];
$insert_data = [
'reservoir_id' => $v['reservoir_id'],
'value' => $v3['value'],
'createtime' => $v3['reportTime'] / 1000,
'updatetime' => $v3['reportTime'] / 1000,
'reporttime' => $v3['reportTime'],
'number' => $v['deviceId']
];
$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());
}
}
}