def construct_from_jdict(cls, jdict): if not jdict: return None if 'calibrated_on' in jdict: # TODO: deprecated date = Datum.date(jdict.get('calibrated_on')) calibrated_on = LocalizedDatetime.construct_from_date(date) else: calibrated_on = Datum.datetime(jdict.get('calibrated-on')) v20 = jdict.get('v20') return Pt1000Calib(calibrated_on, v20)
def construct_from_jdict(cls, jdict): if not jdict: return None tag = jdict.get('tag') rec = LocalizedDatetime.construct_from_iso8601(jdict.get('rec')) command = Command.construct_from_jdict(jdict.get('cmd')) omd = jdict.get('omd') digest = jdict.get('digest') datum = ControlReceipt(tag, rec, command, omd, digest) return datum
def as_localized_datetime( self, local_zone): # may be pytz timezone or datetime timezone # RTC zone... zone_offset = timedelta(hours=0, minutes=0) utc_zone = timezone(zone_offset) # localized... year = RTCDatetime.CENTURY + self.year rtc = datetime(year, self.month, self.day, self.hour, self.minute, self.second, 0, tzinfo=utc_zone) utc = LocalizedDatetime(rtc) # ...to host zone... localized = utc.localize(local_zone) return localized
def construct_from_jdict(cls, jdict): if not jdict: return None tag = jdict.get('tag') attn = jdict.get('attn') rec = LocalizedDatetime.construct_from_iso8601(jdict.get('rec')) cmd_tokens = jdict.get('cmd_tokens') digest = jdict.get('digest') datum = ControlDatum(tag, attn, rec, cmd_tokens, digest) return datum
def construct_from_jdict(cls, jdict): if not jdict: return SensorBaseline(None, 0) if 'calibrated_on' in jdict: # TODO: deprecated date = Datum.date(jdict.get('calibrated_on')) calibrated_on = LocalizedDatetime.construct_from_date(date) else: calibrated_on = Datum.datetime(jdict.get('calibrated-on')) offset = Datum.int(jdict.get('offset')) return SensorBaseline(calibrated_on, offset)
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()
def construct_from_jdict(cls, jdict): if not jdict: return None rec = LocalizedDatetime.construct_from_jdict(jdict.get('rec')) period = jdict.get('per') pm1 = jdict.get('pm1') pm2p5 = jdict.get('pm2.5') pm10 = jdict.get('pm10') bins = jdict.get('bin') bin_1_mtof = jdict.get('mtf1') bin_3_mtof = jdict.get('mtf3') bin_5_mtof = jdict.get('mtf5') bin_7_mtof = jdict.get('mtf7') return OPCDatum(rec, pm1, pm2p5, pm10, period, bins, bin_1_mtof, bin_3_mtof, bin_5_mtof, bin_7_mtof)
def midpoint(self): slope, intercept = self.compute() # validate... if slope is None: return None, None # x domain... x_data = [x for x, _ in self.__data] min_x = min(x_data) max_x = max(x_data) mid_x = min_x + ((max_x - min_x) / 2) rec = LocalizedDatetime.construct_from_timestamp(mid_x, self.__tzinfo) # y val... val = slope * float(mid_x) + intercept return rec, val
def is_valid_start(self): if self.__opts.start is None: return True return LocalizedDatetime.construct_from_iso8601( self.__opts.start) is not None
(cmd.date_path, jstr), file=sys.stderr) exit(1) if cmd.time_path not in paths: print("sample_iso_8601: time path '%s' not in %s" % (cmd.time_path, jstr), file=sys.stderr) exit(1) date = datum.node(cmd.date_path) time = datum.node(cmd.time_path) # ISO 8601... iso = LocalizedDatetime.construct_from_date_time(parser, date, time, tz=zone) if iso is None: print("sample_iso_8601: malformed datetime in %s" % jstr, file=sys.stderr) exit(1) if cmd.timezone is not None and cmd.utc: iso = iso.utc() target = PathDict() target.append(cmd.iso, iso.as_iso8601()) # copy... for path in paths:
def start(self): return LocalizedDatetime.construct_from_iso8601( self.__opts.start) if self.__opts.start else None
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)
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
def end(self): return None if self.__opts.end is None else LocalizedDatetime.construct_from_iso8601( self.__opts.end)
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:
for line in sys.stdin: try: jdict = json.loads(line) except ValueError: continue document_count += 1 try: rec = jdict['rec'] except KeyError: continue # zone shift... datetime = LocalizedDatetime.construct_from_iso8601(rec) if datetime is None: continue jdict['rec'] = datetime.localize(zone).as_iso8601() # report... print(JSONify.dumps(jdict)) sys.stdout.flush() processed_count += 1 # ---------------------------------------------------------------------------------------------------------------- # end...
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("=")
def is_valid_end(self): if self.__opts.end is None: return True return LocalizedDatetime.construct_from_iso8601( self.__opts.end) is not None
def start(self): return None if self.__opts.start is None else LocalizedDatetime.construct_from_iso8601( self.__opts.start)
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))
# ------------------------------------------------------------------------------------------------------------ # run... # start / end times... if cmd.latest: byline = byline_manager.find_latest_byline_for_topic(cmd.topic) if byline is None: exit(0) end = byline.rec start = byline.rec.timedelta(seconds=-0.1) elif cmd.timedelta: end = LocalizedDatetime.now() start = LocalizedDatetime(end - cmd.timedelta) else: end = LocalizedDatetime.now() if cmd.end is None else cmd.end start = cmd.start if cmd.verbose: print("aws_topic_history: start: %s" % start, file=sys.stderr) print("aws_topic_history: end: %s" % end, file=sys.stderr) sys.stderr.flush() # messages... try: for message in message_manager.find_for_topic( cmd.topic, start, end):
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()
@author: Bruno Beloff ([email protected]) """ from datetime import timedelta from datetime import timezone from pytz import timezone as pytz_timezone from scs_core.data.localized_datetime import LocalizedDatetime # -------------------------------------------------------------------------------------------------------------------- # run... print("now...") now = LocalizedDatetime.now() print(now) iso = now.as_iso8601() print(iso) print("-") print("from iso...") loc = LocalizedDatetime.construct_from_iso8601(iso) print(loc) iso = loc.as_iso8601() print(iso) print("-")
# run... prev_time = None for line in sys.stdin: if cmd.verbose: print(line, file=sys.stderr) datum = PathDict.construct_from_jstr(line) if datum is None: break document_count += 1 time = LocalizedDatetime.construct_from_iso8601( datum.node(cmd.path)) interval = Interval.construct(prev_time, time, cmd.precision) print(JSONify.dumps(interval)) sys.stdout.flush() prev_time = time processed_count += 1 # ---------------------------------------------------------------------------------------------------------------- # end... except KeyboardInterrupt: if cmd.verbose: print("sample_interval: KeyboardInterrupt", file=sys.stderr)
def end(self): return LocalizedDatetime.construct_from_iso8601( self.__opts.end) if self.__opts.end else None
for line in sys.stdin: # sample... datum = PathDict.construct_from_jstr(line) if datum is None: continue document_count += 1 try: rec_node = datum.node(cmd.iso) except KeyError: continue rec = LocalizedDatetime.construct_from_iso8601(rec_node) # set checkpoint... if checkpoint is None: checkpoint = generator.enclosing_localised_datetime(rec) # report and reset... if rec.datetime > checkpoint.datetime: aggregate.print(checkpoint) aggregate.reset() filler = checkpoint checkpoint = generator.enclosing_localised_datetime(rec) # fill missing... while cmd.fill:
def save(self, host): if self.__set_on is None: self.__set_on = LocalizedDatetime.now() super().save(host)
'"bin": [76, 36, 19, 6, 1, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], "mtf1": 26, "mtf3": 35, "mtf5": 26, "mtf7": 35}}}' j3 = '{"rec": "2016-10-14T14:19:25.680+01:00", "val": {"opc": {"pm1": 0.9, "pm2p5": 1.7, "pm10": 22.4, "per": 5.0, ' \ '"bin": [62, 23, 8, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], "mtf1": 20, "mtf3": 24, "mtf5": 0, "mtf7": 0}}}' # -------------------------------------------------------------------------------------------------------------------- lr = LinearRegression() print(lr) print("-") d1 = PathDict.construct_from_jstr(j1) print(d1) print("-") rec = LocalizedDatetime.construct_from_iso8601(d1.node('rec')) val = d1.node('val.opc.pm10') lr.append(rec, val) print(lr) print("-") d2 = PathDict.construct_from_jstr(j2) print(d2) print("-") rec = LocalizedDatetime.construct_from_iso8601(d2.node('rec')) val = d2.node('val.opc.pm10') lr.append(rec, val) print(lr)