From 58695542e354bc563779ab92e5215467bcca26d7 Mon Sep 17 00:00:00 2001 From: Eneko Nieto Date: Sat, 2 Mar 2024 19:33:35 +0100 Subject: [PATCH] feat: split CallManager to own file --- call_manager.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ intercom.py | 59 ++----------------------------------------------- 2 files changed, 59 insertions(+), 57 deletions(-) create mode 100644 call_manager.py diff --git a/call_manager.py b/call_manager.py new file mode 100644 index 0000000..a4aa6f2 --- /dev/null +++ b/call_manager.py @@ -0,0 +1,57 @@ +import socket + +NO_CALL = 0 +INCOMING_CALL = 1 +OUTGOING_CALL = 2 +OP_CALL = 'CALL'.encode() +OP_HANG = 'HANG'.encode() +OP_PING = 'PING'.encode() +OP_OK = 'OK'.encode() +OP_ERROR = 'ERROR'.encode() + +class CallManager: + state = NO_CALL + station = None + call_process = None + + def call(self, station): + try: + self.call_process = subprocess.Popen(['parole', '-c', station['host'], '-d', config['audio device']]) + s = socket.socket() + s.connect((station['host'], station['port'])) + s.send(OP_CALL) + response = s.recv(1024) + print('Call notice sent', 'response=', response.decode()) + s.close() + self.state = OUTGOING_CALL + self.station = station + station['red'].on() + except Exception as e: + print('Error al realizar la llamada: ', e) + station['green'].off() + self.call_process.terminate() + self.call_process.communicate() + + def incoming_call(self, station): + self.state = INCOMING_CALL + self.station = station + station['red'].on() + + def hang(self): + print('Hang! self.station', self.station) + if self.station is not None: + self.station['red'].off() + if self.call_process is not None: + self.call_process.terminate() + self.call_process.communicate() + self.state = NO_CALL + self.station = None + + def notify_hang(self): + if self.station is not None: + s = socket.socket() + s.connect((self.station['host'], self.station['port'])) + s.send(OP_HANG) + response = s.recv(1024) + print('Hang notify sent', 'response=', response.decode()) + s.close() diff --git a/intercom.py b/intercom.py index bdd4285..31dd950 100644 --- a/intercom.py +++ b/intercom.py @@ -7,65 +7,10 @@ import socket import yaml import time -NO_CALL = 0 -INCOMING_CALL = 1 -OUTGOING_CALL = 2 -OP_CALL = 'CALL'.encode() -OP_HANG = 'HANG'.encode() -OP_PING = 'PING'.encode() -OP_OK = 'OK'.encode() -OP_ERROR = 'ERROR'.encode() +from call_manager import CallManager, NO_CALL, INCOMING_CALL, OUTGOING_CALL, OP_CALL, OP_HANG, OP_PING, OP_OK, OP_ERROR + CHECK_STATUS_INTERVAL = 120 - -class CallManager: - state = NO_CALL - station = None - call_process = None - - def call(self, station): - try: - self.call_process = subprocess.Popen(['parole', '-c', station['host'], '-d', config['audio device']]) - s = socket.socket() - s.connect((station['host'], station['port'])) - s.send(OP_CALL) - response = s.recv(1024) - print('Call notice sent', 'response=', response.decode()) - s.close() - self.state = OUTGOING_CALL - self.station = station - station['red'].on() - except Exception as e: - print('Error al realizar la llamada: ', e) - station['green'].off() - self.call_process.terminate() - self.call_process.communicate() - - def incoming_call(self, station): - self.state = INCOMING_CALL - self.station = station - station['red'].on() - - def hang(self): - print('Hang! self.station', self.station) - if self.station is not None: - self.station['red'].off() - if self.call_process is not None: - self.call_process.terminate() - self.call_process.communicate() - self.state = NO_CALL - self.station = None - - def notify_hang(self): - if self.station is not None: - s = socket.socket() - s.connect((self.station['host'], self.station['port'])) - s.send(OP_HANG) - response = s.recv(1024) - print('Hang notify sent', 'response=', response.decode()) - s.close() - - callManager = CallManager()