const expireTime = 60000
class Carga {
constructor() {
this.currentHistory = [];
this.lastReceived = new Date().getTime();
this.chargeMean = 0;
// this.addCurrents = this.addCurrents.bind(this);
// this.performCleanup = this.performCleanup.bind(this);
// this.restart = this.restart.bind(this);
// this.getCharge = this.getCharge.bind(this);
}
addCurrents(currents) {
let sum = (this.normalizeValue(parseInt(currents[0])) + this.normalizeValue(parseInt(currents[2])) + this.normalizeValue(parseInt(currents[3])))/10;
let timestamp = new Date().getTime();
if (timestamp - this.lastReceived >= expireTime) {
this.restart();
}
this.performCleanup(timestamp);
this.currentHistory.push({current: sum, timestamp: timestamp});
this.lastReceived = timestamp;
let i;
let charge = 0;
for (i = 0; i < this.currentHistory.length-1; i++) {
charge += (this.currentHistory[i+1].current+this.currentHistory[i].current)/2*((this.currentHistory[i+1].timestamp-this.currentHistory[i].timestamp)/1000);
}
this.chargeMean = charge;
}
performCleanup(now) {
let firstIndex = 0;
for (firstIndex = 0; firstIndex<this.currentHistory.length; firstIndex++) {
if (now - this.currentHistory[firstIndex].timestamp < expireTime)
break;
}
if (firstIndex > 0)
this.currentHistory = this.currentHistory.slice(firstIndex);
}
restart() {
this.currentHistory = []
this.chargeMean = 0;
}
getChargeMean() {
return this.chargeMean;
}
normalizeValue(value) {
return (value > Math.pow(2, 15) ? (value-Math.pow(2, 16)): value)
}
}
module.exports = Carga;
/*global UaNodeId, readSetting, GetDefaultReferencesFromNodeId, Session, addError,
GetBrowseResponseForOneReference, BrowseNextFromResponseAndAssertReferencesMatch,
StatusCode, AssertFalse
*/
function TestBrowseNextWhenMoreReferencesExist( doReleaseContinuationPoints, performCleanup )
{
var nodeToBrowse = UaNodeId.fromString( readSetting( "/Server Test/NodeIds/References/Has 3 Forward References 1" ).toString() );
if( nodeToBrowse === undefined || nodeToBrowse === null )
{
addSkipped( "[Configuration Issue?] Unable to conduct test. Check setting '/Server Test/NodeIds/References/Has 3 Forward References 1'." );
return;
}
// get expected references
var expectedReferences = GetDefaultReferencesFromNodeId( Session, nodeToBrowse );
if( expectedReferences.length < 3 )
{
addError( "Test cannot be completed: node must have at least three references." );
return;
}
// Browse for first reference
var firstResponse = GetBrowseResponseForOneReference( Session, [ nodeToBrowse ] );
if( firstResponse === -1 )
{
return -1;
}
// BrowseNext for second reference
// And validate that the reference from BrowseNext is the second reference (expectedReferences[1])
var response = BrowseNextFromResponseAndAssertReferencesMatch( [ expectedReferences ], 1, 1, doReleaseContinuationPoints, firstResponse.Results );
// Validate the releaseContinuationPoints = false case; the true case is covered within the above function
if( !doReleaseContinuationPoints )
{
if( response.Results.length > 0 )
{
var result = response.Results[0];
if( StatusCode.Good !== result.StatusCode.StatusCode )
{
addError( "StatusCode from BrowseNext is not Good: " + result.StatusCode, result.StatusCode );
}
if( AssertFalse( result.ContinuationPoint.isEmpty(), "ContinuationPoint is empty: " + result.ContinuationPoint ) )
{
BrowseNextFromResponseAndAssertReferencesMatch( [ expectedReferences ], 2, 2, doReleaseContinuationPoints, response.Results );
}
}
}
// perform clean-up?
if( performCleanup !== undefined && performCleanup !== null && performCleanup === true )
{
releaseContinuationPoints( Session, response );
}
return [ firstResponse, response ];
}
// release ContinuationPoints returned in a response; no result checking
function releaseContinuationPoints( session, browseResponse )
{
var request = CreateDefaultBrowseNextRequest( session );
var response = new UaBrowseNextResponse();
request.ReleaseContinuationPoints = true;
for( var i = 0; i < browseResponse.Results.length; i++ )
{
request.ContinuationPoints[i] = browseResponse.Results[i].ContinuationPoint;
}
session.browseNext( request, response );
}