Thanks for the fast answere. I am using one function node that uses several JS-timers and timeouts (setTimeout() and setInterval()) to create a "long lived" simulation.
This is a snippet:
generateForkliftSpeedId = setInterval(generateForkliftSpeed, telemetryIntervalMoving);
setTimeout(function() {
clearInterval(generateForkliftSpeedId); //Stop speed generator.
generateForksRisingTelemetryId = setInterval(generateForksRisingTelemetry,
telemetryIntervalMovingForks);
}, timeToMove/2); //Forklift stopping to load.
This is the whole function code:
[{"id":"425c9e02.96f3","type":"function","z":"3a02ebf.434c414","name":"GenerateTelemetryLaglyftare0","func":"var timeToComplete = msg.payload.timeToComplete * 1000; //ms\nvar timeToMove = timeToComplete - 1000; //ms\nvar telemetryIntervalMoving = 1000;\nvar telemetryIntervalMovingForks=1000;\nvar palletSrcHeight = msg.payload.palletSrcHeight;\nvar palletWeight = msg.payload.palletWeight;\n\nvar forkliftSpeed, forkSpeed = 1, forksIncline=0, forkLoadWeight=0, forkHeightPos = 0, forkliftTippingPoint=12.5;\nvar forkliftSpeedUpperBound = 5; // m/s\nvar forkliftSpeedLowerBound = 1;\nvar tippingRate=4;//4 degrees per unit time.\n\nvar weightCapacity = 600;\nvar heightCapacity=5;\nvar forkliftHeightCapacityBreakingPoint=(weightCapacity/palletWeight)*heightCapacity;//Point where the forklifts start to lean.\nnode.send({payload:forkliftHeightCapacityBreakingPoint});\n\nvar generateForkliftSpeedId, generateForksRisingTelemetryId, generateForkliftLoweringTelemetryId;\n\nfunction generateForkliftSpeed(){\n forkliftSpeed = Math.floor(Math.random() * (forkliftSpeedUpperBound - forkliftSpeedLowerBound + 1)) + forkliftSpeedLowerBound;\n var testMsg = {};\n testMsg.payload = \"Speed: \" + forkliftSpeed;\n node.send(testMsg);\n}\n\nfunction loadPalletAndStartLoweringForks(){\n forkHeightPos=palletSrcHeight;\n clearInterval(generateForksRisingTelemetryId); //Stop lifting forks.\n forkLoadWeight=palletWeight;\n generateForkliftLoweringTelemetryId = setInterval(function(){generateForkliftLoweringTelemetry(true)}, telemetryIntervalMovingForks);\n}\n\nfunction generateForksRisingTelemetry(){\n forkHeightPos += forkSpeed*(telemetryIntervalMovingForks/1000);\n \n //Conditional stop.\n if (forkHeightPos>=heightCapacity && palletSrcHeight>heightCapacity){\n //Forks above capacity, capacity can NOT handle the height.\n forkHeightPos=heightCapacity;\n clearInterval(generateForksRisingTelemetryId); //Stop lifting forks.\n generateForkliftLoweringTelemetryId = setInterval(function(){generateForkliftLoweringTelemetry(false)}, telemetryIntervalMovingForks);\n }else if (forkHeightPos>=palletSrcHeight && palletSrcHeight<heightCapacity){\n //Forks above height, capacity can handle the height.\n loadPalletAndStartLoweringForks();\n }else if (forkHeightPos>=heightCapacity && heightCapacity==palletSrcHeight){\n //Forks above capacity and height, which are the same. Capacity can handle the height.\n loadPalletAndStartLoweringForks();\n }\n node.send({payload:{forkHeight: forkHeightPos, forkCap: heightCapacity, itemHeight: palletSrcHeight}});\n}\n\n/*\n* If the truck doesn't have a pallet, it lowers its forks to 0.\n* If the truck has a pallet that weighs to much for the truck\n* it will lower the pallet to the appropriate height if it gets\n* a command from the GGC. If the pallet weights to much the forklift\n* will start to lean. However, if the truck\n* can handle the weight at the palletSrcHeight, it will keep\n* it there and the function does nothing.\n*/\nfunction generateForkliftLoweringTelemetry(hasPallet){\n \n if (!hasPallet){\n forkHeightPos -= forkSpeed*(telemetryIntervalMovingForks/1000); //Convert to seconds\n //Forklift is free, report back so that the forklift can take a new job.\n if (forkHeightPos<=0){//Adjust the forks.\n forkHeightPos=0;//Lift forks a little.\n clearInterval(generateForkliftLoweringTelemetryId); //Stop lowering forks.\n node.send({payload:\"No pallet\"});\n }\n }else{\n //Forklift starts to lean.\n if (forkHeightPos>=forkliftHeightCapacityBreakingPoint){\n forksIncline+=tippingRate;\n node.send({payload:\"Forklift leaning\"});\n //Forklift tipped over.\n if (forksIncline>=forkliftTippingPoint){\n clearInterval(generateForkliftLoweringTelemetryId); //Stop generate lowering telemetry.\n node.send({payload:\"Forklift tipped over!\"});\n }\n \n }else{\n //Forklift keep moving to destination.\n node.send({payload:\"Forklift moving to dest\"});\n clearInterval(generateForkliftLoweringTelemetryId); //Stop lowering forks.\n generateForkliftSpeedId = setInterval(generateForkliftSpeed, telemetryIntervalMoving);\n setTimeout(function() { //Anonymous function.\n clearInterval(generateForkliftSpeedId); //Stop speed generator.Forklift job done, report back.\n }, timeToMove/2); //Forklift stopping to load.\n }\n }\n \n node.send({payload:forkHeightPos});\n}\n\nfunction main(){\n flow.get('instr0', function(err, instr0){\n if (err){\n node.error(err, msg);\n node.send({payload:\"ERROR\"});\n }else{\n switch (instr0){\n case 0:\n node.send({payload:\"Instruction was 0!!!!\"});\n break;\n case 1:\n node.send({payload:\"Lowering forks!!!!!!!!!!!!!\"});\n break;\n case 2:\n break;\n case 3:\n break;\n default:\n node.send({payload:instr0});\n break;\n }\n flow.set('instr0', 0);\n }\n });\n generateForkliftSpeedId = setInterval(generateForkliftSpeed, telemetryIntervalMoving);\n setTimeout(function() { //Anonymous function.\n clearInterval(generateForkliftSpeedId); //Stop speed generator.\n generateForksRisingTelemetryId = setInterval(generateForksRisingTelemetry, telemetryIntervalMovingForks);\n }, timeToMove/2); //Forklift stopping to load.\n}\n\nmain();\n\n\nreturn msg;","outputs":1,"noerr":0,"x":1610,"y":160,"wires":[["89c570e0.ff0ec"]]}]