analysisShoneMsg.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\command;
  3. use app\common\server\ShOneanalysis;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\Output;
  8. use think\facade\Log;
  9. use Workerman\Lib\Timer;
  10. use Workerman\Worker;
  11. /**
  12. * 30秒执行一次
  13. *
  14. * @author wj
  15. * @date 2023-08-14
  16. */
  17. class analysisShoneMsg extends Command
  18. {
  19. protected function configure()
  20. {
  21. // 指令配置
  22. $this->setName('analysisShoneMsg')
  23. ->setDescription('解析手环one接收信息')
  24. ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start');
  25. // 设置参数
  26. }
  27. protected function execute(Input $input, Output $output)
  28. {
  29. $action = $input->getArgument('action');
  30. global $argv;
  31. array_shift($argv);
  32. $task = new Worker();
  33. $task->count = 4;
  34. $server_sa = new ShOneanalysis();
  35. $server_sa->initanalysisShoneReceiveMsg();
  36. $task->onWorkerStart = function ($task) use ($server_sa) {
  37. //$time = microtime(true);
  38. $pageinfo = $server_sa->getreceivequeuelist();
  39. $totalpage = $pageinfo['totalpage'];
  40. $size = $pageinfo['size'];
  41. for ($i = 0; $i < $totalpage; $i++) {
  42. Timer::add(1, function () use ($size, $server_sa) {
  43. $page = 1;
  44. $list = $server_sa->getreceivequeuelist($page, $size);
  45. if ($list) {
  46. $list = (array) $list;
  47. foreach ($list as $key => $value) {
  48. try {
  49. $linedata = $server_sa->getline($value['msg']);
  50. //长度相等可解析
  51. $content = $linedata['content_str'];
  52. $device_id_code = $linedata['device_id_code'];
  53. $fid = $server_sa->getfacilityid($device_id_code);
  54. $content = $server_sa->getcommandcontent($content);
  55. $server_sa->saveshinfo($fid, $device_id_code, $content, $value['msg']);
  56. $command = $content[0];
  57. $data = $server_sa->createbacksendmsg($fid, $device_id_code, $content, $command);
  58. Log::write($data, 'shouhuan');
  59. $server_sa->receivemsgsuccess($value);
  60. } catch (\Exception $e) {
  61. $msg = $e->getMessage();
  62. echo $msg . "\n";
  63. Log::write($msg, 'shouhuan');
  64. Log::write($e->getTraceAsString(), 'shouhuan');
  65. }
  66. }
  67. }
  68. });
  69. }
  70. // $endtime = microtime(true);
  71. // $difftime = bcsub($endtime, $time);
  72. // var_dump('difftime:' . $difftime);
  73. };
  74. // 运行worker
  75. Worker::runAll();
  76. }
  77. }