Skip to content

Commit

Permalink
correct ChargingSessionData published format
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyilin123 authored and craig8 committed Aug 12, 2024
1 parent 6b77994 commit 87650e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ def read_only_check(self):
raise IOError("Trying to write to a point configured read only: {0}".format(self.attribute_name))
return True

def get_last_non_none_value(self,lst):
"""
Depends on port number, the result could be a list with None value
get last non-None value as result
"""
for item in reversed(lst):
if item is not None:
return item
return None

def get_register(self, result, method, port_flag=True):
"""Gets correct register from API response.
Expand All @@ -150,9 +160,10 @@ def get_register(self, result, method, port_flag=True):
:return: Correct register value cast to appropriate python type. Returns None if there is an error.
"""
try:
value = getattr(result, self.attribute_name)(self.port)[0] \
_log.debug(f'In get_register, to get {self.attribute_name}, the port_flag is {port_flag}')
value = self.get_last_non_none_value(getattr(result, self.attribute_name)(self.port)) \
if port_flag \
else getattr(result, self.attribute_name)(None)[0]
else self.get_last_non_none_value(getattr(result, self.attribute_name)(None))
return self.sanitize_output(self.data_type, value)
except cps.CPAPIException as exception:
if exception._responseCode not in ['153']:
Expand Down Expand Up @@ -386,7 +397,8 @@ def value(self):
result.wait()

# Of Note, due to API limitations, port number is ignored for these calls
return self.get_register(result.value, method, False)
# NOTE: Change this port number for Chargingsession data.
return self.get_register(result.value, method)

@value.setter
def value(self, x):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,17 @@ def get_port_value(port_number, data, attribute):
if flag:
logger.warning("Station does not have a definition for port {0}".format(port_number))
else:
logger.warning("Response does not have Ports defined")
return None
if (attribute in ['sessionID', 'startTime', 'endTime', 'Energy', 'rfidSerialNumber', 'driverAccountNumber',
'driverName']) and int(data['portNumber']) == port_number:
try:
data_attribute = data[attribute]
return data_attribute
except:
logger.warning(f'Response does not have {attribute} field')
return None
else:
logger.warning("Response does not have Ports defined")
return None

@staticmethod
def check_output(attribute, parent_dict):
Expand All @@ -441,6 +450,7 @@ def get_attr_from_response(name_string, response, portNum=None):
else CPAPIResponse.is_not_found(name_string))
else:
list.append(CPAPIResponse.get_port_value(portNum, item, name_string))
logger.debug(f'{name_string} list for {portNum} is {list}')
return list


Expand Down Expand Up @@ -632,12 +642,14 @@ def Type(self, port=None):

def startTime(self, port=None):
if port:
logger.debug(f'startTime port is {port}')
return CPAPIResponse.get_attr_from_response('startTime', self.stations, port)
else:
return [self.pricing_helper('startTime', station) for station in self.stations]

def endTime(self, port=None):
if port:
logger.debug(f'endTime port is {port}')
return CPAPIResponse.get_attr_from_response('endTime', self.stations, port)
else:
return [self.pricing_helper('endTime', station) for station in self.stations]
Expand Down

0 comments on commit 87650e1

Please sign in to comment.