def sampleDistanceWithInterval(self): #launching next timer thread to record temp after specified interval self.nextTimer = threading.Timer(self.sampleInterval, self.sampleDistanceWithInterval, ()) self.nextTimer.start() if (self.recordingLoopActive == True and self.storage.hasSpace()): self.isRecording = True #line below ensures that, even when there is an error recording distance, isRecording won't stay on #The pi has 10 seconds to record temperature threading.Timer(self.sampleLength + 15, self.resetIsRecording, ()).start() timestamp = getTimestamp() try: self.logger.log("[DistanceSensor] started recording distance") end_time = time.time() + self.sampleLength while time.time() < end_time: distance = self.readDistance() timestamp = TimeUtils.getTimestamp() output = "%s %f\n" % (timestamp, distance) self.distLogger.appendToFile(output) self.logger.log("[DistacneReaader] recorded distance") except Exception as e: self.logger.logError("DistanceReader", "Error reading distance", e) self.isRecording = False
def makeBackup(self, newFilepath, piName): if self.filepath is None: return #need a lock so no thread writes to file while we are moving it and creating a new one with self.lock: os.rename(os.path.join(self.filepath,self.filename), os.path.join(newFilepath,self.filename.split(".")[0] + \ "_" + TimeUtils.getTimestamp() + "_" + piName + ".txt")) tempFile = open(os.path.join(self.filepath,self.filename), "w+") tempFile.close()
def makeBackup(self, newFilepath, piName): if self.filepath is None: return #need a lock so no thread writes to file while we are moving it and creating a new one with self.lock: os.rename(os.path.join(self.filepath,self.filename), os.path.join(newFilepath,self.filename.split(".")[0] + \ "_" + TimeUtils.getTimestamp() + "_" + piName + ".txt")) tempFile = open(os.path.join(self.filepath, self.filename), "w+") tempFile.close()
def samplePictureWithInterval(self): #setting up the next timer thread self.nextTimer = threading.Timer(self.sampleInterval, self.samplePictureWithInterval, ()) self.nextTimer.start() if (self.recordingLoopActive == True and self.storage.hasSpace()): self.isRecording = True #line below ensures that, even when there is an error taking a picture, isRecording won't stay on #The pi has 15 seconds to take the picture threading.Timer(15, self.resetIsRecording, ()).start() timestamp = TimeUtils.getTimestamp() try: self.takePicture(self.saveFolder, timestamp+'.png') self.logger.log("[PictureReader] Took picture") except Exception as e: self.logger.logError("PictureReader", "Error taking picture", e) self.isRecording = False
def samplePictureWithInterval(self): #setting up the next timer thread self.nextTimer = threading.Timer(self.sampleInterval, self.samplePictureWithInterval, ()) self.nextTimer.start() if (self.recordingLoopActive == True and self.storage.hasSpace()): self.isRecording = True #line below ensures that, even when there is an error taking a picture, isRecording won't stay on #The pi has 15 seconds to take the picture threading.Timer(15, self.resetIsRecording, ()).start() timestamp = TimeUtils.getTimestamp() try: self.takePicture(self.saveFolder, timestamp + '.png') self.logger.log("[PictureReader] Took picture") except Exception as e: self.logger.logError("PictureReader", "Error taking picture", e) self.isRecording = False
def sampleTemperatureWithInterval(self): #launching next timer thread to record temp after specified interval self.nextTimer = threading.Timer(self.sampleInterval, self.sampleTemperatureWithInterval, ()) self.nextTimer.start() if(self.recordingLoopActive == True and self.storage.hasSpace()): self.isRecording = True #line below ensures that, even when there is an error recording temperature, isRecording won't stay on #The pi has 10 seconds to record temperature threading.Timer(10, self.resetIsRecording, ()).start() try: temperature = self.readTemperature() timestamp = TimeUtils.getTimestamp() output = "%s %s\n" % (timestamp, temperature) #adding temperature to temperature file self.tempLogger.appendToFile(output) self.logger.log("[TemperatureReader] Recorded temperature") except Exception as e: self.logger.logError("TemperatureReader", "Error reading temperature", e) self.isRecording = False
config.write(cfgFileNew) cfgFileNew.close() print "Autostart was set to on successfully!" elif decision is "2": config.set("General", "auto_start", "off") cfgFileNew = open(local_file_path + 'config', "w") config.write(cfgFileNew) cfgFileNew.close() print "Autostart was set to off successfully!" else: print "input not recognized!" elif arg == "-ac" or arg == "-au": piName = config.get("General", "pi_name") archiveSaveSoundName = config.get( "Saving", "sound_save_folder_name") + "_" + TimeUtils.getTimestamp( ) + "_" + piName + "_FORCED" archiveSavePhotoName = config.get( "Saving", "photo_save_folder_name") + "_" + TimeUtils.getTimestamp( ) + "_" + piName + "_FORCED" archiveSaveDir = os.path.join( config.get("Saving", "base_data_directory"), config.get("Saving", "backup_save_folder_name"), "") archiveSaveSoundPath = os.path.join(archiveSaveDir, archiveSaveSoundName) archiveSavePhotoPath = os.path.join(archiveSaveDir, archiveSavePhotoName) archiveSoundReadDir = os.path.join( config.get("Saving", "base_data_directory"),
cfgFileNew = open(local_file_path + 'config', "w") config.write(cfgFileNew) cfgFileNew.close() print "Autostart was set to on successfully!" elif decision is "2": config.set("General", "auto_start", "off") cfgFileNew = open(local_file_path + 'config', "w") config.write(cfgFileNew) cfgFileNew.close() print "Autostart was set to off successfully!" else: print "input not recognized!" elif arg == "-ac" or arg == "-au": piName = config.get("General", "pi_name") archiveSaveSoundName = config.get("Saving", "sound_save_folder_name") + "_" + TimeUtils.getTimestamp() + "_" + piName + "_FORCED" archiveSavePhotoName = config.get("Saving", "photo_save_folder_name") + "_" + TimeUtils.getTimestamp() + "_" + piName + "_FORCED" archiveSaveDir = os.path.join(config.get("Saving", "base_data_directory"),config.get("Saving", "backup_save_folder_name"),"") archiveSaveSoundPath = os.path.join(archiveSaveDir, archiveSaveSoundName) archiveSavePhotoPath = os.path.join(archiveSaveDir, archiveSavePhotoName) archiveSoundReadDir = os.path.join(config.get("Saving", "base_data_directory"), config.get("Saving", "sound_save_folder_name"), "") archivePhotoReadDir = os.path.join(config.get("Saving", "base_data_directory"), config.get("Saving", "photo_save_folder_name"), "") if arg == "-ac": ret = subprocess.call(["sudo", "tar", "-cvzf", archiveSaveSoundPath+".tar.gz","-C", archiveSoundReadDir, "."]) if ret == 0: os.system("sudo rm " + archiveSoundReadDir + "*") ret = subprocess.call(["sudo", "tar", "-cvzf", archiveSavePhotoPath+".tar.gz","-C", archivePhotoReadDir, "."])
def log(self, text): #logText is printed to console and file logText = TimeUtils.getTimestamp() + " " + text print logText if self.filepath is not None: self.appendToFile(logText + "\n")
def archiveCheck(self): if self.keepArchiving: self.isProcessing = True #checking if there are enough files to archive shouldArchiveSound = self.shouldArchive(self.soundDirectory) shouldArchivePictures = self.shouldArchive(self.pictureDirectory) #if there aren't enough files to archive, we do nothing if not shouldArchiveSound and not shouldArchivePictures: self.nextTimer = threading.Timer(self.checkInterval, self.archiveCheck, ()) self.nextTimer.start() self.logger.log("[ArchiveManager] not enough files to archive, exiting") self.isProcessing = False return #if there are enough files, we ask recordingManager to stop recording threads #if it wasn't able to stop the recording threads, we enter this if statement, and do nothing because #we couldn't stop the recording threads if not self.recordingManager.stopRecording(): self.nextTimer = threading.Timer(self.checkInterval, self.archiveCheck, ()) self.nextTimer.start() self.logger.log("[ArchiveManager] could not stop recordings, exiting") self.recordingManager.startRecording() self.isProcessing = False return #if there are enough sound files to archive, we enter this if block and attempt to archive the sound files if shouldArchiveSound: self.logger.log("[ArchiveManager] Attempting to archive sound") try: self.logger.log("[ArchiveManager] Making sound archive") #if makeArchive fails, it will return non 0 value and we will not delete old sound files if self.makeArchive(self.soundDirectoryName, self.soundDirectoryName + '_'+ TimeUtils.getTimestamp() + '_' +self.piName) == 0: self.logger.log("[ArchiveManager] Removing old sound files") #tries to remove all .wav and .mp3 files from the folder try: os.system("sudo rm " + self.soundDirectory + "*.wav") except Exception as e: self.logger.logError("ArchiveManager", "Error removing .wav files", e) try: os.system("sudo rm " + self.soundDirectory + "*.mp3") except Exception as e: self.logger.logError("ArchiveManager", "Error removing .mp3 files", e) else: self.logger.log("[ArchiveManager] Sound archive was not created, exit code was not 0") except Exception as e: self.logger.logError("ArchiveManager", "Error making sound archive", e) #if there are enough picture files to archive, we enter this if block and attempt to archive the picture files if shouldArchivePictures: self.logger.log("[ArchiveManager] Attempting to archive photos") try: self.logger.log("[ArchiveManager] Making photo archive") #if makeArchive fails, it will return non 0 value and we won't delete old photos if self.makeArchive(self.pictureDirectoryName, self.pictureDirectoryName+'_'+ TimeUtils.getTimestamp()+'_'+self.piName) == 0: self.logger.log("[ArchiveManager] Removing old picture files") #removes all .png files try: os.system("sudo rm " + self.pictureDirectory+"*.png") except Exception as e: self.logger.logError("ArchiveManager", "Error removing .png files", e) else: self.logger.log("Photo archive was not created, exit code was not 0") except Exception as e: self.logger.logError("ArchiveManager", "Error making picture archive", e) #we are done archiving files, so we tell the recordingManager to start recording again self.recordingManager.startRecording() #set isProcessing to False to show that we are done archiving self.isProcessing = False #launches next archive timer thread self.nextTimer = threading.Timer(self.checkInterval, self.archiveCheck, ()) self.nextTimer.start()