Can anyone tell me how the websocket message stream distinguishes between a line initializing a connection and a line returning to “normal” status after being used in a call?
I have a separate thread going for general use of node-red but this is a specific question - that likely results from me not knowing enough about the websocket protocols - that I need to resolve a specific use case for using the Wazo APIs.
I am trying to use node-red to show current and historical call counts. The same question could apply if I was using a python program to do the work.
I have it all setup and working with one exception, and that is the above question.
When a line is put into use for a call, the websocket connection has a msg.payload (json string) , showing current_call_count=1:
{"technology":"sip","name":"line-name","registered":true,"current_call_count":1,"id":13}
When the call ends, another msg.payload (json string) is sent, showing current_call_count=0
{"technology":"sip","name":"line-name","registered":true,"current_call_count":0,"id":13}
So by adding 1 for each msg.payload with current_call_count=1 and subtracting 1 for each msg.payload with current_call_count=0 , this lets me capture and display, using node-red graphs (or in python) the current and historical call count.
When a line loses its registration, it sends a similar msg.payload
{"technology":"sip","name":"line-name","registered":false,"current_call_count":0,"id":13}
but I can parse this and not confuse the actual current_call_count because the “registered” entry is “false”
However …
When either 1) a line that previously lost its registration reconnects or 2) a new line is connected, the msg.payload is
{"technology":"sip","name":"line-name","registered":true,"current_call_count":0,"id":13}
which is indistinguishable from a line that is returning to normal after a call completes
so if I use this to increment/decrement my current_call_count, it produces false results.
I have tried to find a different msg.payload that tells me when a line becomes registered, so I can distinguish it from a line that has returned to normal status after a call using that line has ended, but so far, no luck.
Can anyone tell me how to distinguish between a line initializing a connection and a line returning to “normal” status after being used in a call?
If you are interested, the javascript used (in node-red) to accumulate current_call_count is:
// Initialize counter to 0 when node is first started
var totalCallCount = context.get( "totalCallCount" );
if ( typeof( totalCallCount ) == "undefined" ) {
totalCallCount = 0;
}
// Count the number of active lines
// Start this Flow AFTER initializing Wazo
// so that the line and device registrations do not get captured
//
// Current solution (below) is corrupted
// when a line is connected after the flow is running
// since a line connection
// sends (as a separate msg.payload) current_call_count = 0
// See below for temporary measure to deal with this
if ( msg.payload.hasOwnProperty( "current_call_count" ) ) {
if ( msg.payload.current_call_count == 1 ) {
totalCallCount += 1;
} else if ( msg.payload.registered == false ) {
// This handles the line being disconnected
// and prevents use of the current_call_count = 0
// sent as part of the disconnect process
totalCallCount = totalCallCount;
} else if ( msg.payload.current_call_count == 0 ) {
totalCallCount -= 1;
}
// Since I have not yet figured out how to handle new line connections
// I check if the totalCallCount has become corrupted (gone below 0)
// and reset it to 0
// Hopefully soon I figure out how to really deal with this situation
if ( totalCallCount < 0 ) {
totalCallCount = 0;
}
context.set( "totalCallCount" , totalCallCount )
msg.payload = totalCallCount;
return msg;
}