123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- namespace app\admin\controller;
- use think\Controller;
- use think\Session;
- use think\Db;
- use think\Request;
- use think\Log;
- class Dataacceptance extends Controller
- {
- public function getData()
- {
- $Data = input('');
- if (is_array($Data)) {
- $getDataJson = json_encode($Data, 320);
- $getdata = json_decode($getDataJson, true);//转成数组
- } else {
- $getDataJson = $Data;
- $getdata = json_decode($getDataJson, true);//转成数组
- }
- $json_string = json_encode($getdata, JSON_FORCE_OBJECT);
- Log::record('yangpu4GRawData:' . $json_string);
- $bin_data = $getdata['msg']['value']['bin_data'];
- if (empty($bin_data)) {
- $returnInformation['code'] = false;
- $returnInformation['msg'] = "数据格式错误";
- return json_encode($returnInformation);
- }
- Log::record('yangpu4GData' . ':' . $bin_data);
- //数据类型
- $type = substr($bin_data, 8, 2);
- //数据长度
- $length = substr($bin_data, 10, 4);
- //设备类型
- $device_type = substr($bin_data, 14, 2);
- //设备编号截取拼接
- $devide_id1 = ((int)substr($bin_data, 16, 2)) - 30;
- $devide_id2 = ((int)substr($bin_data, 18, 2)) - 30;
- $devide_id3 = ((int)substr($bin_data, 20, 2)) - 30;
- $devide_id4 = ((int)substr($bin_data, 22, 2)) - 30;
- $devide_id5 = ((int)substr($bin_data, 24, 2)) - 30;
- $devide_id6 = ((int)substr($bin_data, 26, 2)) - 30;
- $devide_id7 = ((int)substr($bin_data, 28, 2)) - 30;
- $devide_id8 = ((int)substr($bin_data, 30, 2)) - 30;
- $devide_id9 = ((int)substr($bin_data, 32, 2)) - 30;
- $devide_id10 = ((int)substr($bin_data, 34, 2)) - 30;
- $devide_id11 = ((int)substr($bin_data, 36, 2)) - 30;
- $devide_id12 = ((int)substr($bin_data, 38, 2)) - 30;
- $devide_id13 = ((int)substr($bin_data, 40, 2)) - 30;
- $devide_id14 = ((int)substr($bin_data, 42, 2)) - 30;
- $devide_id15 = ((int)substr($bin_data, 44, 2)) - 30;
- $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;
- //发送时间截取拼接
- $send_time1 = substr($bin_data, 48, 2);
- $send_time2 = substr($bin_data, 50, 2);
- $send_time3 = substr($bin_data, 52, 2);
- $send_time4 = substr($bin_data, 54, 2);
- $send_time5 = substr($bin_data, 56, 2);
- $send_time6 = substr($bin_data, 58, 2);
- $time = '20' . $send_time1 . '-' . $send_time2 . '-' . $send_time3 . ' ' . $send_time4 . ':' . $send_time5 . ':' . $send_time6;
- // print_r($time);
- //电池电量
- $battery_level = hexdec(substr($bin_data, 60, 2));
- //信号强度
- $signal_strength = substr($bin_data, 62, 2);
- if ((int)$signal_strength <= 6 && (int)$signal_strength != 0) {
- $signal_level = 1;
- } else if ((int)$signal_strength <= 12) {
- $signal_level = 2;
- } else if ((int)$signal_strength <= 18) {
- $signal_level = 3;
- } else if ((int)$signal_strength <= 24) {
- $signal_level = 4;
- } else if ((int)$signal_strength <= 31) {
- $signal_level = 5;
- } else {
- $signal_level = 0;
- }
- //设备采集数据解析
- $device_type_10 = hexdec($device_type);
- 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
- $returnInformation['code'] = false;
- $returnInformation['msg'] = "该类型未定义";
- return json_encode($returnInformation);
- }
- $sample_data = substr($bin_data, 64, 8);
- $sample_data_unit1 = substr($bin_data, 64, 2);
- $sample_data_unit2 = substr($bin_data, 66, 2);
- $sample_data_unit3 = substr($bin_data, 68, 2);
- $sample_data_unit4 = substr($bin_data, 70, 2);
- $sample_data_unit1_high = substr($sample_data_unit1, 0, 1);
- $sample_data_unit1_low = substr($sample_data_unit1, 1, 1);
- $sample_data_unit2_high = substr($sample_data_unit2, 0, 1);
- $sample_data_unit2_low = substr($sample_data_unit2, 1, 1);
- $sample_data_unit3_high = substr($sample_data_unit3, 0, 1);
- $sample_data_unit3_low = substr($sample_data_unit3, 1, 1);
- $sample_data_unit4_high = substr($sample_data_unit4, 0, 1);
- $sample_data_unit4_low = substr($sample_data_unit4, 1, 1);
- $value = $sample_data_unit2_low . $sample_data_unit3_high . $sample_data_unit3_low . $sample_data_unit4_high . $sample_data_unit4_low;
- switch ($sample_data_unit2_high) {
- case '1':
- $alarm_value = (int)$value * 0.1;
- break;
- case '2':
- $alarm_value = (int)$value * 0.01;
- break;
- case '3':
- $alarm_value = (int)$value * 0.001;
- break;
- case '4':
- $alarm_value = (int)$value * 0.0001;
- break;
- }
- if ($sample_data_unit1_low == 1) {
- $alarm_type = 'WP' . $sample_data_unit1_high;
- $alarm_value = $alarm_value;
- } else if ($sample_data_unit1_low == 2) {
- $alarm_type = 'WP' . $sample_data_unit1_high;
- $alarm_value = $alarm_value * 0.1;
- } else if ($sample_data_unit1_low == 3) {
- $alarm_type = 'WP' . $sample_data_unit1_high;
- $alarm_value = $alarm_value * 1000;
- } else if ($sample_data_unit1_low == 4) {
- $alarm_type = 'LL' . $sample_data_unit1_high;
- $alarm_value = $alarm_value;
- } else {
- $returnInformation['code'] = false;
- $returnInformation['msg'] = "单位未定义";
- return json_encode($returnInformation);
- }
- switch ($alarm_type) {
- case 'LL0':
- case 'WP0':
- $alarm_data = '正常';
- break;
- case 'WP1':
- $alarm_data = '低压';
- break;
- case 'WP2':
- $alarm_data = '高压';
- break;
- case 'LL3':
- case 'WP3':
- $alarm_data = '故障';
- break;
- case 'LL4':
- case 'WP4':
- $alarm_data = '波动告警';
- break;
- case 'LL5':
- case 'WP5':
- $alarm_data = '碰撞告警';
- break;
- case 'LL6':
- case 'WP6':
- $alarm_data = '倾斜告警';
- break;
- case 'LL7':
- case 'WP7':
- $alarm_data = '水流告警';
- break;
- case 'LL8':
- case 'WP8':
- $alarm_data = '进水告警';
- break;
- case 'LL9':
- case 'WP9':
- $alarm_data = '低电量告警';
- break;
- case 'LL1':
- $alarm_data = '低水位';
- break;
- case 'LL2':
- $alarm_data = '高水位';
- break;
- default:
- $returnInformation['code'] = false;
- $returnInformation['msg'] = "未知告警";
- return json_encode($returnInformation);
- break;
- }
- $res_owner = add("select owner_code,dwtype,company,owner_name from sp_owner where owner_code='" . $device_code . "'");
- if (empty($res_owner)) {
- $returnInformation['code'] = false;
- $returnInformation['msg'] = "该设备未注册";
- return json_encode($returnInformation);
- }
- ////闵行MQTT
- $where_one['device_code'] = $device_code;
- $where_one['push_status'] = 0;
- $device = Db::name('yt_t_mhwater')->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);
- $json_data_qone = $this->mqqt_data_recombination("INFO", $device_code, $res_owner[0]['owner_name'], $battery_level, $signal_level, $alarm_value,$res_owner[0]['dwtype']);
- if (!empty($json_data_qone)){
- $topicYp_one = '/usky/ytDP0002/'.$res_owner[0]['company'].'/'.$device_code.'/info';
- $this->message_push_mh($topicYp_one, $json_data_qone, 0);
- }
- //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);
- $json_data_qtwo = $this->mqqt_data_recombination("ALARM", $device_code, $res_owner[0]['owner_name'], $battery_level, $signal_level, $alarm_value,$res_owner[0]['dwtype']);
- if (!empty($json_data_qtwo)){
- $topicYp_two = '/usky/ytDP0002/'.$res_owner[0]['company'].'/'.$device_code.'/alarm';
- $this->message_push_mh($topicYp_two, $json_data_qtwo, 0);
- }
- //闵行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 = [];
- $param = ["platform_cy" => $device['platform_cy'], "scenetype_cy" => $device['scenetype_cy']];
- 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" => $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" => $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));
- }
- }
- public function mqqt_data_recombination($type, $device_code, $device_name, $electric_quantity, $signal, $water_gage,$dwtype)
- {
- if ($type == 'INFO') {
- $status_valie = "0";
- } else {
- $status_valie = "1";
- }
- if ($dwtype==2){
- $property = "水压";
- }else{
- $property = "水位";
- }
- $array = [
- "devId" => $device_code,
- "connType" => "G",
- "deviceName" => $device_name,
- "devs" => [
- [
- "dp" => [
- [
- "property" => "电量",
- "serial" => "201",
- "value" => '' . $electric_quantity
- ],
- [
- "property" => "信号",
- "serial" => "202",
- "value" => '' . $signal
- ],
- [
- "property" => $property,
- "serial" => "203",
- "value" => '' . $water_gage
- ],
- [
- "property" => "状态",
- "serial" => "204",
- "value" => $status_valie
- ]
- ],
- "deviceName" => $device_name,
- "timeStamp" => "".time(),
- "deviceModel" => "",
- "deviceType" => "".$dwtype,
- "devId" => $device_code
- ]
- ],
- "type" => $type,
- "timeStamp" => "".time()
- ];
- $json_data = json_encode($array);
- return $json_data;
- }
- }
|