import subprocess 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: def __init__(self, config): self.config = config self.state = NO_CALL self.station = None self.call_process = None def call(self, station): try: self.call_process = subprocess.Popen(['parole', '-c', station['host'], '-d', self.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()