forked from kwanghoon/starter-kit-smartapp-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
143 lines (127 loc) · 4.66 KB
/
server.js
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
const SmartApp = require("@smartthings/smartapp");
const express = require("express");
const server = express();
const cors = require("cors");
const PORT = process.env.PORT || 3005;
server.use(express.json());
server.use(cors());
server.post("/", (req, res, next) => {
smartapp.handleHttpCallback(req, res);
});
server.get("/api/image", (req, res) => {
res.json(imageURLS);
});
server.listen(PORT, () =>
console.log(`Server is up and running on port ${PORT}`)
);
const imageURLS = [];
async function handleContactSensor(context, eventData, eventTime) {
console.log("handleContactSensor() is called.");
if (eventData.value === "open") {
context.api.devices.sendCommands(context.config.camera, "switch", "on");
} else {
context.api.devices.sendCommands(context.config.camera, "switch", "off");
}
}
async function handleMotionSensor(context, eventData, eventTime) {
console.log("handleMotionSensor() is called.");
}
async function handleButton(context, eventData, eventTime) {
console.log("handleButton() is called...");
}
async function handleCameraImageCapture(context, eventData, eventTime) {
// Contact Sensor Running - Camera Switch On - After CameraImage capture, you can get imageURL
console.log("handleCameraImageCapture() is called...");
console.log("Image URL : ", eventData.value);
// Below is a code that can check the data obtained by processing the received imageURL in node.js as it is
if (eventData.value) {
const imageURL = eventData.value;
imageURLS.push(imageURL);
console.log(imageURLS);
}
}
async function handleCameraSwitch(context, eventData, eventTime) {
console.log("handleCameraSwitch() is called...");
// Use take command of imageCapture capability when SmartThings camera is turned on by an action
// After the image capture is completed, handleCameraImageCapture can be used to check the presence or absence of image capture
// However, in order to check the image capture result, you must separately put the token in the URL and make a GET request
if (eventData.value === "on") {
context.api.devices.sendCommands(context.config.camera, [
{
capability: "image",
command: "take",
arguments: ["1", "2"],
},
]);
}
}
/* Define the SmartApp */
const smartapp = new SmartApp()
.configureI18n()
.enableEventLogging(2) // logs all lifecycle event requests/responses as pretty-printed JSON. Omit in production
.page("mainPage", (context, page, configData) => {
page.section("Starter kit", section => {
// https://www.samsung.com/sec/smartthings/HOMEKITA/HOMEKITA/
// (1) https://developer.smartthings.com/docs/devices/capabilities/capabilities-reference#contactSensor
section
.deviceSetting("contactSensor")
.capabilities(["contactSensor"])
.permissions("r")
.required(false);
// (2) https://developer.smartthings.com/docs/devices/capabilities/capabilities-reference#motionSensor
section
.deviceSetting("motionSensor")
.capabilities(["motionSensor"])
.permissions("r")
.required(false);
// (3) https://developer.smartthings.com/docs/devices/capabilities/capabilities-reference#button
section
.deviceSetting("smartButton")
.capabilities(["button"])
.permissions("r")
.required(false);
section
.deviceSetting("camera")
.capabilities(["imageCapture", "switch"])
.permissions("rwx")
.required(false);
});
})
.updated(async (context, updateData) => {
await context.api.subscriptions.delete();
await context.api.subscriptions.subscribeToDevices(
context.config.contactSensor,
"contactSensor",
"contact",
"contactSensorHandler"
);
await context.api.subscriptions.subscribeToDevices(
context.config.motionSensor,
"motionSensor",
"motion",
"motionSensorHandler"
);
await context.api.subscriptions.subscribeToDevices(
context.config.smartButton,
"button",
"button",
"buttonHandler"
);
await context.api.subscriptions.subscribeToDevices(
context.config.camera,
"imageCapture",
"image",
"cameraImageCaptureHandler"
);
await context.api.subscriptions.subscribeToDevices(
context.config.camera,
"switch",
"switch",
"cameraSwitchHandler"
);
})
.subscribedEventHandler("contactSensorHandler", handleContactSensor)
.subscribedEventHandler("motionSensorHandler", handleMotionSensor)
.subscribedEventHandler("buttonHandler", handleButton)
.subscribedEventHandler("cameraImageCaptureHandler", handleCameraImageCapture)
.subscribedEventHandler("cameraSwitchHandler", handleCameraSwitch);