feat: split CallManager to own file
This commit is contained in:
57
call_manager.py
Normal file
57
call_manager.py
Normal file
@@ -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()
|
||||
59
intercom.py
59
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()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user