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

tcp协议模块

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