Develop a process simulator logic
Develop a logic that will simulate a long-running task.
Prerequisites:
Set the default external code editor. See Set the default code editor.
- Create the ProcessVariableSimulator object:
- In theProject view, right-clickModeland select .
- Hover-over the object, select , and enterProcessVariableSimulator.
- In thePropertiespane, add four variables by selecting and selectingVariable.
- Rename the variables by hovering-over each variable, selecting and entering:
- IsRunning
- Message
- Step
- Value
- Next toIsRunning, selectInt32and selectBoolean.
- Next toMessage, selectInt32and selectString.
- Develop the ProcessVariableSimulatorLogic script:
- Right-clickProcessVariableSimulatorand select .
- Hover-over the NetLogic, select , and enterProcessVariableSimulatorLogic
- Double-click the NetLogic.The external code editor opens.
- Replace the existing code with the following code:using System.Threading; using UAManagedCore; public class ProcessVariableSimulatorLogic : FTOptix.NetLogic.BaseNetLogic { public override void Start() { var owner = (ProcessVariableSimulator)LogicObject.Owner; valueVariable = owner.ValueVariable; stepVariable = owner.StepVariable; messageVariable = owner.MessageVariable; isRunningVariable = owner.IsRunningVariable; increment = true; periodicTask = new PeriodicTask(IncrementDecrementTask, 500, LogicObject); periodicTask.Start(); longRunningTask = new LongRunningTask(MessageTask, LogicObject); longRunningTask.Start(); } public override void Stop() { periodicTask.Dispose(); periodicTask = null; longRunningTask.Dispose(); longRunningTask = null; } [FTOptix.NetLogic.ExportMethod] public void StartMethod() { periodicTask.Start(); isRunningVariable.Value = true; } [FTOptix.NetLogic.ExportMethod] public void StopMethod() { periodicTask.Cancel(); isRunningVariable.Value = false; } public void IncrementDecrementTask() { int currentValue = valueVariable.Value; int step = stepVariable.Value; if (increment) { currentValue += step; if (currentValue >= 100) { currentValue = 100; increment = false; } } else { currentValue -= step; if (currentValue <= 0) { currentValue = 0; increment = true; } } valueVariable.Value = currentValue; } public void MessageTask() { Thread.Sleep(2000); messageVariable.Value = "PHASE 1"; Thread.Sleep(2000); messageVariable.Value = "PHASE 2"; Thread.Sleep(2000); messageVariable.Value = "PHASE 3"; Thread.Sleep(2000); messageVariable.Value = "PHASE 4"; } private IUAVariable valueVariable; private IUAVariable stepVariable; private IUAVariable messageVariable; private IUAVariable isRunningVariable; private bool increment; private PeriodicTask periodicTask; private LongRunningTask longRunningTask; }
- Save the code.
- Convert the ProcessVariableSimulatorLogic object into an object type:
- In theType viewpane, double-clickModel.
- DragProcessVariableSImulatorfrom theProject viewpane into theType viewpane.
- Create the ProcessVariableSimulator1 object by draggingProcessVariableSImulatorfrom theType viewpane into theModelfolder inProject viewpane.
- Configure the ProcessVariableSimulator1 object:
- In thePropertiespane, set theIsRunningproperty value toTrue.
- Set theStepproperty value to1
Fornire un feedback