private async Task InitializeDevices() { foreach (int key in IOPort.Input.Ports.Keys) { InputPort port = IOPort.Input.Ports[key]; if (port.Status == PortStatus.OK) { await port.InitializeDevice(); } } foreach (int key in IOPort.Output.Ports.Keys) { OutputPort port = IOPort.Output.Ports[key]; if (port.Status == PortStatus.OK) { await port.InitializeDevice(); } } }
/// <summary> /// Checks if all devices are properly connected to the brick. False on any device/port error forcing brick to disconnect /// If true will beep once for OK, twice for OK and devices found (autoConnectDevices = true), Three times ERROR for device/port error(s) /// Test runs on setting PowerUpSelfTest <see cref="Configuration.PowerUpSelfTestOptions"/> /// Brick must be connected. /// </summary> /// <returns>False on Error, otherwise true</returns> private async Task <bool> PowerUpSelfTest() { //do not stop on error detected to enable logging for all errors. bool errorDetected = false; bool autoConnectDevices = Options.PowerUpSelfTest.AutoConnectDevices; bool autoDevicesConnected = false; IEnumerable <PortInfo> list = await InputMethods.PortScan(Socket); List <PortInfo> devices = new List <PortInfo>(); devices.AddRange(list); for (int layer = 0; layer < 4; layer++) { for (int portNumber = 0; portNumber < 4; portNumber++) { int index; PortInfo entry; PortStatus status; string message; #region Input Ports index = (layer * 4) + portNumber; entry = devices[index]; InputPort inputPort = IOPort.Input.Ports[index]; message = $"Brick {(ChainLayer)layer} Port {inputPort.Name}"; switch (entry.Status) { case PortStatus.OK: { status = inputPort.CheckDevice(entry.Device.Value, Options.PowerUpSelfTest.AutoConnectDevices); switch (status) { case PortStatus.Error: { Logger.LogWarning($"Invalid device {inputPort.Device.Type} {message}"); inputPort.Status = PortStatus.Error; errorDetected = true; break; } case PortStatus.Initializing: { autoDevicesConnected = true; break; } } break; } case PortStatus.Empty: { if (inputPort.Status == PortStatus.OK) { Logger.LogWarning($"Device {inputPort.Device.Type} is not connected {message}"); inputPort.Status = PortStatus.Error; //unconnected device! errorDetected = true; } break; } case PortStatus.Error: { Logger.LogWarning($"{message} Device {entry.Device}"); inputPort.Status = PortStatus.Error; // Output device connected to InputPort errorDetected = true; break; } default: { Logger.LogWarning($"{message} Device {entry.Device}"); inputPort.Status = PortStatus.Error; errorDetected = true; break; } } if (!errorDetected && inputPort.Status == PortStatus.OK) { await inputPort.InitializeDevice(); } #endregion #region Output Ports index = 16 + (layer * 4) + portNumber; entry = devices[index]; OutputPort outputPort = IOPort.Output.Ports[index]; message = $"Brick {(ChainLayer)layer} Port {outputPort.Name}"; switch (entry.Status) { case PortStatus.OK: { status = outputPort.CheckDevice(entry.Device.Value, autoConnectDevices); switch (status) { case PortStatus.Error: { Logger.LogWarning($"Invalid device {outputPort.Device.Type} {message}"); outputPort.Status = PortStatus.Error; errorDetected = true; break; } case PortStatus.Initializing: { autoDevicesConnected = true; break; } } break; } case PortStatus.Empty: { if (outputPort.Status == PortStatus.OK) { Logger.LogWarning($"Device {outputPort.Device.Type} is not connected {message}"); outputPort.Status = PortStatus.Error; //unconnected device! errorDetected = true; } break; } case PortStatus.Error: { Logger.LogWarning($"{message} Device {entry.Device}"); outputPort.Status = PortStatus.Error; // InputPort device connected to Outputport errorDetected = true; break; } default: { Logger.LogWarning($"{message} Device {entry.Device}"); outputPort.Status = PortStatus.Error; errorDetected = true; break; } } if (!errorDetected && outputPort.Status == PortStatus.OK) { await outputPort.InitializeDevice(); } #endregion } } int numberOfLoops = 1; // beep once for OK if (errorDetected) { numberOfLoops = 3; //beep three times } else if (autoDevicesConnected) { numberOfLoops = 2; //beep two times devices found, but all is still well :-) } bool beep = false; switch (numberOfLoops) { case 1: { beep = Options.PowerUpSelfTest.BeepOnOK; break; } case 2: { // all is still well so beep once if set anyhow. beep = (Options.PowerUpSelfTest.BeepOnAutoConnect) ? true : Options.PowerUpSelfTest.BeepOnOK; break; } case 3: { beep = Options.PowerUpSelfTest.BeepOnError; break; } } if (beep) { CancellationToken token = Socket.CancellationToken; await Task.Run(async() => { for (int i = 0; i < numberOfLoops; i++) { await SoundMethods.Tone(Socket, 50, 850, 150); await Task.Delay(400, token); } }, token); } if (errorDetected && Options.PowerUpSelfTest.DisconnectOnError) { await Disconnect(); } Logger.LogInformation($"PowerUpSelfTest: {((errorDetected) ? "ERROR" : "OK")}"); return(!errorDetected); }