def restore(self): '''Starts the restore process from a file.''' backup = self.restoreFromFile() recordCount = len(backup) if(recordCount > 0): self.LOGGER.info(str(recordCount) + " record(s) found. Saving to DB") for record in backup: # Set data as valid by default hDataValid = True # Get backup record hData = record if self.VALIDATOR is not None and self.CONFIG.getBooleanConfig("Tolerence", "enabled"): try: validatedData = self.VALIDATOR.validateData(hData) except Exception as e: raise e hDataValid = validatedData[0] if hDataValid is True: hData = validatedData[1] if hDataValid and self.CONFIG.getBooleanConfig("Trigger", "enabled"): # Check trigger conditions which return true or false if it's valid try: hDataValid = self.TRIGGER.checkTriggers(hData) except Exception as e: raise e # Insert the first element in list and remove it if hDataValid: self.LOGGER.info("Inserting: " + str(record.__dict__)) #HistoricalData.insertData(record) HistoricalData.insertData(hData) else: self.LOGGER.info("Skipped backup record") # Remove backup file to prevent duplicate data from being restored. self.LOGGER.info("Restore from backup complete.") self.LOGGER.info("Removing backup file.") self.LOGGER.info("File deleted? " + str(self.deleteBackupFile()))
def run(): '''Reads, parses and stores data''' global CONFIG global LOGGER global VALIDATOR global TRIGGER global DEVICE global OFFLINE global OFFLINEMODE # Read data from USB/Serial data = DEVICE.read() # Instantiate parser xmlParser = Parser() # Parse xml data from device hData = xmlParser.parseXML(data) #valid data hDataValid = True if hData is not None: # Check time override if CONFIG.getBooleanConfig("Application", "useSystemTime"): # Overriding device time with system date time hData.time = datetime.now() else: try: # Parse time from device pre-pending system date tempDate = date.today().isoformat() + " " + hData.time hData.time = datetime.strptime(tempDate, "%Y-%m-%e %H:%M:%S") except ValueError: # Unable to parse time from device LOGGER.error("Error parsing time from device '" + hData.time + "'") # If error checking is enabled if VALIDATOR is not None and CONFIG.getBooleanConfig("Tolerence", "enabled"): try: # Validate data validatedData = VALIDATOR.validateData(hData) # Retrieve validation result hDataValid = validatedData[0] # If data is valid, retrieve the new valid data. The object may have # been cleansed e.g some channels may not meet validation parameters but # other channels might in the reading if hDataValid is True: hData = validatedData[1] except ConnectionException as ce: # Gracefully shutdown if it was unable to validate the data due to # database connection if CONFIG.getBooleanConfig("Application", "enableOffline"): # Set mode to offline OFFLINEMODE = True OFFLINE.backup(hData) else: shutdown() # Only check trigger conditions if it's enabled, not in offline mode and # data is valid after tolerence check if OFFLINEMODE is False and hDataValid and CONFIG.getBooleanConfig("Trigger", "enabled"): # Check trigger conditions which return true or false if it's valid try: hDataValid = TRIGGER.checkTriggers(hData) except ConnectionException as ce: # Gracefully shutdown if it was unable to check triggers due to database connection if CONFIG.getBooleanConfig("Application", "enableOffline"): # Set mode to offline OFFLINEMODE = True OFFLINE.backup(hData) else: shutdown() # Insert data if it passed all checks and mode is not offline if OFFLINEMODE is False: if hDataValid: try: HistoricalData.insertData(hData) except ConnectionException as ce: # Gracefully shutdown if it was unable to check triggers due to database connection if CONFIG.getBooleanConfig("Application", "enableOffline"): # Set mode to offline OFFLINEMODE = True OFFLINE.backup(hData) else: shutdown() else: LOGGER.info("Skipped") else: LOGGER.info("Offline mode: Active")