public void StopOnlineMode() { LogMessage("Stopping online mode"); if (Connection == ConnectionStatus.Connected) { throw new InvalidOperationException("Interface is not in online mode"); } ThrowWhenNotConnected(); _updateValuesTimer.Stop(); _updateValuesTimer.Close(); _updateValuesTimer = null; StopAllOutputs(); try { var responseStopOnline = new ResponseStopOnline(); TxtCommunication.SendCommand(new CommandStopOnline(), responseStopOnline); _onlineStopped?.Invoke(this, new EventArgs()); Connection = ConnectionStatus.Connected; } catch (Exception e) { LogMessage($"Exception while stopping online mode: {e.Message}"); HandleException(e); } LogMessage("Online mode stopped"); }
public void Disconnect() { LogMessage("Disconnecting"); ThrowWhenNotConnected(); try { TxtCommunication.CloseConnection(); Connection = ConnectionStatus.NotConnected; } catch (Exception e) { LogMessage($"Exception while disconnecting: {e.Message}"); Connection = ConnectionStatus.Invalid; HandleException(e); } _disconnected?.Invoke(this, new EventArgs()); TxtCommunication.Dispose(); TxtCommunication = null; TxtCamera.Dispose(); TxtCamera = null; LogMessage("Disconnected"); _masterInterface.ResetValues(); }
public void Connect(string ip) { LogMessage($"Connecting to {ip}"); Ip = ip; if (Connection == ConnectionStatus.Connected || Connection == ConnectionStatus.Online) { throw new InvalidOperationException("Already connected to an interface"); } TxtCommunication?.Dispose(); TxtCommunication = new TxtCommunication(this); TxtCamera = new TxtCameraCommunication(TxtCommunication); try { TxtCommunication.OpenConnection(); Connection = TxtCommunication.Connected ? ConnectionStatus.Connected : ConnectionStatus.NotConnected; } catch (Exception e) { LogMessage($"Exception while connecting: {e.Message}"); Connection = ConnectionStatus.Invalid; HandleException(e); } _masterInterface.ResetValues(); LogMessage("Connected"); _connected?.Invoke(this, new EventArgs()); }
public bool IsValidInterface(string address) { if (TxtCommunication == null) { TxtCommunication = new TxtCommunication(this); } return(TxtCommunication.IsValidInterface(address)); }
public string RequestControllerName(string address) { if (TxtCommunication == null) { TxtCommunication = new TxtCommunication(this); } return(TxtCommunication.RequestControllerName(address)); }
public string GetInterfaceName() { ThrowWhenNotConnected(); try { var responseQueryStatus = new ResponseQueryStatus(); TxtCommunication.SendCommand(new CommandQueryStatus(), responseQueryStatus); return(responseQueryStatus.GetControllerName()); } catch (Exception e) { HandleException(e); } return(string.Empty); }
public void StartOnlineMode() { LogMessage("Starting online mode"); if (Connection == ConnectionStatus.Online) { throw new InvalidOperationException("Already in online mode"); } ThrowWhenNotConnected(); _updateValuesTimer = new Timer(UpdateInterval); _updateValuesTimer.Elapsed += UpdateValuesTimerTick; _updateValuesTimer.AutoReset = true; _soundPlayIndex = 0; _configurationIndex = 0; var responseStartOnline = new ResponseStartOnline(); try { TxtCommunication.SendCommand(new CommandStartOnline(), responseStartOnline); Connection = ConnectionStatus.Online; _updateValuesTimer.Start(); _onlineStarted?.Invoke(this, new EventArgs()); List <int> inputPorts = new List <int>(); for (int i = 0; i < UniversalInputs; i++) { inputPorts.Add(i); } InputValueChangedEventArgs eventArgs = new InputValueChangedEventArgs(inputPorts); _inputValueChanged?.Invoke(this, eventArgs); } catch (Exception e) { LogMessage($"Exception while starting online mode: {e.Message}"); Connection = ConnectionStatus.Invalid; HandleException(e); } LogMessage("Online mode started"); }
public void Dispose() { Connection = ConnectionStatus.NotConnected; if (TxtCommunication != null) { TxtCommunication.Dispose(); TxtCommunication = null; } if (TxtCamera != null) { TxtCamera.Dispose(); TxtCamera = null; } _masterInterface.ResetValues(); }
private void UpdateConfiguration() { ThrowWhenNotConnected(); // ReSharper disable once UseObjectOrCollectionInitializer var commandUpdateConfig = new CommandUpdateConfig(); var responseUpdateConfig = new ResponseUpdateConfig(); // Increase the configuration id to notify that the configuration has changed commandUpdateConfig.ConfigId = (short)++_configurationIndex; for (int i = 0; i < _masterInterface.InputModes.Length; i++) { commandUpdateConfig.Config.UniversalInputs[i].Mode = (byte)_masterInterface.InputModes[i]; } for (int i = 0; i < _masterInterface.InputIsDigital.Length; i++) { commandUpdateConfig.Config.UniversalInputs[i].Digital = _masterInterface.InputIsDigital[i] ? (byte)1 : (byte)0; } for (int i = 0; i < _masterInterface.OutputModes.Length; i++) { commandUpdateConfig.Config.Motor[i] = _masterInterface.OutputModes[i] ? (byte)1 : (byte)0; } try { TxtCommunication.SendCommand(commandUpdateConfig, responseUpdateConfig); } catch (Exception e) { HandleException(e); } _configurationChanged = false; }
public void UpdateValues() { ThrowWhenNotConnected(); if (_configurationChanged) { UpdateConfiguration(); _configurationChanged = false; } var commandExchangeData = new CommandExchangeData(); var responseExchangeData = new ResponseExchangeData(); for (int i = 0; i < _masterInterface.OutputValues.Length; i++) { commandExchangeData.PwmOutputValues[i] = (short)_masterInterface.OutputValues[i]; } commandExchangeData.SoundCommandId = (ushort)_soundPlayIndex; if (_soundChanged) { commandExchangeData.SoundCommandId = (ushort)++_soundPlayIndex; commandExchangeData.SoundIndex = _masterInterface.SoundIndex; commandExchangeData.SoundRepeat = _masterInterface.SountRepeatCount; _soundChanged = false; _soundPlaying = true; } try { TxtCommunication.SendCommand(commandExchangeData, responseExchangeData); } catch (Exception e) { HandleException(e); return; } IList <int> valueChanged = new List <int>(); for (int i = 0; i < responseExchangeData.UniversalInputs.Length; i++) { var newInputValue = responseExchangeData.UniversalInputs[i]; if (_masterInterface.GetInputValue(i) != newInputValue) { _masterInterface.SetInputValue(i, newInputValue); valueChanged.Add(i); } } if (valueChanged.Count > 0) { // Fire an event when an input value has changed InputValueChangedEventArgs eventArgs = new InputValueChangedEventArgs(valueChanged); _inputValueChanged?.Invoke(this, eventArgs); } if (responseExchangeData.SoundCommandId != _soundPlayIndex - 1 && _soundPlaying) { _soundPlaying = false; // Fire an event when the sound playback has finished _soundPlaybackFinished?.Invoke(this, new EventArgs()); } }