App does something :)
This commit is contained in:
@@ -31,12 +31,11 @@ class DataProcessing {
|
||||
|
||||
onStateChange = () => {
|
||||
const newState = this.store.getState();
|
||||
//console.log("DataProcessing state change", this.state, newState);
|
||||
if (newState.settings.deviceURL !== this.state.settings.deviceURL)
|
||||
this.onDataSourceChanged(newState.settings.deviceURL);
|
||||
|
||||
if (newState.session.running && !this.state.session.running) {
|
||||
this.onRunningChanged(newState.session.running);
|
||||
if (newState.session.running !== this.state.session.running) {
|
||||
this.onRunningChanged(newState.session.running, newState.settings.deviceURL);
|
||||
};
|
||||
if(newState.settings.peakDetectorSimpleThreshold !== this.state.settings.peakDetectorSimpleThreshold) {
|
||||
this.peakDetectorSimple = new PeakDetectorSimple(newState.settings.peakDetectorSimpleThreshold, this.onNewPeak);
|
||||
@@ -50,19 +49,21 @@ class DataProcessing {
|
||||
this.dataSource.stop();
|
||||
this.dataSource = null;
|
||||
}
|
||||
this.dataSource = new DeviceHttpDataSource(this.newDeviceURL + "/api/session/data", this.onNewData);
|
||||
this.dataSource = new DeviceHttpDataSource(newDeviceURL + "/api/session/data", this.onNewData);
|
||||
}
|
||||
|
||||
onRunningChanged = (running, deviceURL) => {
|
||||
let req = new XMLHttpRequest();
|
||||
if (running) {
|
||||
//console.log("Starting session");
|
||||
let req = new XMLHttpRequest();
|
||||
console.log("Starting session", deviceURL + "/api/session/start" );
|
||||
req.open("GET", deviceURL + "/api/session/start");
|
||||
req.send();
|
||||
this.dataSource.startIndex = 0;
|
||||
this.dataSource.start();
|
||||
} else {
|
||||
//console.log("Stopping session");
|
||||
console.log("Stopping session");
|
||||
req.open("GET", deviceURL + "/api/session/stop");
|
||||
req.send();
|
||||
this.dataSource.stop();
|
||||
this.dataSource.startIndex = 0;
|
||||
}
|
||||
@@ -73,9 +74,10 @@ class DataProcessing {
|
||||
data.sessionStartTime;
|
||||
data.startIndex;
|
||||
let success = false;
|
||||
if (data.sessionStartTime === this.sessionStartTime && data.startIndex === this.rawMeasurements.length) {
|
||||
if (data.sessionStartTime == this.sessionStartTime && data.startIndex == this.rawMeasurements.size) {
|
||||
// normal case, add received data to measurement array
|
||||
this.rawMeasurements.concat(List(data.values));
|
||||
console.log("success: normal case");
|
||||
this.rawMeasurements = this.rawMeasurements.concat(List(data.values));
|
||||
this.analyzeNewMeasurements(data.startIndex);
|
||||
success = true;
|
||||
}
|
||||
@@ -84,7 +86,12 @@ class DataProcessing {
|
||||
this.sessionStartTime = data.sessionStartTime;
|
||||
this.rawMeasurements = List(data.values);
|
||||
success = true;
|
||||
console.log("New start", this.sessionStartTime, this.rawMeasurements.toArray());
|
||||
} else {
|
||||
console.log("Requery :(");
|
||||
console.log("this.sessionStartTime", this.sessionStartTime);
|
||||
console.log("this.rawMeasurements", this.rawMeasurements.toArray());
|
||||
console.log("data", data);
|
||||
// missed some data -> re-query
|
||||
this.dataSource.startIndex = 0;
|
||||
this.sessionStartTime = 0;
|
||||
@@ -94,21 +101,24 @@ class DataProcessing {
|
||||
|
||||
if (success) {
|
||||
const analysis = this.analyzeNewMeasurements(data.startIndex);
|
||||
this.store.dispatch(reportDeviceData(this.sessionStartTime, data.startIndex, this.rawMeasurements, analysis));
|
||||
const report = reportDeviceData(this.sessionStartTime, data.startIndex, this.rawMeasurements, analysis);
|
||||
console.log("reporting device data", report);
|
||||
this.store.dispatch(report);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
analyzeNewMeasurements = (newDataStartIdx) => {
|
||||
const newPeaks = this.peakDetectorSimple.addVector(this.rawMeasurements.slice(newDataStartIdx));
|
||||
//TODO is ".toArray()" really necessary here?
|
||||
const newPeaks = this.peakDetectorSimple.addVector(this.rawMeasurements.slice(newDataStartIdx).toArray());
|
||||
this.peaks = this.peaks.concat(List(newPeaks));
|
||||
console.log("new peaks", newPeaks, "total peaks", this.peaks.toArray());
|
||||
const totalMomentum = this.rawMeasurements.reduce((sum, x) => sum + x, 0);
|
||||
const peakMax = this.rawMeasurements.reduce((running, x) => max(x, running), 0);
|
||||
const peakMax = this.rawMeasurements.reduce((running, x) => Math.max(x, running), 0);
|
||||
|
||||
// windowed quantities
|
||||
const windowSizeMeasurements = WINDOW_SIZE_SECS * NUM_MEASUREMENTS_PER_SECOND;
|
||||
const windowedSeq = this.rawMeasurements.slice(-windowSizeMeasurements);
|
||||
const peakMaxWindow = windowedSeq.reduce((running, x) => max(x, running), 0);
|
||||
const peakMaxWindow = windowedSeq.reduce((running, x) => Math.max(x, running), 0);
|
||||
const momentumWindow = windowedSeq.reduce((sum, x) => sum + x, 0);
|
||||
|
||||
return {
|
||||
|
||||
@@ -2,11 +2,12 @@ import * as msgpack from 'msgpack-lite';
|
||||
|
||||
class DeviceHttpDataSource {
|
||||
|
||||
constructor(dataUrl, onNewData, pollInterval=1000, startIndex = 0) {
|
||||
constructor(dataUrl, onNewData, pollInterval=2000, startIndex = 0) {
|
||||
this.dataUrl = dataUrl;
|
||||
this.onNewData = onNewData;
|
||||
this.pollInterval = pollInterval;
|
||||
this.startIndex = startIndex;
|
||||
this.timer = null;
|
||||
|
||||
// msgpack setup
|
||||
this.msgpackCodec = msgpack.createCodec();
|
||||
@@ -47,12 +48,13 @@ class DeviceHttpDataSource {
|
||||
//"values", "sessionStartTime", "startIndex"
|
||||
this.onNewData(decoded);
|
||||
} catch (err) {
|
||||
//console.log(err);
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.timer === null) {
|
||||
console.log("Start monitoring");
|
||||
this.timer = setInterval(this.fetchDataHttp, this.pollInterval);
|
||||
return true;
|
||||
} else {
|
||||
@@ -62,6 +64,7 @@ class DeviceHttpDataSource {
|
||||
|
||||
stop() {
|
||||
if (this.timer !== null) {
|
||||
console.log("stop monitoring");
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
return true;
|
||||
|
||||
@@ -8,12 +8,11 @@
|
||||
* value is larger than (threshold + minimum_since_last_peak)
|
||||
*/
|
||||
class PeakDetectorSimple {
|
||||
constructor(threshold, handleNewPeaks) {
|
||||
constructor(threshold) {
|
||||
this._threshold = threshold;
|
||||
this._queue = [];
|
||||
this._last_min = 0;
|
||||
this._counter = 0;
|
||||
this._handleNewPeaks = handleNewPeaks;
|
||||
}
|
||||
|
||||
getThreshold() {
|
||||
@@ -22,14 +21,11 @@ class PeakDetectorSimple {
|
||||
|
||||
addVector(vec) {
|
||||
let result = [];
|
||||
const callbackBackup = this._handleNewPeaks;
|
||||
this._handleNewPeaks = null;
|
||||
for (let i = 0; i < vec.length; ++i) {
|
||||
const res = this.add(vec[i]);
|
||||
if(res !== null)
|
||||
result.push(res);
|
||||
}
|
||||
this._handleNewPeaks = callbackBackup;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -40,7 +36,7 @@ class PeakDetectorSimple {
|
||||
this._queue.shift();
|
||||
}
|
||||
if (this._queue.length !== 3) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
const [last, current, next] = this._queue;
|
||||
const is_maximum = current > next && current > last;
|
||||
|
||||
Reference in New Issue
Block a user