/// <summary> /// 添加一台设备 /// </summary> /// <param name="machine">设备</param> public void AddMachine(BaseMachine machine) { machine.KeepConnect = KeepConnect; lock (_machines) { _machines.Add(machine); } }
/// <summary> /// 移除设备 /// </summary> /// <param name="machine">设备的实例</param> public void RemoveMachine(BaseMachine machine) { lock (_machines) { _machines.Remove(machine); } lock (_unlinkedMachines) { _unlinkedMachines.Remove(machine); } }
/// <summary> /// 设置数据 /// </summary> /// <param name="connectionToken">设备的连接标识</param> /// <param name="values">需要设置的数据</param> /// <returns>是否设置成功</returns> public async Task <bool> SetDatasAsync(string connectionToken, Dictionary <string, double> values) { BaseMachine machine = null; lock (_machines) { machine = _machines.FirstOrDefault(p => p.ConnectionToken == connectionToken); } if (machine == null) { return(false); } return(await machine.SetDatasAsync(SetDataType, values)); }
/// <summary> /// 执行对具体设备的数据更新 /// </summary> /// <param name="machine">设备的实例</param> /// <returns></returns> private async Task RunTask(BaseMachine machine) { try { //调试代码,调试时取消下面一下代码的注释,会同步调用获取数据。 //var ans = machine.GetDatas(); //设置Cancellation Token CancellationTokenSource cts = new CancellationTokenSource(); //超时后取消任务 cts.CancelAfter(TimeSpan.FromSeconds(_getCycle)); //读取数据 var ans = await machine.GetDatasAsync(GetDataType).WithCancellation(cts.Token); if (!machine.IsConnected) { MoveMachineToUnlinked(machine.Id); } else { MoveMachineToLinked(machine.Id); } ReturnValues?.Invoke(new TaskReturnDef() { MachineId = machine.Id, ReturnValues = ans }); } catch (Exception e) { if (!machine.IsConnected) { MoveMachineToUnlinked(machine.Id); } ReturnValues?.Invoke(new TaskReturnDef() { MachineId = machine.Id, ReturnValues = null }); } }
/// <summary> /// 添加一台设备 /// </summary> /// <param name="machine">设备</param> public void AddMachine(BaseMachine machine) { base.AddMachine(machine); }