Пример #1
0
    def getEntry(self, section, key):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            GETENTRY
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        """

        # Give user info
        Logger.debug("Getting entry: " + str(key))

        # Look if entry exists
        if key in section:

            # Get corresponding value
            value = section[key]

            # Give user info
            Logger.debug("Entry found.")

            # Show value
            Logger.debug(lib.JSONize(value))

            # Return it for external access
            return value

        # Otherwise
        else:

            # Give user info
            Logger.debug("No matching entry found.")

            # Return nothing
            return None
Пример #2
0
    def getSection(self, report, branch, make=False):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            GETSECTION
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        """

        # Read section depth
        d = len(branch)

        # First level section is whole report
        section = report.json

        # Give user info
        Logger.debug("Getting section: " + " > ".join(["."] + branch))

        # Loop through whole report to find section
        for i in range(d):

            # Get current branch
            b = branch[i]

            # Check if section report exists
            if b not in section:

                # Make section if desired
                if make:

                    # Give user info
                    Logger.debug("Section not found. Making it...")

                    # Create it
                    section[b] = {}

                # Otherwise
                else:

                    # Raise error
                    raise errors.NoSection

            # Update section
            section = section[b]

        # Give user info
        Logger.debug("Section found.")

        # Show section
        Logger.debug(lib.JSONize(section))

        # Return section
        return section
Пример #3
0
    def __str__(self):

        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            STR
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            String representation of report's content.
        """

        return lib.JSONize({
            "Name": self.name,
            "Directory": self.directory.path,
            "JSON": self.json
        })
Пример #4
0
    def show(self):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            SHOW
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        """

        # Show report
        Logger.debug(
            lib.JSONize({
                "Name": self.name,
                "Directory": self.directory,
                "Date": self.date,
                "JSON": self.json
            }))
Пример #5
0
    def autosens(self):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            AUTOSENS
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        """

        # Get last 24 hours of BGs
        BGs = Reporter.getRecent(self.now, "BG.json", [], 7, True)

        # Show them
        lib.JSONize(BGs)

        # Build BG profile for last 24 hours
        BGProfile = 0
Пример #6
0
    def addEntry(self, section, entry, overwrite=False):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            ADDENTRY
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        """

        # Give user info
        Logger.debug("Adding entry:")

        # Show entry
        Logger.debug(lib.JSONize(entry))

        # Destructure entry
        key, value = entry.items()[0]

        # Look if entry is already in report
        if key in section and not overwrite:

            # Give user info
            Logger.debug("Entry already exists.")

            # Entry was not modified
            return False

        # If not, write it down
        else:

            # Add entry to report
            section[key] = value

            # If overwritten
            if overwrite:

                # Give user info
                Logger.debug("Entry overwritten.")

            # Otherwise
            else:

                # Give user info
                Logger.debug("Entry added.")

            # Entry was modified
            return True
Пример #7
0
def main():
    """
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        MAIN
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    """

    # Get current time
    now = datetime.datetime.now() - datetime.timedelta(days=0)

    # Instanciate a reporter for me
    reporter = Reporter()

    # Get basal profile from pump report
    #reporter.get("pump.json", [], "Basal Profile (Standard)")

    # Get BGs of today
    #reporter.get("BG.json", [], None, now)

    # Get most recent data
    json = reporter.getRecent(now, "BG.json", [], 3, True)

    # Print data
    print lib.JSONize(json)
Пример #8
0
    def scanFrequencies(self, pump, F1 = None, F2 = None, n = 25, sample = 5):

        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            SCANFREQUENCIES
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Scan the air for frequency with best signal strength (best sample
            average RSSI) to tune radio in order to communicate with pump.
        """

        # No frequencies given: default to NA region
        if F1 is None and F2 is None:
            [F1, F2] = self.f["Regions"]["NA"]["Range"]

        # Otherwise: test frequency range
        else:
            self.checkFrequencyRange(F1, F2)

        # Info
        Logger.info("Scanning for best RF to communicate with pump between: " +
            fmt.frequencyRange(F1, F2))

        # Initialize RSSI readings
        RSSIs = {}

        # Go through frequency range
        for f in np.linspace(F1, F2, n, True):

            # Round frequency
            f = round(f, 3)

            # Initialize RSSI value
            RSSIs[f] = []

            # Tune frequency
            self.tune(f)

            # Sample
            for _ in range(sample):

                # Try
                try:

                    # Run pump command
                    pump.model.read(False)

                    # Get last packet
                    pkt = pump.model.command.packets["RX"][-1]

                    # Get RSSI reading and add it
                    RSSIs[f].append(pkt.RSSI["dBm"])

                # On invalid packet or radio error
                except (errors.RadioError, errors.PacketError):

                    # Add fake low RSSI reading
                    RSSIs[f].append(-99)

            # Average readings
            RSSIs[f] = np.mean(RSSIs[f])

        # Show readings
        Logger.debug(lib.JSONize(RSSIs))

        # Check if pump was detected
        if not all(f == -99 for f in RSSIs.values()):

            # Optimize frequency
            f = self.getBestFrequency(RSSIs)

            # Store it
            self.storeBestFrequency(f)

        # Otherwise: pump does not respond
        else:
            raise errors.NoPump

        # Return it
        return f