driver: Update send_cmd fn to use msg_type params

This commit is contained in:
linuswck 2024-04-10 16:48:04 +08:00
parent ceb003e07e
commit 784b8a357b
1 changed files with 22 additions and 38 deletions

View File

@ -132,6 +132,7 @@ class Device:
Example of yielded data:: Example of yielded data::
{ {
'ts': 227657, # Relative Timestamp (ms) 'ts': 227657, # Relative Timestamp (ms)
'msg_type': 'Report' # Indicate it is a 'Report' json object
'laser': { 'laser': {
'pwr_on': False, # Laser Power is On (True/False) 'pwr_on': False, # Laser Power is On (True/False)
'pwr_excursion': False, # Was Laser experienced an Overpowered Condition? (True/False) 'pwr_excursion': False, # Was Laser experienced an Overpowered Condition? (True/False)
@ -154,38 +155,30 @@ class Device:
} }
} }
""" """
retry = 0 return await self._send_cmd(TARGET_DEVICE, "GetStatusReport", msg_type="Report")
while(retry < 3):
response = await self._send_cmd(TARGET_DEVICE, "GetStatusReport")
if response["msg_type"] != "Acknowledge":
return response
status_report = await self._read_response()
if "ts" in status_report:
return status_report
else:
retry += 1
return None
async def get_settings_summary(self): async def get_settings_summary(self):
""" """
Get the current settings of laser and thermostat in a json object Get the current settings of laser and thermostat in a json object
{ {
'msg_type': 'Settings', # Indicate it is a 'Settings' json object
'laser': { 'laser': {
default_pwr_on': False, # Power On Laser Diode at Startup 'default_pwr_on': False, # Power On Laser Diode at Startup
ld_drive_current': { # Laser Diode Output Current(A) 'ld_drive_current': { # Laser Diode Output Current(A)
'value': 0.0, # Value Set 'value': 0.0, # Value Set
'max': 0.3 # Max Value Settable 'max': 0.3 # Max Value Settable
, ,
ld_drive_current_limit': { # Laser Diode Software Current Limit(A) 'ld_drive_current_limit': { # Laser Diode Software Current Limit(A)
'value': 0.3, # Value Set 'value': 0.3, # Value Set
'max': 0.3 # Max Value Settable 'max': 0.3 # Max Value Settable
, ,
pd_responsitivity': { # Laser Diode Software Current Limit(A) 'pd_mon_params': { # Laser Diode Software Current Limit(A)
'responsitivity': None, # Value Set 'responsitivity': None, # Value Set
'i_dark': 0.0 # Max Value Settable 'i_dark': 0.0 # Max Value Settable
, ,
ld_pwr_limit': 0.0 # Laser Diode Power Limit(W) 'ld_pwr_limit': 0.0 # Laser Diode Power Limit(W)
'ld_terms_short: False # Is Laser Diode Terminals short? (True/False)
}, },
'thermostat': { 'thermostat': {
'default_pwr_on': True, # Power on Thermostat at Startup 'default_pwr_on': True, # Power on Thermostat at Startup
@ -236,11 +229,7 @@ class Device:
} }
} }
""" """
return await self._send_cmd(TARGET_DEVICE, "GetSettingsSummary", msg_type="Settings")
response = await self._send_cmd(TARGET_DEVICE, "GetSettingsSummary")
if response["msg_type"] != "Acknowledge":
return response
return await self._read_response()
async def dfu(self): async def dfu(self):
""" """
@ -255,7 +244,7 @@ class Device:
""" """
return await self._send_cmd(TARGET_DEVICE, "SaveFlashSettings") return await self._send_cmd(TARGET_DEVICE, "SaveFlashSettings")
async def load_current_settings_to_flash(self): async def load_current_settings_from_flash(self):
""" """
Restore the laser diode and thermostat settings from flash Restore the laser diode and thermostat settings from flash
""" """
@ -616,11 +605,6 @@ class Kirdy:
"""Returns True if client is connected""" """Returns True if client is connected"""
return self._writer is not None return self._writer is not None
async def _cmd(self, *cmd):
async with self._cmd_lock:
# protect the read-write process from being cancelled midway
line = await asyncio.shield(self._read_write(cmd))
async def end_session(self): async def end_session(self):
"""End session to Kirdy if connected, cancel connection if connecting""" """End session to Kirdy if connected, cancel connection if connecting"""
if self._connecting_task is not None: if self._connecting_task is not None:
@ -656,37 +640,37 @@ class Kirdy:
pass pass
return { "msg_type": "Internal No json object found in response" } return { "msg_type": "Internal No json object found in response" }
async def _send_raw_cmd_handler(self, cmd, lock=True): async def _send_raw_cmd_handler(self, cmd, lock=True, msg_type="Acknowledge"):
if lock: if lock:
async with self._cmd_lock: async with self._cmd_lock:
return await asyncio.shield(self._send_raw_cmd(cmd)) return await asyncio.shield(self._send_raw_cmd(cmd, msg_type))
else: else:
return await asyncio.shield(self._send_raw_cmd(cmd)) return await asyncio.shield(self._send_raw_cmd(cmd, msg_type))
# If the cmd involves a cmd specific data type, # If the cmd involves a cmd specific data type,
# checking is done separately within the functions being called # checking is done separately within the functions being called
async def _send_raw_cmd(self, cmd): async def _send_raw_cmd(self, cmd, msg_type):
retry = 0 retry = 0
while retry < 10: while retry < 10:
self._writer.write(bytes(json.dumps(cmd), "UTF-8")) self._writer.write(bytes(json.dumps(cmd), "UTF-8"))
await self._writer.drain() await self._writer.drain()
response = await self._read_response() response = await self._read_response()
try: try:
if response["msg_type"] == "Acknowledge": if response["msg_type"] == msg_type:
return response return response
except: except:
retry += 1 retry += 1
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
raise NoAckRecv raise NoAckRecv
async def _send_cmd_handler(self, target, cmd, data=None, lock=True): async def _send_cmd_handler(self, target, cmd, data=None, msg_type="Acknowledge", lock=True):
if lock: if lock:
async with self._cmd_lock: async with self._cmd_lock:
return await asyncio.shield(self._send_cmd(target, cmd, data)) return await asyncio.shield(self._send_cmd(target, cmd, data, msg_type))
else: else:
return await asyncio.shield(self._send_cmd(target, cmd, data)) return await asyncio.shield(self._send_cmd(target, cmd, data, msg_type))
async def _send_cmd(self, target, cmd, data): async def _send_cmd(self, target, cmd, data, msg_type):
cmd_dict = {} cmd_dict = {}
if not(target in self._cmd_list.keys()) or not(cmd in self._cmd_list[target].keys()): if not(target in self._cmd_list.keys()) or not(cmd in self._cmd_list[target].keys()):
@ -715,7 +699,7 @@ class Kirdy:
await self._writer.drain() await self._writer.drain()
response = await self._read_response() response = await self._read_response()
try: try:
if response["msg_type"] == "Acknowledge": if response["msg_type"] == msg_type:
return response return response
except: except:
retry += 1 retry += 1