idcolWss.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if (isset($devUpdateList[$id]) && !empty($devUpdateList[$id])) {
  41. $temp = $devUpdateList[$id];
  42. $type = substr($msg[1], 0,2);
  43. if ($type == '99') {
  44. $status = substr($msg[1],18,2);
  45. $devUpdateList[$id]['status'] = $status;
  46. if ($status == '00') {
  47. $devUpdateList[$id]['type'] = 1;
  48. $devUpdateList[$id]['time'] = time();
  49. }elseif ($status == '01') {
  50. $devUpdateList[$id]['type'] = 2;
  51. }
  52. }
  53. }
  54. }
  55. };
  56. };
  57. $mqtt->connect();
  58. Timer::add(0.5, function()use($ws_worker){
  59. // global $clientArr;
  60. global $websocketDB;
  61. $connections = $ws_worker->connections;
  62. // var_dump($connections);
  63. $data = $websocketDB->select('id,client,msg')->from('message')->query();
  64. if (!empty($data)) {
  65. foreach ($data as $v) {
  66. if (!empty($connections[$v['client']])) $connections[$v['client']]->send($v['msg']);
  67. $websocketDB->query("DELETE FROM `message` WHERE id='{$v['id']}'");
  68. }
  69. }
  70. });
  71. // 下发设备升级指令
  72. Timer::add(1, function()use($ws_worker){
  73. global $mqtt;
  74. global $devUpdateList;
  75. global $devList;
  76. global $websocketDB;
  77. $url = 'http://120.77.179.55';
  78. $topick = '/IDCOL/CmdInput/';
  79. $temp = $devList;
  80. if (!empty($temp)) {
  81. foreach ($temp as $k => $v) {
  82. var_dump($v['lamp_id']);
  83. $client = $v['client'];
  84. $lamp = $websocketDB->query("select manu,address,id,number from lampinfo where id = ".$v['lamp_id']);
  85. if (!empty($lamp) && !empty($lamp[0]['id'])) {
  86. $manu = $lamp[0]['manu'];
  87. $address = $lamp[0]['address'];
  88. $number = $lamp[0]['number'];
  89. if (!empty($manu)) {
  90. $firmware_info = $websocketDB->query("select version,id,path from firmware_info where manuId = ".$manu);
  91. if (!empty($firmware_info) && !empty($firmware_info[0]['id'])) {
  92. $file_path = $url.$firmware_info[0]['path'];
  93. $cmd = '190100010000000000';
  94. while (strlen($file_path) < 200) {
  95. $file_path .= '0';
  96. }
  97. // $cmd .= $file_path;
  98. $crc = crc16(pack('H*', $cmd).$file_path);
  99. $mqtt->publish($topick.$address, pack('H*', $cmd).$file_path.pack('H*',).$crc);
  100. $index = strtolower($address);
  101. $devUpdateList[$index] = array('client'=>$client,'lamp_id'=>$v['lamp_id'],'time'=>time(),'type'=>0,'step'=>0,'number'=>$number);
  102. $websocketDB->update('lampinfo')->cols(array('is_update'=>1))->where('id='.$v['lamp_id']);
  103. unset($devList[$k]);
  104. continue;
  105. }else{
  106. $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]];
  107. }
  108. }else{
  109. $data = ['code'=>'9999','status'=>2,'msg'=>'Device not bound to manufacturer','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>$number]];
  110. }
  111. }else{
  112. $data = ['code'=>'9999','status'=>2,'msg'=>'Device does not exist','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'status'=>2,'number'=>'']];
  113. }
  114. $client->send(json_encode($data));
  115. unset($devList[$k]);
  116. }
  117. }
  118. });
  119. // 设备升级进度
  120. Timer::add(2, function()use($ws_worker){
  121. global $devUpdateList;
  122. global $websocketDB;
  123. $temp = $devUpdateList;
  124. if (!empty($temp)) {
  125. foreach ($temp as $k => $v) {
  126. if ($v['type'] == 0) { // 升级指令还没返回
  127. if (time() - $v['time'] > 10) { // 指令超时
  128. $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']]];
  129. unset($devUpdateList[$k]);
  130. }
  131. }elseif ($v['type'] == 1) { // 升级中
  132. if (time() - $v['time'] > 60 * 5) { // 指令超时
  133. $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']]];
  134. unset($devUpdateList[$k]);
  135. }else{
  136. if ($devUpdateList[$k]['step'] < 95) $devUpdateList[$k]['step'] += 1;
  137. $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'step'=>$v['step'],'status'=>2,'number'=>$v['number']]];
  138. }
  139. }elseif ($v['type'] == 2) { // 升级成功
  140. $data = ['code'=>'0000','status'=>2,'msg'=>'Successful operation','data'=>['type'=>'updateDev','id'=>$v['lamp_id'],'step'=>100,'status'=>2,'number'=>$v['number']]];
  141. unset($devUpdateList[$k]);
  142. }
  143. $v['client']->send(json_encode($data));
  144. }
  145. }
  146. });
  147. };
  148. // 启动1个进程对外提供服务
  149. $ws_worker->count = 1;
  150. // 客户端连接回调
  151. $ws_worker->onConnect = function($connection)
  152. {
  153. // global $clientArr;
  154. // 标记连接的客户端
  155. // $key=uniqid();
  156. $key = $connection->id;
  157. $data = array('code'=>'0000','msg'=>'操作成功','data'=>['clientid'=>$key,'status'=>1]);
  158. $connection->send(json_encode($data));
  159. // $clientArr[$key] = $connection;
  160. };
  161. $ws_worker->onMessage = function($connection, $data)
  162. {
  163. global $devList;
  164. $res = json_decode($data,true);
  165. if ($res && isset($res['type'])) {
  166. if ($res['type'] == 'updateDev' && isset($res['id']) && !empty($res['id'])){
  167. var_dump($res);
  168. $arr = explode(',',$res['id']);
  169. if (!empty($arr)) {
  170. var_dump($arr);
  171. foreach ($arr as $key) {
  172. var_dump($key);
  173. $key = intval($key);
  174. if (!empty($key)) {
  175. $devList[] = array('lamp_id'=>intval($key),'client'=>$connection);
  176. }
  177. }
  178. }else{
  179. $connection->close();
  180. }
  181. }else{
  182. $connection->close();
  183. }
  184. }else{
  185. $connection->close();
  186. }
  187. };
  188. // modbus crc 16 校验码计算
  189. function crc16($string) {
  190. $crc = 0xFFFF;
  191. for ($x = 0; $x < strlen($string); $x++) {
  192. $crc = $crc ^ ord($string[$x]);
  193. for ($y = 0; $y < 8; $y++) {
  194. if (($crc & 0x0001) == 0x0001) {
  195. $crc = (($crc >> 1) ^ 0xA001);
  196. } else { $crc = $crc >> 1; }
  197. }
  198. }
  199. $crc = strval(base_convert($crc, 10, 16));
  200. $crc = substr('0000'.$crc, -4);
  201. return substr($crc, -2).substr($crc, 0,2);
  202. }
  203. // 客户端断开连接
  204. // $ws_worker->onClose = function($connection)
  205. // {
  206. // global $clientArr;
  207. // if (!empty($clientArr)) {
  208. // foreach ($clientArr as $k => $v) {
  209. // if ($v == $connection) {
  210. // unset($clientArr[$k]);
  211. // break;
  212. // }
  213. // }
  214. // }
  215. // };
  216. // 运行worker
  217. Worker::runAll();