记录TP5监听GPS设备协议的方法

php学习交流吧

共 10574字,需浏览 22分钟

 ·

2021-06-05 12:02

新建/application/server/controller/Server.php文件


<?php

namespace app\server\controller;use Workerman\Worker;use Workerman\Lib\Timer;use Workerman\Connection\AsyncTcpConnection;use think\Db;class Server { private $connections; private $connection_to_ws; public function index() { $socket = new Worker('websocket://0.0.0.0:2346'); // 设置transport开启ssl,websocket+ssl即wss //$socket->transport = 'ssl'; // 启动1个进程对外提供服务 $socket->count = 1; //给这个进程设置一个array() //$socket->connections = array(); // 当有客户端连接时 $socket->onConnect = function ($connection) { $this->connections[$connection->id] = $connection; }; // 当有客户端连接时 $socket->onMessage = function ($connection, $data) { // $this->connection_to_ws = new AsyncTcpConnection('tcp://101.32.206.28:8018'); // $this->connection_to_ws->connect(); // $this->connection_to_ws->send($data); // $this->connections[$connection->id]=$connection; }; // 当有客户端连接断开时 $socket->onClose = function ($connection) { if (isset($connection->id)) { // 连接断开时删除映射 //unset($this->connections[$connection->id]); } }; $socket->onError = function ($connection, $code, $msg) { echo "error $code $msg\n"; }; $tcp = new Worker('tcp://0.0.0.0:8018'); $tcp->onMessage = function ($connection, $data) { //var_dump($connection); // if(is_null($this->connection_to_ws)) { // $this->connection_to_ws = new AsyncTcpConnection('ws://101.32.206.28:2346'); // $this->connection_to_ws->connect(); // } // $this->connection_to_ws->send($data); if (strpos($data, 'BR00') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $where['macid'] = substr($data, 0, strpos($data, 'BR00')); $result = substr($data, strrpos($data, "BR00") + 4); $where['ymd'] = substr($result, 0, 6); $where['lat'] = substr($result, 7, 10); $where['lng'] = substr($result, 17, 11); $where['su'] = substr($result, 28, 5); $where['his'] = substr($result, 33, 6); $where['direction'] = substr($result, 40, 6); $where['io'] = substr($result, 44, 8); $where['mileage'] = substr($result, 53, 9); $where['gps_content'] = $data; $where['create_time'] = time(); $check = DB::name('gpslist')->where('gps_content', $where['gps_content'])->find(); $mac['lat'] = $where['lat']; $mac['lng'] = $where['lng']; DB::name('merchant_shop_commodity')->where('macid', $where['macid'])->update($mac); if (!$check) { DB::name('gpslist')->insert($where); } } if (strpos($data, 'BR01') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $where['macid'] = substr($data, 0, strpos($data, 'BR01')); $result = substr($data, strrpos($data, "BR01") + 4); $where['ymd'] = substr($result, 0, 6); $where['lat'] = substr($result, 7, 10); $where['lng'] = substr($result, 17, 11); $where['su'] = substr($result, 28, 5); $where['his'] = substr($result, 33, 6); $where['direction'] = substr($result, 40, 6); $where['io'] = substr($result, 44, 8); $where['mileage'] = substr($result, 53, 9); $where['gps_content'] = $data; $where['create_time'] = time(); $check = DB::name('gpslist')->where('gps_content', $where['gps_content'])->find(); DB::name('merchant_shop_commodity')->where('macid', $where['macid'])->update($mac); if (!$check) { DB::name('gpslist')->insert($where); } } var_dump($data); if (strpos($data, 'BP05') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $data = substr($data, 0, strpos($data, 'B')); //登陆应答 $send = '(' . $data . 'AP05)'; var_dump($send); //授时 $time = '(' . $data . 'AP06' . date('YmdHis') . ")"; var_dump($time); //$check = DB::name('merchant_shop_commodity')->where('macid',$data)->find(); // if($check){ // $content = base64_encode($connection); // DB::name('merchant_shop_commodity')->where('macid',$data)->setfield('gpscontent',$content); // } $connection->send($send); $connection->send($time); $this->connections[$connection->id] = $connection; } if (strpos($data, 'BP00') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $data = substr($data, 0, strpos($data, 'B')); //握手应答 $hand = '(' . $data . 'AP01HSO)'; var_dump($hand); $connection->send($hand); $this->connections[$connection->id] = $connection; } if (strpos($data, 'BP01') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $macid = substr($data, 0, strpos($data, 'BP01')); $a = substr($data, strpos($data, 'BP01') + 4); $where['version'] = substr($a, 0, strpos($a, 'CCID')); $where['iccid'] = substr($data, strripos($data, ":") + 1); var_dump($where); $checkiccid = DB::name('merchant_shop_commodity')->where('macid', $macid)->field('version,iccid')->find(); if (!$checkiccid['version'] || !$checkiccid['iccid']) { DB::name('merchant_shop_commodity')->where('macid', $macid)->update($where); } } if (strpos($data, 'BO01') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $where['macid'] = substr($data, 0, strpos($data, 'BO01')); $a = substr($data, strpos($data, 'BO01') + 4); $result = substr($data, strrpos($data, 'BO01') + 4); $where['ymd'] = substr($result, 0, 7); $where['lat'] = substr($result, 8, 10); $where['lng'] = substr($result, 18, 11); $where['su'] = substr($result, 29, 5); $where['his'] = substr($result, 34, 6); $where['direction'] = substr($result, 41, 6); $where['io'] = substr($result, 45, 8); $where['mileage'] = substr($result, 54, 9); $where['level'] = substr($a, 0, strpos($a, '2')); $where['content'] = $data; $where['create_time'] = time(); DB::name('gps_police')->insert($where); //报警应答 $hand = '(' . $where['macid'] . 'AS01' . $where['level'] . ')'; var_dump($hand); $connection->send($hand); $this->connections[$connection->id] = $connection; } if (strpos($data, 'BO02') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $where['macid'] = substr($data, 0, strpos($data, 'BO02')); $a = substr($data, strpos($data, 'BO02') + 4); $result = substr($data, strrpos($data, 'BO01') + 4); $where['ymd'] = substr($result, 0, 7); $where['lat'] = substr($result, 8, 10); $where['lng'] = substr($result, 18, 11); $where['su'] = substr($result, 29, 5); $where['his'] = substr($result, 34, 6); $where['direction'] = substr($result, 41, 6); $where['io'] = substr($result, 45, 8); $where['mileage'] = substr($result, 54, 9); $where['level'] = substr($a, 0, strpos($a, '2')); $where['content'] = $data; $where['create_time'] = time(); DB::name('gps_police')->insert($where); } if (strpos($data, 'BV01') !== false) { $data = str_replace(array( '(', ')' ) , '', $data); $macid = substr($data, 0, strpos($data, 'B')); $instructions = substr($data, -1); $checkdevice = DB::name('merchant_shop_commodity')->where('macid', $macid)->find(); if ($checkdevice) { DB::name('merchant_shop_commodity')->where('macid', $macid)->setfield('instructions', $instructions); } } if (strpos($data, 'AT00') !== false || strpos($data, 'AV010') !== false || strpos($data, 'AV011') !== false || strpos($data, 'AP07') !== false || strpos($data, 'AP16') !== false || strpos($data, 'AV03') !== false || strpos($data, 'AP30') !== false || strpos($data, ' AP00') !== false) { //echo $data; if (isset($this->connections[1])) { $this->connections[1]->send($data); } else { $connection->send($data); } } //var_dump(base64_encode($connection)); }; // 运行worker Worker::runAll(); }}


在项目根目录创建server.php

#!/usr/bin/env php<?phpdefine('APP_PATH', __DIR__ . '/application/');define('BIND_MODULE','server/Server/index');// 加载框架引导文件require __DIR__ . '/thinkphp/start.php';


最后终端启动服务

php server.php start -d


浏览 65
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报