123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- <?php
- namespace app\common\server;
- use app\common\model\FacilityModel;
- use app\common\model\ShoneReceiveInfoModel;
- use app\common\model\ShoneReceiveInfoRecode;
- use app\common\model\ShoneReceiveQueueModel;
- use app\common\model\ShoneSendListModel;
- use app\common\model\ShoneSendQueueModel;
- use think\facade\Log;
- /**
- * 手环字符串解析
- *
- * @author wj
- * @date 2023-08-11
- */
- class ShOneanalysis
- {
- private $num_scale = 16; //16进制
- /**
- * 校验长度
- *
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function checklen($len, $str)
- {
- $strlen = mb_strlen($str);
- if ($len != $strlen) {
- return false;
- }
- return true;
- }
- /**
- * 获取信息
- *
- * @return void
- * @author wj
- * @date 2023-08-11
- */
- public function getline($str)
- {
- //[厂商*设备 ID*内容长度*内容]
- $preg = "/^\[(.*)\*(.*)\*(.*)\*(.*)\]$/";
- preg_match($preg, $str, $match);
- if (empty($match)) {
- throw new \Exception('解析失败:' . $str);
- }
- unset($match[0]);
- $data = array_values($match);
- $sandom_string = $data[0]; //厂商 随机字符串
- $device_id_code = $data[1]; //设置id编码
- $content_len = $data[2]; //内容长度
- $content_str = $data[3]; //内容字符串
- $content_len = $this->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);
- }
- }
- }
|