forked from vtwireless/HLSI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapacityRunner.js
254 lines (186 loc) · 6.25 KB
/
capacityRunner.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'use strict';
// Run a function at regular intervals. Adds a "Start" button and some dynamic
// labels.
//
// ARGUMENTS:
//
// sig: a signal object from Signal().
//
// func: a function that takes to the argument t (time in seconds),
// or an object that has key and function pairs
// The key will be a "select option" label and
// the corresponding function will be a function of time
// in seconds that will be call every tStep (0.01) seconds.
// TODO: add tStep user option.
//
// parentElement: an HTML CSS selector string, or <p> HTML element
// or none (null).
// This adds, HTML, a "Start" button and run numbers
// labels.
//
// opts: options object
//
// opts.tStep: set time step in seconds.
//
function CapacityRunner(sig, func = null, parentElement = null, opts = null) {
if(typeof(func) !== 'function' && typeof(func) !== 'object') {
let msg = "Code ERROR BAD func arguement to CapacityRunner()";
alert(msg);
console.log(msg);
throw(msg);
}
// We make this an object if we have a select options list of functions.
var funcs = false;
if(typeof(func) === 'object') {
funcs = func;
// Pick the first in the list as the current function to run.
func = func[Object.keys(func)[0]];
}
// Generate a unique id for HTML element ids.
var id = (CapacityRunner.count++).toString() + '_capRu';
var innerHTML = '\
\
<input id=start' + id + ' type=button\
value=Start />\
\
<label for=runTime' + id + '>Run Time: </label>\
<output id=runTime' + id + '></output>, \
\
<label for=timeLeft' + id + '>Time Left: </label>\
<output id=timeLeft' + id + '></output>, \
\
<label for=outage' + id + '> Outage: </label>\
<output id=outage' + id + ' style="color: #C00;"></output>, \
\
<label for=bits' + id + '>Total Bits: </label>\
<output id="bits' + id + '" style="color: #0C0;"></output>\
';
if(parentElement === null) {
parentElement = document.createElement('p');
document.body.appendChild(parentElement);
} else if(typeof(parentElement) === 'string') {
parentElement = document.querySelector(parentElement);
}
var tStep = 0.1; // Default tStep.
if(opts && typeof(opts.tStep) !== 'undefined')
// User option to set tStep.
tStep = opts.tStep;
const runTime = 60.0; // seconds
var outage = 0.0; // seconds with no throughput
var timeLeft = runTime; // seconds
var bits = 0; // number of bits in bits
var timerId = null;
var isRunning = false;
function getElement(sel) {
var el = document.querySelector(sel + id);
if(el === null)
alert("Can't get element with selector \""+ sel+ '"');
return el;
}
function SetLabels() {
getElement('#timeLeft').value =
d3.format(".2f")(timeLeft) + " s";
getElement('#outage').value =
d3.format(".2f")(outage) + " s";
getElement('#bits').value =
d3.format(",")(bits);
}
var prevT = 0.0; // seconds
var prevRate;
function getValues() {
if(!isRunning) {
prevT = 0.0;
return;
}
var t = (new Date()).getTime()/1000.0; // seconds
if(prevT === 0.0) {
// We just started so we do not have a time change.
prevRate = sig.rate;
prevT = t;
return;
}
let dt = t - prevT;
if(prevRate <= 0.0)
outage += dt;
else
bits += Math.round(prevRate * dt);
prevRate = sig.rate;
prevT = t;
SetLabels();
}
sig.onChange('rate', getValues);
function RunStep() {
if(!isRunning) return;
// Advance the time.
timeLeft -= tStep;
if(timeLeft <= 0.0)
timeLeft = 0.0;
const t = runTime - timeLeft;
// Call user function:
func(t);
// Get the last value changes.
getValues();
if(timeLeft === 0.0)
Stop();
}
function Reset() {
outage = 0.0; // seconds with no throughput
timeLeft = runTime; // seconds
bits = 0; // number of bits in bits
timerId = null;
SetLabels();
}
function Start() {
if(typeof(func) !== 'function') {
Stop();
return;
}
if(isRunning) return;
Reset();
timerId = setInterval(RunStep, tStep * 1000 /*1000 ms/s*/);
isRunning = true;
getElement('#start').value = "Stop ";
}
function Stop() {
if(!isRunning) return;
// Get the last value changes.
getValues();
// Stop does not also reset, it just stops.
clearTimeout(timerId);
isRunning = false;
getElement('#start').value = "Start";
}
function StartStop() {
if(isRunning) Stop();
else Start();
}
parentElement.innerHTML = innerHTML;
getElement('#runTime').value = d3.format('.2f')(runTime) + " s";
getElement('#timeLeft').value = d3.format('.2f')(timeLeft) + " s";
var startButton = getElement('#start');
startButton.addEventListener('click', StartStop);
if(funcs) {
// We setup a <select> of functions to choice from:
//
let space = document.createTextNode(" ");
startButton.parentNode.insertBefore(space, startButton.nextSibling);
let select = document.createElement('select');
space.parentNode.insertBefore(select, space.nextSibling);
// We have a bunch of functions to pick from, so we make a
// <select> to hold all the optional functions.
Object.keys(funcs).forEach(function(fname) {
let opt = document.createElement('option');
opt.Func = funcs[fname];
opt.innerHTML = fname;
select.appendChild(opt);
});
select.onchange = function() {
// We select the options function:
func = select.options[this.selectedIndex].Func;
// Stop if it's not stopped. ???
Stop();
};
}
Reset();
}
CapacityRunner.count = 0;