-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino.js
165 lines (142 loc) · 3.84 KB
/
arduino.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
const myqtt = require("mqtt");
const client = myqtt.connect("mqtt://broker.hivemq.com");
const spawn = require("child_process").spawn;
const process = require("process");
try {
let state = "";
client.on("connect", () => {
client.subscribe("bot/stop");
client.subscribe("bot/left");
client.subscribe("bot/right");
client.subscribe("bot/down");
client.subscribe("bot/up");
client.subscribe("bot/disconnect");
// sending back request of confiremation
client.publish("bit/connected", "true");
client.publish("but/state", 'true');
});
client.on("message", (topic, message) => {
console.log(message);
switch (topic) {
// send low level request
case "bot/left":
console.log(message);
return handleLeft(message);
case "bot/right":
return handleRight(message);
case "bot/up":
return handleUp(message);
case "bot/down":
return handleDown(message);
case "bot/stop":
return handleStop(message);
}
});
function handleLeft(message) {
try {
if (state != "left") {
console.log("moving left");
state = "left";
client.publish("bot/saate", state);
} else {
throw new Error("cannot move again");
}
} catch (err) {
console.log(err);
}
}
function handleRight(message) {
try {
if (state != "right") {
console.log("moving right");
state = "right";
client.publish("bot/saate", state);
} else {
throw new Error("cannot move right again");
}
} catch (err) {
console.log(err);
}
}
function handleUp(message) {
try {
if (state != "up") {
console.log("moving up");
state = "up";
client.publish("bot/saate", state);
} else {
throw new Error("cannot up move again");
}
} catch (err) {
console.log(err);
}
}
function handleDown(message) {
try {
if (state != "down") {
console.log("moving down");
state = "down";
client.publish("bot/saate", state);
} else {
throw new Error("cannot doen move again");
}
} catch (err) {
console.log(err);
}
}
function handleStop(message) {
try {
if (state != "stop") {
console.log("moving stop");
state = "up";
client.publish("bot/saate", state);
} else {
throw new Error("cannot stop again");
}
} catch (err) {
console.log(err);
}
}
function handleAppExit(options, err) {
if (err) {
console.log(err.stack);
}
if (options.cleanup) {
client.publish("garage/connected", "false");
}
if (options.exit) {
process.exit();
}
}
function handleAppExit(options, err) {
if (err) {
console.log(err.stack);
}
if (options.cleanup) {
client.publish("garage/connected", "false");
}
if (options.exit) {
process.exit();
}
}
} catch (err) {
console.log(err);
}
process.on(
"exit",
handleAppExit.bind(null, {
cleanup: true
})
);
process.on(
"SIGINT",
handleAppExit.bind(null, {
exit: true
})
);
process.on(
"uncaughtException",
handleAppExit.bind(null, {
exit: true
})
);