idcolWss.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. use Workerman\Worker;
  3. require_once __DIR__ . '/Autoloader.php';
  4. require_once './Lib/mysql/src/Connection.php';
  5. use \Workerman\Lib\Timer;
  6. require_once './Lib/MQTT/src/Client.php';
  7. require_once './Lib/MQTT/src/Protocols/Mqtt.php';
  8. require_once './config.php';
  9. $ws_worker = new Worker("websocket://0.0.0.0:8844");
  10. // global $clientArr;
  11. // $clientArr =[];
  12. $ws_worker->onWorkerStart = function($ws_worker)
  13. {
  14. // 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
  15. global $websocketDB;
  16. global $mqtt;
  17. $config = json_decode(file_get_contents('./db_config.php'),true);
  18. $websocketDB = new \Workerman\MySQL\Connection($config['hostname'], '3306', $config['username'], $config['password'], $config['dbname']);
  19. global $devList;
  20. $devList = array();
  21. global $devUpdateList;
  22. $devUpdateList = array();
  23. $options = array(
  24. 'username'=>'weclouds',
  25. 'password'=>'weclouds@1234'
  26. );
  27. $mqtt = new \Workerman\Mqtt\Client('mqtt://streetserver.weclouds.xyz:1883',$options);
  28. $mqtt->onConnect = function($mqtt) {
  29. $room = array(
  30. '/IDCOL/CmdInput/#' => 2,
  31. '/IDCOL/CmdOutput/#' => 2,
  32. );
  33. $mqtt->subscribe($room, null, function($exception, $granted){});
  34. $mqtt->onMessage = function($room, $message){
  35. global $devUpdateList;
  36. global $StrategyReport;
  37. $idArr = explode('/', $room);
  38. $id = $idArr[count($idArr) - 1];
  39. $id = strtolower($id);
  40. if (strpos($room,'CmdOutput')) {
  41. $msg = unpack('H*',$message);
  42. var_dump($msg);
  43. var_dump($room);
  44. if (isset($devUpdateList[$id]) && !empty($devUpdateList[$id])) {
  45. $temp = $devUpdateList[$id];
  46. $type = substr($msg[1], 0,2);
  47. if ($type == '99') {
  48. $status = substr($msg[1],18,2);
  49. $devUpdateList[$id]['status'] = $status;
  50. if ($status == '00') {
  51. $devUpdateList[$id]['type'] = 1;
  52. $devUpdateList[$id]['time'] = time();
  53. }elseif ($status == '01') {
  54. $devUpdateList[$id]['type'] = 2;
  55. }
  56. }
  57. }
  58. }
  59. };
  60. // 下发设备升级指令
  61. Timer::add(1, function()use($mqtt){
  62. // global $mqtt;
  63. global $devUpdateList;
  64. global $devList;
  65. global $websocketDB;
  66. $url = 'http://120.77.179.55';
  67. $topick = '/IDCOL/CmdInput/';
  68. $temp = $devList;
  69. if (!empty($temp)) {
  70. foreach ($temp as $k => $v) {
  71. var_dump($v['lamp_id']);
  72. $client = $v['client'];
  73. $lamp = $websocketDB->query("select manu,address,id,number from lampinfo where id = ".$v['lamp_id']);
  74. if (!empty($lamp) && !empty($lamp[0]['id'])) {
  75. $manu = $lamp[0]['manu'];
  76. $address = $lamp[0]['address'];
  77. $number = $lamp[0]['number'];
  78. if (!empty($manu)) {
  79. $firmware_info = $websocketDB->query("select version,id,path from firmware_info where manuId = ".$manu);
  80. if (!empty($firmware_info) && !empty($firmware_info[0]['id'])) {
  81. $file_path = $url.$firmware_info[0]['path'];
  82. if (strtolower(substr($address, 0,2)) == 'a5') {
  83. $file_path = 'http://3.112.251.179:8080/mc0138/idcol_mc20_sr_a50129.bin';
  84. }elseif (strtolower(substr($address, 0,2)) == 'a8'){
  85. $file_path = 'http://3.112.251.179:8080/mc0138/idcol_mc20_sr_a80129.bin';
  86. }elseif (strtolower(substr($address, 0,2)) == 'aa'){
  87. $file_path = 'http://3.112.251.179:8080/mc0138/idcol_mc20_sr_aa0129.bin';
  88. }
  89. $cmd = '190100010000000000';
  90. for ($i=0; $i < 200; $i++) {
  91. $file_path .= pack('H*','00');
  92. }
  93. $file_path = substr($file_path,0,200);
  94. // $cmd .= $file_path;
  95. $cmd = pack('H*', $cmd);
  96. $crc = crc16($cmd.$file_path);
  97. $mqtt->publish($topick.$address, $cmd.$file_path.pack('H*',$crc));
  98. $index = strtolower($address);
  99. // $devUpdateList[$index] = array('client'=>$client,'lamp_id'=>$v['lamp_id'],'time'=>time(),'type'=>0,'step'=>0,'number'=>$number);
  100. // $websocketDB->update('lampinfo')->cols(array('is_update'=>1))->where('id='.$v['lamp_id']);
  101. unset($devList[$k]);
  102. $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'step'=>100,'number'=>$number]];
  103. // continue;
  104. }else{
  105. $data = ['code'=>'9999','status'=>2,'msg'=>'No firmware file found, please contact your administrator','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>$number]];
  106. }
  107. }else{
  108. $data = ['code'=>'9999','status'=>2,'msg'=>'Device not bound to manufacturer','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>$number]];
  109. }
  110. }else{
  111. $data = ['code'=>'9999','status'=>2,'msg'=>'Device does not exist','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>'']];
  112. }
  113. $client->send(json_encode($data));
  114. unset($devList[$k]);
  115. }
  116. }
  117. });
  118. };
  119. $mqtt->connect();
  120. Timer::add(0.5, function()use($ws_worker){
  121. // global $clientArr;
  122. global $websocketDB;
  123. $connections = $ws_worker->connections;
  124. // var_dump($connections);
  125. $data = $websocketDB->select('id,client,msg')->from('message')->query();
  126. if (!empty($data)) {
  127. foreach ($data as $v) {
  128. if (!empty($connections[$v['client']])) $connections[$v['client']]->send($v['msg']);
  129. $websocketDB->query("DELETE FROM `message` WHERE id='{$v['id']}'");
  130. }
  131. }
  132. });
  133. // 设备升级进度
  134. Timer::add(2, function()use($ws_worker){
  135. global $devUpdateList;
  136. global $websocketDB;
  137. $temp = $devUpdateList;
  138. if (!empty($temp)) {
  139. foreach ($temp as $k => $v) {
  140. if ($v['type'] == 0) { // 升级指令还没返回
  141. if (time() - $v['time'] > 10) { // 指令超时
  142. $data = ['code'=>'9999','status'=>2,'msg'=>'The command is timed out, please try again later','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>$v['number']]];
  143. unset($devUpdateList[$k]);
  144. }
  145. }elseif ($v['type'] == 1) { // 升级中
  146. if (time() - $v['time'] > 60 * 5) { // 指令超时
  147. $data = ['code'=>'9999','status'=>2,'msg'=>'Please try again later. Please upgrade the device','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>$v['number']]];
  148. unset($devUpdateList[$k]);
  149. }else{
  150. if ($devUpdateList[$k]['step'] < 95) $devUpdateList[$k]['step'] += 1;
  151. $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'step'=>$v['step'],'status'=>2,'number'=>$v['number']]];
  152. }
  153. }elseif ($v['type'] == 2) { // 升级成功
  154. $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'step'=>100,'status'=>2,'number'=>$v['number']]];
  155. unset($devUpdateList[$k]);
  156. }
  157. if (isset($data)) $v['client']->send(json_encode($data));
  158. }
  159. }
  160. });
  161. };
  162. // 启动1个进程对外提供服务
  163. $ws_worker->count = 1;
  164. // 客户端连接回调
  165. $ws_worker->onConnect = function($connection)
  166. {
  167. // global $clientArr;
  168. // 标记连接的客户端
  169. // $key=uniqid();
  170. $key = $connection->id;
  171. $data = array('code'=>'0000','msg'=>'操作成功','data'=>['clientid'=>$key,'status'=>1]);
  172. $connection->send(json_encode($data));
  173. // $clientArr[$key] = $connection;
  174. };
  175. $ws_worker->onMessage = function($connection, $data)
  176. {
  177. global $devList;
  178. $res = json_decode($data,true);
  179. if ($res && isset($res['type'])) {
  180. if ($res['type'] == 'updateDev' && isset($res['id']) && !empty($res['id'])){
  181. var_dump($res);
  182. $arr = explode(',',$res['id']);
  183. if (!empty($arr)) {
  184. var_dump($arr);
  185. foreach ($arr as $key) {
  186. var_dump($key);
  187. $key = intval($key);
  188. if (!empty($key)) {
  189. $devList[] = array('lamp_id'=>intval($key),'client'=>$connection);
  190. }
  191. }
  192. }else{
  193. $connection->close();
  194. }
  195. }else{
  196. $connection->close();
  197. }
  198. }else{
  199. $connection->close();
  200. }
  201. };
  202. // modbus crc 16 校验码计算
  203. function crc16($string) {
  204. $crc = 0xFFFF;
  205. for ($x = 0; $x < strlen($string); $x++) {
  206. $crc = $crc ^ ord($string[$x]);
  207. for ($y = 0; $y < 8; $y++) {
  208. if (($crc & 0x0001) == 0x0001) {
  209. $crc = (($crc >> 1) ^ 0xA001);
  210. } else { $crc = $crc >> 1; }
  211. }
  212. }
  213. $crc = strval(base_convert($crc, 10, 16));
  214. $crc = substr('0000'.$crc, -4);
  215. return substr($crc, -2).substr($crc, 0,2);
  216. }
  217. // 客户端断开连接
  218. // $ws_worker->onClose = function($connection)
  219. // {
  220. // global $clientArr;
  221. // if (!empty($clientArr)) {
  222. // foreach ($clientArr as $k => $v) {
  223. // if ($v == $connection) {
  224. // unset($clientArr[$k]);
  225. // break;
  226. // }
  227. // }
  228. // }
  229. // };
  230. // 运行worker
  231. Worker::runAll();