You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.2 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Commons\Log;
  4. use App\Constants\LogLabel;
  5. use App\Model\SpeakerDevic;
  6. use App\Model\Store;
  7. use Hyperf\Di\Annotation\Inject;
  8. use Hyperf\Utils\ApplicationContext;
  9. use App\TaskWorker\AliIotTask;
  10. class DeviceServiceImp implements DeviceServiceInterFace
  11. {
  12. /**
  13. * @Inject
  14. * @var Log
  15. */
  16. protected $log;
  17. /**
  18. * @Inject
  19. * @var IOTServiceInterface
  20. */
  21. protected $IOTService;
  22. /**
  23. * 获取绑定列表
  24. * @param $store_id
  25. * @return \Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection
  26. */
  27. public function getListByStoreId($store_id)
  28. {
  29. return SpeakerDevic::query()->where(['store_id' => $store_id])->get();
  30. }
  31. /**
  32. * 绑定
  33. * @param $dev_name
  34. * @param $store_id
  35. * @return SpeakerDevic|null
  36. * @throws \Throwable
  37. */
  38. public function bindByStoreId($dev_name, $store_id)
  39. {
  40. $sd = null;
  41. if ($this->checkDeviceEnable($dev_name)) {
  42. return $sd;
  43. }
  44. try {
  45. // 获取市场ID
  46. $market_id = Store::query()->where(['id' => $store_id])->value('market_id');
  47. $sd = new SpeakerDevic;
  48. $sd->store_id = $store_id;
  49. $sd->device_name = $dev_name;
  50. $sd->market_id = $market_id;
  51. $sd->bind_time = time();
  52. $sd->saveOrFail();
  53. } catch (Exception $e) {
  54. $this->log->event(LogLabel::DEVICE_LOG, ['msg' => '绑定设备异常:'.$e->getMessage()]);
  55. }
  56. return $sd;
  57. }
  58. /**
  59. * 解绑
  60. * @param $bind_id
  61. * @return int
  62. */
  63. public function unbindById($bind_id)
  64. {
  65. return SpeakerDevic::destroy($bind_id);
  66. }
  67. /**
  68. * 发布语音消息
  69. * @param $store_id
  70. * @param $msg
  71. */
  72. public function pubMsgToStoreByDevName($dev_name, $msg)
  73. {
  74. return $this->IOTService->pub($dev_name, $msg);
  75. }
  76. /**
  77. * 当前设备是否已经被绑定
  78. * @param $dev_name
  79. * @return bool
  80. */
  81. protected function checkDeviceEnable($dev_name)
  82. {
  83. return SpeakerDevic::query()->where(['device_name' => $dev_name])->exists();
  84. }
  85. }