Exemplo n.º 1
0
        /// <summary>
        ///     执行对具体设备的数据更新
        /// </summary>
        /// <param name="machine">设备的实例</param>
        /// <returns></returns>
        protected override async Task RunTask(IMachineProperty <string> machine)
        {
            try
            {
                var ans = await GetValue(machine);

                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
                });
            }
        }
Exemplo n.º 2
0
        /// <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
                });
            }
        }