analysisShoneMsg.php 2.9 KB

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