RemoteVariableSynchronizer() constructor
Create a RemoteVariableSynchronizer object that can contain a list of project variables to be kept synchronized with the related field variables.
public RemoteVariableSynchronizer()
RemoteVariableSynchronizer object must be declared as a class member within the NetLogic.Constructor with the default polling time
In the following example, a variableSynchronizer object is created to keep the value of a Speed project variable synchronized with the value of a field variable that identifies the speed of a motor. The project variable, represented by the motorSpeed object, is added to the variableSynchronizer object via the Add() method.
When the field variable changes its value, the motorSpeed_VariableChange method is executed. When the value of motorSpeed (of the field variable), is greater than 200, a warning message is generated. The default polling time is set to full speed.
private void Start() {
motorSpeed = LogicObject.Owner.GetVariable("Speed");
variableSynchronizer = new RemoteVariableSynchronizer();
variableSynchronizer.Add(motorSpeed);
motorSpeed.VariableChange += MotorSpeed_VariableChange;
}
private void MotorSpeed_VariableChange(object sender, VariableChangeEventArgs e) {
if (motorSpeed.Value > 200) {
Log.Warning("Speed limit reached!");
}
}
private IUAVariable motorSpeed;
private RemoteVariableSynchronizer variableSynchronizer;
In the Stop() method, the end of the synchronization when the NetLogic parent node is removed is caused by invoking the Dispose() method.
Constructor with a custom polling time
In the following example, the polling time for RemoteVariableSynchronizer is set to 5000 milliseconds.
public override void Start()
{
var variable1 = Project.Current.Get<IUAVariable>("Model/Variable1");
variable1.VariableChange += Variable1_VariableChange;
synchronizer = new RemoteVariableSynchronizer(TimeSpan.FromMilliseconds(5000));
synchronizer.Add(variable1);
}
private void Variable1_VariableChange(object sender, VariableChangeEventArgs e)
{
Log.Info($"Variable1 {e.NewValue}");
}
public override void Stop()
{
synchronizer.Dispose();
var variable1 = Project.Current.Get<IUAVariable>("Model/Variable1");
variable1.VariableChange -= Variable1_VariableChange;
}
RemoteVariableSynchronizer synchronizer;
