analysisShoneMsg.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. array_shift($argv);
  33. array_unshift($argv, 'think', $action);
  34. $task = new Worker();
  35. $task->count = 4;
  36. $server_sa = new ShOneanalysis();
  37. $server_sa->initanalysisShoneReceiveMsg();
  38. $task->onWorkerStart = function ($task) use ($server_sa) {
  39. //$time = microtime(true);
  40. $pageinfo = $server_sa->getreceivequeuelist();
  41. $totalpage = $pageinfo['totalpage'];
  42. $size = $pageinfo['size'];
  43. for ($i = 0; $i < $totalpage; $i++) {
  44. Timer::add(1, function () use ($size, $server_sa) {
  45. $page = 1;
  46. $list = $server_sa->getreceivequeuelist($page, $size);
  47. if ($list) {
  48. $list = (array) $list;
  49. foreach ($list as $key => $value) {
  50. try {
  51. $linedata = $server_sa->getline($value['msg']);
  52. //长度相等可解析
  53. $content = $linedata['content_str'];
  54. $device_id_code = $linedata['device_id_code'];
  55. $fid = $server_sa->getfacilityid($device_id_code);
  56. $content = $server_sa->getcommandcontent($content);
  57. $server_sa->saveshinfo($fid, $device_id_code, $content, $value['msg']);
  58. $command = $content[0];
  59. $data = $server_sa->createbacksendmsg($fid, $device_id_code, $content, $command);
  60. Log::write($data, 'shouhuan');
  61. $server_sa->receivemsgsuccess($value);
  62. } catch (\Exception $e) {
  63. $msg = $e->getMessage();
  64. echo $msg . "\n";
  65. Log::write($msg, 'shouhuan');
  66. Log::write($e->getTraceAsString(), 'shouhuan');
  67. }
  68. }
  69. }
  70. });
  71. }
  72. // $endtime = microtime(true);
  73. // $difftime = bcsub($endtime, $time);
  74. // var_dump('difftime:' . $difftime);
  75. };
  76. // 运行worker
  77. Worker::runAll();
  78. }
  79. }