I have a small bottle server that returns random values given a machineID
as shown below:
@app.route('/dataMachine')
@enable_cors
def simulatedMachineData():
prevVals = {'machineID_1': 0,'machineID_2': 0, 'machineID_3': 0,}
devVals = {'machineID_1': 5.3,'machineID_2': 2.1,'machineID_3': 7.1,}
dictVals = bottle.request.query.decode()
machineID = dictVals.get('machine', '')
if machineID not in devVals.keys(): return '-1'
prevVals[machineID] += normal(scale=devVals[machineID])
return str(prevVals[machineID])
This does not have a problem.
Now I have an AngularJS app that ties to interact with this data, as shown below:
var app = angular.module('testBidirection', []);
app.controller('tempCntrl', function ($scope, $http, $interval){
/////////////////////////////////////////////////
// Data to display and its update function
$scope.data = {
'machineID_1':'0',
'machineID_2':'0',
'machineID_3':'0',
};
var machineIDs = ['machineID_1', 'machineID_2', 'machineID_3'];
$scope.machineID = 'machineID_1';
$scope.getData = function(){
for (var i = 0; i < 3; i++) {
$scope.machineID = machineIDs[i];
$http.get('http://localhost:8080/dataMachine?machine=' + $scope.machineID).success(function(data) {
$scope.data[ $scope.machineID ] = data;
});
};
};
//////////////////////////////////////////////////
// Button controller. Use this controller to
// toggle the initiation of getting and
// stopping data retrieval ...
$scope.buttonText = 'start Comm';
$scope.toggleComm = function(){
if ($scope.buttonText == 'start Comm') {
$scope.buttonText = 'stop Comm';
stop = $interval($scope.getData, 2*1000);
} else {
$scope.buttonText = 'start Comm';
$interval.cancel(stop);
stop=undefined;
}
};
});
I believe that the problem is within this function:
$scope.getData = function(){
for (var i = 0; i < 3; i++) {
$scope.machineID = machineIDs[i];
$http.get('http://localhost:8080/dataMachine?machine=' + $scope.machineID).success(function(data) {
$scope.data[ $scope.machineID ] = data;
});
};
};
When I run this script, I see only that the last machine ID changes. An example output on the browser when the script is running is the following:
{"machineID_1":"0","machineID_2":"0","machineID_3":"2.37252542925"}
The value for "machineID_3"
keeps changing every 2 seconds like it should, while that for "machineID_1"
amd "machineID_2"
remains 0.
I can see from my Python output that the server is getting the correct get requests every 2 seconds, as in the example shown below.
127.0.0.1 - - [22/Apr/2016 16:33:32] "GET /dataMachine?machine=machineID_3 HTTP/1.1" 200 14
127.0.0.1 - - [22/Apr/2016 16:33:34] "GET /dataMachine?machine=machineID_1 HTTP/1.1" 200 14
127.0.0.1 - - [22/Apr/2016 16:33:34] "GET /dataMachine?machine=machineID_2 HTTP/1.1" 200 14
127.0.0.1 - - [22/Apr/2016 16:33:34] "GET /dataMachine?machine=machineID_3 HTTP/1.1" 200 14
127.0.0.1 - - [22/Apr/2016 16:33:36] "GET /dataMachine?machine=machineID_1 HTTP/1.1" 200 13
127.0.0.1 - - [22/Apr/2016 16:33:36] "GET /dataMachine?machine=machineID_2 HTTP/1.1" 200 14
127.0.0.1 - - [22/Apr/2016 16:33:36] "GET /dataMachine?machine=machineID_3 HTTP/1.1" 200 13
Am I missing something in the scoping rules?
Resources:
Entire contents of the Bottle Server:
import bottle
from bottle import response
from numpy.random import normal
import json
import pandas as pd
app = bottle.Bottle()
# the decorator
def enable_cors(fn):
def _enable_cors(*args, **kwargs):
# set CORS headers
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
if bottle.request.method != 'OPTIONS':
# actual request; reply with the actual response
return fn(*args, **kwargs)
return _enable_cors
@app.route('/dataMachine')
@enable_cors
def simulatedMachineData():
prevVals = {'machineID_1': 0,'machineID_2': 0,'machineID_3': 0,}
devVals = {'machineID_1': 5.3,'machineID_2': 2.1,'machineID_3': 7.1,}
dictVals = bottle.request.query.decode()
machineID = dictVals.get('machine', '')
if machineID not in devVals.keys(): return '-1'
prevVals[machineID] += normal(scale=devVals[machineID])
return str(prevVals[machineID])
Entire contents of the HTML file:
<!DOCTYPE html>
<html>
<head lang="en">
<title>Plotly Graph Plotter Directive for AngularJS - Demo</title>
<script src="jquery.min.js"></script>
<script src="angular.min.js"></script>
<script src="app3.js"></script>
</head>
<body ng-app="testBidirection">
Testing the return Values ...
<div ng-controller="tempCntrl">
<button ng-click='toggleComm()'>{{buttonText}}</button>
<hr>
{{data}}
<hr>
</div>
</body>
</html>