wang jun hace 1 año
padre
commit
a26db0c63a

+ 84 - 0
application/common/model/FacilityModel.php

@@ -0,0 +1,84 @@
+<?php
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 手环设备表
+ *
+ * @author wj
+ * @date 2023-08-14
+ */
+class FacilityModel extends Model
+{
+    protected $table = "t_facility";
+
+    public function insertData($data)
+    {
+        $data = $this->formatData($data);
+        if (empty($data)) {
+            return false;
+        }
+        $id = $this->insertGetId($data);
+        return $id ? $id : false;
+    }
+    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
+    {
+        $sqlObj = $this->where($where);
+        if ("count" != $field) {
+            if (empty($size)) {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group);
+            } else {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
+            }
+            if ($row) {
+                $data = $sqlObj->find();
+            } else {
+                $data = $sqlObj->select();
+            }
+        } else {
+            $data = $sqlObj->count();
+        }
+        return $data;
+    }
+    /**
+     * 根据id删除信息
+     *
+     * @param  [type] $id
+     * @return void
+     * @author wj
+     * @date 2023-08-14
+     */
+    public function deleteinfobyid($id)
+    {
+        $where = ['id' => $id];
+        $row = $this->where($where)->delete();
+        return empty($row) ? false : $row;
+    }
+    public function updateinfo($where, $updateData)
+    {
+        $row = $this->where($where)->update($updateData);
+        return empty($row) ? false : $row;
+    }
+    private function formatData($data)
+    {
+        $useData = [];
+        $fields = $this->getTableFields();
+        foreach ($data as $key => $value) {
+            if (in_array($key, $fields)) {
+                $useData[$key] = $value;
+            }
+        }
+        return $useData;
+    }
+    public function getInfo($where, $field = "*", $row = true)
+    {
+        $info = $this->field($field)->where($where);
+        if ($row) {
+            $info = $info->find();
+        } else {
+            $info = $info->select();
+        }
+        return empty($info) ? false : $info;
+    }
+}

+ 84 - 0
application/common/model/ShoneReceiveInfoModel.php

@@ -0,0 +1,84 @@
+<?php
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 手环类型 1 接受信息 实时数据
+ *
+ * @author wj
+ * @date 2023-08-14
+ */
+class ShoneReceiveInfoModel extends Model
+{
+    protected $table = "t_shouhuan_one_receive_info";
+
+    public function insertData($data)
+    {
+        $data = $this->formatData($data);
+        if (empty($data)) {
+            return false;
+        }
+        $id = $this->insertGetId($data);
+        return $id ? $id : false;
+    }
+    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
+    {
+        $sqlObj = $this->where($where);
+        if ("count" != $field) {
+            if (empty($size)) {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group);
+            } else {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
+            }
+            if ($row) {
+                $data = $sqlObj->find();
+            } else {
+                $data = $sqlObj->select();
+            }
+        } else {
+            $data = $sqlObj->count();
+        }
+        return $data;
+    }
+    /**
+     * 根据id删除信息
+     *
+     * @param  [type] $id
+     * @return void
+     * @author wj
+     * @date 2023-08-14
+     */
+    public function deleteinfobyid($id)
+    {
+        $where = ['id' => $id];
+        $row = $this->where($where)->delete();
+        return empty($row) ? false : $row;
+    }
+    public function updateinfo($where, $updateData)
+    {
+        $row = $this->where($where)->update($updateData);
+        return empty($row) ? false : $row;
+    }
+    private function formatData($data)
+    {
+        $useData = [];
+        $fields = $this->getTableFields();
+        foreach ($data as $key => $value) {
+            if (in_array($key, $fields)) {
+                $useData[$key] = $value;
+            }
+        }
+        return $useData;
+    }
+    public function getInfo($where, $field = "*", $row = true)
+    {
+        $info = $this->field($field)->where($where);
+        if ($row) {
+            $info = $info->find();
+        } else {
+            $info = $info->select();
+        }
+        return empty($info) ? false : $info;
+    }
+}

+ 84 - 0
application/common/model/ShoneReceiveInfoRecode.php

@@ -0,0 +1,84 @@
+<?php
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 手环类型 1 接受信息 历史数据
+ *
+ * @author wj
+ * @date 2023-08-14
+ */
+class ShoneReceiveInfoRecode extends Model
+{
+    protected $table = "t_shouhuan_one_receive_info_recode";
+
+    public function insertData($data)
+    {
+        $data = $this->formatData($data);
+        if (empty($data)) {
+            return false;
+        }
+        $id = $this->insertGetId($data);
+        return $id ? $id : false;
+    }
+    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
+    {
+        $sqlObj = $this->where($where);
+        if ("count" != $field) {
+            if (empty($size)) {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group);
+            } else {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
+            }
+            if ($row) {
+                $data = $sqlObj->find();
+            } else {
+                $data = $sqlObj->select();
+            }
+        } else {
+            $data = $sqlObj->count();
+        }
+        return $data;
+    }
+    /**
+     * 根据id删除信息
+     *
+     * @param  [type] $id
+     * @return void
+     * @author wj
+     * @date 2023-08-14
+     */
+    public function deleteinfobyid($id)
+    {
+        $where = ['id' => $id];
+        $row = $this->where($where)->delete();
+        return empty($row) ? false : $row;
+    }
+    public function updateinfo($where, $updateData)
+    {
+        $row = $this->where($where)->update($updateData);
+        return empty($row) ? false : $row;
+    }
+    private function formatData($data)
+    {
+        $useData = [];
+        $fields = $this->getTableFields();
+        foreach ($data as $key => $value) {
+            if (in_array($key, $fields)) {
+                $useData[$key] = $value;
+            }
+        }
+        return $useData;
+    }
+    public function getInfo($where, $field = "*", $row = true)
+    {
+        $info = $this->field($field)->where($where);
+        if ($row) {
+            $info = $info->find();
+        } else {
+            $info = $info->select();
+        }
+        return empty($info) ? false : $info;
+    }
+}

+ 74 - 0
application/common/model/ShoneReceiveQueueModel.php

@@ -0,0 +1,74 @@
+<?php
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 接收信息队列
+ *
+ * @author wj
+ * @date 2023-08-14
+ */
+class ShoneReceiveQueueModel extends Model
+{
+    protected $table = "t_shouhuan_one_receive_queue";
+
+    public function insertData($data)
+    {
+        $data = $this->formatData($data);
+        if (empty($data)) {
+            return false;
+        }
+        $id = $this->insertGetId($data);
+        return $id ? $id : false;
+    }
+    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
+    {
+        $sqlObj = $this->where($where);
+        if ("count" != $field) {
+            if (empty($size)) {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group);
+            } else {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
+            }
+            if ($row) {
+                $data = $sqlObj->find();
+            } else {
+                $data = $sqlObj->select();
+            }
+        } else {
+            $data = $sqlObj->count();
+        }
+        return $data;
+    }
+    /**
+     * 根据id删除信息
+     *
+     * @param  [type] $id
+     * @return void
+     * @author wj
+     * @date 2023-08-14
+     */
+    public function deleteinfobyid($id)
+    {
+        $where = ['id' => $id];
+        $row = $this->where($where)->delete();
+        return empty($row) ? false : $row;
+    }
+    public function updateinfo($where, $updateData)
+    {
+        $row = $this->where($where)->update($updateData);
+        return empty($row) ? false : $row;
+    }
+    private function formatData($data)
+    {
+        $useData = [];
+        $fields = $this->getTableFields();
+        foreach ($data as $key => $value) {
+            if (in_array($key, $fields)) {
+                $useData[$key] = $value;
+            }
+        }
+        return $useData;
+    }
+}

+ 85 - 0
application/common/model/ShoneSendListModel.php

@@ -0,0 +1,85 @@
+<?php
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 手环一发送列表
+ *
+ * @author wj
+ * @date 2023-08-14
+ */
+class ShoneSendListModel extends Model
+{
+    protected $table = "t_shouhuan_one_send_list";
+
+    public function insertData($data)
+    {
+        $data = $this->formatData($data);
+        if (empty($data)) {
+            return false;
+        }
+        $id = $this->insertGetId($data);
+        return $id ? $id : false;
+    }
+    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
+    {
+        $sqlObj = $this->where($where);
+        if ("count" != $field) {
+            if (empty($size)) {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group);
+            } else {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
+            }
+            if ($row) {
+                $data = $sqlObj->find();
+            } else {
+                $data = $sqlObj->select();
+            }
+        } else {
+            $data = $sqlObj->count();
+        }
+        return $data;
+    }
+    /**
+     * 根据id删除信息
+     *
+     * @param  [type] $id
+     * @return void
+     * @author wj
+     * @date 2023-08-14
+     */
+    public function deleteinfobyid($id)
+    {
+        $where = ['id' => $id];
+        $row = $this->where($where)->delete();
+        return empty($row) ? false : $row;
+    }
+    public function updateinfo($where, $updateData)
+    {
+        $row = $this->where($where)->update($updateData);
+        return empty($row) ? false : $row;
+    }
+    private function formatData($data)
+    {
+        $useData = [];
+        $fields = $this->getTableFields();
+        foreach ($data as $key => $value) {
+            if (in_array($key, $fields)) {
+                $useData[$key] = $value;
+            }
+        }
+        return $useData;
+    }
+    public function getInfo($where, $field = "*", $row = true)
+    {
+        $info = $this->field($field)->where($where);
+        if ($row) {
+            $info = $info->find();
+        } else {
+            $info = $info->select();
+        }
+        return empty($info) ? false : $info;
+    }
+
+}

+ 84 - 0
application/common/model/ShoneSendQueueModel.php

@@ -0,0 +1,84 @@
+<?php
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 发送信息队列
+ *
+ * @author wj
+ * @date 2023-08-14
+ */
+class ShoneSendQueueModel extends Model
+{
+    protected $table = "t_shouhuan_one_send_queue";
+
+    public function insertData($data)
+    {
+        $data = $this->formatData($data);
+        if (empty($data)) {
+            return false;
+        }
+        $id = $this->insertGetId($data);
+        return $id ? $id : false;
+    }
+    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
+    {
+        $sqlObj = $this->where($where);
+        if ("count" != $field) {
+            if (empty($size)) {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group);
+            } else {
+                $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
+            }
+            if ($row) {
+                $data = $sqlObj->find();
+            } else {
+                $data = $sqlObj->select();
+            }
+        } else {
+            $data = $sqlObj->count();
+        }
+        return $data;
+    }
+    /**
+     * 根据id删除信息
+     *
+     * @param  [type] $id
+     * @return void
+     * @author wj
+     * @date 2023-08-14
+     */
+    public function deleteinfobyid($id)
+    {
+        $where = ['id' => $id];
+        $row = $this->where($where)->delete();
+        return empty($row) ? false : $row;
+    }
+    public function updateinfo($where, $updateData)
+    {
+        $row = $this->where($where)->update($updateData);
+        return empty($row) ? false : $row;
+    }
+    private function formatData($data)
+    {
+        $useData = [];
+        $fields = $this->getTableFields();
+        foreach ($data as $key => $value) {
+            if (in_array($key, $fields)) {
+                $useData[$key] = $value;
+            }
+        }
+        return $useData;
+    }
+    public function getInfo($where, $field = "*", $row = true)
+    {
+        $info = $this->field($field)->where($where);
+        if ($row) {
+            $info = $info->find();
+        } else {
+            $info = $info->select();
+        }
+        return empty($info) ? false : $info;
+    }
+}

+ 595 - 0
application/common/server/ShOneanalysis.php

@@ -0,0 +1,595 @@
+<?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);
+        }
+    }
+}

+ 69 - 4
application/workerman/ShTcp.php

@@ -1,17 +1,82 @@
 <?php
 namespace app\workerman;
 
+use app\common\server\ShOneanalysis;
 use think\facade\Log;
 use think\worker\Server;
+use Workerman\Lib\Timer;
 
 class ShTcp extends Server
 {
-    protected $socket = 'tcp://0.0.0.0:21444';
+    //protected $socket = 'tcp://0.0.0.0:21444';
+    protected $socket = 'tcp://0.0.0.0:9501';
 
+    protected function init()
+    {
+        $this->count = 4; //线程数
+    }
+
+    public function onConnect($connection)
+    {
+
+    }
+    public function onWorkerStart($worker)
+    {
+        define('HEARTBEAT_TIME', 1000);
+        //异步处理
+        $server_sa = new ShOneanalysis();
+        Timer::add(1, function () use ($worker, $server_sa) {
+            if (isset($worker->device_id_code)) {
+                $device_id_code = $worker->device_id_code;
+                foreach ($worker->connections as $connection) {
+                    $info = $server_sa->getsendqueuelist($device_id_code);
+                    $totalpage = $info['totalpage'];
+                    if ($totalpage > 0) {
+                        $size = $info['size'];
+                        for ($page = 1; $page <= $totalpage; $page++) {
+                            $list = $server_sa->getsendqueuelist($device_id_code, 1, $size);
+                            foreach ($list as $key => $value) {
+                                $connection->send(json_encode($value['msg']));
+                                $server_sa->sendmsgsuccess($value);
+                            }
+                        }
+                    }
+                }
+            }
+        });
+
+        Timer::add(1, function () use ($worker) {
+            $time_now = time();
+            foreach ($worker->connections as $connection) {
+                // 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间
+                if (empty($connection->lastMessageTime)) {
+                    $connection->lastMessageTime = $time_now;
+                    continue;
+                }
+                // 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接
+                if ($time_now - $connection->lastMessageTime > HEARTBEAT_TIME) {
+                    //$connection->close();
+                    //设置对应设备下线
+                }
+            }
+        });
+    }
     public function onMessage($connection, $data)
     {
-        //$connection->send(json_encode($data));
-        Log::info("ShTcp");
-        Log::info($data);
+        Log::write("ShTcp", 'shouhuan');
+        Log::write($data, 'shouhuan');
+        $server_sa = new ShOneanalysis();
+        $result = $server_sa->savereceivequeue($data);
+        if (empty($result)) {
+            Log::write("手环接收信息队列,数据保存失败", 'shouhuan');
+        }
+        //$line = $server_sa->getline($data);
+        //$this->device_id_code = $line['device_id_code'];
+        $this->device_id_code = "358800006072996";
+    }
+
+    public function onClose($connection)
+    {
+
     }
 }