KevinProvincialOffice.php
44.6 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
<?php
namespace app\api\controller\reservoir;
use app\common\controller\Api;
use app\common\helper\HttpHelper;
use fast\Http;
use function GuzzleHttp\Psr7\str;
use think\Request;
use think\Db;
use qx\Qx;
use addons\alisms\controller\Index;
set_time_limit(0);
/**
* 对接省厅
* Class KevinProvincialOffice
* @package app\api\controller\reservoir
*/
class KevinProvincialOffice extends Api
{
protected $model = '';
protected $skid = "";//水库id
protected $RSCD = "";//水库代码
protected $STCD = "";//测站编码(站码)
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $access_token = "";
protected $pre_base_url = "http://112.112.128.35:2355";//省平台正式推送域名端口
// protected $pre_base_url = "http://124.222.245.117:2355";//省平台测试推送域名端口
//硬件分类编码 1=水位计 L4_NW ;2=渗流计 L3_SL;3=渗压计 L3_SY(L3_DX);5=雨量计 L3_SL;6=位移检测 L1_GP;
protected $equipmentCateType = [
"1" => "L4_NW",
"2" => "L3_SL",
"3" => "L3_SY",
"5" => "L3_SL",
"6" => "L1_GP"
];
//匠星的部分渗压设备对应 探头高程
protected $equipmentTantouHeight = [
"L3_SY_sy015" => 1644.61,//蚂蚱冲水库-1号渗压计
"L3_SY_sy002" => 1642.75,//蚂蚱冲水库-2号渗压计
"L3_SY_sy003" => 1634.45,//蚂蚱冲水库-3号渗压计
"L3_SY_sy004" => 1631.57,//蚂蚱冲水库-4号渗压计
"L3_SY_sy005" => 1651.2,//蚂蚱冲水库-5号渗压计
"L3_SY_sy006" => 1643.36,//蚂蚱冲水库-6号渗压计
"L3_SY_sy007" => 1641.51,//蚂蚱冲水库-7号渗压计
"L3_SY_sy008" => 1632.93,//蚂蚱冲水库-8号渗压计
"L3_SY_sy009" => 1652.5,//蚂蚱冲水库-9号渗压计
"L3_SY_sy010" => 1662.47,//蚂蚱冲水库-10号渗压计
"L3_SY_sy011" => 1649.69,//蚂蚱冲水库-11号渗压计
"L3_SY_sy012" => 1664.34,//蚂蚱冲水库-12号渗压计
"L3_SY_sy013" => 1652.09,//蚂蚱冲水库-13号渗压计
"L3_SY_sy014" => 1648.15,//蚂蚱冲水库-14号渗压计
"L3_SY_sy018" => 1566.34,//鲁嘎水库-1号渗压计
"L3_SY_sy016" => 1578.64,//鲁嘎水库-2号渗压计
"L3_SY_sy017" => 1577.06,//鲁嘎水库-3号渗压计
];
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
$this->model = new \app\admin\model\reservoir\Equipment;
$param = $this->request->param();
if (empty($param['skid'])) {
$this->error("水库id异常");
}
$this->skid = $param['skid'];
$ww = [];
$ww["reservoir_id"] = ["eq", $this->skid];
$skinfo = Db::name("reservoir_provincial_office_token")->where($ww)->find();
$this->RSCD = $skinfo['RSCD'];
$this->STCD = $skinfo['STCD'];
$this->access_token = $this->getToken();
}
/**
* 1、省厅token获取
*/
public function getToken()
{
$ww = [];
$ww["reservoir_id"] = ["eq", $this->skid];
$data = Db::name("reservoir_provincial_office_token")->where($ww)->find();
if (empty($data) || empty($data['access_token']) || $data['expires_in'] <= time()) {
$res = $this->oauth_token($data['client_id'], $data['client_secret']);
if ($res['status'] == 200) {
$access_token = $res['access_token'];
$expries = 23 * 3600;//有效期10小时,这里写成23小时避免服务器时间误差
if (!empty($access_token)) {
$time = time();
$update_data = [
"access_token" => $access_token,
"expires_in" => $time + $expries,
"updatetime" => $time
];
Db::name("reservoir_provincial_office_token")->where("id", $data['id'])->update($update_data);
}
} else {
$access_token = '';
}
} else {
$access_token = $data['access_token'];
}
return $access_token;
}
/**
* 2-1、获取省厅水位数据(最近10条)
* 使用测站编码STCD
*/
public function getOBZ()
{
//"RSCD=53250340002-A4&STCD=80293221",
$url = $this->pre_base_url . "/api/OBZ?STCD=" . $this->STCD;
$header = [
"Authorization:Bearer " . $this->access_token,
];
$res = kevin_get_header_requests($url, $header);
$res = json_decode($res, true);//转成数组
//dump($res);die;
return $res;
}
/**
* 2-2、获取省厅降雨量数据(最近10条)
* 使用测站编码STCD
*/
public function getOBP()
{
//"RSCD=53250340002-A4&STCD=80293221",
$url = $this->pre_base_url . "/api/OBP?STCD=" . $this->STCD;
$header = [
"Authorization:Bearer " . $this->access_token,
];
$res = kevin_get_header_requests($url, $header);
$res = json_decode($res, true);//转成数组
dump($res);
die;
// return $res;
}
/**
* 2-3、获取省厅所有渗压设备列表(所有渗压设备)
* 使用水库代码RSCD
*/
public function getSPPRMP()
{
//"RSCD=53250340002-A4&STCD=80293221",
$url = $this->pre_base_url . "/api/SPPRMP?RSCD=" . $this->RSCD;
$header = [
"Authorization:Bearer " . $this->access_token,
];
$res = kevin_get_header_requests($url, $header);
$res = json_decode($res, true);//转成数组
dump($res);
die;
// return $res;
}
/**
* 2-3-2、获取省厅渗压数据(最近10条)
* 使用水库代码RSCD
*/
public function getSPPR()
{
//"RSCD=53250340002-A4&STCD=80293221",
$url = $this->pre_base_url . "/api/SPPR?RSCD=" . $this->RSCD;
$header = [
"Authorization:Bearer " . $this->access_token,
];
$res = kevin_get_header_requests($url, $header);
$res = json_decode($res, true);//转成数组
dump($res);
die;
// return $res;
}
/**
* 2-4、获取省厅渗流量推送
* 使用测站编码STCD
*/
public function getSPQN()
{
//"RSCD=53250340002-A4&STCD=80293221",
$url = $this->pre_base_url . "/api/OBP?STCD=" . $this->STCD;
$header = [
"Authorization:Bearer " . $this->access_token,
];
$res = kevin_get_header_requests($url, $header);
$res = json_decode($res, true);//转成数组
dump($res);
die;
}
/**
* 3、水位推送(每小时推)
*/
public function pushOBZ()
{
$all_num = 0;
$succ_num = 0;
$err_num = 0;
//查询水位计设备编号
$deviceId = Db::name('reservoir_equipment')->where(["type" => "1", "reservoir_id" => $this->skid])->value("deviceId");
if (!empty($deviceId)) {
$w = [];
$w["number"] = ["=", $deviceId];
$w["reservoir_id"] = ["=", $this->skid];
$w["status"] = ["=", 200];//省平台那边返回的200就是成功 401是token或者权限问题 506是重复了
//优先查询省系统中最新数据上传到哪个时间点了(从本地查)
$lasttime = Db::name("reservoir_provincial_office_push_log")->where($w)->order("last_succ_time desc")->value("last_succ_time");
//dump($lasttime);die;
//优先查询省系统中最新数据上传到哪个时间点了(从省平台get接口查)
// $res = $this->getOBZ();
// dump($res);
// //初始化时间点
// if (!empty($res) && $res['status'] == 200 && !empty($res['data'])) {
// //省平台上有时间、则拿最新一次上报时间,拿库里大于上报时间的数据循环上报
// $lasttime = strtotime($res['data'][0]["tm"]);
// }
// dump($lasttime);
// dump("---");
$ww = [];
$ww["number"] = ["=", $deviceId];
$ww["reservoir_id"] = ["=", $this->skid];
if (!empty($lasttime)) {
$ww["createtime"] = ["gt", $lasttime];
} else {
$lasttime = strtotime("2023-05-22 23:59:59");
$ww["createtime"] = ["gt", $lasttime];//默认从2023-05-23 00:00:00开始推
}
//离此刻最近时间
$jin_time = Db::name("reservoir_rain_water_level")->where($ww)->order("createtime desc")->value("createtime");
//离此刻最远时间
$yuan_time = Db::name("reservoir_rain_water_level")->where($ww)->order("createtime asc")->value("createtime");
// dump(date("Y-m-d H:i:s",$jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s",$yuan_time));//1683727200 (2023-05-10 22:00)
if (!empty($jin_time) && !empty($yuan_time)) {
$jin_time = strtotime(date("Y-m-d H:00:00", $jin_time)) + 3600;//只保留整点 1684810800(2023-05-23 11:00)
$yuan_time = strtotime(date("Y-m-d H:00:00", $yuan_time));//只保留整点 1683727200(2023-05-10 22:00)
// dump(date("Y-m-d H:i:s",$jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s",$yuan_time));//1683727200 (2023-05-10 22:00)
$hour = ($jin_time - $yuan_time) / 3600;//多少个小时 301
// dump($hour);die;
$start_time = $yuan_time;
// $end_time = $jin_time;//1684810800(2023-05-23 11:00)
for ($i = 1; $i <= $hour; $i++) {
$end_time = $start_time + 3600;
// dump(date("Y-m-d H:i:s",$start_time));
// dump(date("Y-m-d H:i:s",$end_time));
// $start_time = $jin_time - 3600 * $i;//1684807200 (2023-05-23 10:00:00)
$w2 = [];
$ids = [];
$w2["number"] = ["=", $deviceId];
$w2["reservoir_id"] = ["=", $this->skid];
//只拿>=整点且时间最早一条
$firstwaterdata = Db::name("reservoir_rain_water_level")
->where($w2)
->whereTime('createtime', '>=', $start_time)
->whereTime('createtime', '<', $end_time)
->order("createtime asc")->field("id,water_level,number,createtime,number,reservoir_id")->find();
if (!empty($firstwaterdata)) {
//水位数据推送给省平台
$all_num = $all_num + 1;
$rr = $this->pushDataForOBZ($firstwaterdata);
if ($rr['state'] == 1) {
$succ_num = $succ_num + 1;
} else {
$err_num = $err_num + 1;
}
}
$start_time = $end_time;//重置开始时间为结束时间
// $end_time = $start_time;//重置结束时间为上次开始时间
}
}
}
$return_data = [$this->skid . "水位推送总数" => $all_num, "成功" => $succ_num, "失败" => $err_num];
//file_put_contents("kevin_provincial_office.log", date("Y-m-d H:i:s") . "||" . json_encode($return_data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
echo json_encode($return_data, JSON_UNESCAPED_UNICODE);
die;
}
/**
* 水位推送封装
*/
public function pushDataForOBZ($param)
{
$url = $this->pre_base_url . "/api/OBZ";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
"RSCD" => $this->RSCD, // 水库代码
"STCD" => $this->STCD, // 站码
"TM" => date("Y-m-d\TH:i:s", $param['createtime']),//"2023-04-06T00:00:00", // 时间,注意这里是ISO 8601 datetime格式
"Z" => $param['water_level'], // 水位,注意这里是浮点数格式
"ZRCD" => "", // 水位注解码,注意这里是字符串格式
"W" => 0.0 // 蓄水量,注意这里是浮点数格式
];
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
$insert_data = [];
$time = time();
$insert_data["reservoir_id"] = $param['reservoir_id'];
$insert_data["number"] = $param['number'];
$insert_data["reportdata"] = json_encode($data, JSON_UNESCAPED_UNICODE);
$insert_data['createtime'] = $time;
$push_succ = 0;//1推送成功 0失败
if (!empty($res)) {
//记录日志
if ($res['status'] == 200) {
$insert_data['last_succ_time'] = $param['createtime'];//把推送成功的表中时间记录下来
$push_succ = 1;
}
$insert_data['status'] = $res['status'];
}
Db::name("reservoir_provincial_office_push_log")->insertGetId($insert_data);
return ["state" => $push_succ, "data" => $res];
}
/**
* 4、雨量推送(每小时推)
*/
public function pushOBP()
{
$all_num = 0;
$succ_num = 0;
$err_num = 0;
//查询雨量计设备编号
$deviceId = Db::name('reservoir_equipment')->where(["type" => "5", "reservoir_id" => $this->skid])->value("deviceId");
// dump($deviceId);
if (!empty($deviceId)) {
$w = [];
$w["number"] = ["=", $deviceId];
$w["reservoir_id"] = ["=", $this->skid];
$w["status"] = ["=", 200];//省平台那边返回的200就是成功 401是token或权限问题 506是重复了
//优先查询省系统中最新数据上传到哪个时间点了(从本地查)
$lasttime = Db::name("reservoir_provincial_office_push_log")->where($w)->order("last_succ_time desc")->value("last_succ_time");
// dump($lasttime);
//优先查询省系统中最新数据上传到哪个时间点了(从省平台get接口查)
// $res = $this->getOBZ();
// dump($res);
// //初始化时间点
// if (!empty($res) && $res['status'] == 200 && !empty($res['data'])) {
// //省平台上有时间、则拿最新一次上报时间,拿库里大于上报时间的数据循环上报
// $lasttime = strtotime($res['data'][0]["tm"]);
// }
// dump($lasttime);
// dump("---");
$ww = [];
$ww["number"] = ["=", $deviceId];
$ww["reservoir_id"] = ["=", $this->skid];
if (!empty($lasttime)) {
$ww["createtime"] = ["gt", $lasttime];
} else {
$lasttime = strtotime("2023-05-22 23:59:59");
$ww["createtime"] = ["gt", $lasttime];//默认从2023-05-23 00:00:00开始推
}
//离此刻最近时间
$jin_time = Db::name("reservoir_rain_rainfall")->where($ww)->order("createtime desc")->value("createtime");
//离此刻最远时间
$yuan_time = Db::name("reservoir_rain_rainfall")->where($ww)->order("createtime asc")->value("createtime");
// dump(date("Y-m-d H:i:s",$jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s",$yuan_time));//1683727200 (2023-05-10 22:00)
if (!empty($jin_time) && !empty($yuan_time)) {
$jin_time = strtotime(date("Y-m-d H:00:00", $jin_time)) + 3600;//只保留整点 1684810800(2023-05-23 11:00)
$yuan_time = strtotime(date("Y-m-d H:00:00", $yuan_time));//只保留整点 1683727200(2023-05-10 22:00)
// dump(date("Y-m-d H:i:s",$jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s",$yuan_time));//1683727200 (2023-05-10 22:00)
$hour = ($jin_time - $yuan_time) / 3600;//多少个小时 301
// dump($hour);die;
$start_time = $yuan_time;
// $end_time = $jin_time;//1684810800(2023-05-23 11:00)
for ($i = 1; $i <= $hour; $i++) {
$end_time = $start_time + 3600;
// dump(date("Y-m-d H:i:s",$start_time));
// dump(date("Y-m-d H:i:s",$end_time));
// $start_time = $jin_time - 3600 * $i;//1684807200 (2023-05-23 10:00:00)
$w2 = [];
$ids = [];
$w2["number"] = ["=", $deviceId];
$w2["reservoir_id"] = ["=", $this->skid];
//只拿>=整点且时间最早一条
$firstdata = Db::name("reservoir_rain_rainfall")
->where($w2)
->whereTime('createtime', '>=', $start_time)
->whereTime('createtime', '<', $end_time)
->order("createtime asc")->field("id,rainfall,number,createtime,number,reservoir_id")->find();
if (!empty($firstdata)) {
//雨量数据推送给省平台
$all_num = $all_num + 1;
//$rr = $this->pushDataForOBP($firstdata);//默认1小时
$rr = $this->pushDataForOBPMin5($firstdata);//汛期5分钟
if ($rr['state'] == 1) {
$succ_num = $succ_num + 1;
} else {
$err_num = $err_num + 1;
}
}
$start_time = $end_time;//重置开始时间为结束时间
// $end_time = $start_time;//重置结束时间为上次开始时间
}
}
}
$return_data = [$this->skid . "雨量推送总数" => $all_num, "成功" => $succ_num, "失败" => $err_num];
//file_put_contents("kevin_provincial_office.log", date("Y-m-d H:i:s") . "||" . json_encode($return_data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
echo json_encode($return_data, JSON_UNESCAPED_UNICODE);
die;
}
/**
* 雨量推送封装
*/
public function pushDataForOBP($param)
{
$url = $this->pre_base_url . "/api/OBP";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
// "RSCD" => $this->RSCD, // 水库代码
"STCD" => $this->STCD, // 站码
"TM" => date("Y-m-d\TH:i:s", $param['createtime']),//"2023-04-06T00:00:00", // 时间,注意这里是ISO 8601 datetime格式
"DR" => "60", // 时段长(分钟),默认写60
"P" => $param['rainfall'], // 降水量,注意这里是浮点数格式
"PRCD" => "" // 降水量注解码,注意这里是字符串格式
];
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
$insert_data = [];
$time = time();
$insert_data["reservoir_id"] = $param['reservoir_id'];
$insert_data["number"] = $param['number'];
$insert_data["reportdata"] = json_encode($data, JSON_UNESCAPED_UNICODE);
$insert_data['createtime'] = $time;
$push_succ = 0;//1推送成功 0失败
if (!empty($res)) {
//记录日志
if ($res['status'] == 200) {
$insert_data['last_succ_time'] = $param['createtime'];//把推送成功的表中时间记录下来
$push_succ = 1;
}
$insert_data['status'] = $res['status'];
}
Db::name("reservoir_provincial_office_push_log")->insertGetId($insert_data);
return ["state" => $push_succ, "data" => $res];
}
/**
* 雨量推送封装
*/
public function pushDataForOBPMin5($param)
{
$url = $this->pre_base_url . "/api/OBP";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
// "RSCD" => $this->RSCD, // 水库代码
"STCD" => $this->STCD, // 站码
"TM" => date("Y-m-d\TH:i:s", $param['createtime']),//"2023-04-06T00:00:00", // 时间,注意这里是ISO 8601 datetime格式
"DR" => "5", // 时段长(分钟),默认写60
"P" => $param['rainfall'], // 降水量,注意这里是浮点数格式
"PRCD" => "" // 降水量注解码,注意这里是字符串格式
];
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
$insert_data = [];
$time = time();
$insert_data["reservoir_id"] = $param['reservoir_id'];
$insert_data["number"] = $param['number'];
$insert_data["reportdata"] = json_encode($data, JSON_UNESCAPED_UNICODE);
$insert_data['createtime'] = $time;
$push_succ = 0;//1推送成功 0失败
if (!empty($res)) {
//记录日志
if ($res['status'] == 200) {
$insert_data['last_succ_time'] = $param['createtime'];//把推送成功的表中时间记录下来
$push_succ = 1;
}
$insert_data['status'] = $res['status'];
}
Db::name("reservoir_provincial_office_push_log")->insertGetId($insert_data);
return ["state" => $push_succ, "data" => $res];
}
/**
* 5-1、上传压力测点点位(渗压设备的测点信息)
*/
public function pushSPPRMP()
{
$succ_num = 0;
$err_num = 0;
//查询渗压计列表
$equipment_list = Db::name('reservoir_equipment')->where(["type" => "3", "reservoir_id" => $this->skid, "report_province_office" => 0])->order("id asc")->select();
if (!empty($equipment_list)) {
foreach ($equipment_list as $k => $v) {
$data = [];
if ($v['servicetime']) {
$INDT = strtotime($v['servicetime']);
} elseif ($v['createtime']) {
$INDT = $v['servicetime'];
} else {
$INDT = time();
}
$url = $this->pre_base_url . "/api/SPPRMP";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
"RSCD" => $this->RSCD, // 水库代码
"HYCNCD" => $v['deviceId'], // 水工建筑物编号
"MPCD" => $v['deviceId'], // 测点编号
"TXINDT" => date("Y-m-d\TH:i:s", time()),//"2023-05-25T00:00:00", // 考证信息日期,注意这里是ISO 8601 datetime格式
"CH" => $v['id'],//$v['deviceId'], // 桩号
"OFAX" => 0.0, // 轴距,注意这里是浮点数格式
"EL" => $v['orifice_elevation'] ? $v['orifice_elevation'] : 0, // 高程,注意这里是浮点数格式
"MSTP" => 1, // 监测类型1=坝体渗流压力,2=坝基渗流压力,3=绕坝渗流压力,4=扬压力,5=地下水位,6=洞室外水压力,7=围岩渗透压力,9=其他
"PMBTEL" => 0.0, // 透水段底高程,注意这里是浮点数格式
"PMTPEL" => 0.0, // 透水段顶高程,注意这里是浮点数格式
"INDT" => date("Y-m-d\TH:i:s", $INDT),//"2023-04-06T00:00:00", // 安装日期,注意这里是ISO 8601 datetime格式
"DVFCCD" => "", // 仪器出厂编号
"LGTD" => $v['longitude'] ? $v['longitude'] : 0.0, // 经度,注意这里是浮点数格式
"LTTD" => $v['latitude'] ? $v['latitude'] : 0.0, // 纬度,注意这里是浮点数格式
"WKCN" => 1, // 工作状态:工作状态取值及其代码,1为正常,0为失效;
"RM" => $v['name'] // 备注
];
// dump($data);die;
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
if (!empty($res) && $res['status'] == 200) {
$succ_num = $succ_num + 1;
//更新渗压测点上报状态
Db::name("reservoir_equipment")->where("id", $v['id'])->update(["report_province_office" => 1]);
} else {
$err_num = $err_num + 1;
}
}
}
$return_data = [$this->skid . "渗压测点同步总数" => count($equipment_list), "成功" => $succ_num, "失败" => $err_num];
//file_put_contents("kevin_provincial_office.log", date("Y-m-d H:i:s") . "||" . json_encode($return_data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
echo json_encode($return_data, JSON_UNESCAPED_UNICODE);
die;
}
/**
* 5-2、渗压推送(每小时推)
*/
public function pushSPPR()
{
$all_num = 0;
$succ_num = 0;
$err_num = 0;
//查询渗压计设备列表
$equipment_list = Db::name('reservoir_equipment')->where(["type" => "3", "reservoir_id" => $this->skid])->order("id asc")->select();
if (!empty($equipment_list)) {
foreach ($equipment_list as $k => $v) {
$deviceId = $v['deviceId'];
if (!empty($deviceId)) {
$w = [];
$w["number"] = ["=", $deviceId];
$w["reservoir_id"] = ["=", $this->skid];
$w["status"] = ["=", 200];//省平台那边返回的200就是成功 401是token或权限问题 506是重复了
//优先查询省系统中最新数据上传到哪个时间点了(从本地查)
$lasttime = "";
$lasttime = Db::name("reservoir_provincial_office_push_log")->where($w)->order("last_succ_time desc")->value("last_succ_time");
// dump($lasttime);
//优先查询省系统中最新数据上传到哪个时间点了(从省平台get接口查)
// $res = $this->getOBZ();
// dump($res);
// //初始化时间点
// if (!empty($res) && $res['status'] == 200 && !empty($res['data'])) {
// //省平台上有时间、则拿最新一次上报时间,拿库里大于上报时间的数据循环上报
// $lasttime = strtotime($res['data'][0]["tm"]);
// }
// dump($lasttime);
// dump("---");
$ww = [];
$ww["equipment_id"] = ["=", $deviceId];
$ww["reservoir_id"] = ["=", $this->skid];
if (!empty($lasttime)) {
$ww["createtime"] = ["gt", $lasttime];
} else {
$lasttime = strtotime("2023-05-22 23:59:59");
$ww["createtime"] = ["gt", $lasttime];//默认从2023-05-23 00:00:00开始推
}
//离此刻最近时间
$jin_time = "";
$yuan_time = "";
$jin_time = Db::name("reservoir_dam_isotonic")->where($ww)->order("createtime desc")->value("createtime");
//离此刻最远时间
$yuan_time = Db::name("reservoir_dam_isotonic")->where($ww)->order("createtime asc")->value("createtime");
// dump(date("Y-m-d H:i:s", $jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s", $yuan_time));//1683727200 (2023-05-10 22:00)
if (!empty($jin_time) && !empty($yuan_time)) {
$jin_time = strtotime(date("Y-m-d H:00:00", $jin_time)) + 3600;//只保留整点 1684810800(2023-05-23 11:00)
$yuan_time = strtotime(date("Y-m-d H:00:00", $yuan_time));//只保留整点 1683727200(2023-05-10 22:00)
// dump(date("Y-m-d H:i:s", $jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s", $yuan_time));//1683727200 (2023-05-10 22:00)
$hour = ($jin_time - $yuan_time) / 3600;//多少个小时 301
// dump($hour);
// die;
$start_time = $yuan_time;
// $end_time = $jin_time;//1684810800(2023-05-23 11:00)
for ($i = 1; $i <= $hour; $i++) {
$end_time = $start_time + 3600;
// dump(date("Y-m-d H:i:s",$start_time));
// dump(date("Y-m-d H:i:s",$end_time));
// $start_time = $jin_time - 3600 * $i;//1684807200 (2023-05-23 10:00:00)
$w2 = [];
$ids = [];
$w2["equipment_id"] = ["=", $deviceId];
$w2["reservoir_id"] = ["=", $this->skid];
//只拿>=整点且时间最早一条
$firstdata = Db::name("reservoir_dam_isotonic")
->where($w2)
->whereTime('createtime', '>=', $start_time)
->whereTime('createtime', '<', $end_time)
->order("createtime asc")->field("id,value,dx_value,equipment_id as number,temp,createtime,reservoir_id")->find();
// dump($firstdata);die;
if (!empty($firstdata)) {
$firstdata['water_surface_elevation'] = dam_isotonic_attr_data($v['orifice_elevation'], $v['hole_depth'], $firstdata['dx_value'], 2);
//渗压数据推送给省平台
$all_num = $all_num + 1;
$rr = $this->pushDataForSPPR($firstdata);
if ($rr['state'] == 1) {
$succ_num = $succ_num + 1;
} else {
$err_num = $err_num + 1;
}
}
$start_time = $end_time;//重置开始时间为结束时间
// $end_time = $start_time;//重置结束时间为上次开始时间
}
}
}
$return_data = [];
$return_data = [$this->skid . "渗压" . $deviceId . "推送总数" => $all_num, "成功" => $succ_num, "失败" => $err_num];
//file_put_contents("kevin_provincial_office.log", date("Y-m-d H:i:s") . "||" . json_encode($return_data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
echo json_encode($return_data, JSON_UNESCAPED_UNICODE) . "\n<br>";
}
}
}
/**
* 渗压推送封装
*/
public function pushDataForSPPR($param)
{
$url = $this->pre_base_url . "/api/SPPR";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
"RSCD" => $this->RSCD, // 水库代码
// "STCD" => $this->STCD, // 站码
"HYCNCD" => $param['number'],// 水工建筑物编号
"MPCD" => $param['number'],// 测点编号
"MSTM" => date("Y-m-d\TH:i:s", $param['createtime']),//"2023-04-06T00:00:00", // 时间,注意这里是ISO 8601 datetime格式
"TM" => $param['temp'] ? $param['temp'] : 0.0, // 温度,float类型(渗流压力测点的温度值)
"SPPRWL" => $param['water_surface_elevation'] ? $param['water_surface_elevation'] : 0.0 // 渗流压力水位,float类型(测点渗流压力水位高程值)
];
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
$insert_data = [];
$time = time();
$insert_data["reservoir_id"] = $param['reservoir_id'];
$insert_data["number"] = $param['number'];
$insert_data["reportdata"] = json_encode($data, JSON_UNESCAPED_UNICODE);
$insert_data['createtime'] = $time;
$push_succ = 0;//1推送成功 0失败
if (!empty($res)) {
//记录日志
if ($res['status'] == 200) {
$insert_data['last_succ_time'] = $param['createtime'];//把推送成功的表中时间记录下来
$push_succ = 1;
}
$insert_data['status'] = $res['status'];
}
Db::name("reservoir_provincial_office_push_log")->insertGetId($insert_data);
return ["state" => $push_succ, "data" => $res];
}
/**
* 6-1、上传渗流量测点(渗流设备测点信息)
*/
public function pushSPQNMP()
{
$succ_num = 0;
$err_num = 0;
//查询渗流计列表
$equipment_list = Db::name('reservoir_equipment')->where(["type" => "2", "reservoir_id" => $this->skid, "report_province_office" => 0])->order("id asc")->select();
if (!empty($equipment_list)) {
foreach ($equipment_list as $k => $v) {
$data = [];
if ($v['servicetime']) {
$INDT = strtotime($v['servicetime']);
} elseif ($v['createtime']) {
$INDT = $v['servicetime'];
} else {
$INDT = time();
}
$url = $this->pre_base_url . "/api/SPQNMP";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
"RSCD" => $this->RSCD, // 水库代码
"HYCNCD" => $v['deviceId'], // 水工建筑物编号
"MPCD" => $v['deviceId'], // 测点编号
"TXINDT" => date("Y-m-d\TH:i:s", time()),//"2023-05-25T00:00:00", // 考证信息日期,注意这里是ISO 8601 datetime格式
"CH" => $v['id'],//$v['deviceId'], // 桩号
"OFAX" => 0.0, // 轴距,注意这里是浮点数格式
"EL" => $v['orifice_elevation'] ? $v['orifice_elevation'] : 0, // 高程,注意这里是浮点数格式
"INDT" => date("Y-m-d\TH:i:s", $INDT),//"2023-04-06T00:00:00", // 安装日期,注意这里是ISO 8601 datetime格式
"DVFCCD" => "", // 仪器出厂编号
"LGTD" => $v['longitude'] ? $v['longitude'] : 0.0, // 经度,注意这里是浮点数格式
"LTTD" => $v['latitude'] ? $v['latitude'] : 0.0, // 纬度,注意这里是浮点数格式
"WKCN" => 1, // 工作状态:工作状态取值及其代码,1为正常,0为失效;
"RM" => $v['name'] // 备注
];
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
if (!empty($res) && $res['status'] == 200) {
$succ_num = $succ_num + 1;
//更新渗流测点上报状态
Db::name("reservoir_equipment")->where("id", $v['id'])->update(["report_province_office" => 1]);
} else {
$err_num = $err_num + 1;
}
}
}
$return_data = [$this->skid . "渗流测点同步总数" => count($equipment_list), "成功" => $succ_num, "失败" => $err_num];
//file_put_contents("kevin_provincial_office.log", date("Y-m-d H:i:s") . "||" . json_encode($return_data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
echo json_encode($return_data, JSON_UNESCAPED_UNICODE);
die;
}
/**
* 6-2、渗流量推送(每小时推)
*/
public function pushSPQN()
{
$all_num = 0;
$succ_num = 0;
$err_num = 0;
//查询渗流计设备编号
$deviceId = Db::name('reservoir_equipment')->where(["type" => "2", "reservoir_id" => $this->skid])->value("deviceId");
if (!empty($deviceId)) {
$w = [];
$w["number"] = ["=", $deviceId];
$w["reservoir_id"] = ["=", $this->skid];
$w["status"] = ["=", 200];//省平台那边返回的200就是成功 401是token或权限问题 506是重复了
//优先查询省系统中最新数据上传到哪个时间点了(从本地查)
$lasttime = Db::name("reservoir_provincial_office_push_log")->where($w)->order("last_succ_time desc")->value("last_succ_time");
// dump($lasttime);
//优先查询省系统中最新数据上传到哪个时间点了(从省平台get接口查)
// $res = $this->getOBZ();
// dump($res);
// //初始化时间点
// if (!empty($res) && $res['status'] == 200 && !empty($res['data'])) {
// //省平台上有时间、则拿最新一次上报时间,拿库里大于上报时间的数据循环上报
// $lasttime = strtotime($res['data'][0]["tm"]);
// }
// dump($lasttime);
// dump("---");
$ww = [];
$ww["equipment_id"] = ["=", $deviceId];
$ww["reservoir_id"] = ["=", $this->skid];
if (!empty($lasttime)) {
$ww["createtime"] = ["gt", $lasttime];
} else {
$lasttime = strtotime("2023-05-22 23:59:59");
$ww["createtime"] = ["gt", $lasttime];//默认从2023-05-23 00:00:00开始推
}
//离此刻最近时间
$jin_time = Db::name("reservoir_dam_seepage")->where($ww)->order("createtime desc")->value("createtime");
//离此刻最远时间
$yuan_time = Db::name("reservoir_dam_seepage")->where($ww)->order("createtime asc")->value("createtime");
// dump(date("Y-m-d H:i:s",$jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s",$yuan_time));//1683727200 (2023-05-10 22:00)
if (!empty($jin_time) && !empty($yuan_time)) {
$jin_time = strtotime(date("Y-m-d H:00:00", $jin_time)) + 3600;//只保留整点 1684810800(2023-05-23 11:00)
$yuan_time = strtotime(date("Y-m-d H:00:00", $yuan_time));//只保留整点 1683727200(2023-05-10 22:00)
// dump(date("Y-m-d H:i:s",$jin_time));//1684807200 (2023-05-23 10:00)
// dump(date("Y-m-d H:i:s",$yuan_time));//1683727200 (2023-05-10 22:00)
$hour = ($jin_time - $yuan_time) / 3600;//多少个小时 301
// dump($hour);die;
$start_time = $yuan_time;
// $end_time = $jin_time;//1684810800(2023-05-23 11:00)
for ($i = 1; $i <= $hour; $i++) {
$end_time = $start_time + 3600;
// dump(date("Y-m-d H:i:s",$start_time));
// dump(date("Y-m-d H:i:s",$end_time));
// $start_time = $jin_time - 3600 * $i;//1684807200 (2023-05-23 10:00:00)
$w2 = [];
$ids = [];
$w2["equipment_id"] = ["=", $deviceId];
$w2["reservoir_id"] = ["=", $this->skid];
//只拿>=整点且时间最早一条
$firstdata = Db::name("reservoir_dam_seepage")
->where($w2)
->whereTime('createtime', '>=', $start_time)
->whereTime('createtime', '<', $end_time)
->order("createtime asc")->field("id,value,equipment_id as number,createtime,reservoir_id")->find();
if (!empty($firstdata)) {
//渗流数据推送给省平台
$all_num = $all_num + 1;
$rr = $this->pushDataForSPQN($firstdata);
if ($rr['state'] == 1) {
$succ_num = $succ_num + 1;
} else {
$err_num = $err_num + 1;
}
}
$start_time = $end_time;//重置开始时间为结束时间
// $end_time = $start_time;//重置结束时间为上次开始时间
}
}
}
$return_data = [$this->skid . "渗流推送总数" => $all_num, "成功" => $succ_num, "失败" => $err_num];
//file_put_contents("kevin_provincial_office.log", date("Y-m-d H:i:s") . "||" . json_encode($return_data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
echo json_encode($return_data, JSON_UNESCAPED_UNICODE);
die;
}
/**
* 渗流推送封装
*/
public function pushDataForSPQN($param)
{
$url = $this->pre_base_url . "/api/SPQN";
$header = [
"Authorization:Bearer " . $this->access_token,
"Content-type: application/json",
];
$data = [
"RSCD" => $this->RSCD, // 水库代码
// "STCD" => $this->STCD, // 站码
"HYCNCD" => $param['number'],// 水工建筑物编号
"MPCD" => $param['number'],// 测点编号
"MSTM" => date("Y-m-d\TH:i:s", $param['createtime']),//"2023-04-06T00:00:00", // 时间,注意这里是ISO 8601 datetime格式
"TM" => 0.0, // 温度,float类型
"SPQN" => $param['value'], // 渗流量,float类型
"STSPQN" => 0.0 // 标准水温渗流量,float类型
];
$res = kevin_post_header_json_requests($url, $header, $data);
$res = json_decode($res, true);//转成数组
$insert_data = [];
$time = time();
$insert_data["reservoir_id"] = $param['reservoir_id'];
$insert_data["number"] = $param['number'];
$insert_data["reportdata"] = json_encode($data, JSON_UNESCAPED_UNICODE);
$insert_data['createtime'] = $time;
$push_succ = 0;//1推送成功 0失败
if (!empty($res)) {
//记录日志
if ($res['status'] == 200) {
$insert_data['last_succ_time'] = $param['createtime'];//把推送成功的表中时间记录下来
$push_succ = 1;
}
$insert_data['status'] = $res['status'];
}
Db::name("reservoir_provincial_office_push_log")->insertGetId($insert_data);
return ["state" => $push_succ, "data" => $res];
}
/**
* url请求省厅token
*/
public function oauth_token($client_id, $client_secret)
{
$url = $this->pre_base_url . "/api/login";
$data = [
"id" => $client_id,
"key" => $client_secret,
];
$res = kevin_post_header_json_requests($url, [], $data);
$res = json_decode($res, true);//转成数组
return $res;
}
/**
* 2、水库监测点数据上报封装
* 水库监测点数据上报,监测类型编号需根据监测项目类型,列表中编号定义,监测点编号按照实际情况定义并且唯一
* 监测项目类型编号 监测项目类型名称
* SYSW 上游水位
* XYSW 下游水位
* JYL 降雨量
* QW 气温
* SPWYY 水平位移 Y 向
* SPWYX 水平位移 X 向
* CZWY 垂直位移
* BTSYSW 坝体渗压水位
* BTCYGSW 坝体测压管水位
* SLL 渗漏量
* RBSL 绕坝渗流
* YYL 扬压力
* CFKD 测缝开度
* BJSYSW 坝基渗压水位
* BJSYGSW 坝基测压管水位
* GJJ 钢筋计
* MSCLJ 锚索测力计
* KSW 库水位其他方式
* YB 应变
* BQSW 坝前水温
* RKLL 入库流量
* CKLL 出库流量
*/
public function post_data()
{
$url = "http://yn.damclouds.com:13120/dam/api/v2/monitorData/push";
$headers = [
"Authorization" => "Bearer " . $this->access_token
];
$post_data = [
"registerNo" => "", //全国水库注册登记号
"data" => [ //监测点监测数据
"SYSW" => [ //水位
"L4_NW_2" => [
"cz" => 1322.47, //水位值
"time" => 1677121200000 //2023-02-23 11:00:00
]
],
"JYL" => [ //降雨量
"YLd" => [ //日降雨量
"cz" => 1693.82,
"time" => 1620958698000
],
"YLh" => [ //小时降雨量
"cz" => 1693.82,
"time" => 1620958698000
]
]
]
];
}
}