Skip to content

Commit 8570717

Browse files
committed
Update endpoints
1 parent 4746942 commit 8570717

3 files changed

Lines changed: 128 additions & 118 deletions

File tree

app/controllers/iot/command-listener.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ function alterWeather(action) {
171171
});
172172
}
173173

174-
function fireDevices() {
175-
return fetch(`${devices}/devices`, {
176-
method: 'PUT'
174+
function fireDevices(type) {
175+
return fetch(`${devices}/devices/${type}`, {
176+
method: 'GET'
177177
}).catch((e) => {
178178
debug(e);
179179
});
180180
}
181181

182182
function fireAnimalCollars() {
183183
return fetch(`${devices}/animals`, {
184-
method: 'PUT'
184+
method: 'GET'
185185
}).catch((e) => {
186186
debug(e);
187187
});
@@ -195,20 +195,28 @@ function fireOverallFarmStatus() {
195195
});
196196
}
197197

198-
function fireTractors() {
199-
return fetch(`${devices}/devices/tractors`, {
198+
function updateTractorStatus() {
199+
return fetch(`${devices}/devices/tractor`, {
200200
method: 'PUT'
201201
}).catch((e) => {
202202
debug(e);
203203
});
204204
}
205205

206-
setInterval(fireAnimalCollars, 5000);
207-
setInterval(fireDevices, 3000);
206+
setInterval(() => {
207+
fireDevices('tractor');
208+
}, 3361);
209+
setInterval(fireAnimalCollars, 5099);
210+
setInterval(() => {
211+
fireDevices('temperature');
212+
}, 7001);
208213
setInterval(fireOverallFarmStatus, 10000);
214+
setInterval(() => {
215+
fireDevices('humidity');
216+
}, 8009);
209217

210218
if (autoMoveTractors > 0) {
211-
setInterval(fireTractors, autoMoveTractors);
219+
setInterval(updateTractorStatus, autoMoveTractors);
212220
}
213221

214222
// The temperature Gauge does not accept commands,

iot-devices/app.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ iotRouter.get('/status', (req, res) => {
4747
res.status(200).send();
4848
});
4949

50-
iotRouter.put('/devices', (req, res) => {
51-
IoTDevices.fireDevices();
50+
iotRouter.get('/devices/:type', (req, res) => {
51+
IoTDevices.fireDevices(req.params.type);
5252
res.status(204).send();
5353
});
5454

55-
iotRouter.put('/devices/tractors', (req, res) => {
56-
IoTDevices.fireTractorStatus();
55+
iotRouter.put('/devices/tractor', (req, res) => {
56+
IoTDevices.updateTractorStatus();
5757
res.status(204).send();
5858
});
5959

60-
iotRouter.put('/animals', (req, res) => {
60+
iotRouter.get('/animals', (req, res) => {
6161
IoTDevices.fireAnimalCollars();
6262
res.status(204).send();
6363
});

iot-devices/models/devices.js

Lines changed: 106 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function emitOverallFarmStatus() {
236236
}
237237
}
238238

239-
function fireTractorStatus() {
239+
function updateTractorStatus() {
240240
myCache.get('barn').then((state) => {
241241
if (state === 'door-open') {
242242
sendTractorReadings();
@@ -384,127 +384,129 @@ function sendAnimalCollarReadings() {
384384
});
385385
}
386386

387-
function fireDevices() {
387+
function fireDevices(deviceType) {
388388
myCache.get('barn').then((state) => {
389389
if (state === 'door-open') {
390-
sendDeviceReadings();
390+
const deviceIds = myCache.keys();
391+
_.forEach(deviceIds, (deviceId) => {
392+
if (deviceId.replace(/\d/g, '') === deviceType) {
393+
sendDeviceReading(deviceType, deviceId);
394+
}
395+
});
391396
}
392397
});
393398
}
394399

395400
// Update state of Sensors
396-
function sendDeviceReadings() {
397-
const deviceIds = myCache.keys();
401+
function sendDeviceReading(deviceType, deviceId) {
398402
const weather = myCache.get('weather');
399403

400-
_.forEach(deviceIds, (deviceId) => {
401-
getDeviceState(deviceId).then((state) => {
402-
const isSensor = true;
403-
let humid;
404-
let isDry;
405-
let targetTemp;
406-
let location;
404+
getDeviceState(deviceId).then((state) => {
405+
const isSensor = true;
406+
let humid;
407+
let isDry;
408+
let targetTemp;
409+
let location;
410+
411+
switch (deviceType) {
412+
case 'humidity':
413+
humid = parseInt(state.h);
414+
isDry = weather === 'sunny' ? getRandom() > 5 : getRandom() > 7;
415+
416+
// If the water is ON, randomly increase the soil humidity.
417+
if (
418+
weather === 'raining' ||
419+
getWaterState(deviceId, 'humidity') === 'ON'
420+
) {
421+
state.h = humid + (getRandom() % 3);
422+
} else if (isDry && humid > 50) {
423+
state.h = humid - (getRandom() % 3);
424+
} else if (isDry && humid > 30) {
425+
state.h = humid - 3 + (getRandom() % 4);
426+
} else if (humid <= 30) {
427+
state.h = humid + 3 - (getRandom() % 4);
428+
}
407429

408-
switch (deviceId.replace(/\d/g, '')) {
409-
case 'humidity':
410-
humid = parseInt(state.h);
411-
isDry = weather === 'sunny' ? getRandom() > 5 : getRandom() > 7;
412-
413-
// If the water is ON, randomly increase the soil humidity.
414-
if (
415-
weather === 'raining' ||
416-
getWaterState(deviceId, 'humidity') === 'ON'
417-
) {
418-
state.h = humid + (getRandom() % 3);
419-
} else if (isDry && humid > 50) {
420-
state.h = humid - (getRandom() % 3);
421-
} else if (isDry && humid > 30) {
422-
state.h = humid - 3 + (getRandom() % 4);
423-
} else if (humid <= 30) {
424-
state.h = humid + 3 - (getRandom() % 4);
430+
if (state.h > 100) {
431+
state.h = 100;
432+
}
433+
if (state.h < 0) {
434+
state.h = 0;
435+
}
436+
setDeviceState(deviceId, toUltraLight(state), isSensor);
437+
438+
break;
439+
case 'tractor':
440+
getDeviceState(
441+
'targetTractor' + deviceId.replace(/[a-zA-Z]/g, '')
442+
).then((target) => {
443+
location = state.gps.split(',');
444+
state.y = parseFloat(location[0]);
445+
state.x = parseFloat(location[1]);
446+
447+
if (state.d === 'SOWING') {
448+
if (getRandom() > 9) {
449+
state.y =
450+
Math.round((state.y + 0.001 * parseInt(target.x)) * 1000) /
451+
1000;
452+
state.x =
453+
Math.round((state.x + 0.001 * parseInt(target.y)) * 1000) /
454+
1000;
455+
}
425456
}
426457

427-
if (state.h > 100) {
428-
state.h = 100;
458+
if (state.d === 'MOVING') {
459+
state.x =
460+
Math.round((state.x + parseInt(target.x) / 300) * 1000) / 1000;
461+
state.y =
462+
Math.round((state.y + parseInt(target.y) / 300) * 1000) / 1000;
429463
}
430-
if (state.h < 0) {
431-
state.h = 0;
464+
465+
if (getRandom() > 9 && state.d === 'MOVING') {
466+
state.d = 'SOWING';
467+
} else if (getRandom() > 7 && state.d === 'SOWING') {
468+
target.x = -target.x;
469+
target.y = -target.y;
470+
setDeviceState(
471+
'targetTractor' + deviceId.replace(/[a-zA-Z]/g, ''),
472+
toUltraLight(target),
473+
false
474+
);
475+
state.y =
476+
Math.round(
477+
(state.y + Math.abs(parseInt(target.x) / 1000)) * 1000
478+
) / 1000;
479+
state.x =
480+
Math.round(
481+
(state.x + Math.abs(parseInt(target.y) / 1000)) * 1000
482+
) / 1000;
483+
state.d = 'MOVING';
432484
}
485+
486+
state.s = getStatusCode(state.d);
487+
state.gps = state.y + ',' + state.x;
488+
delete state.y;
489+
delete state.x;
433490
setDeviceState(deviceId, toUltraLight(state), isSensor);
491+
});
492+
break;
434493

435-
break;
436-
case 'tractor':
437-
getDeviceState(
438-
'targetTractor' + deviceId.replace(/[a-zA-Z]/g, '')
439-
).then((target) => {
440-
location = state.gps.split(',');
441-
state.y = parseFloat(location[0]);
442-
state.x = parseFloat(location[1]);
443-
444-
if (state.d === 'SOWING') {
445-
if (getRandom() > 9) {
446-
state.y =
447-
Math.round((state.y + 0.001 * parseInt(target.x)) * 1000) /
448-
1000;
449-
state.x =
450-
Math.round((state.x + 0.001 * parseInt(target.y)) * 1000) /
451-
1000;
494+
case 'temperature':
495+
getDeviceState('targetTemp' + deviceId.replace(/[a-zA-Z]/g, '')).then(
496+
(target) => {
497+
if (getRandom() > 7) {
498+
targetTemp = parseInt(target.t);
499+
if (state.t < targetTemp) {
500+
state.t++;
501+
} else if (state.t > targetTemp) {
502+
state.t--;
452503
}
453504
}
454-
455-
if (state.d === 'MOVING') {
456-
state.x =
457-
Math.round((state.x + parseInt(target.x) / 300) * 1000) / 1000;
458-
state.y =
459-
Math.round((state.y + parseInt(target.y) / 300) * 1000) / 1000;
460-
}
461-
462-
if (getRandom() > 9 && state.d === 'MOVING') {
463-
state.d = 'SOWING';
464-
} else if (getRandom() > 7 && state.d === 'SOWING') {
465-
target.x = -target.x;
466-
target.y = -target.y;
467-
setDeviceState(
468-
'targetTractor' + deviceId.replace(/[a-zA-Z]/g, ''),
469-
toUltraLight(target),
470-
false
471-
);
472-
state.y =
473-
Math.round(
474-
(state.y + Math.abs(parseInt(target.x) / 1000)) * 1000
475-
) / 1000;
476-
state.x =
477-
Math.round(
478-
(state.x + Math.abs(parseInt(target.y) / 1000)) * 1000
479-
) / 1000;
480-
state.d = 'MOVING';
481-
}
482-
483-
state.s = getStatusCode(state.d);
484-
state.gps = state.y + ',' + state.x;
485-
delete state.y;
486-
delete state.x;
487505
setDeviceState(deviceId, toUltraLight(state), isSensor);
488-
});
489-
break;
490-
491-
case 'temperature':
492-
getDeviceState('targetTemp' + deviceId.replace(/[a-zA-Z]/g, '')).then(
493-
(target) => {
494-
if (getRandom() > 7) {
495-
targetTemp = parseInt(target.t);
496-
if (state.t < targetTemp) {
497-
state.t++;
498-
} else if (state.t > targetTemp) {
499-
state.t--;
500-
}
501-
}
502-
setDeviceState(deviceId, toUltraLight(state), isSensor);
503-
}
504-
);
505-
break;
506-
}
507-
});
506+
}
507+
);
508+
break;
509+
}
508510
});
509511
}
510512

@@ -654,7 +656,7 @@ module.exports = {
654656
barnDoor,
655657
fireDevices,
656658
fireAnimalCollars,
657-
fireTractorStatus,
659+
updateTractorStatus,
658660
emitOverallFarmStatus,
659661
initDevices,
660662
};

0 commit comments

Comments
 (0)