feat: 初始化 miot_x TypeScript 工作流构建器项目
This commit is contained in:
49
examples/rules/index.ts
Normal file
49
examples/rules/index.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { defineDevices, defineRule } from "../../src/index.ts";
|
||||
|
||||
const devices = defineDevices({
|
||||
corridorLight: {
|
||||
did: "group.1815373077765824512",
|
||||
urn: "urn:miot-spec-v2:device:light:0000A001:mijia-group3:3:0000C802",
|
||||
name: "走廊筒灯",
|
||||
},
|
||||
corridorMotion: {
|
||||
did: "blt.3.1htiptpdgco00",
|
||||
urn: "urn:miot-spec-v2:device:motion-sensor:0000A014:xiaomi-pir1:2",
|
||||
name: "走廊人体传感器",
|
||||
},
|
||||
gateway: {
|
||||
did: "1104758822",
|
||||
urn: "urn:miot-spec-v2:device:gateway:0000A019:xiaomi-hub1:3",
|
||||
name: "中枢网关",
|
||||
},
|
||||
});
|
||||
|
||||
export const eventTurnOn = defineRule("示例-事件触发开灯", ({ device, on }) =>
|
||||
on(device(devices.corridorMotion).event({ siid: 2, eiid: 1008 }))
|
||||
.do(device(devices.corridorLight).set({ siid: 2, piid: 1, value: true })),
|
||||
);
|
||||
|
||||
export const propertyTurnOff = defineRule("示例-属性触发关灯", ({ device, on }) =>
|
||||
on(device(devices.corridorLight).propertyTrigger({ siid: 2, piid: 1, dtype: "boolean", operator: "=", value: true }))
|
||||
.do(device(devices.corridorLight).set({ siid: 2, piid: 1, value: false })),
|
||||
);
|
||||
|
||||
export const conditionBeforeAction = defineRule("示例-状态判断后动作", ({ device, on }) =>
|
||||
on(device(devices.corridorMotion).event({ siid: 2, eiid: 1008 }))
|
||||
.when(device(devices.corridorLight).get({ siid: 2, piid: 1, dtype: "boolean", operator: "=", value: false }))
|
||||
.do(device(devices.corridorLight).set({ siid: 2, piid: 1, value: true })),
|
||||
);
|
||||
|
||||
export const mergedTriggers = defineRule("示例-多触发合并", ({ device, onAny }) =>
|
||||
onAny(
|
||||
device(devices.corridorMotion).event({ siid: 2, eiid: 1008 }),
|
||||
device(devices.gateway).event({ siid: 4, eiid: 1 }),
|
||||
).do(device(devices.corridorLight).set({ siid: 2, piid: 2, value: 100 })),
|
||||
);
|
||||
|
||||
export const delayedAction = defineRule("示例-延时动作", ({ delay, device, on }) =>
|
||||
on(device(devices.corridorMotion).event({ siid: 2, eiid: 1008 }))
|
||||
.do(delay("5s"), device(devices.corridorLight).set({ siid: 2, piid: 1, value: false })),
|
||||
);
|
||||
|
||||
export default [eventTurnOn, propertyTurnOff, conditionBeforeAction, mergedTriggers, delayedAction];
|
||||
Reference in New Issue
Block a user