idcolWss.php 9.8 KB

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