コード例 #1
0
    def conduct(self):
        if self.verbose:
            print("RTC...", file=sys.stderr)

        try:
            I2C.open(Host.I2C_SENSORS)

            # resources...
            now = LocalizedDatetime.now()

            DS1338.init()

            # test...
            rtc_datetime = RTCDatetime.construct_from_localized_datetime(now)
            DS1338.set_time(rtc_datetime)

            time.sleep(2)

            rtc_datetime = DS1338.get_time()
            localized_datetime = rtc_datetime.as_localized_datetime(
                tzlocal.get_localzone())

            self.datum = localized_datetime - now

            if self.verbose:
                print(self.datum, file=sys.stderr)

            # test criterion...
            return 1 <= self.datum.seconds <= 2

        finally:
            I2C.close()
コード例 #2
0
    def find_for_topic(self, topic, start_date, end_date, batch_pause=0.0):
        request_path = '/v1/messages/topic/' + topic

        total = 0
        collection = []

        # request...
        self.__rest_client.connect()

        try:
            for batch in self.__find(request_path, start_date, end_date):
                collection.extend(batch)

                if self.__verbose:
                    now = LocalizedDatetime.now()
                    batch_count = len(batch)
                    total += batch_count

                    print("%s: batch: %d total: %d" %
                          (now.as_iso8601(), batch_count, total),
                          file=sys.stderr)
                    sys.stderr.flush()

                time.sleep(batch_pause)  # prevent "Rate limit exceeded" error

        finally:
            self.__rest_client.close()

        return collection
コード例 #3
0
    def sample(self):
        r = self.__read(self.__CMD_READ_MEASURED_VALUES, 0, 60)

        # density...
        pm1 = Decode.float(r[0:4], '>')
        pm2p5 = Decode.float(r[4:8], '>')
        pm4 = Decode.float(r[8:12], '>')
        pm10 = Decode.float(r[12:16], '>')

        # count...
        pm0p5_count = Decode.float(r[16:20], '>')
        pm1_count = Decode.float(r[20:24], '>')
        pm2p5_count = Decode.float(r[24:28], '>')
        pm4_count = Decode.float(r[28:32], '>')
        pm10_count = Decode.float(r[32:36], '>')

        # typical size...
        tps = Decode.float(r[36:40], '>')

        # time...
        rec = LocalizedDatetime.now()

        # report...
        counts = SPSDatumCounts(pm0p5_count, pm1_count, pm2p5_count, pm4_count,
                                pm10_count)

        return SPSDatum(self.SOURCE, rec, pm1, pm2p5, pm4, pm10, counts, tps)
コード例 #4
0
    def print_status(self, status):
        if not self.__verbose:
            return

        now = LocalizedDatetime.now()
        print("%s:         mqtt: %s" % (now.as_iso8601(), status),
              file=sys.stderr)
        sys.stderr.flush()
コード例 #5
0
    def sample(self):
        try:
            self.obtain_lock()
            self._spi.open()

            # command...
            self.__cmd(self.__CMD_READ_HISTOGRAM)
            chars = self.__read_bytes(62)

            # time...
            rec = LocalizedDatetime.now()

            # bins...
            bins = [Decode.unsigned_int(chars[i:i + 2], '<') for i in range(0, 32, 2)]

            # bin MToFs...
            bin_1_mtof = chars[32]
            bin_3_mtof = chars[33]
            bin_5_mtof = chars[34]
            bin_7_mtof = chars[35]

            # period...
            period = Decode.float(chars[44:48], '<')

            # checksum...
            required = Decode.unsigned_int(chars[48:50], '<')
            actual = sum(bins) % 65536

            if required != actual:
                raise ValueError("bad checksum: required: 0x%04x actual: 0x%04x" % (required, actual))

            # PMx...
            try:
                pm1 = Decode.float(chars[50:54], '<')
            except TypeError:
                pm1 = None

            try:
                pm2p5 = Decode.float(chars[54:58], '<')
            except TypeError:
                pm2p5 = None

            try:
                pm10 = Decode.float(chars[58:62], '<')
            except TypeError:
                pm10 = None

            return OPCDatum(self.SOURCE, rec, pm1, pm2p5, pm10, period, bins,
                            bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof)

        finally:
            self._spi.close()
            self.release_lock()
コード例 #6
0
    def sample(self):
        try:
            self.obtain_lock()
            self.__spi.open()

            self.__spi.xfer([OPCN2.__CMD_READ_HISTOGRAM])
            time.sleep(OPCN2.__CMD_DELAY)

            # bins...
            bins = [None] * 16

            for i in range(16):
                bins[i] = self.__read_int()

            # bin MToFs...
            bin_1_mtof = self.__read_byte()
            bin_3_mtof = self.__read_byte()
            bin_5_mtof = self.__read_byte()
            bin_7_mtof = self.__read_byte()

            # flow rate...
            self.__spi.read_bytes(4)

            # temperature
            self.__spi.read_bytes(4)

            # period...
            period = self.__read_float()

            # checksum...
            self.__read_int()

            # PMx...
            pm1 = self.__read_float()
            pm2p5 = self.__read_float()
            pm10 = self.__read_float()

            now = LocalizedDatetime.now()

            return OPCDatum(now, pm1, pm2p5, pm10, period, bins, bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof)

        finally:
            time.sleep(OPCN2.__CMD_DELAY)

            self.__spi.close()
            self.release_lock()
コード例 #7
0
    def save(self, host):
        if self.__set_on is None:
            self.__set_on = LocalizedDatetime.now()

        super().save(host)
コード例 #8
0
        cmd.print_help(sys.stderr)
        exit(2)

    if cmd.verbose:
        print(cmd, file=sys.stderr)
        sys.stderr.flush()

    # ----------------------------------------------------------------------------------------------------------------
    # resources...

    conf = TimezoneConf.load(Host)

    # ----------------------------------------------------------------------------------------------------------------
    # run...

    now = LocalizedDatetime.now()

    if cmd.list:
        for zone in Timezone.zones():
            print(zone, file=sys.stderr)
        exit(0)

    elif cmd.set():
        if not Timezone.is_valid(cmd.zone):
            print("zone is not valid: %s" % cmd.zone, file=sys.stderr)
            exit(1)

        if cmd.zone != conf.name:
            conf = TimezoneConf(now, cmd.zone)
            conf.save(Host)
コード例 #9
0
            print(api_auth, file=sys.stderr)
            sys.stderr.flush()

        # message manager...
        message_manager = MessageManager(HTTPClient(), api_auth.api_key,
                                         cmd.verbose)

        if cmd.verbose:
            print(message_manager, file=sys.stderr)

        # ------------------------------------------------------------------------------------------------------------
        # run...

        # time...
        if cmd.use_offset():
            end = LocalizedDatetime.now()
            start = end.timedelta(minutes=-cmd.minutes)
        else:
            end = LocalizedDatetime.now() if cmd.end is None else cmd.end
            start = cmd.start

        if cmd.verbose:
            print("start: %s" % start, file=sys.stderr)
            print("end: %s" % end, file=sys.stderr)
            sys.stderr.flush()

        # messages...
        messages = message_manager.find_for_topic(cmd.path, start, end)

        for message in messages:
            document = message if cmd.include_wrapping else message.payload
コード例 #10
0
    def sample(self):
        try:
            self.obtain_lock()
            self._spi.open()

            # command...
            self.__cmd(self.__CMD_READ_HISTOGRAM)
            chars = self.__read_bytes(86)

            # checksum...
            required = Decode.unsigned_int(chars[84:86], '<')
            actual = ModbusCRC.compute(chars[:84])

            if required != actual:
                raise ValueError(
                    "bad checksum: required: 0x%04x actual: 0x%04x" %
                    (required, actual))

            # time...
            rec = LocalizedDatetime.now()

            # bins...
            bins = [
                Decode.unsigned_int(chars[i:i + 2], '<')
                for i in range(0, 48, 2)
            ]

            # bin MToFs...
            bin_1_mtof = chars[48]
            bin_3_mtof = chars[49]
            bin_5_mtof = chars[50]
            bin_7_mtof = chars[51]

            # period...
            raw_period = Decode.unsigned_int(chars[52:54], '<')
            period = round(float(raw_period) / 100.0, 3)

            # temperature & humidity
            raw_temp = Decode.unsigned_int(chars[56:58], '<')
            raw_humid = Decode.unsigned_int(chars[58:60], '<')

            sht = SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp))

            # PMx...
            try:
                pm1 = Decode.float(chars[60:64], '<')
            except TypeError:
                pm1 = None

            try:
                pm2p5 = Decode.float(chars[64:68], '<')
            except TypeError:
                pm2p5 = None

            try:
                pm10 = Decode.float(chars[68:72], '<')
            except TypeError:
                pm10 = None

            return OPCDatum(self.SOURCE, rec, pm1, pm2p5, pm10, period, bins,
                            bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof,
                            sht)

        finally:
            self._spi.close()
            self.release_lock()
コード例 #11
0
    if cmd.ignore_eeprom:
        reporter.report_ignore("EEPROM")

    else:
        try:
            test = EEPROMTest(cmd.verbose)

            test_ok = test.conduct()
            reporter.report_test("EEPROM", test_ok)

        except Exception as ex:
            reporter.report_exception("EEPROM", ex)

    # ----------------------------------------------------------------------------------------------------------------
    # result...

    if cmd.verbose:
        print(reporter, file=sys.stderr)
        print(reporter.result, file=sys.stderr)
        print("-", file=sys.stderr)

    # ----------------------------------------------------------------------------------------------------------------
    # report...

    recorded = LocalizedDatetime.now()
    datum = DFETestDatum(system_id.message_tag(), recorded,
                         Host.serial_number(), cmd.dfe_serial_number,
                         reporter.subjects, afe_datum, reporter.result)

    print(JSONify.dumps(datum))
コード例 #12
0
        pub_comms.connect()

        for message in pub_comms.read():
            try:
                jdict = json.loads(message, object_pairs_hook=OrderedDict)
            except ValueError:
                continue

            publication = Publication.construct_from_jdict(jdict)

            client.publish(publication)

            if cmd.verbose:
                print("%s:         mqtt: done" %
                      LocalizedDatetime.now().as_iso8601(),
                      file=sys.stderr)
                sys.stderr.flush()

            if cmd.echo:
                print(message)
                sys.stdout.flush()

        # ----------------------------------------------------------------------------------------------------------------
        # end...

    except KeyboardInterrupt:
        if cmd.verbose:
            print("aws_mqtt_client: KeyboardInterrupt", file=sys.stderr)

    except Exception as ex:
コード例 #13
0
from scs_core.osio.manager.message_manager import MessageManager

from scs_host.client.http_client import HTTPClient

# --------------------------------------------------------------------------------------------------------------------

org_id = "south-coast-science-dev"
print(org_id)

api_key = "43308b72-ad41-4555-b075-b4245c1971db"
print(api_key)

topic = "/orgs/south-coast-science-dev/exhibition/loc/1/climate"
print(topic)

end_date = LocalizedDatetime.now()
start_date = LocalizedDatetime.construct_from_timestamp(end_date.timestamp() -
                                                        60)

print("start: %s" % start_date)
print("end: %s" % end_date)

print("-")

# --------------------------------------------------------------------------------------------------------------------

# manager...
manager = MessageManager(HTTPClient(), api_key)
print(manager)
print("=")