analysislen($content_len); $result = $this->checklen($content_len, $content_str); if (empty($result)) { throw new \Exception('内容位数错误'); } $data = [ 'device_id_code' => $device_id_code, 'content_len' => $content_len, 'content_str' => $content_str, ]; return $data; } //KA,230814,0,0,96 //WT,130823,160054,V,22.601242,N,113.8302765,E,0.00,0.0,29.2,0,70,96,0,0,00000000,1,1,460,0,8593,265807240,136,0,9999.0 //ICCID,02203002080045057806 public function getcommandcontent($content_str) { $content = explode(',', $content_str); return $content; } /** * 保存手环设备one信息 * * @return void * @author wj * @date 2023-08-11 */ public function saveshouhuanoneinfo() { //保存时时 //保存历史 } //内容解析 //解析长度 public function analysislen($len) { //低位数在前 $len_arr = array_reverse(str_split($len)); $len_num = []; foreach ($len_arr as $key => $value) { if (is_numeric($value) && $value <= 9) { $num = $value; } elseif (ord($value) <= 65 || ord($value) >= 90) { $ord = ord($value); $num = bcadd(bcsub($ord, 65), 10); } $pow = pow($this->num_scale, $key); //16^0 16^1 $len_num[] = bcmul($pow, $num); } $len = array_sum($len_num); return $len; } /** * 获取命令信息 * * @param [type] $command * @return void * @author wj * @date 2023-08-11 */ public function getcommandinfo($command) { $data = [ 'LK' => [ 'content' => [ 'command' => 'LK', ], 'kind' => 'back', ], 'PING' => [ 'content' => [ 'command' => 'PING', 'isbind' => 1, ], 'kind' => 'back', ], 'KA' => [ 'content' => [ 'command' => 'KA', ], 'kind' => 'back', ], 'MONITOR' => [ 'content' => [ 'command' => 'MONITOR', 'telno' => '', ], 'kind' => 'send', ], 'SOS' => [ 'content' => [ 'command' => 'SOS', 'telno1' => '', 'telno2' => '', 'telno3' => '', ], 'kind' => 'send', ], 'VERNO' => [ 'content' => [ 'command' => 'VERNO', ], 'kind' => 'send', ], 'ZONE' => [ 'content' => [ 'command' => 'ZONE', 'zone' => '', ], 'kind' => 'send', ], 'POWEROFF' => [ 'content' => [ 'command' => 'POWEROFF', ], 'kind' => 'send', ], ]; if (!isset($data[$command])) { return false; } return $data[$command]; } public function gettsendcommandinfo($command) { $data = [ 'WT' => [ 'content' => [ 'command' => 'WT', 'date' => '', 'time' => '', 'is_location' => '', 'gps_long' => '', 'gps_long_tag' => '', 'gps_lat' => '', 'gps_lat_tag' => '', 'speed' => '', 'direction' => '', 'poster' => '', 'direction' => '', 'gps_satellite_num' => '', 'gsm_signal_strength' => '', 'electric_quantity' => '', 'step_number' => '', 'roll_number' => '', 'terminal_status' => '', ], 'kind' => 'tsend', 'need_bacl' => 0, ], 'KA' => [ 'content' => [ 'command' => 'KA', 'date' => '', 'step_number' => '', 'roll_number' => '', 'electric_quantity' => '', ], 'kind' => 'tsend', 'need_back' => 1, ], 'LK' => [ 'content' => [ 'command' => 'LK', ], 'kind' => 'tsend', 'need_back' => 1, ], 'MONITOR' => [ 'content' => [ 'command' => 'MONITOR', ], 'kind' => 'tback', ], 'SOS3' => [ 'content' => [ 'command' => 'SOS', ], 'kind' => 'tback', ], 'VERNO' => [ 'content' => [ 'command' => 'VERNO', 'version' => '', ], 'kind' => 'tback', ], ]; if (!isset($data[$command])) { return false; } return $data[$command]; } /** * 获取回复权重 * * @param [type] $command * @return void * @author wj * @date 2023-08-11 */ public function getsendweight($command) { $weight = -1; switch ($command) { case 'POWEROFF': //关机 $weight = 99; break; case 'RESET': //重启 $weight = 98; break; case 'LK': case 'PING': case 'KA': case 'AL': case 'UD2'; $weight = 1; break; case 'UPLOAD': case 'MONITOR': case 'SOS': case 'LANG': case 'ZONE': case 'CENTER': case 'SOSSMS': case 'REMOVE': case 'REMOVESMS': case 'VERNO': case 'CR': case 'SILENCETIME2': case 'WALKTIME': case 'SLEEPTIME': case 'FIND': case 'REMIND': case 'SCHEDULE': case 'PHB': case 'PHBX': case 'DPHBX': case 'MESSAGE': case 'FACTORY': case 'PROFILE': case 'BTEMP2': case 'BODYTEMP': case 'BTWARNSET': case 'APPLOCK': case 'HRTSTART': case 'BPHRT': case 'LSSET': $weight = 0; break; default: $weight = -1; break; } if ($weight < 0) { //不需设置 发送信息 return false; } return $weight; } /** * 获取设别表id * * @return void * @author wj * @date 2023-08-11 */ public function getfacilityid($device_id_code) { $m_f = new FacilityModel(); $where = ['code' => $device_id_code, 'status' => 1]; $finfo = $m_f->getInfo($where, ['id']); if (empty($finfo)) { throw new \Exception("设备信息错误:" . $device_id_code); } $id = $finfo['id']; return $id; } /** * 创建发送信息 * * @param [type] $data * @param [type] $command * @return void * @author wj * @date 2023-08-14 */ public function createbacksendmsg($facility_id, $device_id_code, $data, $command) { $infoarr = $this->gettsendcommandinfo($command); if (!$infoarr) { throw new \Exception("无对应命令数据:" . $command); } $data = $this->getdata($data, $command, $infoarr); $info_content = $data['info_content']; $need_back = $data['need_back']; if ($need_back) { $infoarr = $this->getcommandinfo($command); $data = $this->getdata($data, $command, $infoarr); $info_content = $data['info_content']; $weight = $this->getsendweight($command); $content = implode(',', $info_content); $msgdata = [ 0 => 'DW', 'device_id_code' => $device_id_code, 'len' => str_pad(dechex(mb_strlen($content)), 4, "0", STR_PAD_LEFT), 'content' => $content, ]; $msg = '[' . implode('*', $msgdata) . ']'; $sslInsertData = [ 'facility_id' => $facility_id, 'device_id_code' => $device_id_code, 'send_msg' => $msg, 'send_time' => date('Y-m-d H:i:s'), 'send_weight' => $weight, ]; $m_ssl = new ShoneSendListModel(); $sslid = $m_ssl->insertData($sslInsertData); if (empty($sslid)) { throw new \Exception("发送数据添加失败"); } $data = [ 'msg' => $msg, 'weight' => $weight, 'createtime' => date('Y-m-d H:i:s'), 'device_id_code' => $device_id_code, 'list_id' => $sslid, ]; $m_ssq = new ShoneSendQueueModel(); $id = $m_ssq->insertData($data); if (empty($id)) { throw new \Exception("发送队列数据添加失败"); } return $data; } return false; } /** * 获取数据值 * * @param [type] $data * @param [type] $command * @return void * @author wj * @date 2023-08-14 */ public function getdata($data, $command, $infoarr) { $info_content = $infoarr['content']; foreach (array_keys($info_content) as $key => $value) { if (empty($info_content[$value])) { if (isset($data[$key])) { $info_content[$value] = $data[$key]; } } } $infoarr['info_content'] = $info_content; return $infoarr; } /** * 保存接收信息队列 * * @param [type] $data * @return void * @author wj * @date 2023-08-14 */ public function savereceivequeue($data) { $m_srq = new ShoneReceiveQueueModel(); $data = [ 'msg' => $data, 'createtime' => date('Y-m-d H:i:s'), ]; $id = $m_srq->insertData($data); Log::write('id:' . $id, 'shouhuan'); if (empty($id)) { return false; } return true; } /** * 保存手环信息 * * @return void * @author wj * @date 2023-08-14 */ public function saveshinfo($facility_id, $device_id_code, $data, $original_str) { $command = $data[0]; $infoarr = $this->gettsendcommandinfo($command); if (!$infoarr) { throw new \Exception("无对应命令数据:" . $command); } $data = $this->getdata($data, $data[0], $infoarr); $m_sri = new ShoneReceiveInfoModel(); $m_srir = new ShoneReceiveInfoRecode(); $data = $data['info_content']; $data['facility_id'] = $facility_id; $data['device_id_code'] = $device_id_code; //$m_sri处理 $sriwhere = [ 'facility_id' => $facility_id, 'device_id_code' => $device_id_code, ]; $sriinfo = $m_sri->getInfo($sriwhere); unset($data['command']); if (empty($sriinfo)) { $sriinsertData = $data; $sriinsertData['createtime'] = date('Y-m-d H:i:s'); $sriid = $m_sri->insertData($sriinsertData); if (empty($sriid)) { throw new \Exception("设备信息添加失败:" . $device_id_code); } } else { unset($data['device_id_code']); unset($data['facility_id']); $sriid = $sriinfo['id']; $sriupdateData = $data; $sriupdateData['updatetime'] = date('Y-m-d H:i:s'); $row = $m_sri->updateinfo(['id' => $sriid], $sriupdateData); if (empty($row)) { throw new \Exception("设备信息修改失败:" . $device_id_code); } } //m_srir处理 $data['original_str'] = $original_str; $sririnsertData = $data; $sririnsertData['createtime'] = date('Y-m-d H:i:s'); $sririd = $m_srir->insertData($sririnsertData); if (empty($sririd)) { throw new \Exception("设备历史信息添加失败:" . $device_id_code); } } /** * 获取接收信息队列列表 * * @param [type] $page * @param [type] $size * @return void * @author wj * @date 2023-08-14 */ public function getreceivequeuelist($page = null, $size = null) { $m_srq = new ShoneReceiveQueueModel(); $where = ['status' => 0]; $size = empty($size) ? 20 : $size; if (empty($page)) { $count = $m_srq->getList($where, 'count'); $totalpage = ceil($count / $size); $data = ['totalpage' => $totalpage, 'size' => $size]; return $data; } else { $list = $m_srq->getList($where, '*', $page, $size, 'id asc'); if (empty($list)) { Log::write("手环接收信息队列-无数据", 'shouhuan'); return false; } $list = $list->toArray(); $ids = array_column($list, 'id'); $updatecount = $m_srq->updateinfo(['id' => ['in', $ids], 'status' => 0], ['status' => 1]); if ($updatecount != count($ids)) { Log::write("手环接收信息队列-部分未修改", 'shouhuan'); Log::write("count:" . count($ids) . " update count:" . $updatecount, 'shouhuan'); } if (empty($updatecount)) { Log::write("手环接收信息队列-无修改数据", 'shouhuan'); return false; } return $list; } } /** * 获取发送信息队列列表 * * @param [type] $page * @param [type] $size * @return void * @author wj * @date 2023-08-14 */ public function getsendqueuelist($device_id_code, $page = null, $size = null) { $m_ssq = new ShoneSendQueueModel(); $where = ['status' => 0, 'device_id_code' => $device_id_code]; $size = empty($size) ? 20 : $size; if (empty($page)) { $count = $m_ssq->getList($where, 'count'); $totalpage = ceil($count / $size); $data = ['totalpage' => $totalpage, 'size' => $size]; return $data; } else { $list = $m_ssq->getList($where, '*', $page, $size, 'id asc,weight desc'); if (empty($list)) { Log::write("手环发送信息队列-无数据", 'shouhuan'); return false; } $list = $list->toArray(); $ids = array_column($list, 'id'); $updatecount = $m_ssq->updateinfo(['id' => ['in', $ids], 'status' => 0], ['status' => 1]); if ($updatecount != count($ids)) { Log::write("手环发送信息队列-部分未修改", 'shouhuan'); Log::write("count:" . count($ids) . " update count:" . $updatecount, 'shouhuan'); } if (empty($updatecount)) { Log::write("手环发送信息队列-无修改数据", 'shouhuan'); return false; } return $list; } } public function sendmsgsuccess($item) { $m_ssq = new ShoneSendQueueModel(); $m_ssl = new ShoneSendListModel(); $ssqid = $item['id']; $list_id = $item['list_id']; $sslwhere = ['id' => $list_id, 'is_send' => 0]; $sslinfo = $m_ssl->getInfo($sslwhere); if ($sslinfo) { $row = $m_ssl->updateinfo(['id' => $sslinfo['id']], ['is_send' => 1, 'send_time' => date('Y-m-d H:i:s')]); } $ssqwhere = ['id' => $ssqid]; $ssqinfo = $m_ssq->getInfo($ssqwhere); if ($ssqinfo) { $m_ssq->deleteinfobyid($ssqid); } } }