Ejemplo n.º 1
0
def storageBackend(script):
    if len(script.args) <= 1:
        logger.error(
            "Storage backend must be specified, either 'swift', 's3', 'noop', or 'disk'"
        )
        sys.exit(1)
    if script.args[1].lower() == "disk":
        import disk
        return disk.Disk(script.options.bucket, script.options.noop)
    if script.args[1].lower() == "noop":
        import noop
        return noop.NoOP(script.options.bucket, script.options.noop)
    if script.args[1].lower() == "s3":
        import s3
        if len(script.args) > 2:
            region = script.args[2]
        else:
            region = "us-east-1"
        return s3.S3(script.options.bucket, script.options.sse_c_key, region,
                     script.options.noop)
    if script.args[1].lower() == "swift":
        import swift
        return swift.Swift(script.options.bucket, script.options.noop)

    logger.error(
        "Invalid storage backend, must be 'swift', 's3', 'noop', or 'disk'")
    sys.exit(1)
Ejemplo n.º 2
0
def process_reg_data(reg_num, pull=False):
    s = s3.S3(reg_num)
    dbh = db.DB()
    registree_set = dbh.get_registrees(reg_num)
    fn = build_doc(registree_set, pull)
    s.upload_pdf_file(fn)
    print(f"Processed payment details for reg num {registree_set.reg_num}. File: {fn}")
    pyperclip.copy(f"evince {fn} &")
    def __init__( self ):
        self.__bucketName = ''
        self.__files = defaultdict( )
        self.__userName = ''
        self.__actionName = ''
        self.__taskName = ''
        self.__path = ''

        self.__nodeID = requests.get( "http://localhost:4001/info" ).json()['Name']
        # print("CHECK: ", self.__nodeID )
        # Get the reouserce
        self.s3Obj = s3.S3()

        self.mqttObj = mqtt.MQTT()
        self.mqttObj.start()
        self.updateTopic = "Updates"
Ejemplo n.º 4
0
def storageBackend(script):
    if len(script.args) <= 1:
        logger.error("Storage backend must be specified, either 'swift' or 's3'")
        sys.exit(1)
    if script.args[1].lower() == "noop":
        import noop
        return noop.NoOP(script.options.bucket, script.options.noop)
    if script.args[1].lower() == "s3":
        import s3
        return s3.S3(script.options.bucket,
                     script.options.keyprefix,
                     script.options.noop)
    if script.args[1].lower() == "swift":
        import swift
        return swift.Swift(script.options.bucket, script.options.noop)

    logger.error("Invalid storage backend, must be 'swift', 's3', or 'noop'")
    sys.exit(1)
Ejemplo n.º 5
0
def storageBackend(script):
    if len(script.args) <= 1:
        logger.error(
            "Storage backend must be specified, either: disk, gcs, noop, s3, or swift"
        )
        sys.exit(1)
    if script.args[1].lower() == "disk":
        import disk
        return disk.Disk(script.options.bucket, script.options.noop)
    if script.args[1].lower() == "noop":
        import noop
        return noop.NoOP(script.options.bucket, script.options.noop)
    if script.args[1].lower() == "s3":
        import s3
        s3args = {"region": "us-east-1"}
        for i in script.args[2:]:
            fields = i.split("=")
            if len(fields) > 1:
                gcsargs[fields[0]] = fields[1]
            else:
                gcsargs["region"] = fields[0]
        return s3.S3(script.options.bucket, s3args["region"],
                     script.options.noop)
    if script.args[1].lower() == "swift":
        import swift
        return swift.Swift(script.options.bucket, script.options.noop)
    if script.args[1].lower() == "gcs":
        import gcs
        gcsargs = {"project": "", "region": "us"}
        for i in script.args[2:]:
            fields = i.split("=")
            if len(fields) > 1:
                gcsargs[fields[0]] = fields[1]
            else:
                gcsargs["region"] = fields[0]
        return gcs.GCS(script.options.bucket, gcsargs["project"],
                       gcsargs["region"], script.options.noop)

    logger.error(
        "Invalid storage backend, must be: disk, gcs, noop, s3, or swift")
    sys.exit(1)
Ejemplo n.º 6
0
def send_email(reg_num=None, registree_set=None, fn=None):
    if fn is None:
        s = s3.S3(reg_num)
        fn = s.download_pdf_reg_file(reg_num)

    if reg_num is not None:
        dbh = db.DB()
        registree_set = dbh.get_registrees(args.reg_num)
    elif registree_set is None:
        raise ValueError(
            "Either a reg num to look up or a RegistreeSet should be provided"
        )

    emails = "; ".join(set([r.email for r in registree_set.registrees if r.email]))

    if emails:
        pyperclip.copy(emails)
        print(f"To: addresses copied to clipboard: {emails}")
        input()
        pyperclip.copy(BCC)
        print(f"BCC: addresses copied to clipboard: {BCC}")
        input()
        subject = f"Registration for the 2021 MD410 Convention for {registree_set.registree_names}. Registration number: MDC{registree_set.reg_num:003}"
        pyperclip.copy(subject)
        print(f"Subject copied to clipboard: {subject}")
        input()
        kwds = locals()
        kwds.update(globals())
        body = BODY.format(**kwds)
        pyperclip.copy(body)
        print(body)
        input()
        pyperclip.copy(fn)
        print(os.path.abspath(fn))
    else:
        print("No email addresses supplied")
def process_reg_data(reg_num, rebuild=False, pull=False):
    s = s3.S3(reg_num)
    dbh = db.DB()
    if not rebuild:
        fn = s.download_data_file()

        registree_set = parse_data_file(fn)
        payees = dbh.get_2020_payees()
        print(f"Registrees: {registree_set.registree_names}")
        print(
            "Should any of the below payments from 2020 be applied to this registration?"
        )
        for (r, (name, amt)) in payees.items():
            print(f"{r:003}: {name}: R{amt}")
        print()
        previously_paid = 0
        while True:
            payee_reg = input("Applicable reg num: ")
            if not payee_reg:
                break
            previously_paid += payees[int(payee_reg)][-1]
        if previously_paid:
            registree_set.payments = [
                db.Payment(date(year=2020, month=5, day=1), previously_paid)
            ]
            registree_set.process_payments()
        dbh.save_registree_set(registree_set)

    else:
        registree_set = dbh.get_registrees(reg_num)
    fn = build_doc(registree_set, pull)
    s.upload_pdf_file(fn)
    print(
        f"{'re' if rebuild else ''}processed reg num {registree_set.reg_num}. File: {fn}"
    )
    pyperclip.copy(f"evince {fn} &")
Ejemplo n.º 8
0
class ICan:
    """
    Main class of the iCan application that packages the high level logic of the device.

    Public Methods:
        checkOrientation()
    """

    # SENSORS
    lidSensor = lid.Lid()
    cameraSensor = camera.Camera('/tmp/trash.jpg')

    # ACTUATORS/OUTPUT
    lightOutput = light.Light(21)
    trapdoorOutput = trapdoor.Trapdoor()

    # CLOUD SERVICES AND APIs
    storageService = s3.S3()
    databaseService = database.ICanItems()
    weatherService = local_weather.LocalWeather()
    recognitionService = image_recognition.ImageRecognition()
    predictionService = prediction.Prediction()
    notificationService = notification.Notification()

    # How long to wait to be in "steady" state (seconds)
    WAIT_TIME = 3
    
    # How often readings are taken from the accelerometer (seconds)
    READING_INTERVAL = 0.2

    recentOpenStates = None

    # Take a photo only after the can was just opened and closed (and in steady state)
    photoRecentlyTaken = True

    def __init__(self):
        # Store recent history of states in tuples (horizontal, vertical)
        initialState = (False, False)
        maxLength = int(self.WAIT_TIME / self.READING_INTERVAL)
        self.recentOpenStates = deque(maxLength * [initialState], maxLength)

    def checkOrientation(self):
        """
        Checks the current orientation of the lid, take a photo and process it.
        :return:
        """
        horizontal = self.lidSensor.isHorizontal()
        vertical = self.lidSensor.isVertical()
        print 'H: ', horizontal
        print 'V: ', vertical

        self.recentOpenStates.append((horizontal, vertical))

        if self.isReadyToTakePhoto() and not self.photoRecentlyTaken:
            print 'Taking photo now . . . '
            fileName = self.getFileName()
            self.cameraSensor.setImagePath(fileName)
            self.cameraSensor.takePhoto()
            link = self.uploadPhoto(fileName)

            identifiers = self.recognitionService.getImageIdentifiers(fileName)
            targetPrediction = self.predictionService.getTrashPrediction(identifiers)
            print identifiers
            print targetPrediction

            # Fallback in case nothing is recognized in the image by recognition service
            if len(identifiers) == 0:
                identifiers = ['trash']
                targetPrediction = 'trash'

            self.saveToDatabase(identifiers, targetPrediction, link)
            self.respondToPrediction(identifiers, targetPrediction)
            self.photoRecentlyTaken = True

        if vertical and not horizontal:
            # Lid is open
            self.lightOutput.turnOn()
            self.photoRecentlyTaken = False
        else:
            self.lightOutput.turnOff()

    def saveToDatabase(self, identifiers, targetPrediction, link):
        """
        Save the record of identification to the database.
        :param identifiers: List of identifier strings from the image recognition service
        :param targetPrediction: String prediction from the prediction service
        :param link: Public URL to the image
        :return: Response to save request from the database
        """
        return self.databaseService.addRecord({
            'item_name': ", ".join(identifiers),
            'recyclable': (targetPrediction == 'recyclable'),
            'compostable': (targetPrediction == 'compostable'),
            'timestamp': int(time()),
            'temperature': self.weatherService.getCurrentTemperature(),
            'image': link,
            'user_feedback': False,
        })

    def getFileName(self):
        """
        Return the pseudo unique filename of the next photo to be taken.
        :return: Absolute path to the file as a string
        """
        timestamp = time()
        name = 'trash_' + str(timestamp) + '.jpg'
        path = '/tmp/'
        return path + name

    def isReadyToTakePhoto(self):
        """
        Return if the iCan is ready to take a photo based on current state and previous states.

        If the lid as been closed for the entire duration of the waiting period, then it is
        time to take a photo.

        :return: Boolean on whether the iCan is ready to take a photo
        """
        # Check if the queue of states shows it has been closed
        # for the entire waiting period
        closedState = (True, False)
        return self.recentOpenStates.count(closedState) == self.recentOpenStates.maxlen

    def uploadPhoto(self, fileName):
        """
        Upload given file to cloud storage, write the link to a file and return it
        :param fileName: Absolute path to file
        :return: URL to the file on cloud storage
        """

        # Write the public link to a local file
        link = self.storageService.uploadData(fileName)
        with open('/tmp/photos.txt', 'a') as photosFile:
            photosFile.write(link + "\n")
        print 'URL: ' + link
        return link

    def respondToPrediction(self, identifiers, targetPrediction):
        """
        React to the prediction by either opening the trapdoor or sending a notification.
        :param identifiers: List of string identifiers from Image Recognition Service
        :param targetPrediction: Prediction of 'trash', 'compostable', 'recyclable', etc. from the ML model
        :type targetPrediction: str
        """
        if targetPrediction == 'trash':
            print 'Down the hatch!'
            self.trapdoorOutput.open()
            print 'Waiting . . . '
            sleep(2)
            self.trapdoorOutput.close()
        else:
            print 'Sending Notification'
            identifiersList = ', '.join(identifiers[:3])
            message = 'iCan has detected an item that is: ' + identifiersList
            message = message + "\nCategory " + targetPrediction.upper()
            self.notificationService.sendNotification(message)

    def cleanUp(self):
        """
        Clean up any used I/O pins and close any connections if needed.
        :return: None
        """
        self.trapdoorOutput.cleanUp()
        self.lightOutput.cleanUp()