作者 王晓刚
1 个管道 的构建 通过 耗费 6 秒

gatewaywork部署上线调试

@@ -214,7 +214,8 @@ @@ -214,7 +214,8 @@
214 })(); 214 })();
215 </script> 215 </script>
216 <script> 216 <script>
217 - ws = new WebSocket("ws://gangjuren.w.brotop.cn:9528/"); 217 + ws = new WebSocket("ws://127.0.0.1:9528/");
  218 + // ws = new WebSocket("ws://gangjuren.w.brotop.cn:9528/");
218 ws.onopen = function() { 219 ws.onopen = function() {
219 ws.send("X,5A5A5A5A0000,00,1,83bb,Y"); 220 ws.send("X,5A5A5A5A0000,00,1,83bb,Y");
220 }; 221 };
@@ -65,11 +65,45 @@ class Events @@ -65,11 +65,45 @@ class Events
65 echo "yanzhengshibai"; 65 echo "yanzhengshibai";
66 return ; 66 return ;
67 } 67 }
68 - $str = ",".$number .",". $command_number .",". $date.",";  
69 - $arr = ["X", $number, $command_number, $date, self::crc16($str), "Y"];  
70 - $result = implode(',', $arr);  
71 - //回复  
72 - Gateway::sendToClient($client_id, $result); 68 + //判断是否已有数板仪id
  69 + $row = self::$db->select('*')->from('gjr_equipment')->where("number = '$number'")->row();
  70 + $status = "A5";
  71 + if (!empty($row)) {
  72 + //判断状态
  73 + if($row['status'] != 'normal' || $row['end_time'] < time()){
  74 + $status = "B9";//禁用
  75 + }else {
  76 + $createtime = time();
  77 + $arr1 = [
  78 + 'number' => $number,
  79 + 'command_number' => $command_number,
  80 + ];
  81 + //记录判断是否订阅
  82 + $api_subscrib = self::$db->select('*')->from('gjr_api_subscrib')->where("subscrib_ids like '%$row[id],%'")->row();
  83 + if(!empty($api_subscrib)){
  84 + $api = self::$db->select('*')->from('gjr_api')->where("id = '$api_subscrib[api_id]'")->row();
  85 + echo $api['callback']."\n";
  86 + //请求接口返回状态
  87 + $arr1['number'] = $number;
  88 + $result3 = json_decode(self::httpPost($api['callback'],$arr1),true);
  89 + if($result3['code'] == 1){
  90 + $is_success = 1;
  91 + if($result3['data']['status'] == 'normal'){
  92 + $status = "A5";//正常
  93 + }else{
  94 + $status = "B9";//非正常
  95 + }
  96 + }else{
  97 + $staus = "A5";//接口异常
  98 + }
  99 + }else {
  100 + $status = "A5";//未订阅返回正常
  101 + }
  102 + }
  103 + }
  104 + $str = ",".$number .",". $command_number.",".$status .",". $date.",";
  105 + $arr = ["X", $number, $command_number, $status, $date, self::crc16($str), "Y"];
  106 + $common = implode(',', $arr);
73 } else if ($command_number == "01") { 107 } else if ($command_number == "01") {
74 //数据同步以及状态同步 108 //数据同步以及状态同步
75 $time = $param[3];//时间 109 $time = $param[3];//时间
@@ -85,57 +119,79 @@ class Events @@ -85,57 +119,79 @@ class Events
85 $dianji_status = $param[12];//电机状态 119 $dianji_status = $param[12];//电机状态
86 $yejing_status = $param[13];//液晶状态 120 $yejing_status = $param[13];//液晶状态
87 $wenshidu_status = $param[14];//湿度传感器状态 121 $wenshidu_status = $param[14];//湿度传感器状态
88 - //判断是否已有数板仪id  
89 - $row = self::$db->select('*')->from('gjr_equipment_log')->where("number = '$number'")->row();  
90 - if (empty($row)) {  
91 - $arr = [  
92 - 'number' => $number,  
93 - 'command_number' => $command_number,  
94 - 'time' => $time,  
95 - 'position' => $position,  
96 - 'address' => $address,  
97 - 'quantity' => $quantity,  
98 - 'wendu' => $wendu,  
99 - 'shidu' => $shidu,  
100 - 'papers' => $papers,  
101 - 'papers_count' => $papers_count,  
102 - 'times' => $times,  
103 - 'sensor_status' => $sensor_status,  
104 - 'dianji_status' => $dianji_status,  
105 - 'yejing_status' => $yejing_status,  
106 - 'wenshidu_status' => $wenshidu_status,  
107 - 'socket_data' => json_encode($param),  
108 - 'createtime' => time(),  
109 - ];  
110 - //插入  
111 - self::$db->insert('gjr_equipment_log')->cols($arr)->query();  
112 - } else {  
113 - $arr = [  
114 - 'command_number' => $command_number,  
115 - 'time' => $time,  
116 - 'position' => $position,  
117 - 'address' => $address,  
118 - 'quantity' => $quantity,  
119 - 'wendu' => $wendu,  
120 - 'shidu' => $shidu,  
121 - 'papers' => $papers,  
122 - 'papers_count' => $papers_count,  
123 - 'times' => $times,  
124 - 'sensor_status' => $sensor_status,  
125 - 'dianji_status' => $dianji_status,  
126 - 'yejing_status' => $yejing_status,  
127 - 'wenshidu_status' => $wenshidu_status,  
128 - 'socket_data' => json_encode($param),  
129 - ];  
130 - //更新  
131 - self::$db->update('gjr_equipment_log')->cols($arr)->where("number = '$number'")->query(); 122 + //判断是否已数板仪id
  123 + $row = self::$db->select('*')->from('gjr_equipment')->where("number = '$number'")->row();
  124 + $status = "A5";
  125 + if (!empty($row)) {
  126 + //判断状态
  127 + if($row['status'] != 'normal' || $row['end_time'] < time()){
  128 + $status = "B9";//禁用
  129 + }else {
  130 + $createtime = time();
  131 + $arr1 = [
  132 + 'number' => $number,
  133 + 'command_number' => $command_number,
  134 + 'time' => $time,
  135 + 'position' => $position,
  136 + 'address' => $address,
  137 + 'quantity' => $quantity,
  138 + 'wendu' => $wendu,
  139 + 'shidu' => $shidu,
  140 + 'papers' => $papers,
  141 + 'papers_count' => $papers_count,
  142 + 'times' => $times,
  143 + 'sensor_status' => $sensor_status,
  144 + 'dianji_status' => $dianji_status,
  145 + 'yejing_status' => $yejing_status,
  146 + 'wenshidu_status' => $wenshidu_status,
  147 + 'socket_data' => json_encode($param),
  148 + 'createtime' => $createtime,
  149 + ];
  150 +// $db->beginTrans();
  151 + //数板仪器数据同步记录
  152 + $result1 = self::$db->insert('gjr_equipment_log')->cols($arr1)->query();
  153 + //记录判断是否订阅
  154 + $api_subscrib = self::$db->select('*')->from('gjr_api_subscrib')->where("subscrib_ids like '%$row[id],%'")->row();
  155 + if(!empty($api_subscrib)){
  156 + $api = self::$db->select('*')->from('gjr_api')->where("id = '$api_subscrib[api_id]'")->row();
  157 + //请求接口返回状态
  158 + unset($arr1['socket_data']);
  159 + unset($arr1['createtime']);
  160 + $arr1['number'] = $number;
  161 + $result3 = json_decode(self::httpPost($api['callback'],$arr1),true);
  162 + if($result3['code'] == 1){
  163 + $is_success = 1;
  164 + if($result3['data']['status'] == 'normal'){
  165 + $status = "A5";//正常
  166 + }else{
  167 + $status = "B9";//非正常
  168 + }
  169 + }else{
  170 + $staus = "A5";//接口异常
  171 + }
  172 + $arr2 = [
  173 + 'api_id' => $api_subscrib['api_id'],
  174 + 'type' => 1,
  175 + 'subscrib_id' => $api_subscrib['id'],
  176 + 'notice_url' => $api['callback'],
  177 + 'times' => $api['times'],
  178 + 'content' => json_encode($arr1),
  179 + 'createtime' => time(),
  180 + 'is_success' => empty($is_success) ? 0 : $is_success,
  181 + ];
  182 + //记录订阅通知记录
  183 + $result2 = self::$db->insert('gjr_subscrib_log')->cols($arr2)->query();
  184 + }else {
  185 + $status = "A5";//未订阅返回正常
  186 + }
  187 + }
132 } 188 }
133 - $str = $number . $command_number . $date;  
134 - $arr = ["X", $number, $command_number, $date, self::crc16($str), "Y"];  
135 - $result = implode(',',$arr);  
136 - //回复i  
137 - Gateway::sendToClient($client_id, $result); 189 + $str = $number."," . $command_number."," . $status."," . $date;
  190 + $arr = ["X", $number, $command_number, $status, $date, self::crc16($str), "Y"];
  191 + $common = implode(',',$arr);
138 } 192 }
  193 + //回复i
  194 + Gateway::sendToClient($client_id, $common);
139 } 195 }
140 196
141 /** 197 /**
@@ -291,4 +347,23 @@ class Events @@ -291,4 +347,23 @@ class Events
291 } 347 }
292 return $shu; 348 return $shu;
293 } 349 }
  350 +
  351 + public static function httpPost($url,$data){ // 模拟提交数据函数
  352 + $curl = curl_init(); // 启动一个CURL会话
  353 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  354 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  355 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  356 +// curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  357 + curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
  358 + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  359 + //curl_setopt($curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
  360 + curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
  361 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  362 + $result = curl_exec($curl); // 执行操作
  363 + if (curl_errno($curl)) {
  364 + echo 'Error POST'.curl_error($curl);
  365 + }
  366 + curl_close($curl); // 关键CURL会话
  367 + return $result; // 返回数据
  368 + }
294 } 369 }