Commit 8b4110b1 authored by David Meredith's avatar David Meredith

Ensure writer is always closed, make _send_request priv

parent a87bb8cb
Pipeline #1275 canceled with stages
...@@ -202,7 +202,7 @@ class ClientPlots(QtWidgets.QMainWindow): ...@@ -202,7 +202,7 @@ class ClientPlots(QtWidgets.QMainWindow):
client = HEVClient(polling=False) # just use hevclient for requests client = HEVClient(polling=False) # just use hevclient for requests
await asyncio.sleep(2) await asyncio.sleep(2)
# trigger an alarm # trigger an alarm
await client.send_request("CMD", cmdtype="SET_THRESHOLD_MIN", cmd="APNEA",param=0) await client._send_request("CMD", cmdtype="SET_THRESHOLD_MIN", cmd="APNEA", param=0)
while True: while True:
await asyncio.sleep(60) await asyncio.sleep(60)
except Exception as e: except Exception as e:
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
# for more details. # for more details.
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with hev-sw. If not, see <http://www.gnu.org/licenses/>. # with hev-sw. If not, see <http://www.gnu.org/licenses/> .
# #
# The authors would like to acknowledge the much appreciated support # The authors would like to acknowledge the much appreciated support
# of all those involved with the High Energy Ventilator project # of all those involved with the High Energy Ventilator project
...@@ -96,6 +96,8 @@ class HEVClient(object): ...@@ -96,6 +96,8 @@ class HEVClient(object):
async def polling(self) -> None: async def polling(self) -> None:
"""open persistent connection with server""" """open persistent connection with server"""
writer = None
data = None
while True: while True:
try: try:
reader, writer = await asyncio.open_connection("127.0.0.1", 54320) reader, writer = await asyncio.open_connection("127.0.0.1", 54320)
...@@ -150,9 +152,6 @@ class HEVClient(object): ...@@ -150,9 +152,6 @@ class HEVClient(object):
except KeyError: except KeyError:
raise raise
# close connection
writer.close()
await writer.wait_closed()
except ConnectionRefusedError as e: except ConnectionRefusedError as e:
logging.error(str(e) + " - is the microcontroller running?") logging.error(str(e) + " - is the microcontroller running?")
await asyncio.sleep(2) await asyncio.sleep(2)
...@@ -160,12 +159,17 @@ class HEVClient(object): ...@@ -160,12 +159,17 @@ class HEVClient(object):
# warn and reopen connection # warn and reopen connection
logging.error(e) logging.error(e)
await asyncio.sleep(2) await asyncio.sleep(2)
finally:
# close connection
if writer is not None:
writer.close()
await writer.wait_closed()
def get_updates(self, payload) -> None: def get_updates(self, payload) -> None:
"""Overrideable function called after receiving data from the socket, with that data as an argument""" """Overrideable function called after receiving data from the socket, with that data as an argument"""
pass pass
async def send_request( async def _send_request(
self, self,
reqtype, reqtype,
cmdtype: str = None, cmdtype: str = None,
...@@ -225,19 +229,19 @@ class HEVClient(object): ...@@ -225,19 +229,19 @@ class HEVClient(object):
# print(cmdtype, cmd, param) # print(cmdtype, cmd, param)
try: try:
return asyncio.run( return asyncio.run(
self.send_request("CMD", cmdtype=cmdtype, cmd=cmd, param=param) self._send_request("CMD", cmdtype=cmdtype, cmd=cmd, param=param)
) )
except ConnectionRefusedError as error: except ConnectionRefusedError as error:
logging.error(str(error) + " - is the microcontroller running?") logging.error(str(error) + " - is the microcontroller running?")
def ack_alarm(self, alarm: str) -> bool: def ack_alarm(self, alarm: str) -> bool:
# acknowledge alarm to remove it from the hevserver list # acknowledge alarm to remove it from the hevserver list
return asyncio.run(self.send_request("ALARM", alarm=alarm)) return asyncio.run(self._send_request("ALARM", alarm=alarm))
# def send_personal(self, personal: Dict[str, str]=None ) -> bool: # def send_personal(self, personal: Dict[str, str]=None ) -> bool:
def send_personal(self, personal: str) -> bool: def send_personal(self, personal: str) -> bool:
# acknowledge alarm to remove it from the hevserver list # acknowledge alarm to remove it from the hevserver list
return asyncio.run(self.send_request("PERSONAL", personal=personal)) return asyncio.run(self._send_request("PERSONAL", personal=personal))
def get_values(self) -> Dict: def get_values(self) -> Dict:
# get sensor values from db # get sensor values from db
...@@ -246,12 +250,12 @@ class HEVClient(object): ...@@ -246,12 +250,12 @@ class HEVClient(object):
fastdata = pickle.load(self._mmFile) fastdata = pickle.load(self._mmFile)
except pickle.UnpicklingError as e: except pickle.UnpicklingError as e:
logging.warning(f"Unpicking error {e}") logging.warning(f"Unpicking error {e}")
return None return None # Should return empty dict here?
if type(fastdata) is dict: if type(fastdata) is dict:
return fastdata return fastdata
else: else:
logging.warning("Missing fastdata") logging.warning("Missing fastdata")
return None return None # Should return empty dict here?
def get_readback(self) -> Dict: def get_readback(self) -> Dict:
# get readback from db # get readback from db
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment