Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        private void Run(string[] args)
        {
            Thread.CurrentThread.Name = "Master";

            string applicationPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

            Log.Info(Log.Dashes);
            Log.Info("STARTING " + applicationPath);
            foreach (string arg in args)
            {
                Log.Info(string.Format("arg: \"{0}\"", arg));
            }
            Log.Debug(Thread.CurrentThread.Name + " ThreadId=" + Thread.CurrentThread.ManagedThreadId.ToString("x8"));
            Log.Info(Log.Dashes);

            // We want the Controller class to load now, so that it's static
            // constructor reads the config file.  It's important to do this
            // before starting ANY of the service threads. Calling any
            // random property of the Controller class will cause the class to load.

            Controller.ApplicationPath = applicationPath;

            // Stop the pump and speaker on startup.  This is primarily done because if
            // the debugger is stopped then restarted while the pump or speaker are on, then they're left on.
            // if we don't do this...
            Controller.TurnBuzzerOff();
            Pump.Stop();

            // Instantiate the console service immediately so that we can display
            // the "Starting" message ASAP. Note that we don't yet start the thread,
            // though, since we don't need (yet), to monitor for keypad input.
            LCD.Clear(); // This is the first time we're accessing the LCD class, so it's static constructor will be called here.
            LCD.SetLanguage(Configuration.DockingStation.Language);

            Controller.LastRebootTime = DateTime.UtcNow;

            Controller.FirmwareVersion = string.Format("{0}.{1}.{2}.{3}",
                                                       Assembly.GetExecutingAssembly().GetName().Version.Major,
                                                       Assembly.GetExecutingAssembly().GetName().Version.Minor,
                                                       Assembly.GetExecutingAssembly().GetName().Version.Build,
                                                       Assembly.GetExecutingAssembly().GetName().Version.Revision);

            _consoleService = new ConsoleService(this);
            Log.Debug(ConsoleService.Name + " has been created.");

            ConsoleService.Start();
            // Calling InitializeState now will force the display to refresh and say "Starting"
            // right away. If we don't do this, it remains blank for a second or two.
            ConsoleService.InitializeState();

            try
            {
                Configuration.ServiceMode = Controller.DetermineServiceMode();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            // Did something go wrong trying to read the configuration data from the flash memory?
            if (Configuration.HasConfigurationError && Configuration.DockingStation.IsSerialized())
            {
                Controller.RunState |= Controller.State.ConfigError;
            }

            // Verify that we have a flash card.  We can't do much of anything without one.
            bool haveFlashCard = FlashCard.WaitForMount();

            if (haveFlashCard)
            {
                Log.Info("Found a mounted flash card");
            }
            else
            {
                Log.Fatal("ERROR: FLASH CARD NOT FOUND.");
                Controller.RunState |= Controller.State.FlashCardError;
            }

            Log.Info(Log.Dashes);
            Log.Info("Firmware Version: " + Controller.FirmwareVersion);
            Log.Info(Log.Dashes);
            Controller.LogSystemInfo();
            Log.Info(Log.Dashes);
            Controller.LogProcessorInfo();
            Log.Info(Log.Dashes);
            Controller.LogMemory();
            Log.Info(Log.Dashes);
            if (haveFlashCard)
            {
                FlashCard.Log();
                Log.Info(Log.Dashes);
            }
            Log.Info("MAC Address: " + Controller.GetWiredNetworkAdapter().MacAddress);
            Log.Info(Log.Dashes);

            if (haveFlashCard)
            {
                DataAccess.DataAccess.StartInet();
                DataAccess.DataAccess.StartInetQueue();
            }

            // Before we can start the WebAppService, we need to initialize
            // the password used to login to it.
            // Note that for debug builds, we configure it to not use SLL.
            // There are two reasons for this...
            // 1) SSL slows done iNet DS Configurator, which is annoying
            // when we developers are constantly logging into it to use it.
            // 2) There is some sort of problem when Padarn is using SSL
            // that causes the Visual Studio debugger to lose its connections
            // to the device.
#if DEBUG
            Configuration.ConfigureWebApp(Configuration.DockingStation.WebAppPassword, false);
#else
            Configuration.ConfigureWebApp(Configuration.DockingStation.WebAppPassword, true);
#endif

            if (Controller.RunState == Controller.State.OK)
            {
                InitializeWinsock();
            }

            // AJAY: INS-8380 Service accounts need to perform auto-upgrade on instruments even in error/fail state - DSX
            // If service account is configured to override event priority in admin console,
            // this method reorders events that needs to be executed on docking station.
            if (Configuration.IsRepairAccount() && Configuration.DockingStation.UpgradeOnErrorFail)
            {
                ISC.iNet.DS.DomainModel.EventCode.SetEventPriorityForService();
            }

            // Initialize, wire up and start all necessary services.
            // Note that this will be iterated every time any of the
            // services gets interrupted.

            // Create the services.
            Log.Debug("Creating services...");

            _resourceService = new ResourceService(this);
            Log.Debug(ResourceService.Name + " has been created.");

            _reporterService = new ReporterService(this);
            Log.Debug(ReporterService.Name + " has been created.");

            _executerService = new ExecuterService(this);
            Log.Debug(ExecuterService.Name + " has been created.");

            _switchService = new SwitchService(this);
            Log.Debug(SwitchService.Name + " has been created.");

            _chargingService = new ChargingService(this);
            Log.Debug(ChargingService.Name + " has been created.");

            _webAppService = new WebAppService(this);
            Log.Debug(WebAppService.Name + " has been created.");

            _controllerWrapper = ControllerWrapper.Instance;

            _pumpWrapper = PumpManager.Instance;

            _lcdWrapper = LCDWrapper.Instance;

            _smartCardWrapper = SmartCardWrapper.Instance;

            // Start the services.
            Log.Debug("Starting service threads...");

            if (Controller.RunState == Controller.State.OK)
            {
                ResourceService.Start();
            }

            // Don't start the Switch, Charging, or Executer service if the DS has
            // not been serialized.  Only the Configurator web app will be functional.
            if (Configuration.DockingStation.IsSerialized())
            {
                ExecuterService.Start();

                if (Controller.RunState == Controller.State.OK)
                {
                    SwitchService.Start();
                }

                if (Controller.RunState == Controller.State.OK)
                {
                    ChargingService.Start();
                }
            }
            else
            {
                // update the LCD
                ConsoleService.UpdateState(ConsoleState.NotSerialized);
            }

            if (Controller.RunState == Controller.State.OK)
            {
                ReporterService.Start();
            }

            WebAppService.Start();

            Log.Debug("Service threads started.");

            // the controller's static constructor will initialize the other logging settings,
            // but we always want to log all the initialization stuff above out the serial port
            Log.LogToSerialPort = Configuration.ServiceMode ? true : Configuration.DockingStation.LogToSerialPort;             // always log in service mode

            // Determine if sequenceId passed in as argument. This is number that will be automatically
            // passed in as an argument to the process when Windows CE boots the device.  If process
            // is manually started, then no sequence ID will be passed.  If sequenceId is passed in,
            // then process MUST call SignalStarted when it feels that it's safely up and running
            // in order to continue with the proper boot sequence.
            if (args.Length > 0)
            {
                uint sequenceId = 0;
                try
                {
                    sequenceId = UInt32.Parse(args[0]);
                }
                catch
                {
                    Log.Debug("Invalid sequenceId (" + args[0] + ") found.  SignalStarted not called.");
                }
                if (sequenceId > 0)
                {
                    Log.Debug("SignalStarted(" + sequenceId + ")");
                    WinCeApi.SignalStarted(sequenceId);
                }
            }

            // INS-6183, 6/8/2015 - This watchdog is used in the below while-loop. The  while-loop
            // periodically calls MonitorNetworkConnection(), which calls GetWiredNetworkAdapter(),
            // which calls GetNetworkAdapters().  GetNetworkAdapters() makes a call to OpenNetCF
            // SDF's GetAllNetworkInterfaces() to get the networking info from the OS.
            // Sometimes that call throws a Data Abort (unmanaged exception).  Unmanaged exceptions
            // cannot be caught in .Net, so the exception is thrown outside and above the application,
            // which causes OS to display a message box informing of the Data Abort.  The message box
            // waits for a user to press its OK button, so the thread that threw the Data Abort is
            // effectly hung waiting for somebody to press an OK button which is never going to happen.
            // The data abort may be thrown by a thread other this Master thread. But this watchdog
            // for the master thread should still work in that situation.  This is because the
            // aforementioned GetNetworkAdapters has a "lock" block in it. If another thread calls
            // GetNetworkAdapters, it will a obtain a lock, then call into SDF's GetAllNetworkInterfaces.
            // If the data abort is then thrown, then GetNetworkAdapters will not have a chance to
            // release its lock. Then, next time master thread trys to call GetNetworkAdapters, it will
            // hang trying to obtain a lock, which it will never be able to do, so the watchdog timer
            // will eventually expire, causing a reboot, as desired.
            WatchDog watchDog = new WatchDog("MasterWatchDog", 60000, Log.LogToFile);               // reboot if not refreshed in 60 seconds.
            watchDog.Start();

            ///////////////////////////////////////////////////////////////////////
            // Used for smart card debugging.  Please leave this code here for now.
            ///////////////////////////////////////////////////////////////////////
            //            I2CTestThread i2cTest= new I2CTestThread();
            //            i2cTest.StartWork();

            MonitorNetworkConnection();

            Controller.LogMemoryUsage();

            // Now that we've fired off the child worker threads, we have nothing more to do
            // except hang out and stay alive.  Lower our priority and take a nap.
            //Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
            int sleepTime = 5000;
            int lastCheck = 0;

            while (true)
            {
                Thread.Sleep(sleepTime);

                lastCheck += sleepTime;

                if (lastCheck >= 300000)                   // 5 minutes
                {
                    MonitorNetworkConnection();

                    //Logger.Debug( "Total Up Time: " + ( runTime / 1000 ) + " seconds" );
                    //Controller.LogMemoryUsage();
                    lastCheck = 0;
                }

                if (Controller.RunState != Controller.State.OK)
                {
                    string errNum = ((int)Controller.RunState).ToString("x8");
                    // If a menu is active, skip the operation for now. (the only menu that should
                    // possibly ever be active is the factory reset confirmation menu).
                    if (!ConsoleService.IsMenuActive)
                    {
                        Log.Trace("SYSTEM ERROR ENCOUNTERED with error number " + errNum);

                        if (errNum.Equals("00000001"))
                        {
                            ConsoleService.UpdateState(ConsoleState.ContactISCCode1011);
                        }
                        else if (errNum.Equals("00000002"))
                        {
                            ConsoleService.UpdateState(ConsoleState.ContactISCCode1012);
                        }
                        else if (errNum.Equals("00000004"))
                        {
                            ConsoleService.UpdateState(ConsoleState.ContactISCCode1014);
                        }
                        else if (errNum.Equals("00000008"))
                        {
                            ConsoleService.UpdateState(ConsoleState.ContactISCCode1018);
                        }
                        else if (errNum.Equals("00000010"))
                        {
                            ConsoleService.UpdateState(ConsoleState.ContactISCCode10110);
                        }
                        else
                        {
                            ConsoleService.UpdateState(ConsoleState.ContactISCCode10160);
                        }
                    }
                }

                if (lastCheck == 0 || lastCheck >= 10000)
                {
                    watchDog.Refresh();
                }
            }
        } // end-Run
        /// <summary>
        /// This method implements the thread start for this service.
        /// </summary>
        protected override void Run()
        {
            try
            {
                bool activated    = Configuration.Schema.Activated;
                bool synchronized = Configuration.Schema.Synchronized;

                //Suresh 13-APR-2012 INS-4519 (DEV JIRA)
                //If the docking station is Initialized and Activated, but not Synchronized then set the console state to Synchronization.
                //Note: We cannot do this after Discover() method, because ExecuterService.Discover() method and
                //ExecuterService.Run( ExchangeStatusOperation ) method runs on mutual-exclusion lock and that causes "Synchronization" state not to be changed
                //until ExchangeStatusOperation completely finishes but by that time Synchronization will be completed.
                if ((_initialized == true) && (activated == true || Configuration.ServiceMode) && (synchronized == false))
                {
                    bool online = Inet.IsOnline;

                    Log.Trace("Schema is not synced with iNet. Connected=" + online);

                    // We don't bother getting the current state (which does a 'lock') until we know AllSynced is false.
                    ConsoleState state = Master.ConsoleService.CurrentState;

                    if (state == ConsoleState.Ready ||
                        state == ConsoleState.Discovering ||
                        state == ConsoleState.Synchronization ||
                        state == ConsoleState.SynchronizationError)
                    {
                        Master.ConsoleService.UpdateState(online ? ConsoleState.Synchronization : ConsoleState.SynchronizationError);
                    }
                }

                // Anytime an account number or activation changes, we need to do a re-discover and force another settings.
                // This is due to database being reset due to the account change, and the new database will no longer
                // have info regarding currenly attached cylinders.
                string curAccountNum = Configuration.Schema.AccountNum;
                if ((_wasAccountNum != curAccountNum) || (_wasActivated != activated))
                {
                    if (_wasAccountNum != curAccountNum)
                    {
                        Log.Info(string.Format("{0} detected changed Account. Old=\"{1}\", New=\"{2}\"", Name, _wasAccountNum, curAccountNum));
                    }

                    if (_wasActivated != activated)
                    {
                        Log.Info(string.Format("{0} detected changed Activation. Old=\"{1}\", New=\"{2}\"", Name, _wasActivated, activated));
                    }

                    _wasAccountNum = curAccountNum;
                    _wasActivated  = activated;

                    Log.Debug(string.Format("{0} initiating Discovery (account/activation changed).", Name));

                    Master.ExecuterService.Discover();
                    _initialized = true;

                    InitialReadSettingsNeeded = ReadSettingsNeeded = ReadAllCards = true; // need to do a full readsettings

                    return;                                                               // Simply return. Yes, we need to perform a ReadSettings, but it'll happen the next time Run is called.
                }

                // Special handling for docking stations in manufacturing accounts...
                if (Configuration.Schema.IsManufacturing)
                {
                    // If activated (not a Cal Station), and not online, then
                    // display error message saying that it needs to be online.
                    if (Configuration.Schema.Activated && !Inet.IsOnline)
                    {
                        Master.ConsoleService.UpdateState(ConsoleState.MfgNotConnected);
                        return;
                    }

                    ConsoleState state = Master.ConsoleService.CurrentState;

                    if (state == ConsoleState.MfgNotConnected)
                    {
                        Master.ConsoleService.UpdateState(ConsoleState.MfgConnected);
                    }

                    else if (state == ConsoleState.MfgConnected)
                    {
                        Master.ConsoleService.UpdateState(ConsoleState.Ready);
                    }
                }

                // Until we find out that we've successfully connected to iNet
                // in order to obtain an account number, factory cylinder info, etc.,
                // don't do anything more than a one-time Discover.  The initial Discover
                // is also needed in order to jump start the ExecuterService's thread into
                // doing something. (It won't do anything on bootup as it's 'next action' is null.)
                if (!synchronized)
                {
                    if (!_initialized)
                    {
                        Log.Debug(string.Format("{0} initiating initial Discovery (unsynchronized).", Name));

                        DockingStationEvent dsEvent = Master.ExecuterService.Discover();
                        Controller.LogDockingStation(dsEvent.DockingStation);
                        _initialized = true;

                        InitialReadSettingsNeeded = ReadSettingsNeeded = ReadAllCards = true; // need to do a full readsettings
                    }
                    return;                                                                   // Simply return. Yes, we might need to perform a ReadSettings but, if needed, it'll happen the next time Run is called.
                }

                // Force a Discovery if we're just booting up.
                if (!_initialized)
                {
                    Log.Debug(string.Format("{0} initiating initial Discovery.", Name));
                    DockingStationEvent dsEvent = Master.ExecuterService.Discover();
                    Controller.LogDockingStation(dsEvent.DockingStation);
                    _initialized = true;
                    return;
                }

                // Check if instrument has been docked or undocked and force a Discovery if something has been docked or undocked.
                if (!Master.Instance.SwitchService.InitialReadSettingsNeeded && CheckDockedStatus())
                {
                    Log.Debug(string.Format("{0} initiating Discovery.", Name));
                    Master.ExecuterService.Discover();
                    _initialized = true;
                    return;
                }

                // Check card readers for card insertions / removals, and for pressure
                // switch changes.  If any card or pressure switch changes detected,
                // then we need to force a Settings Read.
                ReadSettingsNeeded = CheckIgasPorts() || ReadSettingsNeeded;

                // When we first start up, we always want to perform a settings read operation,
                // and send the information to the server.
                if (ReadSettingsNeeded)
                {
                    ReadSettings(ReadAllCards);
                    ReadSettingsNeeded = ReadAllCards = false;
                }

                _lastCaughtException = null;                      // no error encountered on this iteration of Run(), so clear the last caught error.
            }
            catch (InstrumentSystemAlarmException systemalarmmex) // SGF  Nov-23-2009  DSW-355  (DS2 v7.6)
            {
                Master.ConsoleService.UpdateState(ConsoleState.InstrumentSystemAlarm);
                //Suresh 06-FEB-2012 INS-2622
                Master.ExecuterService.ReportExceptionError(systemalarmmex);   //Suresh 15-SEPTEMBER-2011 INS-1593
            }
            catch (HardwareConfigurationException hce)
            {
                // Set docked flag to false.  Otherwise, if user just reconfigures the hardware on the
                // docking station without redocking the instrument, the IDS will think that it's already
                // docked and won't want to do a discover.
                _wasDocked = false;
                Master.ConsoleService.UpdateState(ConsoleService.MapHardwareConfigError(hce));
            }
            catch (Exception e)
            {
                Log.Error(Name, e);

                // If the menu is currently active, then we don't want to interrupt what the user is doing for an error
                // that will likely be hit again in a few seconds and can be displayed when the menu is not active.
                // Viewing the Cylinders menu screen could be helpful for troubleshooting smart card issues.
                // The first DeviceDriverException should be uploaded to iNet.
                if (e is DeviceDriverException && Master.Instance.ConsoleService.CurrentState != ConsoleState.Menu)
                {
                    // DeviceDriverExceptions that the SwitchService can encounter will be from checking smart card presence,
                    // pressure switch presence, or pressure switch state.
                    if (((DeviceDriverException)e).DeviceHardware == DeviceHardware.SmartCardPresence)
                    {
                        Master.Instance.ConsoleService.UpdateState(ConsoleState.IGasError, ConsoleServiceResources.IGASERROR_SMARTCARDS);
                    }
                    else
                    {
                        Master.Instance.ConsoleService.UpdateState(ConsoleState.IGasError, ConsoleServiceResources.IGASERROR_PRESSURESWITCHES);
                    }

                    // Give the ConsoleService time to display the error state.
                    Thread.Sleep(3000);
                }

                // don't upload CommunicationAbortedException or InstrumentNotDockedException to iNet
                if (e is CommunicationAbortedException || e is InstrumentNotDockedException)
                {
                    Master.Instance.ConsoleService.UpdateState(ConsoleState.UndockedInstrument);
                    Thread.Sleep(3000);
                    _wasDocked = false;
                    // A heartbeat is called to get the executer service to update the LCD screen (to go idle)
                    // in case the instrument remains undocked.
                    Master.ExecuterService.HeartBeat();
                }
                // Whenever we catch an exception, report it to iNet, but only if it's not
                // the same as the last error - to prevent a problem where we repeatedly get,
                // say, a device driver exception trying to check smart cards or pressure switches.
                // In that situation, we don't want to upload the error continuously to iNet.
                else if (_lastCaughtException == null || _lastCaughtException.GetType() != e.GetType())
                {
                    Master.ReporterService.ReportError(new DockingStationError(e, DockingStationErrorLevel.Warning));
                    Master.ExecuterService.HeartBeat(); //	Perform one heartbeat to force the error to upload asap.
                    _lastCaughtException = e;
                }
            }
        }