Skip to content

Commit 1fc675e

Browse files
committed
Expose fs and sampleRate for value subscription
1 parent 52b7c28 commit 1fc675e

3 files changed

Lines changed: 46 additions & 25 deletions

File tree

README.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Before all examples, you need:
2323

2424
.. code:: javascript
2525
26-
import studio from cdp_client
26+
import studio from cdp-client
2727
2828
Global API
2929
~~~~~~~~~~
@@ -350,12 +350,16 @@ node.child(name)
350350
// use the load object referring to CPULoad child in current node
351351
}
352352
353-
node.subscribeToValues(valueConsumer)
354-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
353+
node.subscribeToValues(valueConsumer, fs, sampleRate)
354+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
355355
356356
- Arguments
357357
358358
Function(value, timestamp) valueConsumer - timestamp in nanoseconds since EPOCH presented as long int
359+
360+
fs - maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
361+
362+
sampleRate - maximum amount of value updates sent per second (controls the amount of data transferred). Zero means all samples must be provided. Defaults to 0.
359363
360364
- Usage
361365
@@ -383,14 +387,18 @@ node.unsubscribeFromValues(valueConsumer)
383387
Unsubscribe given callback from value changes on this node.
384388
385389
386-
node.subscribeToChildValues(name, valueConsumer)
387-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
390+
node.subscribeToChildValues(name, valueConsumer, fs, sampleRate)
391+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388392
389393
- Arguments
390394
391395
name
392396
393397
Function(value, timestamp) valueConsumer - timestamp in nanoseconds since EPOCH presented as long int
398+
399+
fs - maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
400+
401+
sampleRate - maximum amount of value updates sent per second (controls the amount of data transferred). Zero means all samples must be provided. Defaults to 0.
394402
395403
- Usage
396404

index.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ studio.internal = (function(proto) {
329329
var parent = undefined;
330330
var id = nodeId;
331331
var app = appConnection;
332-
var childRequests = new Map();
333332
var structureFetched = false;
334333
var childMap = new Map();
335334
var givenPromises = new Map();
@@ -402,8 +401,7 @@ studio.internal = (function(proto) {
402401
parent = nodeParent;
403402
lastInfo = protoInfo;
404403
id = protoInfo.node_id;
405-
if (valueSubscriptions.length > 0)
406-
app.makeGetterRequest(id, 5, false);
404+
this.async._makeGetterRequest();
407405
};
408406

409407
this.add = function(node) {
@@ -446,7 +444,7 @@ studio.internal = (function(proto) {
446444
this.receiveValue = function (nodeValue, nodeTimestamp) {
447445
lastValue = nodeValue;
448446
for (var i = 0; i < valueSubscriptions.length; i++) {
449-
valueSubscriptions[i](nodeValue, nodeTimestamp);
447+
valueSubscriptions[i][0](nodeValue, nodeTimestamp);
450448
}
451449
};
452450

@@ -482,24 +480,19 @@ studio.internal = (function(proto) {
482480
app.makeStructureRequest(id);
483481
};
484482

485-
this.async.subscribeToValues = function(valueConsumer) {
486-
if (valueSubscriptions.length == 0) {
487-
app.makeGetterRequest(id, 5, false);
488-
}
489-
valueSubscriptions.push(valueConsumer);
483+
this.async.subscribeToValues = function(valueConsumer, fs, sampleRate) {
484+
valueSubscriptions.push([valueConsumer, fs, sampleRate]);
485+
this._makeGetterRequest();
490486
};
491487

492488
this.async.unsubscribeFromValues = function(valueConsumer) {
493489
for (var i = 0; i < valueSubscriptions.length; i++) {
494-
if (valueConsumer == valueSubscriptions[i]) {
490+
if (valueConsumer == valueSubscriptions[i][0]) {
495491
valueSubscriptions.splice(i, 1);
496492
break;
497493
}
498494
}
499-
500-
if (valueSubscriptions.length == 0) {
501-
app.makeGetterRequest(id, 0, true);
502-
}
495+
this._makeGetterRequest();
503496
};
504497

505498
this.async.addChild = function(name, modelName) {
@@ -515,6 +508,19 @@ studio.internal = (function(proto) {
515508
app.makeSetterRequest(id, lastInfo.value_type, value, timestamp);
516509
//when offline must queue or update pending set request and call set callbacks ...???
517510
};
511+
512+
this.async._makeGetterRequest = function() {
513+
if (valueSubscriptions.length > 0) {
514+
var maxFs = Math.max.apply(Math, valueSubscriptions.map(v => v[1]));
515+
var maxSampleRate = Math.max.apply(Math, valueSubscriptions.map(v => v[2]));
516+
//by studio api protocol 0 is the highest sample rate (all samples), so override maxSampleRate if 0 is found
517+
const zeroRate = valueSubscriptions.find(e => e[2] == 0);
518+
maxSampleRate = zeroRate ? zeroRate[2] : maxSampleRate;
519+
app.makeGetterRequest(id, maxFs, maxSampleRate, false);
520+
} else {
521+
app.makeGetterRequest(id, 1, 0, true);
522+
}
523+
}
518524
}
519525

520526
obj.AppConnection = function(url, notificationListener) {
@@ -633,11 +639,14 @@ studio.internal = (function(proto) {
633639
send(msg);
634640
};
635641

636-
this.makeGetterRequest = function(id, fs, stop) {
642+
this.makeGetterRequest = function(id, fs, sampleRate, stop) {
637643
var msg = new proto.Container();
638644
var request = new proto.ValueRequest();
639645
request.node_id = id;
640646
request.fs = fs;
647+
if (sampleRate) {
648+
request.sample_rate = sampleRate;
649+
}
641650
if (stop) {
642651
request.stop = stop;
643652
}
@@ -961,20 +970,24 @@ studio.api = (function(internal) {
961970
* Subscribe to value changes on this node.
962971
*
963972
* @param {valueConsumer} valueConsumer
973+
* @param {fs} Maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
974+
* @param {sampleRate} Maximum amount of value updates sent per second (controls the amount of data transferred). Zero means all samples must be provided. Defaults to 0.
964975
*/
965-
this.subscribeToValues = function(valueConsumer) {
966-
node.async.subscribeToValues(valueConsumer);
976+
this.subscribeToValues = function(valueConsumer, fs=5, sampleRate=0) {
977+
node.async.subscribeToValues(valueConsumer, fs, sampleRate);
967978
};
968979

969980
/**
970981
* Subscribe to node child value changes on this node.
971982
*
972983
* @param {string} name
973984
* @param {valueConsumer} valueConsumer
985+
* @param {fs} Maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
986+
* @param {sampleRate} Maximum amount of value updates sent per second (controls the amount of data transferred). Zero means all samples must be provided. Defaults to 0.
974987
*/
975-
this.subscribeToChildValues = function(name, valueConsumer) {
988+
this.subscribeToChildValues = function(name, valueConsumer, fs=5, sampleRate=0) {
976989
instance.child(name).then(function (child) {
977-
child.subscribeToValues(valueConsumer);
990+
child.subscribeToValues(valueConsumer, fs, sampleRate);
978991
}, function (){ console.log("subscribeToChildValues() Child not found "+ name) });
979992
};
980993

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cdp-client",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "A simple Javascript interface for the CDP Studio development platform that allows Javascript applications to interact with",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)