Device.php 3.3 KB

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