Exemple #1
0
        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");
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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");
        }
Exemple #4
0
        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;
        }
Exemple #5
0
        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());
            }
        }