Keep X payload in a buffer in order to use previous payload
Keep X payload in a buffer in order to use with previous payload
//Choose the number of payload you want to keep (5 payload in this example)
const NbPayload = 5 ;
//Declare an array empty or get the current ArrayBuf (the ArrayBuf will be available from the flow)
let array = context.get("ArrayBuf") || [];
//Push the current payload in the array
array.push(msg.payload);
//if the length of the array is upper than the number of payload you want to keep...
if(array.length > NbPayload)
{
//...we remove the first payload of the array
array.shift();
}
//Save the array in ArrayBuf
context.set("ArrayBuf",array);
Now you can get a payload from the buffer like this :
//At the end of the function, you can get the previous payload
//Index of Current payload = NbPayload-1 (because index of the array start at 0 ! )
//Index of Previous Payload = NbPayload-2
PreviousTag1 = context.get("ArrayBuf")[NbPayload-2].Tag1
//Same thing as "_doJava.get("tag1", -1)" in MX Brain
Keep only some tags from X payload in a buffer
Keep only some tags (tagToKeep) from X payload (NbPayload) in a buffer in order to use previous tags
//1) Choose the number of payload you want to keep
const NbPayload = 5 ;
//2) Configure the list of the tag from the payload you want to keep
const tagToKeep =[
"tag1",
"tag2",
"tag3",
]
//Declare an object
let ObjToKeep = {};
//Keep tags and value from the "tagToKeep" list
for(let tag of tagToKeep)
{
ObjToKeep[`${tag}`] = msg.payload[tag];
}
//Declare an empty array or get the current ArrayBuf
let array = context.get("ArrayBuf") || [];
//Push the current ObjToKeep in the array
array.push(ObjToKeep);
//if the length of the array is upper than the number of "ObjToKeep" you want to keep..
if(array.length > NbPayload)
{
//..we remove the first ObjToKeep of the array
array.shift();
}
//Save the array in ArrayBuf
context.set("ArrayBuf",array);
Now you can get a tag from the buffer like this :
//At the end of the function, you can get the previous ObjToKeep
//Index of Current ObjToKeep = NbPayload-1 (because index of the array start at 0 ! )
//Index of Previous ObjToKeep= NbPayload-2
PreviousTag1 = context.get("ArrayBuf")[NbPayload-2].Tag1
//Same thing as "_doJava.get("tag1", -1)" in MX Brain
Pivot and group information from different payloads with a common ID
In that use case we want pivot and group information from different payload which have common "ID" (like the example below)
For example move from those 3 payload below :
(Here the common ID, is the concatenation of "tag1" and "tag2")
payload1= {tag1: "1/2/3189",
tag2: "9",
tag3: Name_1,
tag4: useless_1,
tag5: Value_1,
DATE: "2021-01-28T17:06:00.000Z"}
payload2= {tag1: "1/2/3189",
tag2: "9",
tag3: Name_2,
tag4: useless_2,
tag5: Value_2,
DATE: "2021-01-28T17:06:00.000Z"}
payload3= {tag1: "1/2/3189",
tag2: "9",
tag3: Name_3,
tag4: useless_3,
tag5: Value_3,
DATE: "2021-01-28T17:06:00.000Z"}
to a NewPayload :
NewPayload = {Name_1: Value_1,
Name_2: Value_2,
Name_3: Value_3,
tag1: "1/2/3189",
tag2: "9",
DATE: "2021-01-28T17:06:00.000Z"}
//Create an array to get the buffer or empty array
let arrayBuf=context.get("buffer") || [];
//Create a unique ID (Pivot Index) (here "tag1" and "tag2")
msg.payload.ID = msg.payload.tag1 + "_" + msg.payload.tag2;
//if the payload is empty or the tag to create index doesn't exist
if (typeof(msg.payload) !== 'object' && !msg.payload.hasOwnProperty('tag1')) {
//..return nothing
return;
}
//if buffer is empty and it's the first payload
if (arrayBuf.length===0 && msg.payload.hasOwnProperty('tag1'))
{
//initialise the buffer with this payload..
context.set("buffer",[msg.payload]);
//..return nothing
return;
}
//if the payload have the same ID as the element in the buffer
else if (msg.payload.ID === arrayBuf[0].ID)
{
//Check if the ID is not just "_" (without "tag1" and "tag2" )
if(arrayBuf[0].ID === "_") {
//And clean the buffer
context.set("buffer",null)
return;
}
//Push the payload in the buffer
arrayBuf.push(msg.payload)
//Save the arrayBuf in the context buffer
context.set("buffer",arrayBuf)
return;
}
//If the current payload has a different ID than the elements of the arraybuf
else
{
//Save the new payload in the context buffer
context.set("buffer", [msg.payload])
//----Do the Pivot for all element of the arrayBuf---
//Initialise the payload to send
msg.payload = {};
let i = 0;
arrayBuf.forEach((item) =>
{
//Generate in a loop new elements of the payload (to keep without pivot)
if (i === 0) {
//Keep some tag without pivot
msg.payload.tag1 = item.tag1;
msg.payload.tag2 = item.tag2;
msg.payload.DATE = item.DATE;
}
i++;
//Pivot and affect the value of the tag5 in the tag3
msg.payload[item.tag3]=item.tag5;
})
return msg;
}