コード例 #1
0
        public void OnErrorOccured(NandakaDevice device, DeviceError error, ILog log)
        {
            log.AppendMessage(LogMessageType.Error, $"Error occured with {device}. Reason: {error}");

            if (!IsDeviceShouldBeStopped(device, error))
            {
                return;
            }

            switch (error)
            {
            case DeviceError.ErrorReceived:
            case DeviceError.WrongPacketData:
                device.State = DeviceState.Corrupted;
                break;

            case DeviceError.NotResponding:
                device.State = DeviceState.NotResponding;
                break;

            default:
                device.State = DeviceState.Disconnected;
                break;
            }

            log.AppendMessage(LogMessageType.Error, $"Device has reached the max number of errors. {device} will be disconnected");
        }
コード例 #2
0
        public NandakaDevice GetNextDevice(IReadOnlyCollection <NandakaDevice> slaveDevices, ILog log, out bool isUpdateCycleCompleted)
        {
            while (true)
            {
                if (!_enumerator.MoveNext())
                {
                    UpdateEnumerator(slaveDevices);
                    continue;
                }

                NandakaDevice nextDevice = _enumerator.Current;
                if (nextDevice == null)
                {
                    throw new NandakaBaseException("Next device is null");
                }

                if (nextDevice.State != DeviceState.Connected)
                {
                    continue;
                }

                isUpdateCycleCompleted = nextDevice.Address == _lastDeviceInCycle.Address;

                return(_enumerator.Current);
            }
        }
コード例 #3
0
        private bool IsDeviceShouldBeStopped(NandakaDevice device, DeviceError newError)
        {
            int errorCount = device.ErrorCounter[newError];

            device.ErrorCounter[newError] = errorCount + 1;

            return(errorCount > _maxErrorInRowCount);
        }
コード例 #4
0
        public NandakaDevice GetNextDevice()
        {
            NandakaDevice nextDevice = _updatePolicy.GetNextDevice(SlaveDevices, _log, out bool isUpdateCycleCompleted);

            if (isUpdateCycleCompleted)
            {
                Thread.Sleep(_updatePolicy.UpdateTimeout);
            }

            return(nextDevice);
        }
コード例 #5
0
        private void UpdateEnumerator(IReadOnlyCollection <NandakaDevice> slaveDevices)
        {
            IEnumerable <NandakaDevice> devicesToUpdate = slaveDevices
                                                          .Where(device => device.State == DeviceState.Connected)
                                                          .ToArray();

            if (devicesToUpdate.All(device => device.State != DeviceState.Connected))
            {
                throw new DeviceNotFoundException("All devices is not connected");
            }

            _lastDeviceInCycle = devicesToUpdate.Last();
            _enumerator        = devicesToUpdate.GetEnumerator();
        }
コード例 #6
0
        public void OnUnexpectedDeviceResponse(IReadOnlyCollection <NandakaDevice> slaveDevices, NandakaDevice expectedDevice, int responseDeviceAddress, ILog log)
        {
            log.AppendMessage(LogMessageType.Warning, $"Message from unexpected device {responseDeviceAddress} received");

            NandakaDevice responseDevice = slaveDevices.FirstOrDefault(device => device.Address == responseDeviceAddress);

            if (responseDevice != null && IsDeviceSkipPreviousMessage(responseDevice))
            {
                log.AppendMessage(LogMessageType.Error,
                                  $"Device {responseDevice} is responding too long and will be disconnected");
                responseDevice.State = DeviceState.Corrupted;
                return;
            }

            log.AppendMessage(LogMessageType.Error, $"Device {expectedDevice} response with wrong address");
            expectedDevice.State = DeviceState.Corrupted;
        }
コード例 #7
0
 public void OnMessageReceived(NandakaDevice device, ILog log)
 {
     device.ErrorCounter.Clear();
 }
コード例 #8
0
 private static bool IsDeviceSkipPreviousMessage(NandakaDevice device)
 {
     return(device.ErrorCounter.ContainsKey(DeviceError.NotResponding));
 }
コード例 #9
0
ファイル: ForceUpdatePolicy.cs プロジェクト: Vlbager/Nandaka
 public void OnUnexpectedDeviceResponse(IReadOnlyCollection <NandakaDevice> slaveDevices, NandakaDevice expectedDevice, int responseDeviceAddress, ILog log)
 {
     log.AppendMessage(LogMessageType.Warning, $"Message from unexpected device {responseDeviceAddress} received");
 }
コード例 #10
0
ファイル: ForceUpdatePolicy.cs プロジェクト: Vlbager/Nandaka
 public void OnErrorOccured(NandakaDevice device, DeviceError error, ILog log)
 {
     log.AppendMessage(LogMessageType.Error, $"Error occured with {device}. Reason: {error}");
 }
コード例 #11
0
ファイル: ForceUpdatePolicy.cs プロジェクト: Vlbager/Nandaka
 public void OnMessageReceived(NandakaDevice device, ILog log)
 {
     // Empty.
 }
コード例 #12
0
 public void OnUnexpectedDeviceResponse(NandakaDevice expectedDevice, int responseDeviceAddress)
 {
     _updatePolicy.OnUnexpectedDeviceResponse(SlaveDevices, expectedDevice, responseDeviceAddress, _log);
 }
コード例 #13
0
 public void OnErrorOccured(NandakaDevice device, DeviceError error)
 {
     _updatePolicy.OnErrorOccured(device, error, _log);
 }
コード例 #14
0
 public void OnMessageReceived(NandakaDevice device)
 {
     _updatePolicy.OnMessageReceived(device, _log);
 }
コード例 #15
0
 public void AddSlaveDevice(NandakaDevice slaveDevice)
 {
     // bad design. Need to rework register logic.
     slaveDevice.Reflect();
     _slaveDevices.Add(slaveDevice);
 }
コード例 #16
0
 public SlaveDeviceManager(NandakaDevice device)
 {
     _log   = Log.Instance;
     Device = device;
 }