where($where_one)->find(); if (!empty($device)){ $json_data_one=$this->mh_array_turn_json(1,$device,$signal_level*20,$battery_level,strtotime($time),'',$alarm_type); if (!empty($json_data_one)){ $this->message_push_mh("mh/water/info",$json_data_one,0); } } $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')"; Log::record('yangpu4GRawSql:' . $sql); $R = addDataInfo($sql); //addDataInfoInsert if ($alarm_data=='低压'||$alarm_data=='高压'|$alarm_data=='低水位'|$alarm_data=='高水位'|$alarm_data=='故障'|$alarm_data=='波动告警'){ $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) VALUES ('','tp_water_pressure','".$device_code."','".$time."','','拓普索尔4G','','".$alarm_type."','".$battery_level."','".$signal_level."','".$alarm_value."','拓普索尔','','0','','','','','0','','')"; Log::record('yangpu4GAlarmSql:' . $sql1); $Res = addDataInfoInsert($sql1); //闵行MQTT if ($alarm_type=='WP1'||$alarm_type=='WP2'||$alarm_type=='LL9'||$alarm_type=='WP9'){ if (!empty($device)){ $json_data_two=$this->mh_array_turn_json(2,$device,$signal_level*20,$battery_level,strtotime($time),'',$alarm_type); if (!empty($json_data_two)){ $this->message_push_mh("mh/water/alert",$json_data_two,0); } $json_data_three=$this->date_array_turn_json($Res,$device,strtotime($time),$alarm_data,0,'',''); if (!empty($json_data_three)){ $this->message_push_mh("mh/water/statistics",$json_data_three,0); } $json_data_four=$this->date_array_turn_json($Res,$device,strtotime($time),$alarm_data,1,'',''); if (!empty($json_data_four)){ $this->message_push_mh("mh/water/statistics",$json_data_four,0); } } } // print_r('id'.$Res); $phone_data=add("select phone from sp_owner_phone where owner_code='".$res_owner[0]['company']."' group by phone"); $openids = $this->array_column1($phone_data, "phone"); $wx_data['IMEI']=$device_code; $wx_data['phone']=$openids; $wx_data['evt']=$alarm_type; $wx_data['time']=$time; $wx_data['insert_id']=$Res; $wx_data['bz']=''; $wx_data['user_from']='water_form'; $wx_data['Status']=$alarm_type; if ($res_owner[0]['dwtype']==2){ $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water"; }else{ $url = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Yeweiwarn/message_water"; } Log::record('yangpu4GWxUrl'.':'.$url); Log::record('yangpu4GWxData'.':'.json_encode($wx_data, JSON_FORCE_OBJECT)); $res = $this->curl($url,$wx_data); print_r($res); } $returnInformation['code'] = true; $returnInformation['msg'] = "成功"; return json_encode($returnInformation); } public function array_column1($rows, $column_key, $index_key = null) { $data = array(); if (empty($index_key)) { foreach ($rows as $row) { $data[] = $row[$column_key]; } $data=implode(",",$data); } else { foreach ($rows as $row) { $data[$row[$index_key]] = $row[$column_key]; } } return $data; } public function curl($url,$data=''){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $info = curl_exec($ch); if (curl_errno($ch)) { $info = 'ERROR: ' . curl_error($ch); } curl_close($ch); return $info; } /** * @param $type 1心跳 2报警 * @param $DeviceCode 设备编号 * @param $signalLevel 信号 * @param $volt 电量 * @param $createdTime 时间 时间戳 * @param $param 暂留 * @param $alarm_type 告警编码 * @return false|string|null */ public function mh_array_turn_json($type,$device,$signalLevel,$volt,$createdTime,$param,$alarm_type){ $param = []; if ($type==1){ if (!empty($device)){ $arr = [ "deviceCode" => $device['mh_device_code'], "signalLevel"=>$signalLevel, "volt"=>$volt, "lon"=>(double)$device['longitude'], "lat"=>(double)$device['latitude'], "createdTime"=>$createdTime, "param"=>(object)$param ]; $json_data=json_encode($arr); }else{ $json_data = null; } }else{ if (!empty($device)){ $eventCode1 = "02"; $eventName1 = "预警事件"; switch ($alarm_type) { case 'WP1': case 'WP2': $eventCode2 = '02008'; $eventName2 = '水压报警'; break; case 'LL9': case 'WP9': $eventCode2 = '02002'; $eventName2 = '电量不足'; break; default: $eventCode2 = '02000'; $eventName2 = '其他'; break; } $arr = [ "deviceCode" => $device['mh_device_code'], "lon"=>(double)$device['longitude'], "lat"=>(double)$device['latitude'], "createdTime"=>$createdTime, "addr"=>"上海市", "eventCode1"=>$eventCode1, "eventName1"=>$eventName1, "eventCode2"=>$eventCode2, "eventName2"=>$eventName2, "description"=>"备注信息", "param"=>(object)$param ]; $json_data=json_encode($arr); }else{ $json_data = null; } } return $json_data; } public function date_array_turn_json($insert_id,$device,$createdTime,$ajDescribe,$ajFlag,$enclosure,$image){ if (!empty($device)){ $arr = [ "id" => $insert_id, "deviceCode"=>$device['mh_device_code'], "createTime" =>date("Y-m-d H:i:s",$createdTime) , "ajType"=>$device['aj_type'], "street"=>$device['street'], "address"=>$device['company_address'].','.$device['device_address'], "grid"=>$device['grid'], "ajDescribe"=>$ajDescribe, "dutyGroup"=>$device['company_name'], "ajFlag"=>$ajFlag, "endTime"=>date("Y-m-d H:i:s",$createdTime+86400), "enclosure"=>$enclosure, "image"=>$image ]; $json_data=json_encode($arr); }else{ $json_data=null; } return $json_data; } public function message_push_mh($topic,$msg,$qos){ vendor('phpMQTT.phpMQTT'); $server = '47.98.201.73'; // 服务器IP $port = 1883; // 服务器端口 $username = 'usky'; // 用户名 $password = 'usky'; // 密码 $client_id = 'pub_' . uniqid(); $mqtt = new \phpMQTT($server, $port, $client_id); if ($mqtt->connect(true, NULL, $username, $password)) { $mqtt->publish($topic, $msg, $qos); usleep(100000); $mqtt->close(); Log::record('MH—4G-MQTT-Push success!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg)); } else { Log::record('MH—4G-MQTT-Time out!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg)); } } }