public async Task <Parameter> GetParameter(EltraDevice device, ushort index, byte subIndex) { Parameter result = null; if (device != null) { if (device.SearchParameter(index, subIndex) is Parameter parameterEntry) { var parameterValue = await GetParameterValue(device, parameterEntry.Index, parameterEntry.SubIndex); if (parameterValue != null) { parameterEntry.SetValue(parameterValue); } result = parameterEntry; } else { result = await GetParameter(device.ChannelId, device.NodeId, index, subIndex); } if (result != null && result.Device == null) { result.Device = device; } } return(result); }
public async Task <ParameterValue> GetParameterValue(EltraDevice device, ushort index, byte subIndex) { ParameterValue result = null; if (device != null) { if (ParameterRegistrationCache.FindParameter(index, subIndex, out var registeredParameter)) { if (registeredParameter.CanUseCache) { var parameter = device.SearchParameter(index, subIndex) as XddParameter; if (parameter != null) { result = parameter.ActualValue; } } else { result = await GetParameterValue(device.ChannelId, device.NodeId, index, subIndex); if (result != null) { registeredParameter.LastModified = DateTime.Now; } } } else { result = await GetParameterValue(device.ChannelId, device.NodeId, index, subIndex); } } return(result); }
public async Task <Parameter> GetParameter(EltraDevice device, string uniqueId) { Parameter result = null; if (device != null) { var parameterBase = device.SearchParameter(uniqueId); if (parameterBase is Parameter parameter) { result = await GetParameter(device.ChannelId, device.NodeId, parameter.Index, parameter.SubIndex); } else { result = await GetParameter(device.ChannelId, device.NodeId, parameterBase.Index, 0x0); } if (result != null) { result.Device = device; } } return(result); }
/// <summary> /// RegisterParameterUpdate /// </summary> /// <param name="device"></param> /// <param name="uniqueId"></param> /// <param name="priority"></param> /// <param name="waitForResult"></param> public bool RegisterParameterUpdate(EltraDevice device, string uniqueId, ParameterUpdatePriority priority = ParameterUpdatePriority.Low, bool waitForResult = false) { #pragma warning disable 4014 if (!string.IsNullOrEmpty(uniqueId) && device != null && device.SearchParameter(uniqueId) is XddParameter parameterEntry) { bool result = false; if (!ParameterRegistrationCache.IsParameterRegistered(uniqueId, parameterEntry.Index, parameterEntry.SubIndex, out var instanceCount)) { Task.Run(async() => { var command = await GetDeviceCommand(device, "RegisterParameterUpdate"); if (command != null) { command.SetParameterValue("Index", parameterEntry.Index); command.SetParameterValue("SubIndex", parameterEntry.SubIndex); command.SetParameterValue("Priority", (int)priority); result = await ExecuteCommandAsync(command); if (!result) { instanceCount = ParameterRegistrationCache.RemoveParameter(uniqueId); MsgLogger.WriteError($"{GetType().Name} - RegisterParameterUpdate", $"parameter could't be registered - '{uniqueId}'"); } else { MsgLogger.WriteDebug($"{GetType().Name} - RegisterParameterUpdate", $"registered parameter '{uniqueId}', instance count = {instanceCount}"); } } }).ConfigureAwait(waitForResult); } else { if (ParameterRegistrationCache.IncreaseCounter(uniqueId, out var registeredParameter)) { MsgLogger.WriteDebug($"{GetType().Name} - RegisterParameterUpdate", $"register parameter '{uniqueId}', instance count = {registeredParameter.InstanceCount}"); } else { MsgLogger.WriteError($"{GetType().Name} - RegisterParameterUpdate", $"register parameter '{uniqueId}', instance count = {instanceCount}"); } } } else { MsgLogger.WriteError($"{GetType().Name} - RegisterParameterUpdate", $"parameter '{uniqueId}' not found"); } #pragma warning restore 4014 return(true); }
private async Task PlayWithParameterStatistics(EltraDevice device) { //another approach to find parameter instance in object dictionary - by parameter unique id var counterParameter = device.SearchParameter("PARAM_Counter") as Parameter; //get some statistics of the parameter lifetime var stats = await counterParameter.GetValueHistoryStatistics(DateTime.MinValue, DateTime.Now); if (stats != null) { Console.WriteLine($"items count = {stats.EntriesCount}, size in bytes {stats.SizeInBytes}, {stats.Created}"); } }
/// <summary> /// UnregisterParameterUpdate /// </summary> /// <param name="device"></param> /// <param name="uniqueId"></param> /// <param name="priority"></param> /// <param name="waitForResult"></param> public bool UnregisterParameterUpdate(EltraDevice device, string uniqueId, ParameterUpdatePriority priority = ParameterUpdatePriority.Low, bool waitForResult = false) { if (device != null && !string.IsNullOrEmpty(uniqueId) && device.SearchParameter(uniqueId) is Parameter parameterEntry) { bool result = false; if (ParameterRegistrationCache.CanUnregister(uniqueId, out var registeredParameter)) { var t = Task.Run(async() => { var command = await GetDeviceCommand(device, "UnregisterParameterUpdate"); if (command != null) { command.SetParameterValue("Index", parameterEntry.Index); command.SetParameterValue("SubIndex", parameterEntry.SubIndex); command.SetParameterValue("Priority", (int)priority); result = await ExecuteCommandAsync(command); if (!result) { ParameterRegistrationCache.AddParameter(registeredParameter); MsgLogger.WriteError($"{GetType().Name} - UnregisterParameterUpdate", $"parameter could't be unregistered - '{uniqueId}'"); } else { MsgLogger.WriteDebug($"{GetType().Name} - UnregisterParameterUpdate", $"unregistered parameter '{uniqueId}'"); } } }).ConfigureAwait(waitForResult); } else { registeredParameter?.Release(); MsgLogger.WriteDebug($"{GetType().Name} - UnregisterParameterUpdate", $"unregister parameter '{uniqueId}', instance count = {registeredParameter?.InstanceCount}"); result = true; } } else { MsgLogger.WriteError($"{GetType().Name} - UnregisterParameterUpdate", $"unregister: cannot find registered parameter '{uniqueId}'"); } return(true); }
public async Task <List <ParameterValue> > GetParameterValueHistory(EltraDevice device, ushort index, byte subIndex, DateTime from, DateTime to) { List <ParameterValue> result = null; if (device != null) { var parameter = device.SearchParameter(index, subIndex); if (parameter != null) { result = await GetParameterValueHistory(device.ChannelId, device.NodeId, parameter.UniqueId, from, to); } } return(result); }
public async Task <bool> PlayWithDevice(EltraDevice device) { bool result = false; Console.WriteLine($"device = {device.Name}, node id = {device.NodeId}, version = {device.Version}, serial number = {device.Identification.SerialNumber:X8}"); //0x3000, 0x00 is address of the counter parameter in object dictionary var counterParameter = device.SearchParameter(0x3000, 0x00) as Parameter; var controlWordParameter = device.SearchParameter(0x6040, 0x00) as Parameter; var cp = await device.GetParameter(0x3000, 0x00) as Parameter; if (counterParameter != null && controlWordParameter != null) { const ushort readyFlag = 1; const ushort sleepingFlag = 0; //let's get the current value stored in local object dictionary (can be outdated) controlWordParameter.GetValue(out ushort controlWord); // force parameter update, ok, now we are sure that the value is synchronized with master object dictionary await controlWordParameter.ReadValue(); //control word is defined as UINT16 - RW, let's modify value of this parameter in local object dictionary controlWordParameter.SetValue(readyFlag); //synchronize value with master object dictionary await controlWordParameter.Write(); // this method is reliable, but to keep the local object dictionary up-to-date not quiete effective // (object dictionary can contain more than 100 parameters) // your agent usually, doesn't need the current value information about all parameters // to limit the read requests, you can specify explicite the parameters you are interested in Console.WriteLine($"register parameter 0x{counterParameter.Index:X4}:0x{counterParameter.SubIndex:X2} for updates"); //we would like to be informed each time the parameter is changed counterParameter.ParameterChanged += OnParameterChanged; // to activate this feature, call RegisterUpdate // priority is transfered to the master and is up to master to decide what low, high or medium priority is counterParameter.AutoUpdate(ParameterUpdatePriority.High); //from now on, our local object dictionary will be actualized each time the {counterParameter} is changed by remote party // execute command on the remote master device, // our dummy master has 2 simple methods, start and stop counting // let's start counting and observe how our counter parameter {counterParameter} is changing ... // on the end, we will stop counting, to give our master some peace ... var sampleCommands = new SampleCommands(device); await sampleCommands.PlayWithDeviceCommands(); //we don't need the notifications at this point counterParameter?.StopUpdate(ParameterUpdatePriority.High); //remove events handling counterParameter.ParameterChanged -= OnParameterChanged; //in case our parameter is supporting <backup> flag, we can grab some historic data await PlayWithParameterStatistics(device); //reset control word parameter value controlWordParameter.SetValue(sleepingFlag); result = true; } return(result); }