Dataacceptance.php.bak.2021-11-30 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 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. //addDataInfoInsert
  201. if ($alarm_data=='低压'||$alarm_data=='高压'|$alarm_data=='低水位'|$alarm_data=='高水位'|$alarm_data=='故障'|$alarm_data=='波动告警'){
  202. $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)
  203. VALUES ('','tp_water_pressure','".$device_code."','".$time."','','拓普索尔4G','','".$alarm_type."','".$battery_level."','".$signal_level."','".$alarm_value."','拓普索尔','','0','','','','','0','','')";
  204. Log::record('yangpu4GAlarmSql:' . $sql1);
  205. $Res = addDataInfoInsert($sql1);
  206. //闵行MQTT
  207. // if ($alarm_type=='WP1'||$alarm_type=='WP2'||$alarm_type=='LL9'||$alarm_type=='WP9'){
  208. // if (!empty($device)){
  209. // $json_data_two=$this->mh_array_turn_json(2,$device,$signal_level*20,$battery_level,strtotime($time),'',$alarm_type);
  210. // if (!empty($json_data_two)){
  211. // $this->message_push_mh("mh/water/alert",$json_data_two,0);
  212. // }
  213. // $json_data_three=$this->date_array_turn_json($Res,$device,strtotime($time),$alarm_data,0,'','');
  214. // if (!empty($json_data_three)){
  215. // $this->message_push_mh("mh/water/statistics",$json_data_three,0);
  216. //
  217. // }
  218. // $json_data_four=$this->date_array_turn_json($Res,$device,strtotime($time),$alarm_data,1,'','');
  219. // if (!empty($json_data_four)){
  220. // $this->message_push_mh("mh/water/statistics",$json_data_four,0);
  221. // }
  222. // }
  223. // }
  224. // print_r('id'.$Res);
  225. $phone_data=add("select phone from sp_owner_phone where owner_code='".$res_owner[0]['company']."' group by phone");
  226. $openids = $this->array_column1($phone_data, "phone");
  227. $wx_data['IMEI']=$device_code;
  228. $wx_data['phone']=$openids;
  229. $wx_data['evt']=$alarm_type;
  230. $wx_data['time']=$time;
  231. $wx_data['insert_id']=$Res;
  232. $wx_data['bz']='';
  233. $wx_data['user_from']='water_form';
  234. $wx_data['Status']=$alarm_type;
  235. if ($res_owner[0]['dwtype']==2){
  236. $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water";
  237. }else{
  238. $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Yeweiwarn/message_water";
  239. }
  240. Log::record('yangpu4GWxUrl'.':'.$url);
  241. Log::record('yangpu4GWxData'.':'.json_encode($wx_data, JSON_FORCE_OBJECT));
  242. $res = $this->curl($url,$wx_data);
  243. print_r($res);
  244. }
  245. $returnInformation['code'] = true;
  246. $returnInformation['msg'] = "成功";
  247. return json_encode($returnInformation);
  248. }
  249. public function array_column1($rows, $column_key, $index_key = null) {
  250. $data = array();
  251. if (empty($index_key)) {
  252. foreach ($rows as $row) {
  253. $data[] = $row[$column_key];
  254. }
  255. $data=implode(",",$data);
  256. } else {
  257. foreach ($rows as $row) {
  258. $data[$row[$index_key]] = $row[$column_key];
  259. }
  260. }
  261. return $data;
  262. }
  263. public function curl($url,$data=''){
  264. $ch = curl_init();
  265. curl_setopt($ch, CURLOPT_URL, $url);
  266. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  267. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  268. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  269. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  270. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  271. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  272. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  273. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  274. $info = curl_exec($ch);
  275. if (curl_errno($ch)) {
  276. $info = 'ERROR: ' . curl_error($ch);
  277. }
  278. curl_close($ch);
  279. return $info;
  280. }
  281. /**
  282. * @param $type 1心跳 2报警
  283. * @param $DeviceCode 设备编号
  284. * @param $signalLevel 信号
  285. * @param $volt 电量
  286. * @param $createdTime 时间 时间戳
  287. * @param $param 暂留
  288. * @param $alarm_type 告警编码
  289. * @return false|string|null
  290. */
  291. public function mh_array_turn_json($type,$device,$signalLevel,$volt,$createdTime,$param,$alarm_type){
  292. // $param = [];
  293. $param = ["platform_cy"=>$device['platform_cy'],"scenetype_cy"=>$device['scenetype_cy']];
  294. if ($type==1){
  295. if (!empty($device)){
  296. $arr = [
  297. "deviceCode" => $device['mh_device_code'],
  298. "signalLevel"=>$signalLevel,
  299. "volt"=>$volt,
  300. "lon"=>(double)$device['longitude'],
  301. "lat"=>(double)$device['latitude'],
  302. "createdTime"=>$createdTime,
  303. "param"=>$param
  304. ];
  305. $json_data=json_encode($arr);
  306. }else{
  307. $json_data = null;
  308. }
  309. }else{
  310. if (!empty($device)){
  311. $eventCode1 = "02";
  312. $eventName1 = "预警事件";
  313. switch ($alarm_type) {
  314. case 'WP1':
  315. case 'WP2':
  316. $eventCode2 = '02008';
  317. $eventName2 = '水压报警';
  318. break;
  319. case 'LL9':
  320. case 'WP9':
  321. $eventCode2 = '02002';
  322. $eventName2 = '电量不足';
  323. break;
  324. default:
  325. $eventCode2 = '02000';
  326. $eventName2 = '其他';
  327. break;
  328. }
  329. $arr = [
  330. "deviceCode" => $device['mh_device_code'],
  331. "lon"=>(double)$device['longitude'],
  332. "lat"=>(double)$device['latitude'],
  333. "createdTime"=>$createdTime,
  334. "addr"=>"上海市",
  335. "eventCode1"=>$eventCode1,
  336. "eventName1"=>$eventName1,
  337. "eventCode2"=>$eventCode2,
  338. "eventName2"=>$eventName2,
  339. "description"=>"备注信息",
  340. "param"=>$param
  341. ];
  342. $json_data=json_encode($arr);
  343. }else{
  344. $json_data = null;
  345. }
  346. }
  347. return $json_data;
  348. }
  349. public function date_array_turn_json($insert_id,$device,$createdTime,$ajDescribe,$ajFlag,$enclosure,$image){
  350. if (!empty($device)){
  351. $arr = [
  352. "id" => $insert_id,
  353. "deviceCode"=>$device['mh_device_code'],
  354. "createTime" =>date("Y-m-d H:i:s",$createdTime) ,
  355. "ajType"=>$device['aj_type'],
  356. "street"=>$device['street'],
  357. "address"=>$device['company_address'].','.$device['device_address'],
  358. "grid"=>$device['grid'],
  359. "ajDescribe"=>$ajDescribe,
  360. "dutyGroup"=>$device['company_name'],
  361. "ajFlag"=>$ajFlag,
  362. "endTime"=>date("Y-m-d H:i:s",$createdTime+86400),
  363. "enclosure"=>$enclosure,
  364. "image"=>$image
  365. ];
  366. $json_data=json_encode($arr);
  367. }else{
  368. $json_data=null;
  369. }
  370. return $json_data;
  371. }
  372. public function message_push_mh($topic,$msg,$qos){
  373. vendor('phpMQTT.phpMQTT');
  374. $server = '47.98.201.73'; // 服务器IP
  375. $port = 1883; // 服务器端口
  376. $username = 'usky'; // 用户名
  377. $password = 'usky'; // 密码
  378. $client_id = 'pub_' . uniqid();
  379. $mqtt = new \phpMQTT($server, $port, $client_id);
  380. if ($mqtt->connect(true, NULL, $username, $password)) {
  381. $mqtt->publish($topic, $msg, $qos);
  382. usleep(100000);
  383. $mqtt->close();
  384. Log::record('MH—4G-MQTT-Push success!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  385. } else {
  386. Log::record('MH—4G-MQTT-Time out!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  387. }
  388. }
  389. }