/// <summary> /// Replaces the current iNet database with a new, empty database. The "queue" database is not touched. /// </summary> static public Schema ResetInet(string accountNum, bool active, bool isManufacturing, string serviceCode) { Log.Info(string.Format("{0}: Resetting {1} database to default provided with firmware...", _name, Controller.INET_DB_NAME)); //lock ( FlashCard.Lock ) { Log.Trace("Thread=" + Thread.CurrentThread.Name + " Mutex.WaitOne"); Mutex.WaitOne(); try { // Clear out all handles to the database. SQLiteConnection.ClearAllPools(); DatabaseDelete(Controller.INET_DB_NAME); StartInet(); // re-start. It will automatically do a DatabaseRestore //INS-2460 - When the database is reset, delete the cached firmware file directory if (Directory.Exists(firmwareFolderPath)) { var di = new DirectoryInfo(firmwareFolderPath); di.Attributes &= ~FileAttributes.ReadOnly; //Remove read only attributes if any di.Delete(true); } new SchemaDataAccess().UpdateAccount(accountNum, active, isManufacturing, serviceCode); Schema schema = new SchemaDataAccess().Find(true) ?? new Schema(); schema.Log(); Log.Info(string.Format("{0}: Reset complete.", _name)); return(schema); } finally { Log.Trace("Thread=" + Thread.CurrentThread.Name + " Mutex.ReleaseMutex"); Mutex.ReleaseMutex(); } } }
/// <summary> /// </summary> /// <remarks> /// Makes sure the database exists on the flash card. /// /// ...If not found on flash card, then backup copy of empty /// database provided in firmware is copied to flash card. /// /// ...If found on flash card, then the version of the database /// is checked. This is because the firmware version may be incompatible /// with the database found on flashcard which will happen when /// the firmware is upgraded (or downgraded) to a different version. /// If database version is considered incompatible, then the database /// is deleted from the flash card and the backup copy of empty /// database provided in the firmware is copied to flash card. /// </remarks> static public void StartInet() { Log.Info(string.Format("{0}: Initializing...", _name)); //lock ( FlashCard.Lock ) { Controller.RunState &= ~Controller.State.InetDbError; // Clear existing DataBaseError bit. _startedError = null; // clear any existing error Log.Trace("Thread=" + Thread.CurrentThread.Name + " Mutex.WaitOne"); Mutex.WaitOne(); string dbName = Controller.INET_DB_NAME; try { // make sure database exists on the flash card. Copy blank database // provided with firmware image to flash card if necessary. if (!DatabaseRestore(Controller.INET_DB_NAME)) // will return false if database already existed on flash { // If journal file is found on startup, then SchemaDataAccess.Find should cause it to be rolled back. bool hotJournalExists = HotJournalExists(dbName); if (hotJournalExists) { Log.Warning(string.Format("{0}: \"HOT\" JOURNAL FILE FOUND FOR {1}!", _name, dbName)); } // We query the schema for two reasons: 1) We need its version number. // 2) The query is done in a writable transaction, so that any hot journal // file should be rolled back. Schema schema = new SchemaDataAccess().Find(false); // See if it was rolled back. if (hotJournalExists) { hotJournalExists = HotJournalExists(dbName); } if (hotJournalExists) { Log.Error(string.Format("{0}\"HOT\" JOURNAL FILE NOT ROLLED BACK FOR {1}!", _name, dbName)); } if (schema.Version != INET_VERSION) { Log.Info(string.Format("{0}: Incompatible schema version ({1}) found for {2}.", _name, schema.Version, dbName)); Log.Info(string.Format("{0}: Expected schema version {1}", _name, INET_VERSION)); // make sure account num in old database makes it to the new database. Configuration.Schema = ResetInet(schema.AccountNum, schema.Activated, schema.IsManufacturing, schema.ServiceCode); } else { Configuration.Schema = schema; Configuration.Schema.Log(); } } else { Configuration.Schema = new SchemaDataAccess().Find(false); Configuration.Schema.Log(); } Log.Info(string.Format("{0}: {1} database initialized.", _name, dbName)); } catch (Exception e) { _startedError = e.ToString(); Controller.RunState |= Controller.State.InetDbError; Log.Error(string.Format("{0}: FAILED TO INITIALIZE {1} DATABASE!", _name, dbName), e); } finally { try { Log.Trace("Thread=" + Thread.CurrentThread.Name + " Mutex.ReleaseMutex"); Mutex.ReleaseMutex(); } catch (Exception e) { Log.Error("Thread=" + Thread.CurrentThread.Name, e); throw; } } } }