-
Notifications
You must be signed in to change notification settings - Fork 6
/
smart-light.js
140 lines (116 loc) · 3.32 KB
/
smart-light.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
// Require the Grow.js build and johnny-five library.
const Thing = require('Grow.js');
const five = require('johnny-five');
const later = require('later');
// Use local time, not UTC.
later.date.localTime();
// See http://johnny-five.io/ to connect devices besides arduino.
const board = new five.Board();
// When board emits a 'ready' event run this start function.
board.on('ready', function start() {
// Define variables
var power = new five.Pin(11),
LED = new five.Pin(13),
lightSensor = new five.Sensor('A1');
power.high();
// Create a new thing.
var light = new Thing({
uuid: 'meow',
token: 'meow',
component: 'SmartLight',
properties: {
state: 'off',
threshold: 300,
interval: 1000,
currently: null,
lightconditions: null,
cycles: {
day: {
schedule: 'after 7:00am'
},
night: {
schedule: 'after 8:00pm'
}
}
},
start: function () {
var interval = this.get('interval');
this.interval = setInterval(function () {
light.light_data();
light.check_light_data();
}, interval);
var client = new Hs100Api.Client();
client.startDiscovery().on('plug-new', (plug) => {
if (plug.name === 'tplinkplug1') {
console.log('Light connected');
this.light = plug;
this.light.getInfo().then((data)=> {
if (data.sysInfo.relay_state === 1) {
this.set('state', 'on');
} else {
this.set('state', 'off');
}
}).catch(
(reason) => {
console.log('Handle rejected promise ('+reason+') here.');
}
);
}
});
this.parseCycles(this.get('cycles'));
},
stop: function () {
clearInterval(this.interval);
this.removeAllListeners();
},
day: function () {
console.log('It is day!');
this.set('currently', 'day');
this.call('turn_on');
},
night: function () {
console.log('It is night!');
this.set('currently', 'night');
this.call('turn_off');
},
turn_on: function () {
console.log('Light on');
// if (this.light) {
// this.light.setPowerState(true);
// }
LED.high()
this.set('state', 'on');
},
turn_off: function () {
console.log('Light off');
// if (this.light) {
// this.light.setPowerState(false);
// }
LED.low()
this.set('state', 'off');
},
light_data: function () {
let value = lightSensor.value;
light.emit('light', value);
},
check_light_data: function () {
let threshold = this.get('threshold');
let state = this.get('state');
let currently = this.get('currently');
if ((lightSensor.value < threshold) &&
(this.get('lightconditions') !== 'dark') &&
(currently === 'day')) {
console.log('Too dark for daylight hours, turning on light.')
this.set('lightconditions', 'dark');
this.call('turn_on');
}
else if ((lightSensor.value >= threshold) &&
(this.get('lightconditions') !== 'light') &&
(currently === 'day')) {
this.set('lightconditions', 'light');
this.call('turn_off');
}
}
});
light.connect({});
});