-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·201 lines (178 loc) · 4.98 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
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
#!/usr/bin/node
// Power Brain App v2.0
// OSC Bridge
// MQTT Bridge
// WebSocket for web
// const config = require('./mod/config')
global.alive = 0;
global.motion = 0;
global.lightactivity = 0;
global.keyactivity = 0;
global.webactivity = 0;
global.touchactivity = 0;
global.beat = 0;
global.playing = {};
global.power_a = false;
global.power_b = false;
global.power_c = false;
global.power_d = false;
global.wifi = 0;
global.uptime = 0;
const mqtt = require("./mod/mqtt");
const sonicpi = require("./mod/sonicpi");
const screen = require("./mod/screen");
const system = require("./mod/system");
const lights = require("./mod/lights");
// const touch = require('./mod/touch')
const tower = require("./mod/tower");
const socket = require("./mod/socket");
// Render the screen.
screen.screen.render();
screen.screen.on("keypress", function (key) {
global.keyactivity = 1;
activityCheck(false);
clearTimeout(keytimeout);
keytimeout = setTimeout(function () {
global.keyactivity = 0;
screen.updateStatus(activityStatus());
screen.log("Killing keys after 10s inactivity");
}, 10000);
screen.updateStatus(activityStatus());
});
// Bind to OSC
sonicpi.bind();
// State
let keytimeout;
//updateSystem
//
function activityStatus() {
return (
"\n" +
"Motion: " +
global.motion +
" Touch:" +
global.touchactivity +
" Keys:" +
global.keyactivity +
" Alive:" +
global.alive +
" Lights: " +
global.lightactivity
);
}
// Alive or dead?
function activityCheck(rerun = true) {
if (
(global.motion || global.touchactivity || global.keyactivity) &&
!global.alive
) {
// recent motion
global.alive = 1;
// log('Alive check')
screen.log("We become alive 👍🏻");
//system.say('Welcome Human, come play with me')
system.welcome();
}
// totaly dead
if (
!global.motion &&
!global.touchactivity &&
!global.keyactivity &&
global.alive
) {
// motion is off
global.alive = 0;
screen.log("We are dead 🤢");
system.say("Going stand by..");
// TOOD
lights.allLightStandby();
// movie sample
// sonicpi.play(300)
// aroma
//sonicpi.play(200)
tower.powerOff();
}
screen.updateStatus(activityStatus());
// TODO
// mqtt.publish('app/beat', 'app alive '+Date.now())
//console.debug("redraw ",activityStatus())
if (rerun) {
setTimeout(activityCheck, 5000); // run every 10s
}
}
// MOTION
mqtt.on("message", function (topic, message) {
// message is Buffer
if (topic === "motion") {
const json = JSON.parse(message.toString());
// if (json.e === 'motion') {
// //sonicpi.motion()
// let max = 200000
// let point = max / 100
// let amp = json.pixel / max
// if(amp > 0.1) {
// // sonicpi.motion(amp)
// }
// global.motion = 1
// }
if (json.e === "start") {
// activityCheck(false)
// system.say('Motion strated')
// screen.mqttlog('Motind strated', json)
if (!global.alive) {
// we dead bring it back to live
global.motion = 1;
activityCheck(false);
// system.say("Motion is on");
screen.mqttlog("Motin started", json);
system.welcome();
}
}
if (json.e === "end") {
global.motion = 0;
activityCheck(false);
system.say("Going stand by");
screen.mqttlog("Motind ended", json);
}
} else if (topic == "wled/a/status" ) {
// TODO only if they from off -> online . track it.
screen.log("Lights A become " + message.toString());
// system.say("Lights A are " + message.toString());
global.lightactivity = 1;
screen.updateStatus(activityStatus());
} else if (topic == "wled/c/status") {
// TODO only if they from off -> online . track it.
screen.log("Lights C become " + message.toString());
// system.say("Lights C are " + message.toString());
global.lightactivity = 1;
screen.updateStatus(activityStatus());
} else if (topic == "turtlecar/status") {
screen.log("turtlecar become " + message.toString());
// global.wifi = message.toString()
// screen.updateSystem(screen.systemStatus())
if (message.toString() == "online") {
system.say("Mr Turtle is " + message.toString());
sonicpi.sample("/srv/coco.wav");
} else if(message.toString() == "offline") {
system.say("Turtle is leaving us, till next time!");
sonicpi.sample("/srv/sad.wav");
}
} else if (topic == "tele/tasmota_01F72B/LWT") {
screen.log("PowerSwitch become " + message.toString());
// system.say("PowerSwitch is " + message.toString());
} else if (topic == "network/dhcp") {
// system.say("We got another one");
sonicpi.siren();
} else if (topic == "play/movie") {
sonicpi.movie();
} else {
screen.mqttlog(topic, message.toString())
}
});
// lights.allLightRandom()
system.say("Tower Brain Started");
sonicpi.play(100);
sonicpi.play(200);
activityCheck(true);
screen.updateStatus(activityStatus());
screen.updateSystem(screen.systemStatus());