-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliam.js
More file actions
262 lines (237 loc) · 7.41 KB
/
liam.js
File metadata and controls
262 lines (237 loc) · 7.41 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
/*jshint esversion: 6 */
(() => {
'use strict';
// content of index.js
let Setcommands = {
'state': 202,
};
let topic = -1;
let mqtt = require('mqtt');
const server_port = 8000;
let voltAvg = [];
let arrayLen = 100;
// Web
var express = require('express');
var app = express();
var appApi = express();
const http = require('http');
let mower = {
'looptime': 0,
'battery':-1,
'avgvolt': 0,
'v_min': 10000,
'v_max': 0,
'state': -1,
'statename':'',
'avg_len': 0,
'state_starttime' : -1,
'message':'',
'update_time':''
};
// Server
var server = http.createServer(app);
// Socket
var io = require('socket.io')(server);
//#region MQTT
let mqttToken = "abc123",
mqttclient;
var mqttopts = {
port: 1883,
host: "palm-home", // host does NOT include port
// hostname?: string
// path?: string
protocol: 'mqtt',
/**
* 10 seconds, set to 0 to disable
*/
keepalive: 60 * 5,
/**
* 'mqttjs_' + Math.random().toString(16).substr(2, 8)
*/
clientId: "Liam_" + Date.now(),
/**
* 'MQTT'
*/
// protocolId:"mqtt",
// // /**
// // * 4
// // */
// protocolVersion: "4",
/**
// * true, set to false to receive QoS 1 and 2 messages while offline
// */
clean: true,
/**
* 1000 milliseconds, interval between two reconnections
*/
reconnectPeriod: 10 * 1000,
/**
* 30 * 1000 milliseconds, time to wait before a CONNACK is received
*/
connectTimeout: 30 * 10000,
/**
* the username required by your broker, if any
*/
username: "liam_" + Date.now(),
/**
* the password required by your broker, if any
*/
// password?: string
/**
* a Store for the incoming packets
// */
// incomingStore?: Store
// /**
// * a Store for the outgoing packets
// */
// outgoingStore?: Store
// queueQoSZero?: boolean
// reschedulePings?: boolean
// servers?: Array<{
// host: string
// port: number
// }>
// /**
// * true, set to false to disable re-subscribe functionality
// */
// resubscribe?: boolean
// /**
// * a message that will sent by the broker automatically when the client disconnect badly.
// */
// will?: {
// /**
// * the topic to publish
// */
// topic: string
// /**
// * the message to publish
// */
// payload: string
// /**
// * the QoS
// */
// qos: QoS
// /**
// * the retain flag
// */
// retain: boolean
};
mqttclient = mqtt.connect(mqttopts);
mqttclient.on('error', (error) => {
console.log("# MQTT LIAM ERROR");
});
mqttclient.on('offline', (error) => {
console.log("# MQTT LIAM host offline");
});
mqttclient.on('close', (error) => {
console.log("# MQTT LIAM connection closed");
});
mqttclient.on('connect', () => {
console.log("# MQTT is connected");
mqttclient.subscribe('#');
});
mqttclient.on('message', (topic, message) => {
try {
var mJson = JSON.parse(message);
switch (topic.toString().toLowerCase()) {
case "/liam/1/event/lastmessage":
mower.message = mJson.message;
break;
case "/liam/1/event/battery":
mower.battery = mJson.battery;
break;
case "/liam/1/event/state":
mower.state = mJson.state;
mower.statename = mJson.name;
break;
case "/liam/1/event/looptime":
mower.looptime = mJson.looptime;
break;
case '/liam/1/cmd_resp':
if(mJson.state)
{
mower.state = mJson.state;
mower.statename = mJson.name;
}
break;
default:
break;
}
mower.update_time = new Date().toLocaleDateString();
console.dir(topic);
console.dir(mJson);
io.emit('GUI_Message', mower);
} catch (error) {
console.log("ERROR in mqttclient.on(message :" + error + '\n' +"This was the error: "+ message);
}
});
//#endregion
//#region WEBSOCKET CLIENT --> SERVER
io.on('connection', (client) => {
console.log('_ Websocket client connected');
client.emit('GUI_Message', mower);
client.on('join', (data) => {
client.emit('GUI_Message', mower);
}); // Join
client.on('disconnect', () => {
mower.Message = "Mower disconnected";
console.log(mower.Message);
client.emit('GUI_Message',mower.Message);
});
client.on('mower__action', (mower) => {
let mowerJson = {};
let cmd = -1;
try {
console.log("detta är mower commad");
console.log(Setcommands[mower.command]);
switch (mower.set_get) {
case 'set':
console.log('set value');
topic = '/liam/1/cmd/set';
cmd = Setcommands[mower.command];
if (cmd === -1)
console.log("Detta gick inte bra. ");
mowerJson[0] = cmd;
mowerJson[1] = mower.value;
console.dir(mower);
break;
case 'get':
console.log(' get value');
break;
default:
console.log("Defalut value");
break;
}
} catch (error) {
console.log("Could nor parse json");
console.dir(mower);
}
publishCommnad(mowerJson);
});
});
//#region WEBSOCKET CLIENT --> SERVER
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/node_modules'));
app.get('/.', function (req, res, next) {
console.log("=========================");
res.send(404,'Sorry no such entry');
});
app.get('/Liam', function (req, res, next) {
console.log("=========================");
res.sendFile(__dirname + '/public/html/index.html');
});
//#region Server
server.listen(server_port, function () {
console.log("> Server listening on : " + server_port);
});
server.on('error', function (e) {
console.log("HTTPPORT is occupied, start as sudo or change port");
console.log(e);
});
function publishCommnad(mowerJson) {
console.log("detta är vad som kommer skickas mot klipparen.");
console.dir(mowerJson);
if (topic != -1)
mqttclient.publish(topic, JSON.stringify(mowerJson) + '\r\n');
}
})();