Preview.php
14.2 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
namespace app\api\controller\v7\screen;
use app\common\controller\Api;
use fast\Http;
use think\Db;
use think\Exception;
use think\Validate;
/**
* 管网预演
* Class Preview
* @package app\api\controller\v7\screen
*/
class Preview extends Api
{
public $noNeedLogin = ['*'];
public $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
}
//控制阀门总览
public function overview(){
$result = [
'total_num' => 159,
'normal_num' => 0,
'warning_num' => 0,
'rate' => '0%',
];
$this->success('成功', $result);
}
//流量数据
public function flowData(){
$result = [
'x' => ['01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00'],
'in' => [58,132,134,120,122,150,128,56],
'out' => [135,162,158,122,120,152,132,68],
];
$this->success('成功', $result);
}
//水压数据
public function pressureData(){
$reportLevelList = [
'0' => '无',
'1' => '轻微',
'2' => '严重',
];
$order = [
'a.createtime' => 'DESC'
];
$field = [
'b.name',
'a.number',
'a.value',
'a.report_level',
'a.deal_note',
'a.createtime',
'b.longitude',
'b.latitude'
];
//导出数据列表返回50条
$tableName = 'new_equip_hydraulicpress';
$tableNameData = 'new_equip_hydraulicpress_data';
$list = Db::name($tableNameData)
->alias('a')
->field($field)
->join("{$tableName} b", 'a.number=b.deviceId', 'LEFT')
->order($order)
->limit(0,50)
->select();
foreach ($list as &$row){
$row['createtime_text'] = date('Y-m-d H:i:s');
$row['report_level_text'] = $reportLevelList[$row['report_level']];
}
$this->success('成功', $list);
// $result = [
// [
// 'device_name' => '水压计No.1',
// 'device_code' => 'ex86sa',
// 'device_position' => '勐甸村31公里处',
// 'pressure' => '1.25MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.2',
// 'device_code' => 'de87x5',
// 'device_position' => '勐甸村11公里处',
// 'pressure' => '0.8MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.3',
// 'device_code' => 'w25csa',
// 'device_position' => '勐甸村5公里处',
// 'pressure' => '1.0MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.4',
// 'device_code' => '5s5ax6',
// 'device_position' => '勐甸村9公里处',
// 'pressure' => '1.6MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.5',
// 'device_code' => 'deghs4',
// 'device_position' => '勐甸村11公里处',
// 'pressure' => '2.0MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.6',
// 'device_code' => 'ex8745',
// 'device_position' => '勐甸村28公里处',
// 'pressure' => '2.5MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.7',
// 'device_code' => 'eg246w',
// 'device_position' => '勐甸村29公里处',
// 'pressure' => '0.63MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.8',
// 'device_code' => 'esc46w',
// 'device_position' => '勐甸村34公里处',
// 'pressure' => '1.10MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.9',
// 'device_code' => 'eyuh25',
// 'device_position' => '勐甸村10公里处',
// 'pressure' => '0.93MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.10',
// 'device_code' => '256hjj',
// 'device_position' => '勐甸村22公里处',
// 'pressure' => '0.83MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// [
// 'device_name' => '水压计No.11',
// 'device_code' => '687ety',
// 'device_position' => '勐甸村31公里处',
// 'pressure' => '0.73MPa',
// 'times' => date('Y-m-d H:i'),
// ],
// ];
// $this->success('成功', $result);
}
//预演模型
public function models(){
$result = [
'intro' => '管网预演模型是依托管网拓扑结构,应用水力学原理建立的教学模型并加入智能算法。通过水力模型计算、历史数据分析可以全面了解供水管网任意位置点位的流量、压力和流速等水力状态,为管网规划、设计、改造、维护、分析、调度提供了全面的、细节话的动态模拟。',
'lists' => [
'/uploads/20231224/FoN06J19nMnSf0KXoPQYMLkK8MbH.jpg',
'/uploads/20231224/FoN06J19nMnSf0KXoPQYMLkK8MbH.jpg',
'/uploads/20231224/FoN06J19nMnSf0KXoPQYMLkK8MbH.jpg',
]
];
$this->success('成功', $result);
}
//历史预演及预案
public function history(){
$result = [
[
'title' => '水库阀门预演',
'times' => date('Y-m-d H:i'),
'level' => '1级预警',
'programme' => '启用橙色自动应急预案',
],
[
'title' => '水压测试预演',
'times' => date('Y-m-d H:i'),
'level' => '1级预警',
'programme' => '启用橙色自动应急预案',
],
[
'title' => '渗漏测试预演',
'times' => date('Y-m-d H:i'),
'level' => '1级预警',
'programme' => '启用橙色自动应急预案',
],
[
'title' => '供水量测试预演',
'times' => date('Y-m-d H:i'),
'level' => '1级预警',
'programme' => '启用橙色自动应急预案',
],
[
'title' => '监控测试预演',
'times' => date('Y-m-d H:i'),
'level' => '1级预警',
'programme' => '启用橙色自动应急预案',
],
];
$this->success('成功', $result);
}
//损失分析
public function loss(){
$result = [
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
],
[
'position' => '勐甸村36公里处阀门',
'content' => '阀门处水压问题',
]
];
$this->success('成功', $result);
}
//读取模型数据
public function readModelData(){
$mid = $this->request->param('mid');
$type = $this->request->param('type');
if (!in_array($type, [0,1,2,3,4,5])){
$this->error('type参数错误');
}
if (empty($mid)){
$this->error('模型ID必须');
}
$dataFile = Db::name('pipes_rehearsalmodel')->where(['id' => $mid])->value('data_file');
if (empty($dataFile)){
$data = [];
}else{
$config = get_addon_config('qiniu');
$url = $config['cdnurl'].$dataFile;
$dataStr = file_get_contents($url);
$data = json_decode($dataStr, true);
if (!empty($data)){
if ($type == 0){
foreach ($data as $key=>$value){
if ($value['code'] == 'LJKdm6' || $value['type'] == 3 || $value['type'] == 4 || $value['type'] == 5){//todo 该节点会报错,暂时去除掉
unset($data[$key]);
}
}
}else{
foreach ($data as $key=>$value){
if ($value['type'] != $type || $value['code'] == 'LJKdm6'){//todo 该节点会报错,暂时去除掉
unset($data[$key]);
}
}
}
}
}
$this->success('成功', array_values($data));
}
//预演模型参数
public function parameters(){
$params = $this->request->param();
$validate = new Validate([
'name|方案名称' => 'require|chsDash',
'duration|模型运行的时间长度' => 'require|integer',
'timestep|模拟间隔时间' => 'require|integer',
'mid|模型ID' => 'require|integer|gt:0',
'junctions|节点' => 'require',
]);
if (!$validate->check($params)) {
$this->error($validate->getError());
}
if (is_array($params['junctions'])){
$params['junctions'] = json_encode($params['junctions']);
}
Db::startTrans();
try {
$params['createtime'] = time();
$id = Db::name('pipes_preview_parameters')->insertGetId($params);
if (!$id){
exception('数据保存失败');
}
//调用python接口,传送数据
$url = "http://121.37.3.63:5000/run_model";
$data = [
'mid' => $params['mid'],
'param' => [
'junctions' => json_decode($params['junctions'],true),
'pipes' => [],
'pumps' => [],
'tanks' => [
[
'name' => 'HEoF4C',
'init_level' => 0
]
],
'valves' => [],
'reservoirs' => [],
'model' => []
]
];
$data = json_encode($data);
$option = [
CURLOPT_HTTPHEADER => [ // 设置请求头部信息
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($data),
'Cache-Control: no-cache',
'Pragma: no-cache'
]
];
$response = Http::post($url,$data,$option);
if (is_string($response)){
$response = json_decode($response, true);
}
//记录运行日志
$log = [
'params' => json_decode($data, true),
'response' => $response,
];
file_put_contents(RUNTIME_PATH.'pipes_preview_log_'.date('Y-m-d-H-i-s') . '.log', 'json串格式:'.PHP_EOL.json_encode($log).PHP_EOL.'PHP数组格式:'.PHP_EOL.var_export($log,true), FILE_APPEND);
if (empty($response['code']) || $response['code'] !== 200){
exception('获取预演结果失败:'.$response['msg']);
}
//把返回的结果加上坐标
if(isset($response['data']['junctions']) && is_array($response['data']['junctions'])){
foreach ($response['data']['junctions'] as &$item){
//默认写死查type=2的表
$find = Db::name("pipes_well")->where(['well_code' => $item['name']])->whereNull('deletetime')->find();
if ($find){
$item['lng'] = $find['well_lng'];
$item['lat'] = $find['well_lat'];
}else{
$item['lng'] = '';
$item['lat'] = '';
}
}
unset($item);
}
$updateData = [
'junctions_result' => json_encode($response['data']['junctions'] ?? []),
'model_result' => json_encode($response['data']['model'] ?? []),
'pipes_result' => json_encode($response['data']['pipes'] ?? []),
'pumps_result' => json_encode($response['data']['pumps'] ?? []),
'reservoirs_result' => json_encode($response['data']['reservoirs'] ?? []),
'tanks_result' => json_encode($response['data']['tanks'] ?? []),
'valves_result' => json_encode($response['data']['valves'] ?? []),
];
Db::name('pipes_preview_parameters')->where(['id' => $id])->update($updateData);
Db::commit();
}catch (Exception $e){
Db::rollback();
$this->error($e->getMessage());
}
$this->success('操作成功', $response['data']);
}
}