diff --git a/app/common/collections.js b/app/common/collections.js index c45a563..84c7465 100644 --- a/app/common/collections.js +++ b/app/common/collections.js @@ -1,3 +1,4 @@ SensorData = new Mongo.Collection("sensorData"); ControllerState = new Mongo.Collection("controller_states"); +Picture = new Mongo.Collection("pictures"); diff --git a/app/public/images/noImage.png b/app/public/images/noImage.png new file mode 100644 index 0000000..757a9bf Binary files /dev/null and b/app/public/images/noImage.png differ diff --git a/app/server/api.js b/app/server/api.js index 7f654d9..a13ea58 100644 --- a/app/server/api.js +++ b/app/server/api.js @@ -10,7 +10,7 @@ Api.addRoute('sensorData', { }, { post: function() { reactToSensorData(this.bodyParams); - SensorData.insert({ + var sensorObject = { temperatureValue: parseFloat(this.bodyParams.temperatureValue), humidityValue: parseFloat(this.bodyParams.humidityValue), tankLevel0: this.bodyParams.tankLevel0, @@ -24,7 +24,8 @@ Api.addRoute('sensorData', { owner: this.bodyParams.owner, controllerId: this.bodyParams.controllerId, created_at: new Date() - }); + }; + SensorData.insert(sensorObject); return []; } }); @@ -47,11 +48,11 @@ reactToSensorData = function(nextSensorReading) { var shouldStartAutomaticPumping = (!state.in_valve || state.in_valve === 'closed') && ((parseInt(nextSensorReading.tankFull) === 0) && startPumpingLevelReached && (state.out_valve === 'closed' || state.out_valve === 'closing')); - console.log("State: ", state); + console.log("State: ", state); var shouldStartPumping = shouldStartAutomaticPumping && config && !config.manualInflow; - console.log("shouldStartAutomaticPumping: ", shouldStartAutomaticPumping); + console.log("shouldStartAutomaticPumping: ", shouldStartAutomaticPumping); console.log("shouldStartPumping: ", shouldStartPumping); @@ -75,11 +76,11 @@ reactToSensorData = function(nextSensorReading) { else if (nextSensorReading.stopPumpingAt == 'TANKLEVEL4') stopPumpingLevelReached = (parseInt(nextSensorReading.tankLevel4) === 1) else stopPumpingLevelReached = false; - var shouldStopAutomaticPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || stopPumpingLevelReached || state.out_valve === 'open' || state.out_valve === 'opening'); + var shouldStopAutomaticPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || stopPumpingLevelReached || state.out_valve === 'open' || state.out_valve === 'opening'); - var shouldStopPumping = shouldStopAutomaticPumping && config && !config.manualInflow; + var shouldStopPumping = shouldStopAutomaticPumping && config && !config.manualInflow; - console.log("shouldStopPumping: ", shouldStopPumping); + console.log("shouldStopPumping: ", shouldStopPumping); if (shouldStopPumping) { @@ -116,10 +117,50 @@ Api.addRoute('state/:id', { } }); +Api.addRoute('picture/:id', { + authRequired: false +}, { + post: function() { + console.log("setting picture", this.bodyParams); + return Picture.upsert({ + controller_id: this.urlParams.id + }, { + '$set': { + 'picture_base64': this.bodyParams.picture_base64, + 'time': new Date() + } + }); + }, + get: function() { + var id = this.urlParams.id; + var res = this.response; + var picture_src = "/images/noImage.png"; + var pictureEntry = Picture.findOne({ + controller_id: id + }); + if (pictureEntry) { + var imageData = pictureEntry.picture_base64; + picture_src = "data:image/jpeg;charset=utf-8;base64," + imageData; + } + return { + picture: picture_src + }; + } +}); + +function base64_to_buffer(base64string) { + var buf; + if (typeof Buffer.from === "function") { + buf = Buffer.from(base64string, 'base64'); + } else { + buf = new Buffer(base64string, 'base64'); + } + return buf; +} function stateOrDefault(id) { var stateEntry = ControllerState.findOne({ - controller_id: id, + controller_id: id }); if (stateEntry === undefined) {