Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LEAN Raspberry Remote server-side snippets
var device = args.device.value;
var newDevice = devices.insert(device);
newDevice.id = newDevice.$loki;
newDevice.state = false;
devices.ensureUniqueIndex('id');
db.saveDatabase();
for (var i = 0; i < devices.data.length; i++) {
if (devices.data[i]["id"] == args.id.value) {
devices.remove(devices.data[i]);
}
}
if(devices.data.length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(devices.data));
}
else {
res.end();
}
var loki = require('lokijs');
var db = new loki('devices.json');
var devices = db.addCollection('devices', { uniqueIndices: ['id'] });
var powerOutlet = (function () {
var _setState = function (ipAddress, remotePort, stateVal) {
var success = false;
var socket = net.connect(remotePort, ipAddress, function () {
socket.write(stateVal);
success = true;
});
socket.on('error', function (err) {
console.log("Error: " + err);
});
socket.end();
return success;
};
return {
on: function (ipAddress, remotePort) {
return _setState(ipAddress, remotePort, '10001021');
},
off: function (ipAddress, remotePort) {
return _setState(ipAddress, remotePort, '10001020');
}
};
})();
var success = false;
for (var i = 0; i < devices.data.length; i++) {
var currentDevice = devices.data[i];
if (currentDevice.id == args.id.value) {
var state = args.state.value;
var oldState = currentDevice.state;
var ipAddress = currentDevice.ipAddress;
var remotePort = currentDevice.remotePort;
if (state) {
success = powerOutlet.on(ipAddress, remotePort);
}
else {
success = powerOutlet.off(ipAddress, remotePort);
}
if (success) {
currentDevice.state = state;
currentDevice.error = false;
currentDevice.success = true;
}
else {
currentDevice.state = oldState;
currentDevice.error = true;
currentDevice.success = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment