-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatagen.js
107 lines (95 loc) · 3.22 KB
/
datagen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var sampleObj = genSampleData();
var lastDay = -1;
//console.log(JSON.stringify(sampleObj));
var count = 0;
for (var key in sampleObj) {
if (!sampleObj.hasOwnProperty(key)) continue;
var obj = sampleObj[key];
//console.log(JSON.stringify(obj));
count++;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(obj.epoch);
//console.log(d.getDay());
if(d.getDay()!==lastDay){
lastDay = d.getDay();
console.log("New Day : "+d.toString()+" "+ getWeekNumber(d));
}
//console.log(count);
//dataArray.push([d, obj.hr, obj.hrDev])
}
function getWeekNumber(d) {
// Copy date so don't modify original
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
// Get first day of year
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
// Return array of year and week number
return [d.getUTCFullYear(), weekNo];
}
// var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
// d.setUTCSeconds(epoch);
function genSampleData() {
var startUtc = Math.floor((new Date()).getTime() / 1000);
var lastUtc;
var historyObject = {};
var timeIncriment = 300;
var samplesPerDay = 288; // at 300 seconds interval
var maxSamples = 4012;
var maxStepsPerInterval = 60;
var currentSteps = 0;
var maxHR = 180;
var maxHRDev = 30;
var startingBattVolts = 4.20;
var currentBattVolts;
var voltsPerSample = 0.00625;
lastUtc = startUtc;
currentBattVolts = startingBattVolts;
for (var i = 0; i < maxSamples; i++) {
currentSteps = getSteps(currentSteps,maxStepsPerInterval);
currentBattVolts = getBatteryVoltage(currentBattVolts,voltsPerSample);
if(currentBattVolts<3.3){
currentBattVolts = startingBattVolts;
}
var newSample = {
"epoch": lastUtc, // unix timestamp
"steps": currentSteps,
"hr": getHR(maxHR),
"hrDev": getHRDev(maxHRDev),
"batt": currentBattVolts,
"aux1": getAux(),
"aux2": getAux(),
"aux3": getAux()
}
historyObject[lastUtc] = newSample;
lastUtc += timeIncriment;
}
//console.log(JSON.stringify(historyObject));
return historyObject;
}
function getSteps(steps,maxStepsPerInterval) {
var newSteps = getRandomInt(0, maxStepsPerInterval);
return steps + newSteps;
}
function getHR(maxHR) {
return getRandomInt(0, maxHR);
}
function getHRDev(maxHRDev) {
return getRandomInt(0, maxHRDev);
}
function getBatteryVoltage(volts,voltsPerSample) {
var battDecrease = getRandomArbitrary(0, voltsPerSample);
return (volts - battDecrease).toFixed(3);
}
function getAux() {
return getRandomInt(0, 255);
}
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}