def midpoint(self, ndigits=None): # validate... if not self.has_midpoint(): return None, None # single value... if len(self) == 1: for timestamp, value in self.__data: return LocalizedDatetime.construct_from_timestamp( timestamp, self.__tzinfo), float(value) # multiple values... slope, intercept = self.line() # x domain... x_data = [x for x, _ in self.__data] min_x = min(x_data) max_x = max(x_data) mid_x = mean((min_x, max_x)) rec = LocalizedDatetime.construct_from_timestamp(mid_x, self.__tzinfo) # y val... value = slope * float(mid_x) + intercept return rec, value if ndigits is None else round(value, ndigits)
def construct_from_jstr(cls, jstr): if not jstr: return None # document... document = PathDict.construct_from_jstr(jstr) if not document: return None # upload... upload_node = document.node(cls.UPLOAD_FIELD) upload = LocalizedDatetime.construct_from_iso8601(upload_node) if upload is None: raise ValueError(upload_node) # rec... rec_node = document.node(cls.REC_FIELD) rec = LocalizedDatetime.construct_from_iso8601(rec_node) if rec is None: raise ValueError(rec_node) # offset... td = upload - rec offset = Timedelta(days=td.days, seconds=td.seconds) return UploadInterval(upload, rec, offset)
def construct_from_jdict(cls, jdict): if not jdict: return None org = jdict.get('org') group = jdict.get('group') loc = jdict.get('loc') topic = jdict.get('topic') device = jdict.get('device') parameters = jdict.get('parameters') duration = jdict.get('duration') checkpoint = jdict.get('checkpoint') agency_code = jdict.get('agency-code') site_code = jdict.get('site-code') pocs = jdict.get('pocs') upload_start = LocalizedDatetime.construct_from_jdict( jdict.get('upload-start')) upload_end = LocalizedDatetime.construct_from_jdict( jdict.get('upload-end')) return MappingTask(org, group, loc, topic, device, parameters, duration, checkpoint, agency_code, site_code, pocs, upload_start, upload_end)
def is_valid(self): if self.client_id is None or (self.__opts.start is None and self.minutes is None): return False if self.__opts.start is not None and LocalizedDatetime.construct_from_iso8601( self.__opts.start) is None: return False if self.__opts.end is not None and LocalizedDatetime.construct_from_iso8601( self.__opts.end) is None: return False return True
def next_localised_datetime(self, localised_datetime): # parse... date_time = localised_datetime.datetime tzinfo = localised_datetime.tzinfo date = date_time.date() time = date_time.time() # compute... day_increment, next_hour, next_minute, next_second = self.next( time.hour, time.minute, time.second) # construct... checkpoint = datetime(date.year, month=date.month, day=date.day, hour=next_hour, minute=next_minute, second=next_second, tzinfo=tzinfo) if day_increment: checkpoint += timedelta(days=1) return LocalizedDatetime(checkpoint)
def print(self, status): if not self.__verbose: return now = LocalizedDatetime.now().utc() print("%s: mqtt: %s" % (now.as_time(), status), file=sys.stderr) sys.stderr.flush()
def update(self): # time... display_datetime = self.__show_time and Host.time_is_synchronized() datetime = LocalizedDatetime.now() if display_datetime else None # network... nmcli = NMCLi.find() homes = {} if nmcli is None else nmcli.connections # message... message = self.__status_message # PSU... if self.__psu_report_filename: psu_report = self.__psu_report_class.load( self.__psu_report_filename) batt_percent = None if psu_report is None else psu_report.batt_percent else: batt_percent = None # MQTT queue... if self.__queue_report_filename: queue_report = QueueReport.load(self.__queue_report_filename) queue_message = self.__QUEUE_STATE[queue_report.queue_state()] message += ':' + queue_message # GPS quality... if self.__gps_report_filename: gps_report = GPSDatum.load(self.__gps_report_filename) gps_quality = gps_report.quality message += ' GPS:' + str(gps_quality) return self.render(datetime, BattDisplay(batt_percent), homes, message)
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().utc() 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
def construct_from_jdict(cls, jdict): if not jdict: return None date = LocalizedDatetime.construct_from_iso8601(jdict.get('date')) payload = MessagePayload.construct_from_jdict(jdict.get('payload')) return Message(date, payload)
def append(self, rec: LocalizedDatetime, value): count = len(self) if count == 0: self.__start_timestamp = rec.timestamp() timestamp = rec.timestamp( ) - self.__start_timestamp if self.__time_relative else rec.timestamp( ) # remove oldest? if self.__tally is not None and count == self.__tally: del self.__data[0] # append... self.__data.append((Decimal(timestamp), Decimal(value))) self.__tzinfo = rec.tzinfo
def construct_from_jdict(cls, jdict): if not jdict: return None rec = LocalizedDatetime.construct_from_jdict(jdict.get('rec')) p_a = jdict.get('pA') return NDIRPressureDatum(rec, p_a)
def construct_from_jdict(cls, jdict): if not jdict: return None time = LocalizedDatetime.construct_from_iso8601(jdict.get('time')) period = Timedelta.construct_from_jdict(jdict.get('up')) users = int(jdict.get('users')) load = UptimeLoad.construct_from_jdict(jdict.get('load')) return UptimeDatum(time, period, users, load)
def construct_from_uri(cls, uri): if not uri: return None # parse... parse = urllib.parse.urlparse(urllib.parse.unquote(uri)) params = urllib.parse.parse_qs(parse[4]) if 'start-date' not in params or 'end-date' not in params: return None # construct... start_date = LocalizedDatetime.construct_from_iso8601(params['start-date'][0]) end_date = LocalizedDatetime.construct_from_iso8601(params['end-date'][0]) if not start_date or not end_date: return None return NextMessageQuery(start_date, end_date)
def datetime(iso_datetime): if iso_datetime is None: return None try: value = LocalizedDatetime.construct_from_iso8601(iso_datetime) except (TypeError, ValueError): return None return value
def construct_from_jdict(cls, jdict): if not jdict: return None device = jdict.get('device') topic = jdict.get('topic') rec = LocalizedDatetime.construct_from_iso8601( jdict.get('last_write')) # as provided by web API return cls(device, topic, rec)
def construct_from_jdict(cls, jdict): if not jdict: return None device = jdict.get('device') topic = jdict.get('topic') upload = LocalizedDatetime.construct_from_iso8601(jdict.get('upload')) payload = jdict.get('payload') return Message(device, topic, upload, payload)
def construct_from_jdict(cls, jdict): if not jdict: return None rec = LocalizedDatetime.construct_from_jdict(jdict.get('rec')) pile_ref = jdict.get('pile-ref') pile_act = jdict.get('pile-act') thermistor = jdict.get('therm') return NDIRMeasureVoltageDatum(rec, pile_ref, pile_act, thermistor)
def __open_file(self): self.log.timeline_start = LocalizedDatetime.now().utc() self.__clear_space() self.log.mkdir() self.__file = open(self.log.file_path(), "w") self.__writer = csv.writer(self.__file, quoting=csv.QUOTE_MINIMAL) if self.__paths: self.__writer.writerow(self.__paths)
def construct_from_message_jdict(cls, jdict): if not jdict: return None client_id = jdict.get('device') path = jdict.get('topic') earliest_publication = LocalizedDatetime.construct_from_jdict( jdict.get('date')) client_id = DeviceTopic(client_id, path, earliest_publication) return client_id
def append(self, document: PathDict): pk_value = document.node(self.pk_path) if self.pk_is_iso8601: datetime = LocalizedDatetime.construct_from_iso8601(pk_value) if datetime is None: raise ValueError(pk_value) pk_value = datetime self.__documents[pk_value] = document
def construct_from_jdict(cls, jdict): if not jdict: return None client_id = jdict.get('client-id') path = jdict.get('path') earliest_publication = LocalizedDatetime.construct_from_jdict( jdict.get('earliest-pub')) client_id = DeviceTopic(client_id, path, earliest_publication) return client_id
def sample(self): rec = LocalizedDatetime.now().utc() self.__ndir.sample() try: time.sleep(self.__interval) except KeyboardInterrupt: pass co2_datum = self.__ndir.get_sample_gas() return GasesSample(self.__tag, rec, co2_datum, None, None)
def sample(self): rec = LocalizedDatetime.now().utc() self.__ndir.sample() try: time.sleep(self.__interval) except KeyboardInterrupt: pass p_a = self.__ndir.get_sample_pressure() return NDIRPressureDatum(rec, p_a)
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') 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 sample(self): rec = LocalizedDatetime.now().utc() self.__ndir.sample() try: time.sleep(self.__interval) except KeyboardInterrupt: pass sample = self.__ndir.get_sample_voltage() voltage_datum = NDIRSampleVoltageDatum.construct_from_sample(sample) return GasesSample(self.__tag, rec, voltage_datum, None, None)
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 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 SensorBaseline(None, 0, 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')) offset = jdict.get('offset') environment = BaselineEnvironment.construct_from_jdict(jdict.get('env')) return SensorBaseline(calibrated_on, offset, environment)
def construct_from_jdict(cls, jdict, skeleton=False): if not jdict: return None calibrated_on = LocalizedDatetime.construct_from_iso8601( jdict.get('calibrated-on')) r_comp_0 = jdict.get('r-comp-0') temp_co = jdict.get('temp-co') full_cap_rep = jdict.get('full-cap-rep') full_cap_nom = jdict.get('full-cap-nom') cycles = jdict.get('cycles') return cls(calibrated_on, r_comp_0, temp_co, full_cap_rep, full_cap_nom, cycles)