def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.logger = Logger()

        self.clientId = self.configuration.imgur_client_id()
        self.clientSecret = self.configuration.imgur_client_secret()
        self.imagePath = self.configuration.imgur_latest_photo_root_path()
示例#2
0
class Logger():
    __metaclass__ = Singleton
    
    __LOG_FILE_NAME = "/home/pi/Desktop/PeepNee/sample/errorLog.txt"
    
    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.minimalLogLevel = ErrorLevelEnum[self.configuration.logging_log_level()]
        self.logTo = LogToEnum[self.configuration.logging_log_to()]

    def log_information(self, message):
        if(self.minimalLogLevel <= ErrorLevelEnum.Information):
            self.__write('INFO: ' + message)
            
    def log_error(self, message):
        if(self.minimalLogLevel <= ErrorLevelEnum.Error):        
            self.__write('ERROR: ' + message)
            
    def log_critical(self, message):
        if(self.minimalLogLevel <= ErrorLevelEnum.Critical):
            self.__write('CRITICAL: ' + message)
            #TODO: Send email if configured
    
    def __write(self, message):
        logType = self.logTo 
        
        if(logType == LogToEnum.Console):
            print(message)

        elif(logType == LogToEnum.File):
            f = open(self.__LOG_FILE_NAME, "a")
            f.write(message + '\n')
            f.flush()
            f.close()
class ImageUploadManager():
    __VISION_API_ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'

    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.logger = Logger()

        self.clientId = self.configuration.imgur_client_id()
        self.clientSecret = self.configuration.imgur_client_secret()
        self.imagePath = self.configuration.imgur_latest_photo_root_path()

    def uploadImage(self):
        try:
            client = ImgurClient(self.clientId, self.clientSecret)
            uploaded_image = client.upload_from_path(self.imagePath,
                                                     config=None,
                                                     anon=True)

            return uploaded_image
        except BaseException as e:
            self.logger.log_critical('<ImageUploadManager.uploadImage> => ' +
                                     str(e))

    def extractImageLabelsByPublicUri(self, uri):
        try:
            requestBody = {
                "requests": [{
                    "image": {
                        "source": {
                            "imageUri": uri
                        }
                    },
                    "features": [{
                        "type": "LABEL_DETECTION",
                        "maxResults": 3
                    }]
                }]
            }

            authParams = {"key": self.configuration.gvapi_apiKey()}
            response = requests.post(
                self.__VISION_API_ENDPOINT_URL,
                params=authParams,
                json=requestBody).json()['responses'][0]['labelAnnotations']

            return list(map(lambda x: x['description'], response))
        except BaseException as e:
            self.logger.log_error(
                '<ImageUploadManager.extractImageLabelsByPublicUri> => ' +
                str(e))
            raise


# Usage example
# ium = ImageUploadManager()
# ium.extractImageLabelsByPublicUri('https://ichef.bbci.co.uk/news/976/cpsprodpb/1363B/production/_89591497_juvenilesaltwater.jpg')
# print('About to upload a picture...')
# urlToUploadedImage = ium.uploadImage()
# print('Picture was uploaded. Its public URL is: ' + str(urlToUploadedImage['link']))
示例#4
0
    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.timeToKeepTheBoxOpen = self.configuration.box_time_to_keep_the_box_open(
        )
        self.logger = Logger()

        self.servoManager = ServoManager()
        self.proximitySensorManager = ProximitySensorManager()
示例#5
0
    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.firebaseConfig = {
            "apiKey": self.configuration.fbase_apiKey(),
            "authDomain": self.configuration.fbase_authDomain(),
            "databaseURL": self.configuration.fbase_databaseUrl(),
            "storageBucket": self.configuration.fbase_storageBucket()
        }

        self.firebase = pyrebase.initialize_app(self.firebaseConfig)
        self.db = self.firebase.database()

        self.turnOffWasRequested = False
示例#6
0
 def __init__(self):
     self.configuration = GlobalConfigurationWrapper()
     self.logger = Logger()
     
     self.firebaseConfig = {
       "apiKey": self.configuration.fbase_apiKey(),
       "authDomain": self.configuration.fbase_authDomain(),
       "databaseURL": self.configuration.fbase_databaseUrl(),
       "storageBucket": self.configuration.fbase_storageBucket()
     }
     
     self.firebase = pyrebase.initialize_app(self.firebaseConfig)
     self.db = self.firebase.database()
     
     self.__load_local_default_settings()
     self.__load_default_settings_from_firebase()
示例#7
0
class BoxOpeningManager():
    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.timeToKeepTheBoxOpen = self.configuration.box_time_to_keep_the_box_open(
        )
        self.logger = Logger()

        self.servoManager = ServoManager()
        self.proximitySensorManager = ProximitySensorManager()

    def start_box_opening_procedure(self):
        try:
            self.servoManager.openMailbox()

            t_end = time.time() + (int)(self.timeToKeepTheBoxOpen)
            while time.time() < t_end:
                continue

            while True:
                if not self.proximitySensorManager.object_in_front():
                    self.servoManager.closeMailbox()
                    break
                else:
                    time.sleep(1)
        except BaseException as e:
            self.logger.log_critical(
                '<BoxOpeningManager.start_box_opening_procedure> => ' + str(e))


# Usage example
# bom = BoxOpeningManager()
# bom.start_box_opening_procedure()
class CameraSensorManager():
    __metaclass__ = Singleton

    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.pathToLatestCapture = self.configuration.imgur_latest_photo_root_path(
        )
        self.logger = Logger()

    def take_picture(self):
        try:
            self.camera = PiCamera()
            self.camera.start_preview()
            sleep(3)
            self.camera.capture(self.pathToLatestCapture, use_video_port=True)
            self.camera.stop_preview()
        except BaseException as e:
            self.logger.log_critical('<CameraSensorManager.take_picture> => ' +
                                     str(e))
        finally:
            self.camera.close()


# Usage example:
# camera = CameraSensorManager()
# print('About to take a picture...')
# camera.take_picture()
# print('Picture was taken!')
示例#9
0
 def __init__(self):
     self.configuration = GlobalConfigurationWrapper()
     self.hmiConfiguration = HmiConfigurationWrapper()
     self.boxOpeningManager = BoxOpeningManager()
     self.logger = Logger()
     self.soundManager = SoundManager()
     
     self.serial = serial.Serial(port='/dev/serial0',baudrate=9600,timeout=1.0)
     self.worker_thread = threading.Thread(target=self.__idle_start, args=())
示例#10
0
class TurnOffRequestHandler():
    __metaclass__ = Singleton

    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.firebaseConfig = {
            "apiKey": self.configuration.fbase_apiKey(),
            "authDomain": self.configuration.fbase_authDomain(),
            "databaseURL": self.configuration.fbase_databaseUrl(),
            "storageBucket": self.configuration.fbase_storageBucket()
        }

        self.firebase = pyrebase.initialize_app(self.firebaseConfig)
        self.db = self.firebase.database()

        self.turnOffWasRequested = False

    def idle(self):
        try:
            self.turn_off_request_stream = self.db.child(
                "Mailboxes/" + self.configuration.box_id()).stream(
                    self.__turn_off_request_stream_handler)
        except BaseException as e:
            self.logger.log_critical('<TurnOffRequestHandler.idle> => ' +
                                     str(e))
            raise

    def turnOffIsRequested(self):
        return self.turnOffWasRequested

    def dispose(self):
        self.turn_off_request_stream.close()

    def __turn_off_request_stream_handler(self, message):
        if (message["path"] == '/turnOffRequested'
                and message["data"] == True):
            self.turnOffWasRequested = True
            time.sleep(3)
            self.db.child("Mailboxes/" + self.configuration.box_id()).update(
                {"turnOffRequested": False})
示例#11
0
 def __init__(self):
     self.configuration = GlobalConfigurationWrapper()
     self.minimalLogLevel = ErrorLevelEnum[self.configuration.logging_log_level()]
     self.logTo = LogToEnum[self.configuration.logging_log_to()]
示例#12
0
class FirebaseManager():
    # Additional seconds are added because of the network latency.
    __NETWORK_LATENCY_COMPROMISE_SECONDS = 4

    def __init__(self):
        self.configuration = GlobalConfigurationWrapper()
        self.logger = Logger()
        
        self.firebaseConfig = {
          "apiKey": self.configuration.fbase_apiKey(),
          "authDomain": self.configuration.fbase_authDomain(),
          "databaseURL": self.configuration.fbase_databaseUrl(),
          "storageBucket": self.configuration.fbase_storageBucket()
        }
        
        self.firebase = pyrebase.initialize_app(self.firebaseConfig)
        self.db = self.firebase.database()
        
        self.__load_local_default_settings()
        self.__load_default_settings_from_firebase()
        
    def submit_mail_item(self, ocrText, snapshotUrl, associatedImageTags):
        try:
            mailReceivedAt = datetime.datetime.now(datetime.timezone.utc)
            waitForUserResponseUntil = email.utils.format_datetime(mailReceivedAt + datetime.timedelta(seconds=int(self.timeToWaitBeforeOpenOrClose) + self.__NETWORK_LATENCY_COMPROMISE_SECONDS))
            numberOfImageTags = len(associatedImageTags)
            data = {
                "mailboxId": (int)(self.boxId),
                "ocrText":ocrText,
                "snapshotUrl":snapshotUrl,
                "status": MailItemStatus.Pending,
                "receivedAt": email.utils.format_datetime(mailReceivedAt),
                "waitForResponseUntil": waitForUserResponseUntil,
                "topScoreImageTag": associatedImageTags[0] if numberOfImageTags > 0 else "",
                "middleScoreImageTag": associatedImageTags[1] if numberOfImageTags > 1 else "",
                "lowestScoreImageTag": associatedImageTags[2] if numberOfImageTags > 2 else ""
            }
            
            try:
                uploadResult = self.db.child("MailItems").push(data)
                self.referenceToNewlyAddedMailItem = uploadResult['name']
                self.NewlyAddedMailItemStatus = MailItemStatus.Pending
            except BaseException as e:
                pass
            
            if self.referenceToNewlyAddedMailItem is not None:
                self.logger.log_information('<FirebaseManager.__start_waiting_for_user_response> => New item successfully created and stream was created')
                self.new_mail_item_update_stream = self.db.child("MailItems/" + self.referenceToNewlyAddedMailItem).stream(self.__new_mail_item_update_stream_handler)
               
            newMailItemStatus = self.__start_waiting_for_user_response()
            
            return {
                    "mailItemStatus": newMailItemStatus,
                    "openByDefault": self.openByDefault
                    }
        except BaseException as e:
            self.logger.log_critical('<FirebaseManager.submit_mail_item> => ' + str(e))
            raise
    
    def toggle_mailbox_active_status(self, isActive):
        try:
            self.db.child("Mailboxes").child(self.boxId).update({"isActive": isActive})
        except BaseException as e:
            self.logger.log_critical('<FirebaseManager.toggle_mailbox_active_status> => ' + str(e))
            raise
            
    def __new_mail_item_update_stream_handler(self, message):
        if message["data"] is not None and message["data"]["status"] is not None:
            self.NewlyAddedMailItemStatus = int(message["data"]["status"])

    def __start_waiting_for_user_response(self):
        t_end = time.time() + self.timeToWaitBeforeOpenOrClose + self.__NETWORK_LATENCY_COMPROMISE_SECONDS
        while time.time() < t_end:
            continue

        if self.NewlyAddedMailItemStatus == MailItemStatus.Pending:
            if self.openByDefault:
                self.db.child("MailItems").child(self.referenceToNewlyAddedMailItem).update({ "status": MailItemStatus.Accepted })
            else:
                self.db.child("MailItems").child(self.referenceToNewlyAddedMailItem).update({ "status": MailItemStatus.Declined })     
        
        if self.new_mail_item_update_stream is not None:
            self.logger.log_information('<FirebaseManager.__start_waiting_for_user_response> => Closing update stream..')
            self.new_mail_item_update_stream.close()
            
        return self.NewlyAddedMailItemStatus
            
    def __load_default_settings_from_firebase(self):
        try:
            mailbox = self.db.child("Mailboxes/" + self.boxId).get()
            if mailbox.val() is not None:
                self.openByDefault = mailbox.val().get('openByDefault')
                self.timeToWaitBeforeOpenOrClose = mailbox.val().get('timeToWaitBeforeOpenOrClose')
            
        except BaseException as e:
            self.logger.log_error('<FirebaseManager.__load_default_settings_from_firebase> => ' + str(e))
            raise
    
    def __load_local_default_settings(self):
        self.boxId = self.configuration.box_id()
        self.openByDefault = self.configuration.box_open_by_default()
        self.timeToWaitBeforeOpenOrClose = self.configuration.box_time_to_wait_before_open_or_close()

# Usage example
# fbm = FirebaseManager()
# fbm.toggle_mailbox_active_status(True)
# fbm.submit_mail_item("ocre value", "https://i.pinimg.com/236x/29/01/3f/29013f4c4884c0907b9f5694b5bf402b--angry-meme-british.jpg", ["car", "vehicle", "police"])
 def __init__(self):
     self.configuration = GlobalConfigurationWrapper()
     self.pathToLatestCapture = self.configuration.imgur_latest_photo_root_path(
     )
     self.logger = Logger()