Device.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\controller\device;
  3. use app\admin\services\MqttMessageClient;
  4. use app\common\controller\Backend;
  5. use app\admin\model\Task;
  6. use fast\Arr;
  7. use think\Db;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 设备管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Device extends Backend
  16. {
  17. /**
  18. * Device模型对象
  19. * @var \app\admin\model\Device
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Device;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['group', 'task'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. function task()
  57. {
  58. if ($this->request->isAjax()) {
  59. if (false === $this->request->isPost()) {
  60. $this->error(__('Invalid parameters'));
  61. }
  62. $ids = Arr::get($this->request->post('row/a'), 'ids',);
  63. if (empty($ids)) {
  64. $this->error(__('Parameter %s can not be empty', 'ids'));
  65. }
  66. $count = 0;
  67. Db::startTrans();
  68. try {
  69. $list = $this->model->where($this->model->getPk(), 'in', explode(',', $ids))->select();
  70. foreach ($list as $item) {
  71. $count += $item->allowField(true)->isUpdate(true)->save([
  72. 'task_id' => Arr::get($this->request->param('row/a'), 'task_id'),
  73. 'status' => 0,
  74. ]);
  75. }
  76. Db::commit();
  77. } catch (PDOException|Exception $e) {
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. }
  81. if ($count) {
  82. $this->success();
  83. }
  84. $this->error(__('No rows were updated'));
  85. }
  86. $this->view->assign('taskList', Task::getTaskList());
  87. return $this->view->fetch();
  88. }
  89. public function multi($ids = null)
  90. {
  91. if (false === $this->request->isPost()) {
  92. $this->error(__('Invalid parameters'));
  93. }
  94. $ids = $ids ?: $this->request->post('ids');
  95. if (empty($ids)) {
  96. $this->error(__('Parameter %s can not be empty', 'ids'));
  97. }
  98. if (false === $this->request->has('params')) {
  99. $this->error(__('No rows were updated'));
  100. }
  101. parse_str($this->request->post('params'), $values);
  102. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  103. if (empty($values)) {
  104. $this->error(__('You have no permission'));
  105. }
  106. $adminIds = $this->getDataLimitAdminIds();
  107. if (is_array($adminIds)) {
  108. $this->model->where($this->dataLimitField, 'in', $adminIds);
  109. }
  110. $count = 0;
  111. Db::startTrans();
  112. try {
  113. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  114. foreach ($list as $item) {
  115. $count += $item->allowField(true)->isUpdate(true)->save($values);
  116. MqttMessageClient::getInstance()->publish('/device/' . $item->device_sn . '/master', $values['status'] ? 'stop' : 'start', 0);
  117. }
  118. Db::commit();
  119. } catch (PDOException|Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($count) {
  124. $this->success();
  125. }
  126. $this->error(__('No rows were updated'));
  127. }
  128. function taskSrart()
  129. {
  130. $server = 'broker.emqx.io';
  131. $port = 1883;
  132. $clientId = 'test-publisher';
  133. $mqtt = new \PhpMqtt\Client\MQTTClient($server, $port, $clientId);
  134. $mqtt->connect();
  135. $mqtt->publish('testtopic/1001', 'start', 0);
  136. $mqtt->close();
  137. }
  138. function taskStop()
  139. {
  140. }
  141. }