forked from planetbeing/homekit-edison-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·277 lines (218 loc) · 7.22 KB
/
Copy pathindex.js
File metadata and controls
executable file
·277 lines (218 loc) · 7.22 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2015 - 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//HOMEKIT INIT
var Accessory = require('./lib/Accessory.js').Accessory;
var Bridge = require('./lib/Bridge.js').Bridge;
var Camera = require('./lib/Camera.js').Camera;
var Service = require('./lib/Service.js').Service;
var Characteristic = require('./lib/Characteristic.js').Characteristic;
var uuid = require('./lib/util/uuid');
var AccessoryLoader = require('./lib/AccessoryLoader.js');
var StreamController = require('./lib/StreamController.js').StreamController;
var storage = require('node-persist');
// ensure Characteristic subclasses are defined
var HomeKitTypes = require('./lib/gen/HomeKitTypes');
module.exports = {
init: init,
Accessory: Accessory,
Bridge: Bridge,
Camera: Camera,
Service: Service,
Characteristic: Characteristic,
uuid: uuid,
AccessoryLoader: AccessoryLoader,
StreamController: StreamController
}
function init(storagePath) {
// initialize our underlying storage system, passing on the directory if needed
if (typeof storagePath !== 'undefined')
storage.initSync({ dir: storagePath });
else
storage.initSync(); // use whatever is default
}
//END HOMEKIT INDEX Files
"use strict";
// The program is using the Node.js built-in `fs` module
// to load the config.json and any other files needed
var fs = require("fs");
// The program is using the Node.js built-in `path` module to find
// the file path to needed files on disk
var path = require("path");
// Load configuration data from `config.json` file. Edit this file
// to change to correct values for your configuration
var config = JSON.parse(
fs.readFileSync(path.join(__dirname, "config.json"))
);
// Initialize the hardware for whichever kit we are using
var board;
if (config.kit) {
board = require("./" + config.kit + ".js");
} else {
board = require('./grove.js');
}
board.init(config);
var datastore = require("./datastore");
var mqtt = require("./mqtt");
//Rest button
var mraa = require('mraa');
// Using GPIO pin 2, which corresponds to D2 on the baseboard.
var resetButton = new mraa.Gpio(2);
// The program handles events generated by the various connected
// hardware devices using the Node.js built-in `events` module
var events = new (require("events").EventEmitter)();
// The program is using the `twilio` module
// to make the remote calls to Twilio service
// to send SMS alerts
var twilio;
var smsSent = false;
if (config.TWILIO_ACCT_SID && config.TWILIO_AUTH_TOKEN) {
twilio = require("twilio")(config.TWILIO_ACCT_SID,
config.TWILIO_AUTH_TOKEN);
}
// Send an SMS alert that a possible fire has been detected
function notifySMS() {
if (!config.TWILIO_ACCT_SID || !config.TWILIO_AUTH_TOKEN) {
return;
}
// only send an SMS every 1 minute
if (smsSent) {
return;
}
var opts = { to: config.NUMBER_TO_SEND_TO,
from: config.TWILIO_OUTGOING_NUMBER,
body: "fire alarm" };
// send SMS
twilio.sendMessage(opts, function(err, response) {
if (err) { return console.error("err:", err); }
console.log("SMS sent", response);
});
smsSent = true;
setTimeout(function() {
smsSent = false;
}, 1000 * 60);
}
// Display and then store record in the remote datastore
// of each time a fire alarm condition has been triggered
function notify() {
console.log("fire alarm");
notifySMS();
var payload = { value: "fire alarm", datetime: new Date().toISOString() };
datastore.log(config, payload);
mqtt.log(config, payload);
}
// Loops every 500ms to check if the ambient temperature
// has exceeded the threshold, indicating that a possible
// fire emergency exists
function monitorTemperature() {
var prev = 0;
setInterval(function() {
var currentTemp = board.getTemperature();
var currentLight = board.getLight();
board.message("Temperature: " + currentTemp);
board.message("Light Level: " + currentLight, 1);
// check if fire alarm should be triggered
if (prev < config.ALARM_THRESHOLD && currentTemp >= config.ALARM_THRESHOLD) {
events.emit("start-alarm");
}
if (prev >= config.ALARM_THRESHOLD && currentTemp < config.ALARM_THRESHOLD) {
events.emit("stop-alarm");
}
prev = currentTemp;
//AUTOMATION LOGIC
/**
//TOO COLD
if ( flueTemp < 150 ){
fireStatus = low;
//// Fire is just starting
if( flameSensor == true ){
notify("Keep an eye on it.");
damper.position = 100;
}
//fire is dying
if( flameSensor == false ){
alarm();
notify("Low fire. Creosote danger. Add wood.");
damper.position = 0;
}
}
// JUST RIGHT
if ( flueTemp > 150 && flueTemp <= 400){
fireStatus = good;
alarm();
notify("Nice fire.")
damper.position = 50;
}
//TOO HOT
if ( flueTemp > 400 && flueTemp <= 600){
fireStatus = hot;
if( flameSensor == true ){
alarm();
notify("The fire is too hot. Adjusting damper."
) damper.position = 20;
}
if( flameSensor == false ){
alarm();
notify("Your fire is out but your chimney is on fire."
) damper.position = 0;
}
}
**/
//monitor Button
//readButtonValue();
}, 500);
}
// Called to start the alarm when a possible fire
function alarm() {
notify();
var tick = true;
board.color("red");
board.message("fire detected!", 1);
board.buzz();
board.closeDamper();
var interval = setInterval(function() {
board.color(tick ? "white" : "red");
if (tick) { board.stopBuzzing(); } else { board.buzz(); }
tick = !tick;
}, 250);
events.once("stop-alarm", function() {
clearInterval(interval);
board.reset();
});
}
function readButtonValue() {
console.log("Button value is " + resetButton.value());
}
// The main function makes sure the alarm buzzer and LCD
// are turned off, then starts checking the ambient temperature
// using the connected hardware.
// The custom event `start-alarm` is fired, if a possible
// fire emergency exists, which calls the `alarm()` function.
function main() {
board.reset();
board.bootSounds();
//board.motorCheck();
board.openDamper();
monitorTemperature();
events.on("start-alarm", alarm);
}
main();