diff --git a/pythondcs.py b/pythondcs.py index 9105088..bdd9425 100644 --- a/pythondcs.py +++ b/pythondcs.py @@ -21,7 +21,10 @@ def _iterjson_reads(reply): n=0 for item in ijson.items(raw, 'item'): # Convert to datetimes and floats where needed - item["startTime"] = datetime.strptime(item["startTime"],"%Y-%m-%dT%H:%M:%S%z") + try: + item["startTime"] = datetime.strptime(item["startTime"],"%Y-%m-%dT%H:%M:%S%z") + except ValueError: + item["startTime"] = datetime.strptime(item["startTime"],"%Y-%m-%dT%H:%M:%S.%f%z") if type(item["totalValue"]) == ijson.common.decimal.Decimal: item["totalValue"] = float(item["totalValue"]) if type(item["periodValue"]) == ijson.common.decimal.Decimal: @@ -36,7 +39,10 @@ def _json_reads(reply): results = reply.json() for item in results: # Convert to datetimes - item["startTime"] = datetime.strptime(item["startTime"],"%Y-%m-%dT%H:%M:%S%z") + try: + item["startTime"] = datetime.strptime(item["startTime"],"%Y-%m-%dT%H:%M:%S%z") + except ValueError: + item["startTime"] = datetime.strptime(item["startTime"],"%Y-%m-%dT%H:%M:%S.%f%z") print(f"All {len(results)} readings retreived") return results diff --git a/pythondcspro.py b/pythondcspro.py index 05e0e39..f010035 100644 --- a/pythondcspro.py +++ b/pythondcspro.py @@ -113,7 +113,7 @@ def delete_modbus_device(self, id): """Deletes the modbus device with the given 'id'.""" subpath = "/ModbusDevices/" with self.lock: - reply = self.s.delete(self.rooturl+subpath++str(int(id))) + reply = self.s.delete(self.rooturl+subpath+str(int(id))) reply.raise_for_status() print("Modbus Device Deleted Successfully") def get_meter_tree(self,id=0,recursively=True,groupsOnly=False,withoutRegister=False): @@ -145,26 +145,34 @@ def get_meter_tree(self,id=0,recursively=True,groupsOnly=False,withoutRegister=F ) reply.raise_for_status() return reply.json() - def get_calibration_reads(self,registerId): - """Retreive a list of all calibration readings for the given registerId""" + def get_calibration_reads(self,registerId, startIndex=0, maxCount=2**31-1): + """Retreive a list of calibration readings for the given registerId""" subpath = "/CalibrationReadings/" with self.lock: reply = self.s.get( self.rooturl+subpath, params = { "registerId":registerId, - "startIndex":0, - "maxCount":2**31-1, + "startIndex":startIndex, + "maxCount":maxCount, }) reply.raise_for_status() # Just get relevent parts of the object returned result = reply.json()["calibrationReadings"] # Convert the datetime strings to real datetime objects which are tz aware for item in result: - item["timestamp"] = datetime.strptime( - item["timestamp"],"%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc) - item["startTime"] = datetime.strptime( - item["startTime"],"%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc) + try: + item["timestamp"] = datetime.strptime( + item["timestamp"],"%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc) + except ValueError: + item["timestamp"] = datetime.strptime( + item["timestamp"],"%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc) + try: + item["startTime"] = datetime.strptime( + item["startTime"],"%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc) + except ValueError: + item["startTime"] = datetime.strptime( + item["startTime"],"%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc) return result def get_meters_by_idc(self, macAddress): """Returns a list of all meters defined in DCS (excluding registers)