Dataacceptance.php.bak.2021-12-01 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Session;
  5. use think\Db;
  6. use think\Request;
  7. use think\Log;
  8. class Dataacceptance extends Controller
  9. {
  10. public function getData()
  11. {
  12. $Data = input('');
  13. if (is_array($Data)) {
  14. $getDataJson = json_encode($Data, 320);
  15. $getdata = json_decode($getDataJson, true);//转成数组
  16. } else {
  17. $getDataJson = $Data;
  18. $getdata = json_decode($getDataJson, true);//转成数组
  19. }
  20. $json_string = json_encode($getdata, JSON_FORCE_OBJECT);
  21. Log::record('yangpu4GRawData:' . $json_string);
  22. $bin_data = $getdata['msg']['value']['bin_data'];
  23. if (empty($bin_data)) {
  24. $returnInformation['code'] = false;
  25. $returnInformation['msg'] = "数据格式错误";
  26. return json_encode($returnInformation);
  27. }
  28. Log::record('yangpu4GData' . ':' . $bin_data);
  29. //数据类型
  30. $type = substr($bin_data, 8, 2);
  31. //数据长度
  32. $length = substr($bin_data, 10, 4);
  33. //设备类型
  34. $device_type = substr($bin_data, 14, 2);
  35. //设备编号截取拼接
  36. $devide_id1 = ((int)substr($bin_data, 16, 2)) - 30;
  37. $devide_id2 = ((int)substr($bin_data, 18, 2)) - 30;
  38. $devide_id3 = ((int)substr($bin_data, 20, 2)) - 30;
  39. $devide_id4 = ((int)substr($bin_data, 22, 2)) - 30;
  40. $devide_id5 = ((int)substr($bin_data, 24, 2)) - 30;
  41. $devide_id6 = ((int)substr($bin_data, 26, 2)) - 30;
  42. $devide_id7 = ((int)substr($bin_data, 28, 2)) - 30;
  43. $devide_id8 = ((int)substr($bin_data, 30, 2)) - 30;
  44. $devide_id9 = ((int)substr($bin_data, 32, 2)) - 30;
  45. $devide_id10 = ((int)substr($bin_data, 34, 2)) - 30;
  46. $devide_id11 = ((int)substr($bin_data, 36, 2)) - 30;
  47. $devide_id12 = ((int)substr($bin_data, 38, 2)) - 30;
  48. $devide_id13 = ((int)substr($bin_data, 40, 2)) - 30;
  49. $devide_id14 = ((int)substr($bin_data, 42, 2)) - 30;
  50. $devide_id15 = ((int)substr($bin_data, 44, 2)) - 30;
  51. $device_code = $devide_id1 . $devide_id2 . $devide_id3 . $devide_id4 . $devide_id5 . $devide_id6 . $devide_id7 . $devide_id8 . $devide_id9 . $devide_id10 . $devide_id11 . $devide_id12 . $devide_id13 . $devide_id14 . $devide_id15;
  52. //发送时间截取拼接
  53. $send_time1 = substr($bin_data, 48, 2);
  54. $send_time2 = substr($bin_data, 50, 2);
  55. $send_time3 = substr($bin_data, 52, 2);
  56. $send_time4 = substr($bin_data, 54, 2);
  57. $send_time5 = substr($bin_data, 56, 2);
  58. $send_time6 = substr($bin_data, 58, 2);
  59. $time = '20' . $send_time1 . '-' . $send_time2 . '-' . $send_time3 . ' ' . $send_time4 . ':' . $send_time5 . ':' . $send_time6;
  60. // print_r($time);
  61. //电池电量
  62. $battery_level = hexdec(substr($bin_data, 60, 2));
  63. //信号强度
  64. $signal_strength = substr($bin_data, 62, 2);
  65. if ((int)$signal_strength <= 6 && (int)$signal_strength != 0) {
  66. $signal_level = 1;
  67. } else if ((int)$signal_strength <= 12) {
  68. $signal_level = 2;
  69. } else if ((int)$signal_strength <= 18) {
  70. $signal_level = 3;
  71. } else if ((int)$signal_strength <= 24) {
  72. $signal_level = 4;
  73. } else if ((int)$signal_strength <= 31) {
  74. $signal_level = 5;
  75. } else {
  76. $signal_level = 0;
  77. }
  78. //设备采集数据解析
  79. $device_type_10 = hexdec($device_type);
  80. if ($device_type_10 != 1 && $device_type_10 != 2 && $device_type_10 != 3 && $device_type_10 != 4) {//&& $device_type_10 != 7 && $device_type_10 != 8 && $device_type_10 != 9
  81. $returnInformation['code'] = false;
  82. $returnInformation['msg'] = "该类型未定义";
  83. return json_encode($returnInformation);
  84. }
  85. $sample_data = substr($bin_data, 64, 8);
  86. $sample_data_unit1 = substr($bin_data, 64, 2);
  87. $sample_data_unit2 = substr($bin_data, 66, 2);
  88. $sample_data_unit3 = substr($bin_data, 68, 2);
  89. $sample_data_unit4 = substr($bin_data, 70, 2);
  90. $sample_data_unit1_high = substr($sample_data_unit1, 0, 1);
  91. $sample_data_unit1_low = substr($sample_data_unit1, 1, 1);
  92. $sample_data_unit2_high = substr($sample_data_unit2, 0, 1);
  93. $sample_data_unit2_low = substr($sample_data_unit2, 1, 1);
  94. $sample_data_unit3_high = substr($sample_data_unit3, 0, 1);
  95. $sample_data_unit3_low = substr($sample_data_unit3, 1, 1);
  96. $sample_data_unit4_high = substr($sample_data_unit4, 0, 1);
  97. $sample_data_unit4_low = substr($sample_data_unit4, 1, 1);
  98. $value = $sample_data_unit2_low . $sample_data_unit3_high . $sample_data_unit3_low . $sample_data_unit4_high . $sample_data_unit4_low;
  99. switch ($sample_data_unit2_high) {
  100. case '1':
  101. $alarm_value = (int)$value * 0.1;
  102. break;
  103. case '2':
  104. $alarm_value = (int)$value * 0.01;
  105. break;
  106. case '3':
  107. $alarm_value = (int)$value * 0.001;
  108. break;
  109. case '4':
  110. $alarm_value = (int)$value * 0.0001;
  111. break;
  112. }
  113. if ($sample_data_unit1_low == 1) {
  114. $alarm_type = 'WP' . $sample_data_unit1_high;
  115. $alarm_value = $alarm_value;
  116. } else if ($sample_data_unit1_low == 2) {
  117. $alarm_type = 'WP' . $sample_data_unit1_high;
  118. $alarm_value = $alarm_value * 0.1;
  119. } else if ($sample_data_unit1_low == 3) {
  120. $alarm_type = 'WP' . $sample_data_unit1_high;
  121. $alarm_value = $alarm_value * 1000;
  122. } else if ($sample_data_unit1_low == 4) {
  123. $alarm_type = 'LL' . $sample_data_unit1_high;
  124. $alarm_value = $alarm_value;
  125. } else {
  126. $returnInformation['code'] = false;
  127. $returnInformation['msg'] = "单位未定义";
  128. return json_encode($returnInformation);
  129. }
  130. switch ($alarm_type) {
  131. case 'LL0':
  132. case 'WP0':
  133. $alarm_data = '正常';
  134. break;
  135. case 'WP1':
  136. $alarm_data = '低压';
  137. break;
  138. case 'WP2':
  139. $alarm_data = '高压';
  140. break;
  141. case 'LL3':
  142. case 'WP3':
  143. $alarm_data = '故障';
  144. break;
  145. case 'LL4':
  146. case 'WP4':
  147. $alarm_data = '波动告警';
  148. break;
  149. case 'LL5':
  150. case 'WP5':
  151. $alarm_data = '碰撞告警';
  152. break;
  153. case 'LL6':
  154. case 'WP6':
  155. $alarm_data = '倾斜告警';
  156. break;
  157. case 'LL7':
  158. case 'WP7':
  159. $alarm_data = '水流告警';
  160. break;
  161. case 'LL8':
  162. case 'WP8':
  163. $alarm_data = '进水告警';
  164. break;
  165. case 'LL9':
  166. case 'WP9':
  167. $alarm_data = '低电量告警';
  168. break;
  169. case 'LL1':
  170. $alarm_data = '低水位';
  171. break;
  172. case 'LL2':
  173. $alarm_data = '高水位';
  174. break;
  175. default:
  176. $returnInformation['code'] = false;
  177. $returnInformation['msg'] = "未知告警";
  178. return json_encode($returnInformation);
  179. break;
  180. }
  181. $res_owner = add("select owner_code,dwtype,company,owner_name from sp_owner where owner_code='" . $device_code . "'");
  182. if (empty($res_owner)) {
  183. $returnInformation['code'] = false;
  184. $returnInformation['msg'] = "该设备未注册";
  185. return json_encode($returnInformation);
  186. }
  187. ////闵行MQTT
  188. $where_one['device_code'] = $device_code;
  189. $where_one['push_status'] = 0;
  190. $device = Db::name('yt_t_mhwater')->where($where_one)->find();
  191. if (!empty($device)) {
  192. $json_data_one = $this->mh_array_turn_json(1, $device, $signal_level * 20, $battery_level, strtotime($time), '', $alarm_type);
  193. if (!empty($json_data_one)) {
  194. $this->message_push_mh("mh/water/info", $json_data_one, 0);
  195. }
  196. }
  197. $sql = "INSERT INTO sp_d" . $device_code . " (id,device_code,port,time,status,address,ncmd,data1,data2,data3,data4,data5) VALUES ('','" . $device_code . "','tp_water_pressure','" . $time . "','','','','" . $alarm_type . "','" . $battery_level . "','" . $signal_level . "','" . $alarm_value . "','11111111111')";
  198. Log::record('yangpu4GRawSql:' . $sql);
  199. $R = addDataInfo($sql);
  200. $json_data_qone = $this->mqqt_data_recombination("INFO", $device_code, $res_owner[0]['owner_name'], $battery_level, $signal_level, $alarm_value);
  201. if (!empty($json_data_qone)){
  202. $topicYp_one = '/usky/ytDP0002/'.$res_owner[0]['company'].'/'.$device_code.'/info';
  203. $this->message_push_mh($topicYp_one, $json_data_qone, 0);
  204. }
  205. //addDataInfoInsert
  206. if ($alarm_data == '低压' || $alarm_data == '高压' | $alarm_data == '低水位' | $alarm_data == '高水位' | $alarm_data == '故障' | $alarm_data == '波动告警') {
  207. $sql1 = "INSERT INTO sp_sj2017 (id,port,device_code,time,status,address,ncmd,data1,data2,data3,data4,data5,cllxr,clsj,clr,clnr,clwb,cldh,clzt,cllx,video)
  208. VALUES ('','tp_water_pressure','" . $device_code . "','" . $time . "','','拓普索尔4G','','" . $alarm_type . "','" . $battery_level . "','" . $signal_level . "','" . $alarm_value . "','拓普索尔','','0','','','','','0','','')";
  209. Log::record('yangpu4GAlarmSql:' . $sql1);
  210. $Res = addDataInfoInsert($sql1);
  211. $json_data_qtwo = $this->mqqt_data_recombination("ALARM", $device_code, $res_owner[0]['owner_name'], $battery_level, $signal_level, $alarm_value);
  212. if (!empty($json_data_qtwo)){
  213. $topicYp_two = '/usky/ytDP0002/'.$res_owner[0]['company'].'/'.$device_code.'/alarm';
  214. $this->message_push_mh($topicYp_two, $json_data_qtwo, 0);
  215. }
  216. //闵行MQTT
  217. // if ($alarm_type=='WP1'||$alarm_type=='WP2'||$alarm_type=='LL9'||$alarm_type=='WP9'){
  218. // if (!empty($device)){
  219. // $json_data_two=$this->mh_array_turn_json(2,$device,$signal_level*20,$battery_level,strtotime($time),'',$alarm_type);
  220. // if (!empty($json_data_two)){
  221. // $this->message_push_mh("mh/water/alert",$json_data_two,0);
  222. // }
  223. // $json_data_three=$this->date_array_turn_json($Res,$device,strtotime($time),$alarm_data,0,'','');
  224. // if (!empty($json_data_three)){
  225. // $this->message_push_mh("mh/water/statistics",$json_data_three,0);
  226. //
  227. // }
  228. // $json_data_four=$this->date_array_turn_json($Res,$device,strtotime($time),$alarm_data,1,'','');
  229. // if (!empty($json_data_four)){
  230. // $this->message_push_mh("mh/water/statistics",$json_data_four,0);
  231. // }
  232. // }
  233. // }
  234. // print_r('id'.$Res);
  235. $phone_data = add("select phone from sp_owner_phone where owner_code='" . $res_owner[0]['company'] . "' group by phone");
  236. $openids = $this->array_column1($phone_data, "phone");
  237. $wx_data['IMEI'] = $device_code;
  238. $wx_data['phone'] = $openids;
  239. $wx_data['evt'] = $alarm_type;
  240. $wx_data['time'] = $time;
  241. $wx_data['insert_id'] = $Res;
  242. $wx_data['bz'] = '';
  243. $wx_data['user_from'] = 'water_form';
  244. $wx_data['Status'] = $alarm_type;
  245. if ($res_owner[0]['dwtype'] == 2) {
  246. $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water";
  247. } else {
  248. $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Yeweiwarn/message_water";
  249. }
  250. Log::record('yangpu4GWxUrl' . ':' . $url);
  251. Log::record('yangpu4GWxData' . ':' . json_encode($wx_data, JSON_FORCE_OBJECT));
  252. $res = $this->curl($url, $wx_data);
  253. print_r($res);
  254. }
  255. $returnInformation['code'] = true;
  256. $returnInformation['msg'] = "成功";
  257. return json_encode($returnInformation);
  258. }
  259. public function array_column1($rows, $column_key, $index_key = null)
  260. {
  261. $data = array();
  262. if (empty($index_key)) {
  263. foreach ($rows as $row) {
  264. $data[] = $row[$column_key];
  265. }
  266. $data = implode(",", $data);
  267. } else {
  268. foreach ($rows as $row) {
  269. $data[$row[$index_key]] = $row[$column_key];
  270. }
  271. }
  272. return $data;
  273. }
  274. public function curl($url, $data = '')
  275. {
  276. $ch = curl_init();
  277. curl_setopt($ch, CURLOPT_URL, $url);
  278. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  279. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  280. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  281. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  282. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  283. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  284. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  285. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  286. $info = curl_exec($ch);
  287. if (curl_errno($ch)) {
  288. $info = 'ERROR: ' . curl_error($ch);
  289. }
  290. curl_close($ch);
  291. return $info;
  292. }
  293. /**
  294. * @param $type 1心跳 2报警
  295. * @param $DeviceCode 设备编号
  296. * @param $signalLevel 信号
  297. * @param $volt 电量
  298. * @param $createdTime 时间 时间戳
  299. * @param $param 暂留
  300. * @param $alarm_type 告警编码
  301. * @return false|string|null
  302. */
  303. public function mh_array_turn_json($type, $device, $signalLevel, $volt, $createdTime, $param, $alarm_type)
  304. {
  305. // $param = [];
  306. $param = ["platform_cy" => $device['platform_cy'], "scenetype_cy" => $device['scenetype_cy']];
  307. if ($type == 1) {
  308. if (!empty($device)) {
  309. $arr = [
  310. "deviceCode" => $device['mh_device_code'],
  311. "signalLevel" => $signalLevel,
  312. "volt" => $volt,
  313. "lon" => (double)$device['longitude'],
  314. "lat" => (double)$device['latitude'],
  315. "createdTime" => $createdTime,
  316. "param" => $param
  317. ];
  318. $json_data = json_encode($arr);
  319. } else {
  320. $json_data = null;
  321. }
  322. } else {
  323. if (!empty($device)) {
  324. $eventCode1 = "02";
  325. $eventName1 = "预警事件";
  326. switch ($alarm_type) {
  327. case 'WP1':
  328. case 'WP2':
  329. $eventCode2 = '02008';
  330. $eventName2 = '水压报警';
  331. break;
  332. case 'LL9':
  333. case 'WP9':
  334. $eventCode2 = '02002';
  335. $eventName2 = '电量不足';
  336. break;
  337. default:
  338. $eventCode2 = '02000';
  339. $eventName2 = '其他';
  340. break;
  341. }
  342. $arr = [
  343. "deviceCode" => $device['mh_device_code'],
  344. "lon" => (double)$device['longitude'],
  345. "lat" => (double)$device['latitude'],
  346. "createdTime" => $createdTime,
  347. "addr" => "上海市",
  348. "eventCode1" => $eventCode1,
  349. "eventName1" => $eventName1,
  350. "eventCode2" => $eventCode2,
  351. "eventName2" => $eventName2,
  352. "description" => "备注信息",
  353. "param" => $param
  354. ];
  355. $json_data = json_encode($arr);
  356. } else {
  357. $json_data = null;
  358. }
  359. }
  360. return $json_data;
  361. }
  362. public function date_array_turn_json($insert_id, $device, $createdTime, $ajDescribe, $ajFlag, $enclosure, $image)
  363. {
  364. if (!empty($device)) {
  365. $arr = [
  366. "id" => $insert_id,
  367. "deviceCode" => $device['mh_device_code'],
  368. "createTime" => date("Y-m-d H:i:s", $createdTime),
  369. "ajType" => $device['aj_type'],
  370. "street" => $device['street'],
  371. "address" => $device['company_address'] . ',' . $device['device_address'],
  372. "grid" => $device['grid'],
  373. "ajDescribe" => $ajDescribe,
  374. "dutyGroup" => $device['company_name'],
  375. "ajFlag" => $ajFlag,
  376. "endTime" => date("Y-m-d H:i:s", $createdTime + 86400),
  377. "enclosure" => $enclosure,
  378. "image" => $image
  379. ];
  380. $json_data = json_encode($arr);
  381. } else {
  382. $json_data = null;
  383. }
  384. return $json_data;
  385. }
  386. public function message_push_mh($topic, $msg, $qos)
  387. {
  388. vendor('phpMQTT.phpMQTT');
  389. $server = '47.98.201.73'; // 服务器IP
  390. $port = 1883; // 服务器端口
  391. $username = 'usky'; // 用户名
  392. $password = 'usky'; // 密码
  393. $client_id = 'pub_' . uniqid();
  394. $mqtt = new \phpMQTT($server, $port, $client_id);
  395. if ($mqtt->connect(true, NULL, $username, $password)) {
  396. $mqtt->publish($topic, $msg, $qos);
  397. usleep(100000);
  398. $mqtt->close();
  399. Log::record('MH—4G-MQTT-Push success!(' . $topic . ')[' . date('Y-m-d H:i:s') . ']:' . json_encode($msg));
  400. } else {
  401. Log::record('MH—4G-MQTT-Time out!(' . $topic . ')[' . date('Y-m-d H:i:s') . ']:' . json_encode($msg));
  402. }
  403. }
  404. public function mqqt_data_recombination($type, $device_code, $device_name, $electric_quantity, $signal, $water_gage)
  405. {
  406. if ($type == 'INFO') {
  407. $status_valie = "0";
  408. } else {
  409. $status_valie = "1";
  410. }
  411. $array = [
  412. "devId" => $device_code,
  413. "connType" => "G",
  414. "deviceName" => $device_name,
  415. "devs" => [
  416. [
  417. "dp" => [
  418. [
  419. "property" => "电量",
  420. "serial" => "201",
  421. "value" => '' . $electric_quantity
  422. ],
  423. [
  424. "property" => "信号",
  425. "serial" => "202",
  426. "value" => '' . $signal
  427. ],
  428. [
  429. "property" => "水压",
  430. "serial" => "203",
  431. "value" => '' . $water_gage
  432. ],
  433. [
  434. "property" => "状态",
  435. "serial" => "204",
  436. "value" => $status_valie
  437. ]
  438. ],
  439. "deviceName" => $device_name,
  440. "timeStamp" => "".time(),
  441. "deviceModel" => "",
  442. "deviceType" => "2",
  443. "devId" => $device_code
  444. ]
  445. ],
  446. "type" => $type,
  447. "timeStamp" => "".time()
  448. ];
  449. $json_data = json_encode($array);
  450. return $json_data;
  451. }
  452. }