Commit 2025f431 authored by 冯超鹏's avatar 冯超鹏

tcp协议模块

parent 50361874
Pipeline #185 canceled with stage
......@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Http\Controllers\Auth\SwooleCommandMeTcpController;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use App\Http\Controllers\Auth\ZehongTcpController;
class swooleMeTcp extends Command
{
public $tcp;
......@@ -81,11 +82,14 @@ class swooleMeTcp extends Command
//监听数据接收事件
$this->tcp->on('Receive', function ($serv, $fd, $from_id, $data) {
//验证数据格式
$serv->send($fd,$data);
if($this->formatData($data)){
$swooletcp = new SwooleCommandMeTcpController();
$swooletcp->swooletcplist($data);
}
// $serv->send($fd,$data);
// if($this->formatData($data)){
// $swooletcp = new SwooleCommandMeTcpController();
// $swooletcp->swooletcplist($data);
// }
$swooletcp = new ZehongTcpController();
// $swooletcp->swooletcplist($data);
$serv->send($fd,$swooletcp->swooletcplist($data));
});
//监听连接关闭事件
......
......@@ -70,7 +70,7 @@ class zehongTcp extends Command
$this->tcp->on('Connect', function ($serv, $fd) {
$data = [
'stats[得到当前 Server 的活动 TCP 连接数]' => $this->tcp->stats(),
'getClientInfo[获取连接的信息]' => $this->tcp->getClientInfo($fd, 1, true)
'getClientInfo[获取连swooletcplist接的信息]' => $this->tcp->getClientInfo($fd, 1, true)
];//链接信息写入.log
Log::channel('slack')->info($data);
......
......@@ -21,59 +21,85 @@ class ZehongTcpController extends Controller
}
}
// 解开数据包
protected function substrdata($data)
{
$deviceId = substr($data, 0, 36); // 设备编号
$cmd = substr($data, 40, 2); // 命令:01:开机 02: 正常交互
$deviceStatus = substr($data, 42, 2); // 状态
$value = substr($data, 44, 4); // 浓度值
$reserved = substr($data, 48, 20); // 预留
$battery = substr($data, 68, 2); // 电池
$signal = substr($data, 70, 2); // 信号强度
$control = substr($data, 72, 2); // 监测控制
$time = substr($data, 74, 12); // 测量时间
$alarmLow = substr($data, 86, 4); // 低报阈值
$alarmHight = substr($data, 90, 4); // 高报阈值
$cycle = substr($data, 94, 4); // 检测周期
$timeSampling = substr(98, 4); // 采样时间
$reserved1 = substr($data, 102, 20); // 预留
// 设备状态对照表
if (is_string($data) && strlen($data) == 26) {
$deviceId = substr($data, 0, 15); // 设备编号
$status = substr($data, 15, 1); // 状态
$devicenp = substr($data, 16, 3); // 浓度
$devicefamen = substr($data, 19, 1);
$devicechongqi = substr($data, 20, 1);
$devicezijian = substr($data, 21, 1);
$devicecrc = substr($data, 22, 4);
$isdata = substr($data, 0, 22);
if($this->getPrc($isdata) == $devicecrc){
$data = ['id'=>$deviceId,'status'=>$status,'np'=>$devicenp,'famen'=>$devicefamen,'chongqi'=>$devicechongqi,'zijian'=>$devicezijian];
$totcp = json_decode($this->mysqldb($data),true);
if ($totcp == "暂无设备"){
return $totcp;
}
if($totcp['nd'] < 999 ){
$totcp['nd'] = '0' . $totcp['nd'];
}
$totcpdata = $totcp['devicenum'] .$totcp['devicepolice'] .$totcp['nd'] . $totcp['shutoff_status'] . $devicechongqi . $devicezijian;
$crcdata = $this->getPrc($totcpdata);
return $totcpdata . $crcdata;
}
} else {
return "数据格式错误";
}
}
//解出prc
protected function getPrc($msg)
{
/**
* 1- 正常
* 2- 检测中
* 3- 低报
* 4- 高报
* 5- 电池欠压
* 6- 水位报警
* 7- 传感器故障
* 8- 泵故障
* 9- 设备故障
* If your input string is HEX-formatted you should comment #15 line
* for example: $msg="0123" equals to 0x0123 without leading 0x
*/
switch (base_convert($deviceStatus, 16, 10)) {
case '1':
case '2':
$status = 1;
break;
case '3':
$status = 5;
break;
case '4':
$status = 6;
break;
case '5':
$status = 14;
break;
case '6':
$status = 4;
break;
case '7':
$status = 3;
break;
default:
$status = 15;
//conversion input string to plain HEX string. See comment above
$msg = bin2hex($msg);
//pack HEX-formatted $msg to the pure HEX array
$data = pack('H*', $msg);
//initialize crc as start HEX value;
$crc = 0xFFFF;
//loop $data array of HEX bytes
for ($i = 0; $i < strlen($data); $i++) {
//xor bits in the first byte of HEX value
$crc ^= ord($data[$i]);
//loop to shift every of 8 bits
for ($j = 8; $j != 0; $j--) {
//shift bits to the right and xor with polynome
if (($crc & 0x0001) != 0) {
$crc >>= 1;
$crc ^= 0xA001;
} else $crc >>= 1;
}
}
// return explode('0',$deviceId);
return hexdec($deviceId);
//return the result as HEX-formatted string
$data = strrev(sprintf('%04X', $crc));
$crc1 = strrev(substr($data, 0, 2));
$crc2 = strrev(substr($data, 2, 2));
return $crc1 . $crc2;
}
//获取上报数据从数据库查询
public function mysqldb($data){
if($data != []){
$datainfo = DB::table('device')
->where('devicenum','=',$data['id'])
->update(['nd'=>intval($data['np']),'shutoff_status'=>$data['famen'],'devicepolice'=>$data['status'] == 0 ? '1' : $data['status']]);
$todata = DB::table('device')
->where('devicenum','=',$data['id'])
->select('devicenum','nd','shutoff_status','devicepolice')
->first();
if ($todata){
return json_encode($todata);
}else{
return "暂无设备";
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment