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