-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
144 lines (130 loc) · 4.25 KB
/
app.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
144
const Alarm = require("./Alarm");
//const Lock = require("./Lock");
var config = require("./config.json");
var alarm = new Alarm(config.username, config.password, config.code, config.lockserial);
//var lock;
const express = require("express");
const app = express();
const port = 3010;
app.get("/", (request, response) => {
if (alarm.installation == null) {
console.log("/: Init Failed");
initAlarm();
response.status(500);
response.json(getJsonState("ERROR", "Init Failed"));
} else {
console.log("/: Fetching");
console.log("/: Response: Versiure State API started");
response.status(200);
response.send("Versiure State API started");
}
});
app.get("/state", (request, response) => {
console.log("/state: Start " + new Date().toISOString());
if (alarm.installation == null) {
console.log("/state: Init Failed");
initAlarm();
response.status(500);
response.json(getJsonState("ERROR", "Init Failed"));
} else {
console.log("/state: Fetching");
alarm.getCurrentAlarmState(function (data) {
console.log("/state: Response: " + JSON.stringify(data));
if (data == "DISARMED") {
response.status(200);
response.json(getJsonState(data, ""));
} else if (data == "ARMED_AWAY") {
response.status(200);
response.json(getJsonState(data, ""));
} else if (data == "ARMED_HOME") {
response.status(200);
response.json(getJsonState(data, ""));
} else {
response.status(500);
response.json(getJsonState("ERROR", data["errorMessage"]));
}
});
}
});
app.get("/overview", (request, response) => {
console.log("/overview: Start " + new Date().toISOString());
if (alarm.installation == null) {
console.log("/overview: Init Failed");
initAlarm();
response.status(500);
response.json(getJsonState("ERROR", "Init Failed"));
} else {
console.log("/overview: Fetching");
alarm.getCurrentInstallation(function (data) {
console.log("/overview: Response: " + JSON.stringify(data));
response.status(200);
response.json(data);
});
}
});
app.get("/state/arm", (request, response) => {
if (alarm.installation == null) {
console.log("/state/arm: Init Failed");
initAlarm();
response.status(500);
response.json(getJsonState("ERROR", "Init Failed"));
} else {
console.log("/state/arm: Setting " + new Date().toISOString());
alarm.setTargetAlarmState("ARMED_AWAY", function (data) {
console.log("/state/arm: Response: Received");
console.log(data)
response.status(200);
response.json(getJsonState("ARMED_AWAY", "New State"));
});
}
});
app.get("/state/disarm", (request, response) => {
if (alarm.installation == null) {
console.log("/state/disarm: Init Failed");
initAlarm();
response.status(500);
response.json(getJsonState("ERROR", "Init Failed"));
} else {
console.log("/state/disarm: Setting " + new Date().toISOString());
alarm.setTargetAlarmState("DISARMED", function (data) {
console.log("/state/disarm: Response: Received");
response.status(200);
response.json(getJsonState("DISARMED", data.armStateDisarm ? "New State" : "Current State"));
});
}
});
app.get("/state/home", (request, response) => {
if (alarm.installation == null) {
console.log("/state/home: Init Failed");
initAlarm();
response.status(500);
response.json(getJsonState("ERROR", "Init Failed"));
} else {
console.log("/state/home: Setting " + new Date().toISOString());
alarm.setTargetAlarmState("ARMED_HOME", function (data) {
console.log("/state/home: Response: Received");
console.log(data)
response.status(200);
response.json(getJsonState("ARMED_HOME", data.armStateArmHome ? "New State" : "Current State"));
});
}
});
app.listen(port, (err) => {
if (err) {
return console.log("something bad happened", err);
}
initAlarm();
console.log("server is listening on " + port);
});
function initAlarm() {
alarm.init(function (data) {
//console.log("Setting up Lock...");
//lock = new Lock(alarm);
console.log("done");
});
}
app.set("json spaces", 40);
function getJsonState(state, message) {
json = '{"state":"' + state + '","message":"' + message + '"}';
return JSON.parse(json);
}