wzh 5 лет назад
Родитель
Сommit
226184e49c
2 измененных файлов с 80 добавлено и 16 удалено
  1. 1 1
      api/application/config/response.php
  2. 79 15
      websocket/idcolWss.php

+ 1 - 1
api/application/config/response.php

@@ -186,7 +186,7 @@ $config['response_en']['0012'] = "cannot find file";
 $config['response_en']['0013'] = "No authority";
 $config['response_en']['0014'] = 'Parameter error';
 $config['response_en']['0015'] = 'Upload failed';
-$config['response_en']['0016'] = 'The command is timed out, please try again later.';
+$config['response_en']['0016'] = 'The command is timed out, please try again later';
 //登录 0100-0199
 $config['response_en']['0100'] = 'User name or password cannot be empty';
 $config['response_en']['0101'] = 'Wrong user name or password';

+ 79 - 15
websocket/idcolWss.php

@@ -31,7 +31,7 @@ $ws_worker->onWorkerStart = function($ws_worker)
     $mqtt->onConnect = function($mqtt) {
 
         $room = array(
-            '/IDCOL/CmdInput/#' => 2,
+            // '/IDCOL/CmdInput/#' => 2,
             '/IDCOL/CmdOutput/#' => 2,
         );
 
@@ -54,6 +54,7 @@ $ws_worker->onWorkerStart = function($ws_worker)
                         $devUpdateList[$id]['status'] = $status;
                         if ($status == '00') {
                             $devUpdateList[$id]['type'] = 1;
+                            $devUpdateList[$id]['time'] = time();
                         }elseif ($status == '01') {
                             $devUpdateList[$id]['type'] = 2;
                         }
@@ -86,30 +87,77 @@ $ws_worker->onWorkerStart = function($ws_worker)
         global $devList;
         global $websocketDB;
 
+        $url = 'http://120.77.179.55';
+        $topick = '/IDCOL/CmdInput/';
         $temp = $devList;
         if (!empty($temp)) {
-            foreach ($temp as $v) {
+            foreach ($temp as $k => $v) {
+                $client = $v['client'];
                 $lamp = $websocketDB->query("select manu,address,id from lampinfo where id = ".$v['lamp_id']);
-                if (empty($lamp) || empty($lamp[0]['id'])) {
-                    # code...
+                if (!empty($lamp) && !empty($lamp[0]['id'])) {
+                    $manu = $lamp[0]['id']['manu'];
+                    $address = $lamp[0]['id']['address'];
+                    if (!empty($manu)) {
+                        $firmware_info = $websocketDB->query("select version,id,path from firmware_info where manuId = ".$v['manu']);
+                        if (!empty($firmware_info) && !empty($firmware_info[0]['id'])) {
+                            $file_path = $url.$firmware_info[0]['path'];
+                            $cmd = '190100010000000000';
+                            while (strlen($file_path) < 200) {
+                                $file_path .= '0';
+                            }
+                            $cmd .= $file_path;
+                            $cmd = crc16(unpack('H*', $cmd));
+                            $mqtt->publish($topick.$address, unpack('H*', $cmd));
+
+                            $index = strtolower($address);
+                            $devUpdateList[$index] = array('client'=>$client,'lamp_id'=>$v['lamp_id'],'time'=>time(),'type'=>0,'step'=>0);
+                            $websocketDB->update('lampinfo')->cols(array('is_update'=>1))->where('id='.$v['lamp_id']);
+                            unset($devList[$k]);
+                            continue;
+                        }else{
+                            $data = ['code'=>'9999','status'=>2,'msg'=>'No firmware file found, please contact your administrator','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'status'=>2]];
+                        }
+                    }else{
+                        $data = ['code'=>'9999','status'=>2,'msg'=>'Device not bound to manufacturer','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'status'=>2]];
+                    }
+                }else{
+                    $data = ['code'=>'9999','status'=>2,'msg'=>'Device does not exist','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'status'=>2]];
                 }
+                $client->send(json_encode($data));
+                unset($devList[$k]);
             }
         }
     });
 
     // 设备升级进度
     Timer::add(2, function()use($ws_worker){
-        // global $clientArr;
-        // global $websocketDB;
-        // $connections = $ws_worker->connections;
-        // // var_dump($connections);
-        // $data = $websocketDB->select('id,client,msg')->from('message')->query();
-        // if (!empty($data)) {
-        //     foreach ($data as $v) {
-        //         if (!empty($connections[$v['client']])) $connections[$v['client']]->send($v['msg']); 
-        //         $websocketDB->query("DELETE FROM `message` WHERE id='{$v['id']}'");
-        //     }
-        // }
+        global $devUpdateList;
+        global $websocketDB;
+        $temp = $devUpdateList;
+
+        if (!empty($temp)) {
+            foreach ($temp as $k => $v) {
+                if ($v['type'] == 0) {  // 升级指令还没返回
+                    if (time() - $v['time'] > 10) {  // 指令超时
+                        $data = ['code'=>'9999','status'=>2,'msg'=>'The command is timed out, please try again later','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'status'=>2]];
+                        unset($devUpdateList[$k]);
+                    }
+                }elseif ($v['type'] == 1) {  // 升级中
+                    if (time() - $v['time'] > 60 * 5) {  // 指令超时
+                        $data = ['code'=>'9999','status'=>2,'msg'=>'Please try again later. Please upgrade the device','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'status'=>2]];
+                        unset($devUpdateList[$k]);
+                    }else{
+                        if ($devUpdateList[$k]['step'] < 95) $devUpdateList[$k]['step'] += 1;
+                        
+                        $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'step'=>$v['step'],'status'=>2]];
+                    }
+                }elseif ($v['type'] == 2) {  // 升级成功
+                    $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'devUpdate','id'=>$v['lamp_id'],'step'=>100,'status'=>2]];
+                    unset($devUpdateList[$k]);
+                }
+                $v['client']->send(json_encode($data));
+            }
+        }
     });
 };
 
@@ -152,6 +200,22 @@ $ws_worker->onMessage = function($connection, $data)
         $connection->close();
     }
 };
+
+// modbus crc 16 校验码计算
+function crc16($string) {
+  $crc = 0xFFFF;
+  for ($x = 0; $x < strlen ($string); $x++) {
+    $crc = $crc ^ ord($string[$x]);
+    for ($y = 0; $y < 8; $y++) {
+      if (($crc & 0x0001) == 0x0001) {
+        $crc = (($crc >> 1) ^ 0xA001);
+      } else { $crc = $crc >> 1; }
+    }
+  }
+  $crc = strval(base_convert($crc, 10, 16));
+  $crc = substr('0000'.$crc, -4);
+  return substr($crc, -2).substr($crc, 0,2);
+}
 // 客户端断开连接
 // $ws_worker->onClose = function($connection)
 // {