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::
{
'ts': 227657, # Relative Timestamp (ms)
'msg_type': 'Report' # Indicate it is a 'Report' json object
'laser': {
'pwr_on': False, # Laser Power is On (True/False)
'pwr_excursion': False, # Was Laser experienced an Overpowered Condition? (True/False)
@ -154,38 +155,30 @@ class Device:
}
}
"""
retry = 0
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
return await self._send_cmd(TARGET_DEVICE, "GetStatusReport", msg_type="Report")
async def get_settings_summary(self):
"""
Get the current settings of laser and thermostat in a json object
{
'msg_type': 'Settings', # Indicate it is a 'Settings' json object
'laser': {
default_pwr_on': False, # Power On Laser Diode at Startup
ld_drive_current': { # Laser Diode Output Current(A)
'default_pwr_on': False, # Power On Laser Diode at Startup
'ld_drive_current': { # Laser Diode Output Current(A)
'value': 0.0, # Value Set
'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
'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
'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': {
'default_pwr_on': True, # Power on Thermostat at Startup
@ -236,11 +229,7 @@ class Device:
}
}
"""
response = await self._send_cmd(TARGET_DEVICE, "GetSettingsSummary")
if response["msg_type"] != "Acknowledge":
return response
return await self._read_response()
return await self._send_cmd(TARGET_DEVICE, "GetSettingsSummary", msg_type="Settings")
async def dfu(self):
"""
@ -255,7 +244,7 @@ class Device:
"""
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
"""
@ -616,11 +605,6 @@ class Kirdy:
"""Returns True if client is connected"""
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):
"""End session to Kirdy if connected, cancel connection if connecting"""
if self._connecting_task is not None:
@ -656,37 +640,37 @@ class Kirdy:
pass
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:
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:
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,
# 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
while retry < 10:
self._writer.write(bytes(json.dumps(cmd), "UTF-8"))
await self._writer.drain()
response = await self._read_response()
try:
if response["msg_type"] == "Acknowledge":
if response["msg_type"] == msg_type:
return response
except:
retry += 1
await asyncio.sleep(0.1)
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:
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:
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 = {}
if not(target in self._cmd_list.keys()) or not(cmd in self._cmd_list[target].keys()):
@ -715,14 +699,14 @@ class Kirdy:
await self._writer.drain()
response = await self._read_response()
try:
if response["msg_type"] == "Acknowledge":
if response["msg_type"] == msg_type:
return response
except:
retry += 1
await asyncio.sleep(0.1)
raise NoAckRecv
async def report_mode(self, report_interval = 0.0, buffer_size = 16384):
async def report_mode(self, report_interval = 0.0, buffer_size=16384):
"""
Start reporting device status in json object. Optional report_interval can be added to discard unwanted samples.
Only the latest status report received within the buffer is returned.