2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package cn.iocoder.yudao.module.iot.dal.dataobject.rule;
 
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertConfigDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleActionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleConditionOperatorEnum;
import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleConditionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.rule.IotSceneRuleTriggerTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.Jackson3TypeHandler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * IoT 场景联动规则 DO
 *
 * @author 芋道源码
 */
@TableName(value = "iot_scene_rule", autoResultMap = true)
@KeySequence("iot_scene_rule_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IotSceneRuleDO extends TenantBaseDO {
 
    /**
     * 场景联动编号
     */
    @TableId
    private Long id;
    /**
     * 场景联动名称
     */
    private String name;
    /**
     * 场景联动描述
     */
    private String description;
    /**
     * 场景联动状态
     *
     * 枚举 {@link CommonStatusEnum}
     */
    private Integer status;
 
    /**
     * 最后触发时间
     */
    private LocalDateTime lastTriggerTime;
 
    /**
     * 场景定义配置
     */
    @TableField(typeHandler = Jackson3TypeHandler.class)
    private List<Trigger> triggers;
 
    /**
     * 场景动作配置
     */
    @TableField(typeHandler = Jackson3TypeHandler.class)
    private List<Action> actions;
 
    /**
     * 场景定义配置
     */
    @Data
    public static class Trigger {
 
        // ========== 事件部分 ==========
 
        /**
         * 场景事件类型
         *
         * 枚举 {@link IotSceneRuleTriggerTypeEnum}
         * 1. {@link IotSceneRuleTriggerTypeEnum#DEVICE_STATE_UPDATE} 时,operator 非空,并且 value 为在线状态
         * 2. {@link IotSceneRuleTriggerTypeEnum#DEVICE_PROPERTY_POST}
         *    {@link IotSceneRuleTriggerTypeEnum#DEVICE_EVENT_POST} 时,identifier、operator 非空,并且 value 为属性值
         * 3. {@link IotSceneRuleTriggerTypeEnum#DEVICE_EVENT_POST}
         *    {@link IotSceneRuleTriggerTypeEnum#DEVICE_SERVICE_INVOKE} 时,identifier 非空,但是 operator、value 为空
         * 4. {@link IotSceneRuleTriggerTypeEnum#TIMER} 时,conditions 非空,并且设备无关(无需 productId、deviceId 字段)
         */
        private Integer type;
 
        /**
         * 产品编号
         *
         * 关联 {@link IotProductDO#getId()}
         */
        private Long productId;
        /**
         * 设备编号
         *
         * 关联 {@link IotDeviceDO#getId()}
         * 特殊:如果为 {@link IotDeviceDO#DEVICE_ID_ALL} 时,则是全部设备
         */
        private Long deviceId;
        /**
         * 物模型标识符
         *
         * 对应:{@link IotThingModelDO#getIdentifier()}
         */
        private String identifier;
        /**
         * 操作符
         *
         * 枚举 {@link IotSceneRuleConditionOperatorEnum}
         */
        private String operator;
        /**
         * 参数(属性值、在线状态)
         * <p>
         * 如果有多个值,则使用 "," 分隔,类似 "1,2,3"。
         * 例如说,{@link IotSceneRuleConditionOperatorEnum#IN}、{@link IotSceneRuleConditionOperatorEnum#BETWEEN}
         */
        private String value;
 
        /**
         * CRON 表达式
         */
        private String cronExpression;
 
        // ========== 条件部分 ==========
 
        /**
         * 触发条件分组(状态条件分组)的数组
         * <p>
         * 第一层 List:分组与分组之间,是“或”的关系
         * 第二层 List:条件与条件之间,是“且”的关系
         */
        private List<List<TriggerCondition>> conditionGroups;
 
    }
 
    /**
     * 触发条件(状态条件)
     */
    @Data
    public static class TriggerCondition {
 
        /**
         * 触发条件类型
         *
         * 枚举 {@link IotSceneRuleConditionTypeEnum}
         * 1. {@link IotSceneRuleConditionTypeEnum#DEVICE_STATE} 时,operator 非空,并且 value 为在线状态
         * 2. {@link IotSceneRuleConditionTypeEnum#DEVICE_PROPERTY} 时,identifier、operator 非空,并且 value 为属性值
         * 3. {@link IotSceneRuleConditionTypeEnum#CURRENT_TIME} 时,operator 非空(使用 DATE_TIME_ 和 TIME_ 部分),并且 value 非空
         */
        private Integer type;
 
        /**
         * 产品编号
         *
         * 关联 {@link IotProductDO#getId()}
         */
        private Long productId;
        /**
         * 设备编号
         *
         * 关联 {@link IotDeviceDO#getId()}
         */
        private Long deviceId;
        /**
         * 标识符(属性)
         *
         * 关联 {@link IotThingModelDO#getIdentifier()}
         */
        private String identifier;
        /**
         * 操作符
         *
         * 枚举 {@link IotSceneRuleConditionOperatorEnum}
         */
        private String operator;
        /**
         * 参数
         *
         * 如果有多个值,则使用 "," 分隔,类似 "1,2,3"。
         * 例如说,{@link IotSceneRuleConditionOperatorEnum#IN}、{@link IotSceneRuleConditionOperatorEnum#BETWEEN}
         */
        private String param;
 
    }
 
    /**
     * 场景动作配置
     */
    @Data
    public static class Action {
 
        /**
         * 执行类型
         *
         * 枚举 {@link IotSceneRuleActionTypeEnum}
         * 1. {@link IotSceneRuleActionTypeEnum#DEVICE_PROPERTY_SET} 时,params 非空
         *    {@link IotSceneRuleActionTypeEnum#DEVICE_SERVICE_INVOKE} 时,params 非空
         * 2. {@link IotSceneRuleActionTypeEnum#ALERT_TRIGGER} 时,alertConfigId 为空,因为是 {@link IotAlertConfigDO} 里面关联它
         * 3. {@link IotSceneRuleActionTypeEnum#ALERT_RECOVER} 时,alertConfigId 非空
         */
        private Integer type;
 
        /**
         * 产品编号
         *
         * 关联 {@link IotProductDO#getId()}
         */
        private Long productId;
        /**
         * 设备编号
         *
         * 关联 {@link IotDeviceDO#getId()}
         */
        private Long deviceId;
 
        /**
         * 标识符(服务)
         * <p>
         * 关联 {@link IotThingModelDO#getIdentifier()}
         */
        private String identifier;
 
        /**
         * 请求参数
         *
         * 一般来说,对应 {@link IotDeviceMessage#getParams()} 请求参数
         */
        private String params;
 
        /**
         * 告警配置编号
         *
         * 关联 {@link IotAlertConfigDO#getId()}
         */
        private Long alertConfigId;
 
    }
 
}