@@ -37,6 +37,9 @@ studio.protocol = (function(ProtoBuf) {
3737 obj . Node = studioBuilder . build ( "Node" ) ;
3838 obj . VariantValue = studioBuilder . build ( "VariantValue" ) ;
3939 obj . ValueRequest = studioBuilder . build ( "ValueRequest" ) ;
40+ obj . EventRequest = studioBuilder . build ( "EventRequest" ) ;
41+ obj . EventCodeFlags = studioBuilder . build ( "EventInfo.CodeFlags" ) ;
42+ obj . EventStatusFlags = studioBuilder . build ( "EventInfo.StatusFlags" ) ;
4043 obj . ChildAdd = studioBuilder . build ( "ChildAdd" ) ;
4144 obj . ChildRemove = studioBuilder . build ( "ChildRemove" ) ;
4245
@@ -335,6 +338,7 @@ studio.internal = (function(proto) {
335338 var childIterators = new Array ( ) ;
336339 var valueSubscriptions = new Array ( ) ;
337340 var structureSubscriptions = new Array ( ) ;
341+ var eventSubscriptions = new Array ( ) ;
338342 var lastValue ;
339343 var lastInfo = null ; //when we get this, if there are any child requests we need to fetch child fetch too
340344 var valid = true ;
@@ -402,6 +406,8 @@ studio.internal = (function(proto) {
402406 lastInfo = protoInfo ;
403407 id = protoInfo . node_id ;
404408 this . async . _makeGetterRequest ( ) ;
409+ for ( var i = 0 ; i < eventSubscriptions . length ; i ++ )
410+ app . makeEventRequest ( id , eventSubscriptions [ i ] [ 1 ] , false ) ;
405411 } ;
406412
407413 this . add = function ( node ) {
@@ -448,6 +454,12 @@ studio.internal = (function(proto) {
448454 }
449455 } ;
450456
457+ this . receiveEvent = function ( event ) {
458+ for ( var i = 0 ; i < eventSubscriptions . length ; i ++ ) {
459+ eventSubscriptions [ i ] [ 0 ] ( event ) ;
460+ }
461+ } ;
462+
451463 this . async = { } ;
452464
453465 this . async . onDone = function ( resolve , reject , apiNode ) {
@@ -495,6 +507,22 @@ studio.internal = (function(proto) {
495507 this . _makeGetterRequest ( ) ;
496508 } ;
497509
510+ this . async . subscribeToEvents = function ( eventConsumer , startingFrom ) {
511+ eventSubscriptions . push ( [ eventConsumer , startingFrom ] ) ;
512+ app . makeEventRequest ( id , startingFrom , false ) ;
513+ } ;
514+
515+ this . async . unsubscribeFromEvents = function ( eventConsumer ) {
516+ for ( var i = 0 ; i < eventSubscriptions . length ; i ++ ) {
517+ if ( eventConsumer == eventSubscriptions [ i ] [ 0 ] ) {
518+ eventSubscriptions . splice ( i , 1 ) ;
519+ break ;
520+ }
521+ }
522+ if ( eventSubscriptions . length === 0 )
523+ app . makeEventRequest ( id , 0 , true ) ;
524+ } ;
525+
498526 this . async . addChild = function ( name , modelName ) {
499527 app . makeChildAddRequest ( id , name , modelName ) ;
500528 } ;
@@ -655,6 +683,21 @@ studio.internal = (function(proto) {
655683 send ( msg ) ;
656684 } ;
657685
686+ this . makeEventRequest = function ( id , startingFrom , stop ) {
687+ var msg = new proto . Container ( ) ;
688+ var request = new proto . EventRequest ( ) ;
689+ request . node_id = id ;
690+ if ( stop ) {
691+ request . stop = stop ;
692+ }
693+ if ( startingFrom != undefined ) {
694+ request . starting_from = startingFrom ;
695+ }
696+ msg . message_type = proto . ContainerType . eEventRequest ;
697+ msg . event_request = [ request ] ;
698+ send ( msg ) ;
699+ } ;
700+
658701 this . makeChildAddRequest = function ( id , name , modelName ) {
659702 var msg = new proto . Container ( ) ;
660703 var request = new proto . ChildAdd ( ) ;
@@ -783,6 +826,26 @@ studio.internal = (function(proto) {
783826 }
784827 }
785828
829+ function parseEventResponse ( protoResponse ) {
830+ for ( var i = 0 ; i < protoResponse . length ; i ++ ) {
831+ var variantValue = protoResponse [ i ] ;
832+ for ( var j = 0 ; j < variantValue . node_id . length ; j ++ ) {
833+ var node = nodeMap . get ( variantValue . node_id [ j ] ) ;
834+ if ( node ) {
835+ var event = {
836+ id : variantValue . id ,
837+ sender : variantValue . sender ,
838+ code : variantValue . code ,
839+ status : variantValue . status ,
840+ timestamp : variantValue . timestamp ,
841+ data : variantValue . data
842+ } ;
843+ node . receiveEvent ( event ) ;
844+ }
845+ }
846+ }
847+ }
848+
786849 function reauthenticate ( userAuthResult , metadata ) {
787850 var request = new studio . api . Request ( metadata . systemName , metadata . applicationName , metadata . cdpVersion , metadata . systemUseNotification , userAuthResult ) ;
788851 notificationListener . credentialsRequested ( request )
@@ -824,6 +887,9 @@ studio.internal = (function(proto) {
824887 case proto . ContainerType . eStructureChangeResponse :
825888 parseStructureChangeResponse ( protoContainer . structure_change_response ) ;
826889 break ;
890+ case proto . ContainerType . eEventResponse :
891+ parseEventResponse ( protoContainer . event_response ) ;
892+ break ;
827893 case proto . ContainerType . eCurrentTimeResponse :
828894 break ;
829895 case proto . ContainerType . eReauthResponse :
@@ -1038,6 +1104,37 @@ studio.api = (function(internal) {
10381104 node . async . unsubscribeFromStructure ( structureConsumer ) ;
10391105 } ;
10401106
1107+
1108+ /**
1109+ * Subscribe to value changes on this node.
1110+ *
1111+ * @param {valueConsumer } valueConsumer
1112+ * @param {fs } Maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
1113+ * @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.
1114+ */
1115+ this . subscribeToValues = function ( valueConsumer , fs = 5 , sampleRate = 0 ) {
1116+ node . async . subscribeToValues ( valueConsumer , fs , sampleRate ) ;
1117+ } ;
1118+
1119+ /**
1120+ * Subscribe to events on this node.
1121+ *
1122+ * @param {eventConsumer } eventConsumer
1123+ * @param {startingFrom } If > 0, past events starting from this timestamp (in UTC nanotime) are re-forwarded.
1124+ */
1125+ this . subscribeToEvents = function ( eventConsumer , startingFrom ) {
1126+ node . async . subscribeToEvents ( eventConsumer , startingFrom ) ;
1127+ } ;
1128+
1129+ /**
1130+ * Unsubscribe given callback from events on this node.
1131+ *
1132+ * @param {eventConsumer } eventConsumer
1133+ */
1134+ this . unsubscribeFromEvents = function ( eventConsumer ) {
1135+ node . async . unsubscribeFromEvents ( eventConsumer ) ;
1136+ } ;
1137+
10411138 /**
10421139 * Add child Node to this Node.
10431140 *
0 commit comments