Esempio n. 1
0
        /// <summary>
        /// Start handler.
        /// MSDN documentaion recommends not to rely on args in general.
        /// We use config file for any parameters and ignore command line args.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            EnsureCertificateValidation();

            ILightControl lightControl = null;

            this.stateMachine = new StateMachine();

            this.remoteRecorderSync = new RemoteRecorderSync((IStateMachine)this.stateMachine);

            if (string.Compare(Properties.Settings.Default.DeviceType, "Delcom", StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Set up of Delcom light (with button) device.
                this.delcomLight = new DelcomLight((IStateMachine)this.stateMachine);
                lightControl     = this.delcomLight as ILightControl;

                if (!this.delcomLight.Start())
                {
                    // It is desired to block starting the service, but that prevents the installer completes
                    // when the device is not installed. (vital=yes causes failure, vital=no becomes hung.)
                    // As a workaround, service continues to start, but with error log message.
                    Trace.TraceError("Failed to start up Delcom Light component. Service continues to run, but the device is not recognized without restart of the service.");
                    lightControl     = null;
                    this.delcomLight = null;
                }
            }
            // TODO: add here for device specific start up when another device type is added.
            else
            {
                throw new InvalidOperationException("Specified device type is not supported: " + Properties.Settings.Default.DeviceType);
            }

            // Start processing of the state machine.
            this.stateMachine.Start(this.remoteRecorderSync, lightControl);
        }
        /// <summary>
        /// Stop handler.
        /// </summary>
        protected override void OnStop()
        {
            if (this.remoteRecorderSync != null)
            {
                this.remoteRecorderSync.Stop();
                this.remoteRecorderSync = null;
            }

            if (this.delcomLight != null)
            {
                this.delcomLight.Stop();
                this.delcomLight = null;
            }

            if (this.stateMachine != null)
            {
                this.stateMachine.Stop();
                this.stateMachine = null;
            }

            if (this.tcpComm != null)
            {
                this.tcpComm.Stop();
                this.tcpComm = null;
            }
        }
        /// <summary>
        /// Start handler.
        /// MSDN documentaion recommends not to rely on args in general.
        /// We use config file for any parameters and ignore command line args.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            EnsureCertificateValidation();

            ILightControl lightControl = null;

            this.stateMachine = new StateMachine();

            this.remoteRecorderSync = new RemoteRecorderSync((IStateMachine)this.stateMachine);

            if (string.Compare(Properties.Settings.Default.DeviceType, "Delcom", StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Set up of Delcom light (with button) device.
                this.delcomLight = new DelcomLight((IStateMachine)this.stateMachine);
                lightControl = this.delcomLight as ILightControl;

                if (!this.delcomLight.Start())
                {
                    Trace.TraceError("Failed to start up Delcom Light component. Terminate.");
                    throw new ApplicationException("Failed to start up Delcom Light component. Terminate.");
                }
            }
            // TODO: add here for device specific start up when another device type is added.
            else
            {
                throw new InvalidOperationException("Specified device type is not supported: " + Properties.Settings.Default.DeviceType);
            }

            // Start processing of the state machine.
            this.stateMachine.Start(this.remoteRecorderSync, lightControl);
        }
        /// <summary>
        /// Start handler.
        /// MSDN documentaion recommends not to rely on args in general.
        /// We use config file for any parameters and ignore command line args.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            EnsureCertificateValidation();

            ILightControl lightControl = null;

            this.stateMachine = new StateMachine();

            this.remoteRecorderSync = new RemoteRecorderSync((IStateMachine)this.stateMachine);

            if (string.Compare(Properties.Settings.Default.DeviceType, "Delcom", StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Set up of Delcom light (with button) device.
                this.delcomLight = new DelcomLight((IStateMachine)this.stateMachine);
                lightControl     = this.delcomLight as ILightControl;

                if (!this.delcomLight.Start())
                {
                    Trace.TraceError("Failed to start up Delcom Light component. Terminate.");
                    throw new ApplicationException("Failed to start up Delcom Light component. Terminate.");
                }
            }
            // TODO: add here for device specific start up when another device type is added.
            else
            {
                throw new InvalidOperationException("Specified device type is not supported: " + Properties.Settings.Default.DeviceType);
            }

            // Start processing of the state machine.
            this.stateMachine.Start(this.remoteRecorderSync, lightControl);
        }
Esempio n. 5
0
        /// <summary>
        ///     Program main loop
        /// </summary>
        public void Main()
        {
            //Create new DelcomLight object and start it's thread to listen for input from the button
            DelcomLight dLight = new DelcomLight(new EnqueueStateMachineInput(this.AddInputToStateMachineQueue),
                                                 RRLightProgram.Properties.Settings.Default.HoldDuration);

            //Create new remote recorder sync object to poll recorder state and input changes into state machine
            RemoteRecorderSync rSync = new RemoteRecorderSync(new EnqueueStateMachineInput(this.AddInputToStateMachineQueue));

            //Initialize state machine. Pass in Light and RemoteRecorder
            StateMachine sm = new StateMachine(dLight, rSync);

            // Main thread loop
            // Loop endlessly until we're asked to stop
            while (!this.shouldStop)
            {
                StateMachine.StateMachineInputArgs argsToProcess = null;

                // lock only while we're inspecting and changing the queue
                lock (stateMachineInputQueue)
                {
                    // if the queue has anything, then work on it
                    if (stateMachineInputQueue.Any())
                    {
                        // dequeue
                        argsToProcess = stateMachineInputQueue.Dequeue();
                    }
                }

                if (argsToProcess != null)
                {
                    if (Program.RunFromConsole)
                    {
                        Trace.TraceInformation(DateTime.Now + ": Processing input: ");
                        Trace.TraceInformation(DateTime.Now + ": " + argsToProcess.Input.ToString() + " " +
                                               DateTime.UtcNow.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture));
                        Trace.Flush();
                    }

                    // send the input to the state machine
                    sm.ProcessStateMachineInput(argsToProcess);
                }
                else
                {
                    // else sleep
                    Thread.Sleep(50);
                }
            }
        }
        /// <summary>
        /// Set ILightControl interface, RemoteRecoderSync, and start processing thread.
        /// </summary>
        /// <param name="remoteRecorder">Remote recorder controller. Cannot be null.</param>
        /// <param name="lightControl">Light control interface. May be null.</param>
        public void Start(RemoteRecorderSync remoteRecorder, ILightControl lightControl)
        {
            if (remoteRecorder == null)
            {
                throw new ArgumentException("remoteRecorder cannot be null.");
            }
            if (this.inputProcessThread != null)
            {
                throw new ApplicationException("StateMachine.Start() is called while running.");
            }

            this.remoteRecorder = remoteRecorder;
            this.light          = lightControl ?? new EmptyLightControl();

            this.inputProcessThread = new Thread(this.InputProcessLoop);
            TraceVerbose.Trace("State machine is starting.");
            inputProcessThread.Start();
        }
        /// <summary>
        /// Stop handler.
        /// </summary>
        protected override void OnStop()
        {
            if (this.remoteRecorderSync != null)
            {
                this.remoteRecorderSync.Stop();
                this.remoteRecorderSync = null;
            }

            if (this.delcomLight != null)
            {
                this.delcomLight.Stop();
                this.delcomLight = null;
            }

            if (this.stateMachine != null)
            {
                this.stateMachine.Stop();
                this.stateMachine = null;
            }
        }
        /// <summary>
        /// Set ILightControl interface, RemoteRecoderSync, and start processing thread.
        /// </summary>
        /// <param name="remoteRecorder">Remote recorder controller. Cannot be null.</param>
        /// <param name="lightControl">Light control interface. May be null.</param>
        public void Start(RemoteRecorderSync remoteRecorder, ILightControl lightControl)
        {
            if (remoteRecorder == null)
            {
                throw new ArgumentException("remoteRecorder cannot be null.");
            }
            if (this.inputProcessThread != null)
            {
                throw new ApplicationException("StateMachine.Start() is called while running.");
            }

            this.remoteRecorder = remoteRecorder;
            this.light = lightControl ?? new EmptyLightControl();

            this.inputProcessThread = new Thread(this.InputProcessLoop);
            TraceVerbose.Trace("State machine is starting.");
            inputProcessThread.Start();
        }