Skip to content

Commit

Permalink
Timestamp handling tweaks
Browse files Browse the repository at this point in the history
Mainly tweaks to be tollerant of timestmps with fractional seconds, and a few minor tweaks to the pro module
  • Loading branch information
jarvisms committed Jul 2, 2019
1 parent 2b2e674 commit a3fda2d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
10 changes: 8 additions & 2 deletions pythondcs.py
Expand Up @@ -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:
Expand All @@ -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

Expand Down
26 changes: 17 additions & 9 deletions pythondcspro.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a3fda2d

Please sign in to comment.