idcolWss.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }elseif ($status == '01') {
  49. $devUpdateList[$id]['type'] = 2;
  50. }
  51. }
  52. }
  53. }
  54. };
  55. };
  56. $mqtt->connect();
  57. Timer::add(0.5, function()use($ws_worker){
  58. // global $clientArr;
  59. global $websocketDB;
  60. $connections = $ws_worker->connections;
  61. // var_dump($connections);
  62. $data = $websocketDB->select('id,client,msg')->from('message')->query();
  63. if (!empty($data)) {
  64. foreach ($data as $v) {
  65. if (!empty($connections[$v['client']])) $connections[$v['client']]->send($v['msg']);
  66. $websocketDB->query("DELETE FROM `message` WHERE id='{$v['id']}'");
  67. }
  68. }
  69. });
  70. // 下发设备升级指令
  71. Timer::add(1, function()use($ws_worker){
  72. global $mqtt;
  73. global $devUpdateList;
  74. global $devList;
  75. global $websocketDB;
  76. $temp = $devList;
  77. if (!empty($temp)) {
  78. foreach ($temp as $v) {
  79. $lamp = $websocketDB->query("select manu,address,id from lampinfo where id = ".$v['lamp_id']);
  80. if (empty($lamp) || empty($lamp[0]['id'])) {
  81. # code...
  82. }
  83. }
  84. }
  85. });
  86. // 设备升级进度
  87. Timer::add(2, function()use($ws_worker){
  88. // global $clientArr;
  89. // global $websocketDB;
  90. // $connections = $ws_worker->connections;
  91. // // var_dump($connections);
  92. // $data = $websocketDB->select('id,client,msg')->from('message')->query();
  93. // if (!empty($data)) {
  94. // foreach ($data as $v) {
  95. // if (!empty($connections[$v['client']])) $connections[$v['client']]->send($v['msg']);
  96. // $websocketDB->query("DELETE FROM `message` WHERE id='{$v['id']}'");
  97. // }
  98. // }
  99. });
  100. };
  101. // 启动1个进程对外提供服务
  102. $ws_worker->count = 1;
  103. // 客户端连接回调
  104. $ws_worker->onConnect = function($connection)
  105. {
  106. // global $clientArr;
  107. // 标记连接的客户端
  108. // $key=uniqid();
  109. $key = $connection->id;
  110. $data = array('code'=>'0000','msg'=>'操作成功','data'=>['clientid'=>$key,'status'=>1]);
  111. $connection->send(json_encode($data));
  112. // $clientArr[$key] = $connection;
  113. };
  114. $ws_worker->onMessage = function($connection, $data)
  115. {
  116. global $devList;
  117. $res = json_decode($data,true);
  118. if ($res && isset($res['type'])) {
  119. if ($res['type'] == 'updateDev' && isset($res['id']) && !empty($res['id'])){
  120. $arr = explode(','$res['id']);
  121. if (!empty($arr)) {
  122. foreach ($arr as $key) {
  123. if (is_int($key) && !empty($key)) {
  124. $devList[] = array('lamp_id'=>intval($key),'client'=>$connection);
  125. }
  126. }
  127. }else{
  128. $connection->close();
  129. }
  130. }else{
  131. $connection->close();
  132. }
  133. }else{
  134. $connection->close();
  135. }
  136. };
  137. // 客户端断开连接
  138. // $ws_worker->onClose = function($connection)
  139. // {
  140. // global $clientArr;
  141. // if (!empty($clientArr)) {
  142. // foreach ($clientArr as $k => $v) {
  143. // if ($v == $connection) {
  144. // unset($clientArr[$k]);
  145. // break;
  146. // }
  147. // }
  148. // }
  149. // };
  150. // 运行worker
  151. Worker::runAll();