Skip to content

Commit

Permalink
output readable again
Browse files Browse the repository at this point in the history
  • Loading branch information
DietrichChristopeit committed May 16, 2021
1 parent 870da83 commit 7a14277
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 216 deletions.
12 changes: 6 additions & 6 deletions MainProgs/Experiment_CMDs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding=utf-8
"""
"""
MainProgs.Experiment_CMDS
=========================
Expand All @@ -10,7 +9,7 @@
Here some example action sequences are defined that the Lego(c) Model should perform.
It could be used as a template for other experiments.
It could be used as a template for other oldExperiments.
Basic Structure:
................
Expand Down Expand Up @@ -125,7 +124,8 @@ async def main():
time_to_stalled=0.2,
stall_bias=0.2,
clockwise=MOVEMENT.CLOCKWISE,
debug=True, )
debug=True,
)

FWD: SingleMotor = SingleMotor(name='FWD',
server=('127.0.0.1', 8888),
Expand All @@ -148,7 +148,7 @@ async def main():
# Connect the devices with the Server and make them get notifications

try:
connectdevices = await e.setupConnectivity(devices=[HUB, STR, FWD, RWD, FWD_RWD])
connectdevices = await e.setupConnectivity(devices=[HUB, STR]) #, STR, FWD, RWD, FWD_RWD])
except TimeoutError:
prg_out_msg(f"SETUP TIMED OUT", MESSAGE_STATUS.FAILED)
return
Expand Down Expand Up @@ -227,7 +227,7 @@ async def main():
cmd_id='0° mid 2.')
prg_out_msg(f"JUST CHECKING '0°' 2.: POS IN DEG: \t {STR.port_value.m_port_value_DEG}")

await RWD.START_SPEED_TIME(5000, CW(80), on_stalled=RWD.STOP(cmd_id='RWD STOP', cmd_debug=True), cmd_id='RWD SPEED TIME')
await RWD.START_SPEED_TIME(5000, CW(80), on_stalled=RWD.STOP(cmd_id='RWD STOP', debug=True), cmd_id='RWD SPEED TIME')

while True:
prg_out_msg(f"JUST CHECKING '0° LEFT': POS IN DEG: \t {STR.last_value.m_port_value_DEG}")
Expand Down
File renamed without changes.
86 changes: 43 additions & 43 deletions legoBTLE/device/ADevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ class ADevice(ABC):
"""

async def _delay_before(self, delay: float, when: str = 'n', cmd_id: str = f"DELAY BEFORE/AFTER SEND",
dbg_cmd: bool = False):
debug: bool = False):
if delay is not None:
if str.lower(when) == 'n':
_when = 'NO DELAY'
debug_info(f"[{self.name}:{self.port}].{cmd_id} delay {_when} is set to {delay}: IGNORE DELAY",
debug=dbg_cmd)
debug=debug)
return
elif str.lower(when) == 'b':
_when = 'BEFORE'
msg_a = debug_info_begin(f"[{self.name}:{self.port}].{cmd_id} delay {_when} is set to {delay}: ",
debug=dbg_cmd)
debug=debug)
msg_b = debug_info_end(f"[{self.name}:{self.port}].{cmd_id} delay {_when} is set to {delay}: ",
debug=dbg_cmd)
debug=debug)
elif str.lower(when) == 'a':
_when = 'AFTER'
msg_a = debug_info_begin(f"[{self.name}:{self.port}].{cmd_id} delay {_when} is set to {delay}: ",
debug=dbg_cmd)
debug=debug)
msg_b = debug_info_end(f"[{self.name}:{self.port}].{cmd_id} delay {_when} is set to {delay}: ",
debug=dbg_cmd)
debug=debug)
else:
raise ValueError

debug_info_begin(msg_a, debug=dbg_cmd)
debug_info_begin(msg_a, debug=debug)
await sleep(delay)
debug_info_end(msg_b, debug=dbg_cmd)
debug_info_end(msg_b, debug=debug)
return True

@property
Expand Down Expand Up @@ -482,7 +482,7 @@ def ext_srv_notification(self) -> EXT_SERVER_NOTIFICATION:
raise NotImplementedError

@abstractmethod
async def ext_srv_notification_set(self, ext_srv_notification: EXT_SERVER_NOTIFICATION, cmd_debug: bool):
async def ext_srv_notification_set(self, ext_srv_notification: EXT_SERVER_NOTIFICATION, debug: bool = False):
raise NotImplementedError

@property
Expand All @@ -493,7 +493,7 @@ async def EXT_SRV_DISCONNECT_REQ(self,
delay_before: float = None,
delay_after: float = None,
cmd_id: str = 'EXT_SRV_DISCONNECT_REQ',
dbg_cmd: bool = None,
debug: bool = None,
) -> bool:
"""Request to disconnect this device from the server.
Expand All @@ -505,7 +505,7 @@ async def EXT_SRV_DISCONNECT_REQ(self,
Fractional seconds to delay return from this method.
cmd_id : str, optional
An arbitrary id to identify this method (e.g. in debugging message).
dbg_cmd : bool, optional
debug : bool, optional
Switch on/off debug message specifically for this method.
If `None`, the setting at object creation decides.
Expand All @@ -519,36 +519,36 @@ async def EXT_SRV_DISCONNECT_REQ(self,
ConnectionError, IncompleteReadError
"""
dbg_cmd = self.debug if dbg_cmd is None else dbg_cmd
debug = self.debug if debug is None else debug

command = CMD_EXT_SRV_DISCONNECT_REQ(port=self.port)

debug_info_header(f"[{self.name}:{self.port}] {C.OKBLUE}{C.BOLD} +++ {cmd_id} +++ {C.ENDC}", debug=dbg_cmd)
debug_info_header(f"[{self.name}:{self.port}] {C.OKBLUE}{C.BOLD} +++ {cmd_id} +++ {C.ENDC}", debug=debug)
if self.ext_srv_disconnected.set():
debug_info(f"[{self.name}:{self.port}] +++ {cmd_id}: ALREADY DISCONNECTED", debug=dbg_cmd)
debug_info_footer(f"[{self.name}:{self.port}] {C.OKBLUE}{C.BOLD}+++ {cmd_id} +++ {C.ENDC}", debug=dbg_cmd)
debug_info(f"[{self.name}:{self.port}] +++ {cmd_id}: ALREADY DISCONNECTED", debug=debug)
debug_info_footer(f"[{self.name}:{self.port}] {C.OKBLUE}{C.BOLD}+++ {cmd_id} +++ {C.ENDC}", debug=debug)
return True # already disconnected
else:
if delay_before is not None:
debug_info_begin(f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_BEFORE / {self.name} "
f" WAITING FOR {delay_before}", debug=dbg_cmd)
f" WAITING FOR {delay_before}", debug=debug)

await sleep(delay_before)

debug_info_end(f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_BEFORE / {self.name} "
f"WAITING FOR {delay_before}", debug=dbg_cmd)
f"WAITING FOR {delay_before}", debug=debug)

debug_info_begin(f"{cmd_id} +++ [{self.name}:{self.port}]: SEND CMD: {command.COMMAND.hex()}",
debug=dbg_cmd)
debug=debug)

s = await self._cmd_send(command)

debug_info_end(f"{cmd_id} +++ [{self.name}:{self.port}]: SEND CMD: {command.COMMAND.hex()}",
debug=dbg_cmd)
debug=debug)
if not s:
debug_info(f"{cmd_id} +++ [{self.name}:{self.port}]: Sending CMD_EXT_SRV_DISCONNECT_REQ: failed",
debug=dbg_cmd)
debug_info_footer(f"{cmd_id} +++ [{self.name}:{self.port}]", debug=dbg_cmd)
debug=debug)
debug_info_footer(f"{cmd_id} +++ [{self.name}:{self.port}]", debug=debug)
raise ConnectionError(f"[{self.name}:??]- [MSG]: UNABLE TO ESTABLISH CONNECTION... aborting...")
else:
try:
Expand All @@ -558,23 +558,23 @@ async def EXT_SRV_DISCONNECT_REQ(self,
debug_info(
f"{cmd_id} +++ [{self.name}:{self.port}]: Sending CMD_EXT_SRV_DISCONNECT_REQ: failed... "
f"Server didn't answer... (->{ire.args})",
debug=dbg_cmd)
debug_info_footer(f"{cmd_id} +++ [{self.name}:{self.port}]", debug=dbg_cmd)
debug=debug)
debug_info_footer(f"{cmd_id} +++ [{self.name}:{self.port}]", debug=debug)
raise ire
else:
UpStreamMessageBuilder(data=data, debug=dbg_cmd).build()
UpStreamMessageBuilder(data=data, debug=debug).build()
if delay_after is not None:
debug_info_begin(
f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_AFTER / WAITING FOR {delay_after}",
debug=dbg_cmd)
debug=debug)

await sleep(delay_after)

debug_info_end(
f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_AFTER / WAITING FOR {delay_after}",
debug=dbg_cmd)
debug=debug)

debug_info_footer(f"{cmd_id} +++ [{self.name}:{self.port}]", debug=dbg_cmd)
debug_info_footer(f"{cmd_id} +++ [{self.name}:{self.port}]", debug=debug)
return s

async def RESET(self,
Expand All @@ -583,7 +583,7 @@ async def RESET(self,
delay_before: float = None,
delay_after: float = None,
cmd_id: str = None,
dbg_cmd: bool = None,
debug: bool = None,
) -> bool:
"""Resets the current device.
Expand All @@ -597,7 +597,7 @@ async def RESET(self,
delay_before :
delay_after :
cmd_id :
dbg_cmd : bool, optional
debug : bool, optional
Switch on/off debug message specifically for this method.
If `None`, the setting at object creation decides.
Expand All @@ -607,48 +607,48 @@ async def RESET(self,
True if all is good, False otherwise.
"""
dbg_cmd = self.debug if dbg_cmd is None else dbg_cmd
debug = self.debug if debug is None else debug

command = CMD_HW_RESET(port=self.port)

debug_info_header(f"THE {cmd_id} +++ [{self.name}:{self.port}]", debug=dbg_cmd)
debug_info_header(f"THE {cmd_id} +++ [{self.name}:{self.port}]", debug=debug)
debug_info(f"{cmd_id} +++ [{self.name}:{self.port}]: RESET AT THE GATES... \t{C.WARNING}WAITING...{C.ENDC}",
debug=dbg_cmd)
debug=debug)

self.port_free.clear()

debug_info(f"{cmd_id} +++ [{self.name}:{self.port}]: RESET AT THE GATES... \t{C.OKBLUE}PASS... {C.ENDC}",
debug=dbg_cmd)
debug=debug)

if delay_before is not None:
debug_info_begin(f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_BEFORE", debug=dbg_cmd)
debug_info_begin(f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_BEFORE", debug=debug)
debug_info(f"{cmd_id} +++ [{self.name}:{self.port}]: DELAY_BEFORE... WAITING FOR {delay_before}..."
f"{C.BOLD}{C.OKBLUE}START{C.ENDC}", debug=dbg_cmd)
f"{C.BOLD}{C.OKBLUE}START{C.ENDC}", debug=debug)
await sleep(delay_before)
debug_info(f"DELAY_BEFORE / {C.WARNING}{self.name} {C.WARNING} WAITING FOR {delay_before}... "
f"{C.BOLD}{C.OKGREEN}DONE{C.ENDC}", debug=dbg_cmd)
f"{C.BOLD}{C.OKGREEN}DONE{C.ENDC}", debug=debug)

debug_info_begin(f"{self.name}.RESET({self.port[0]}) SENDING {command.COMMAND.hex()}...", dbg_cmd)
debug_info_begin(f"{self.name}.RESET({self.port[0]}) SENDING {command.COMMAND.hex()}...", debug)

if wait_cond:
wcd = asyncio.create_task(self._on_wait_cond_do(wait_cond=wait_cond))
await asyncio.wait({wcd}, timeout=wait_cond_timeout)

s = await self._cmd_send(command)

debug_info_end(f"{self.name}.RESET({self.port[0]}) SENDING COMPLETE...", dbg_cmd)
debug_info_end(f"{self.name}.RESET({self.port[0]}) SENDING COMPLETE...", debug)

if delay_after is not None:

debug_info_begin(f"DELAY_AFTER / {C.WARNING}{self.name} "
f"{C.WARNING}WAITING FOR {delay_after}... "
f"{C.BOLD}{C.OKBLUE}START{C.ENDC}", debug=dbg_cmd)
f"{C.BOLD}{C.OKBLUE}START{C.ENDC}", debug=debug)

await sleep(delay_after)

debug_info_begin("DELAY_AFTER / {C.WARNING}{self.name} "
f"{C.WARNING}WAITING FOR {delay_after}... "
f"{C.BOLD}{C.OKGREEN}DONE{C.ENDC}", debug=dbg_cmd)
f"{C.BOLD}{C.OKGREEN}DONE{C.ENDC}", debug=debug)
self.port_free.set()
return s

Expand All @@ -658,7 +658,7 @@ async def REQ_PORT_NOTIFICATION(self,
delay_before: float = None,
delay_after: float = None,
cmd_id: str = 'REQ_PORT_NOTIFICATION',
cmd_debug: bool = None,
debug: bool = None,
) -> bool:
"""Request to receive notifications for the device's Port.
Expand All @@ -679,7 +679,7 @@ async def REQ_PORT_NOTIFICATION(self,
await self.port_free.wait()
self.port_free.clear()

await self._delay_before(delay=delay_before, dbg_cmd=cmd_debug)
await self._delay_before(delay=delay_before, debug=debug)

# _wait_until part
if waitUntilCond is not None:
Expand Down Expand Up @@ -871,7 +871,7 @@ async def _dispatch_return_data(self, data: bytearray) -> bool:
"""
RETURN_MESSAGE = UpStreamMessageBuilder(data, debug=True).build()
if RETURN_MESSAGE.m_header.m_type == MESSAGE_TYPE.UPS_DNS_EXT_SERVER_CMD:
await self.ext_srv_notification_set(RETURN_MESSAGE, cmd_debug=self.debug)
await self.ext_srv_notification_set(RETURN_MESSAGE, debug=self.debug)
elif RETURN_MESSAGE.m_header.m_type == MESSAGE_TYPE.UPS_PORT_VALUE:
await self.port_value_set(RETURN_MESSAGE)
elif RETURN_MESSAGE.m_header.m_type == MESSAGE_TYPE.UPS_PORT_CMD_FEEDBACK:
Expand Down
Loading

0 comments on commit 7a14277

Please sign in to comment.