+ 1 سال پیش
والد
کامیت
7ccc70d988

+ 36 - 4
application/admin/command/AutoTask.php

@@ -2,9 +2,13 @@
 
 namespace app\admin\command;
 
+use app\admin\model\TaskSteps;
+use app\admin\services\MqttMessageClient;
 use think\console\Command;
 use think\console\Input;
 use think\console\Output;
+use think\Db;
+use think\helper\Arr;
 
 class AutoTask extends Command
 {
@@ -17,9 +21,37 @@ class AutoTask extends Command
 
     public function execute(Input $input, Output $output)
     {
-        //TODO STEP1 获取当前的时间
-        //TODO 根据时间获取需要下发的指令
-        //TODO 下发指令
-        echo "自动执行启动了";
+        while (true) {
+            $time = Date('H:i');
+            $list = TaskSteps::with(['task.device'])->whereRaw("DATE_FORMAT(start_time, '%H:%i')='{$time}'")->whereOrRaw("DATE_FORMAT(end_time, '%H:%i')='{$time}'")->select();
+            if ($list) {
+                foreach ($list as $item) {
+                    $device_sn = $item->task->device[0]->device_sn;
+                    if ($device_sn && substr($item->start_time, 0, 5) === $time) {
+                        // 发送开始
+                        $this->publish($device_sn, 'RUN', $item->name, [
+                            'SCRIPT' => base64_encode("log('aa')"),
+                            'CONFIG' => [],
+                        ]);
+                        $this->publish($device_sn, 'START', $item->name, []);
+                    }
+                    if ($device_sn && substr($item->end_time, 0, 5) === $time) {
+                        // 发送结束
+                        $this->publish($device_sn, 'STOP', $item->name, []);
+                    }
+                }
+            }
+            sleep(60);
+        }
+    }
+
+    public function publish($device_sn, $action, $name, $data = [])
+    {
+        MqttMessageClient::getInstance()->publish('/device/' . $device_sn . '/master', json_encode([
+            'SCRIPT_ACTION' => $action,
+            'SCRIPT_NAME' => $name,
+            'SCRIPT_DATA' => $data,
+        ]), 0);
+        echo sprintf("发送指令 %s %s %s %s\n", $device_sn, $action, $name, json_encode($data));
     }
 }

+ 42 - 0
application/admin/controller/device/Device.php

@@ -2,6 +2,7 @@
 
 namespace app\admin\controller\device;
 
+use app\admin\services\MqttMessageClient;
 use app\common\controller\Backend;
 use app\admin\model\Task;
 use fast\Arr;
@@ -96,6 +97,47 @@ class Device extends Backend
         return $this->view->fetch();
     }
 
+    public function multi($ids = null)
+    {
+        if (false === $this->request->isPost()) {
+            $this->error(__('Invalid parameters'));
+        }
+        $ids = $ids ?: $this->request->post('ids');
+        if (empty($ids)) {
+            $this->error(__('Parameter %s can not be empty', 'ids'));
+        }
+
+        if (false === $this->request->has('params')) {
+            $this->error(__('No rows were updated'));
+        }
+        parse_str($this->request->post('params'), $values);
+        $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
+        if (empty($values)) {
+            $this->error(__('You have no permission'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds)) {
+            $this->model->where($this->dataLimitField, 'in', $adminIds);
+        }
+        $count = 0;
+        Db::startTrans();
+        try {
+            $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
+            foreach ($list as $item) {
+                $count += $item->allowField(true)->isUpdate(true)->save($values);
+                MqttMessageClient::getInstance()->publish('/device/' . $item->device_sn . '/master', $values['status'] ? 'stop' : 'start', 0);
+            }
+            Db::commit();
+        } catch (PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($count) {
+            $this->success();
+        }
+        $this->error(__('No rows were updated'));
+    }
+
     function taskSrart()
     {
         $server = 'broker.emqx.io';

+ 4 - 1
application/admin/model/Task.php

@@ -46,7 +46,10 @@ class Task extends Model
         return isset($list[$value]) ? $list[$value] : '';
     }
 
-
+    public function device()
+    {
+        return $this->hasMany(Device::class, 'task_id', 'id');
+    }
 
 
 }

+ 5 - 0
application/admin/model/TaskSteps.php

@@ -27,6 +27,11 @@ class TaskSteps extends Model
     protected $append = [
         'status_text'
     ];
+
+    public function task()
+    {
+        return $this->hasOne('Task', 'id', 'task_id');
+    }
     
 
     protected static function init()

+ 8 - 4
application/jobs/DeviceReportEvent.php

@@ -46,10 +46,14 @@ class DeviceReportEvent
     protected function online($device_id, $data)
     {
         $device = Device::where("device_sn", $device_id)->find();
-        if (!$device) {
-            return false;
+        if ($device) {
+            $device->status = 1;
+            $device->save();
+        } else {
+            Device::insertGetId([
+                "device_sn" => $device_id,
+                "status" => 1
+            ]);
         }
-        $device->status = 1;
-        $device->save();
     }
 }