-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
112 lines (101 loc) · 2.58 KB
/
index.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
'use strict';
const env = process.env.ENV || 'production';
const modelLib = require('./model');
const lightLib = (env === 'production')
? require('./light')
: require('./light-simulator');
const buttonLib = (env === 'production')
? require('./button')
: require('./button-simulator');
const lamp = lightLib.create(40);
const modelListener = event => {
switch(event) {
case modelLib.LAMP_ON:
lamp.on();
break;
case modelLib.LAMP_OFF:
lamp.off();
break;
case modelLib.LAMP_FLASH:
lamp.flash();
break;
}
};
const model = modelLib.create('thing-001', {}, modelListener);
const button = buttonLib.create(37, () => model.set(true));
// setTimeout(() => model.set(false), 5000);
// setTimeout(() => model.set(true), 7000);
// light.init();
// button.init();
//
// const device = iot.device({
// keyPath: 'certs/private.pem.key',
// certPath: 'certs/certificate.pem.crt',
// caPath: 'certs/root-CA.crt',
// clientId: 'sub001',
// region: 'us-east-1'
// });
//
// device
// .on('connect', () => {
// console.log('connect', topic);
// device.subscribe(topic);
// // device.publish('topic_2', JSON.stringify({ test_data: 1}));
// });
//
// device
// .on('message', (topic, payload) => {
// flashDisplay();
// // console.log('message', topic, payload.toString());
// });
//
// const updateDisplay = state => {
// if (state.lamp) {
// light.on();
// } else {
// light.off();
// }
// };
//
// const flashDisplay = () => {
// light.flash();
// thing.get(thingName);
// };
//
// const thing = iot.thingShadow({
// keyPath: 'certs/private.pem.key',
// certPath: 'certs/certificate.pem.crt',
// caPath: 'certs/root-CA.crt',
// clientId: 'thing001',
// region: 'us-east-1'
// });
//
// thing.on('connect', () => {
// console.log('connect', thingName);
// thing.register(thingName, {}, () => {
// console.log("registered");
// thing.get(thingName);
// // synchronize once in a while, probably should
// // use the timeout message instead when I understand
// // all this a little better
// const sync = () => {
// setTimeout(() => {
// thing.get(thingName);
// sync();
// },3000);
// };
// sync();
// });
// });
//
// thing.on('status', (thingName, stat, clientToken, stateObject) => {
// if (stateObject.state.desired) {
// updateDisplay(stateObject.state.desired);
// }
// });
//
// thing.on('delta', (thingName, stateObject) => {
// updateDisplay(stateObject.state);
// });
//
// button.listen(() => console.log("ding"));