|
|
@@ -2,13 +2,15 @@ package scene
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "fmt"
|
|
|
"github.com/gogf/gf/database/gredis"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- scenePrefix = "scene:"
|
|
|
- dataExpires = 7200
|
|
|
+ timerScenePrefix = "scene:timer:*"
|
|
|
+ deviceScenePrefix = "scene:device_status:*"
|
|
|
+ weatherScenePrefix = "scene:weather:*"
|
|
|
+ scenePrefix = "scene:*"
|
|
|
+ dataExpires = 7200
|
|
|
)
|
|
|
|
|
|
type GetSceneParams struct {
|
|
|
@@ -18,17 +20,53 @@ type GetSceneParams struct {
|
|
|
}
|
|
|
|
|
|
type InfoScene struct {
|
|
|
- Id string `json:"id"`
|
|
|
- Name string `json:"name"`
|
|
|
- Status int `json:"status"`
|
|
|
- Action string `json:"action"`
|
|
|
- Desc string `json:"desc"`
|
|
|
- SceneType string `json:"scene_type"`
|
|
|
- DeviceType string `json:"device_type"`
|
|
|
- Config string `json:"config"`
|
|
|
- Time string `json:"time"`
|
|
|
+ SceneId string `json:"scene_id"`
|
|
|
+ Conditions []*Conditions `json:"conditions"`
|
|
|
+ Actions []*Action `json:"actions"`
|
|
|
+}
|
|
|
+
|
|
|
+type Conditions struct {
|
|
|
+ TaskId string `json:"task_id"`
|
|
|
+ Times int `json:"times"` // 执行次数 -1 表示无限次
|
|
|
+ Cron string `json:"cron"`
|
|
|
+
|
|
|
+ Key string `json:"key"` // redis key
|
|
|
+ DeviceType string `json:"device_type"` // 设备类型
|
|
|
+ DeviceId string `json:"device_id"` // 设备id
|
|
|
+ SubDeviceId string `json:"sub_device_id"` // 子设备id
|
|
|
+ FieldType int `json:"field_type"` // 字段类型 1字符串 2数值
|
|
|
+ Field string `json:"field"` // 字段名
|
|
|
+ TargetValue string `json:"target_value"` // 值
|
|
|
+ Operator int `json:"operator"` // 比较类型 数值比较 1 > 2 >= 3 = 4 <= 5 < 6 !=
|
|
|
+
|
|
|
+ Location string `json:"location"` // 地点
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+type Action struct {
|
|
|
+ DeviceID string `json:"device_id"` // 设备ID
|
|
|
+ SubDeviceId string `json:"sub_device_id"` // 实体子设备Id,如果需要
|
|
|
+ ActionExecutor string `json:"action_executor"` // 动作对象类型
|
|
|
+ ExecutorProperty *TaskExecutorProperty `json:"executor_property"` // 动作执行明细
|
|
|
+ PlcPubMessage *PlcPubMessage `json:"plc_pub_message"` // PLC消息
|
|
|
+}
|
|
|
+
|
|
|
+// TaskExecutorProperty 定时任务执行动作执行参数
|
|
|
+type TaskExecutorProperty struct {
|
|
|
+ FunctionCode string `json:"function_code"`
|
|
|
+ FunctionValue map[string]interface{} `json:"function_value"`
|
|
|
+ DelaySeconds int64 `json:"delay_seconds"`
|
|
|
+}
|
|
|
+
|
|
|
+type PlcPubMessage struct {
|
|
|
+ Topic string `json:"topic"`
|
|
|
+ Payload []byte `json:"payload"`
|
|
|
}
|
|
|
|
|
|
+type AllSceneResult struct {
|
|
|
+ Total int `json:"total"`
|
|
|
+ Data []*InfoScene `json:"data"`
|
|
|
+}
|
|
|
type Info struct {
|
|
|
Key string `json:"key"`
|
|
|
SceneId string `json:"scene_id"`
|
|
|
@@ -61,7 +99,7 @@ func (a *Manager) SaveScene(info *Info) error {
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- _, err = a.redisClient.Do("EXPIRE", info.Key, 0)
|
|
|
+ _, err = a.redisClient.Do("EXPIRE", info.Key, -1)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
@@ -86,11 +124,77 @@ func (a *Manager) DeleteScene(key string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// GetAllScenes 查询所有场景信息s
|
|
|
-func (a *Manager) GetAllScenes() ([]*InfoScene, error) {
|
|
|
+// GetAllScenesPage 查询定时场景
|
|
|
+func (a *Manager) GetAllScenesPage(code string, pi, ps int) (AllSceneResult, error) {
|
|
|
+ var key string
|
|
|
+ switch code {
|
|
|
+ case "time":
|
|
|
+ key = timerScenePrefix
|
|
|
+ case "device":
|
|
|
+ key = deviceScenePrefix
|
|
|
+ case "weather":
|
|
|
+ key = weatherScenePrefix
|
|
|
+ default:
|
|
|
+ key = scenePrefix
|
|
|
+ }
|
|
|
+
|
|
|
+ var result AllSceneResult
|
|
|
// 使用KEYS命令获取所有匹配前缀的键s
|
|
|
- keys, err := a.redisClient.DoVar("KEYS", scenePrefix+"*")
|
|
|
- fmt.Printf("使用KEYS命令获取所有匹配前缀的键%v", keys)
|
|
|
+ keys, err := a.redisClient.DoVar("KEYS", key)
|
|
|
+ if err != nil {
|
|
|
+ return result, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否有数据
|
|
|
+ if keys.IsEmpty() {
|
|
|
+ return result, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ var start, end int
|
|
|
+ // 转换为字符串切片
|
|
|
+ keyList := keys.Strings()
|
|
|
+ result.Total = len(keyList)
|
|
|
+ if len(keyList) > 0 {
|
|
|
+ start = (pi - 1) * ps
|
|
|
+ end = pi*ps - 1
|
|
|
+ if end > len(keyList)-1 {
|
|
|
+ end = len(keyList) - 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 存储所有结果
|
|
|
+ scenes := make([]*InfoScene, 0)
|
|
|
+ var scene InfoScene
|
|
|
+ for i := start; i <= end; i++ {
|
|
|
+ r, err := a.redisClient.DoVar("GET", keyList[i])
|
|
|
+ if err != nil {
|
|
|
+ return result, err
|
|
|
+ }
|
|
|
+ err = r.Struct(&scene)
|
|
|
+ if err != nil {
|
|
|
+ return result, err
|
|
|
+ }
|
|
|
+ scenes = append(scenes, &scene)
|
|
|
+ }
|
|
|
+ return result, nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetAllScenes 查询定时场景
|
|
|
+func (a *Manager) GetAllScenes(code string) ([]*InfoScene, error) {
|
|
|
+ var key string
|
|
|
+ switch code {
|
|
|
+ case "time":
|
|
|
+ key = timerScenePrefix
|
|
|
+ case "device":
|
|
|
+ key = deviceScenePrefix
|
|
|
+ case "weather":
|
|
|
+ key = weatherScenePrefix
|
|
|
+ default:
|
|
|
+ key = scenePrefix
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用KEYS命令获取所有匹配前缀的键s
|
|
|
+ keys, err := a.redisClient.DoVar("KEYS", key)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -106,18 +210,16 @@ func (a *Manager) GetAllScenes() ([]*InfoScene, error) {
|
|
|
// 存储所有结果
|
|
|
scenes := make([]*InfoScene, 0)
|
|
|
var scene InfoScene
|
|
|
- for _, key := range keyList {
|
|
|
-
|
|
|
- result, err := a.redisClient.DoVar("GET", key)
|
|
|
+ for _, v := range keyList {
|
|
|
+ r, err := a.redisClient.DoVar("GET", v)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- err = result.Struct(&scene)
|
|
|
+ err = r.Struct(&scene)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
scenes = append(scenes, &scene)
|
|
|
}
|
|
|
-
|
|
|
return scenes, nil
|
|
|
}
|