Skip to content

Commit

Permalink
remove duplicate temperatures #131
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarobock committed Apr 9, 2014
1 parent c64cec3 commit 1d10bbb
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 16 deletions.
2 changes: 1 addition & 1 deletion etc/POST_CEBADA.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"coldStatus": true,
"heatStatus": false,
"source": "1",
"temperature": 14.0,
"temperature": 19.0,
"temperatureMax": 20,
"temperatureMin": 19,
"temperatureExt": 14.1
Expand Down
1 change: 1 addition & 0 deletions etc/bin/post_json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST -d @"etc/POST_CEBADA.json" http://localhost:3000/TempDeviceReport -H "Accept: Application/json" -H "Content-Type: application/json"
1 change: 1 addition & 0 deletions etc/bin/post_json_prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST -d @"etc/POST_CEBADA.json" http://brew-o-matic.eu01.aws.af.cm/TempDeviceReport -H "Accept: Application/json" -H "Content-Type: application/json"
11 changes: 8 additions & 3 deletions public/js/RecipeCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,14 @@

module.controller("RecipeTemperatureCtrl", function($scope, TempDeviceReport, pushListener) {

$scope.temperatures = TempDeviceReport.query({recipe_id: $scope.recipe._id}, function() {
$scope.updateChart();
});
$scope.reload = function() {
$scope.temperatures = TempDeviceReport.query({recipe_id: $scope.recipe._id}, function() {
$scope.updateChart();
});
};

$scope.reload();

function onNewTemperature(temp) {
$scope.temperatures.push(temp);
$scope.updateChart();
Expand All @@ -480,6 +484,7 @@
pushListener.off("TEMP_DEVICE_REPORT_" + $scope.recipe._id, onNewTemperature);
});


$scope.updateChart = function() {
var cols = [{
"id": "day",
Expand Down
12 changes: 10 additions & 2 deletions public/partial/recipe-temperature.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
<div class="col-md-10">
<div class="panel panel-default">
<div class="panel-body" >
<div class="row">
<div class="col-md-12">
<button ng-click="reload()" type="button" class="btn btn-default btn-xs pull-right">
<span class="glyphicon glyphicon-refresh"></span>
</button>
</div>
</div>
<br/>
<table class="table table-bordered table-condensed table-hover">
<thead>
<tr>
Expand All @@ -20,8 +28,8 @@
</tr>
</thead>
<tbody>
<tr ng-repeat="temperature in temperatures | orderBy:'timestamp'">
<td>{{temperature.timestamp|ts2date|date:'dd-MM-yyyy HH:mm'}}</td>
<tr class="animate-repeat" ng-repeat="temperature in temperatures | orderBy:'timestamp'">
<td>{{temperature.timestamp|ts2date|date:'dd-MM-yyyy HH:mm:ss'}}</td>
<td>{{temperature.source}}</td>
<td><b>{{temperature.temperature}}</b></td>
<td>{{temperature.temperatureExt}}</td>
Expand Down
1 change: 1 addition & 0 deletions public/template/abm-combo-object.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<select class="form-control input-sm"
ng-model="value[header.field]"
ng-options="value._id as value.NAME for value in header.data">
<option></option>
</select>
59 changes: 49 additions & 10 deletions routes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ exports.TempDeviceReport.findAll = function(req, res) {

exports.TempDeviceReport.save = function(req, res) {
var temp = req.body;
console.log("ACA!!!!",temp);
// console.log("ACA!!!!",temp);
delete temp._id;

if ( !temp.timestamp ) {
Expand All @@ -109,18 +109,57 @@ exports.TempDeviceReport.save = function(req, res) {
//Busco el dispositivo correspondiente y obtengo el ID de la receta que corresponde.
model.TempDevice.find({code:temp.code}).exec(function(err, device) {

if ( !err && device && device.length > 0 ) {
//Solo guardo si existe el dispositivo y si el mismo esta vinculado a una receta
if ( !err && device && device.length > 0 && device[0].recipe_id) {
temp.recipe_id = device[0].recipe_id;

model.TempDeviceReport.findByIdAndUpdate(id,temp,{upsert:true}).exec(function(err,results) {
// console.log('err', err);
// console.log('results', results);
if ( temp.recipe_id ) {
push.emit("TEMP_DEVICE_REPORT_" + temp.recipe_id,results);
}
res.send(results);

//Elimino posibles duplicados de valores
model.TempDeviceReport.find({
recipe_id: device[0].recipe_id
}).sort("timestamp").exec(function (err, duplicated) {
if ( duplicated.length > 2 ) {

var base = duplicated[duplicated.length-1];
var toRemove = null;

var finish = false;
var i = duplicated.length-2;
while ( !finish && i>=0 ) {
var actual = duplicated[i--];
if (
actual.source == base.source &&
actual.temperature == base.temperature &&
actual.temperatureExt == base.temperatureExt &&
actual.temperatureMax == base.temperatureMax &&
actual.temperatureMin == base.temperatureMin &&
actual.coldStatus == base.coldStatus &&
actual.heatStatus == base.heatStatus ) {
// actual.remove();
if ( toRemove ) toRemove.remove();
toRemove = actual;
console.log("ELIMIMAR");
} else {
console.log("FINiSH",actual,base);
finish = true;
}
}

}
});
});

} else {
res.send(500);
}

model.TempDeviceReport.findByIdAndUpdate(id,temp,{upsert:true}).exec(function(err,results) {
// console.log('err', err);
// console.log('results', results);
if ( temp.recipe_id ) {
push.emit("TEMP_DEVICE_REPORT_" + temp.recipe_id,results);
}
res.send(results);
});

});

Expand Down

0 comments on commit 1d10bbb

Please sign in to comment.