SipPhoneCallCallBack.__init__()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
logger = logging.getLogger(__name__)
6
logger.debug("%s loaded", __name__)
7
8
import threading
0 ignored issues
show
Unused Code introduced by Thomas Meissner
The import threading seems to be unused.
Loading history...
9
import datetime
0 ignored issues
show
Unused Code introduced by Thomas Meissner
The import datetime seems to be unused.
Loading history...
10
import time
0 ignored issues
show
Unused Code introduced by Thomas Meissner
The import time seems to be unused.
Loading history...
11
import os
0 ignored issues
show
Unused Code introduced by Thomas Meissner
The import os seems to be unused.
Loading history...
12
import pjsua as pj
13
from doorpi import DoorPi
14
15
class SipPhoneCallCallBack(pj.CallCallback):
16
17
    Lib = None
18
19
    __DTMF = ''
20
    __possible_DTMF = []
21
22
    def __init__(self, PlayerID = None, call = None):
0 ignored issues
show
Unused Code introduced by Thomas Meissner
The argument call seems to be unused.
Loading history...
23
        logger.debug("__init__")
24
        self.PlayerID = PlayerID
25
        self.Lib = pj.Lib.instance()
26
27
        DoorPi().event_handler.register_event('OnCallMediaStateChange', __name__)
28
        DoorPi().event_handler.register_event('OnCallStateChange', __name__)
29
        DoorPi().event_handler.register_event('OnCallStateConnect', __name__)
30
        DoorPi().event_handler.register_event('AfterCallStateConnect', __name__)
31
        DoorPi().event_handler.register_event('OnCallStateDisconnect', __name__)
32
        DoorPi().event_handler.register_event('AfterCallStateDisconnect', __name__)
33
        DoorPi().event_handler.register_event('OnCallStateDismissed', __name__)
34
        DoorPi().event_handler.register_event('OnCallStart', __name__)
35
        DoorPi().event_handler.register_event('OnDTMF', __name__)
36
37
        self.__possible_DTMF = DoorPi().config.get_keys('DTMF')
38
        for DTMF in self.__possible_DTMF:
39
            DoorPi().event_handler.register_event('OnDTMF_'+DTMF, __name__)
40
41
        DoorPi().event_handler('OnCallStart', __name__)
42
43
    def __del__(self):
44
        self.destroy()
45
46
    def destroy(self):
47
        logger.debug("destroy")
48
        DoorPi().event_handler.unregister_source(__name__, True)
49
50
    def on_media_state(self):
51
        logger.debug("on_media_state (%s)",str(self.call.info().media_state))
52
        DoorPi().event_handler('OnCallMediaStateChange', __name__, {
53
            'remote_uri': self.call.info().remote_uri,
54
            'media_state': str(self.call.info().media_state)
55
        })
56
57
    def on_state(self):
58
        logger.debug("on_state (%s)", self.call.info().state_text)
59
        DoorPi().event_handler('OnCallStateChange', __name__, {
60
            'remote_uri': self.call.info().remote_uri,
61
            'state': self.call.info().state_text
62
        })
63
64 View Code Duplication
        if self.call.info().state in [pj.CallState.CONFIRMED] \
0 ignored issues
show
Duplication introduced by Thomas Meissner
This code seems to be duplicated in your project.
Loading history...
65
        and self.call.info().media_state == pj.MediaState.ACTIVE:
66
            DoorPi().event_handler('OnCallStateConnect', __name__, {
67
                'remote_uri': self.call.info().remote_uri
68
            })
69
            call_slot = self.call.info().conf_slot
70
            # Connect the call to each side
71
            self.Lib.conf_connect(call_slot, 0)
72
            self.Lib.conf_connect(0, call_slot)
73
            logger.debug("conneted Media to call_slot %s",str(call_slot))
74
75
            DoorPi().event_handler('AfterCallStateConnect', __name__, {
76
                'remote_uri': self.call.info().remote_uri
77
            })
78
79 View Code Duplication
        if self.call.info().state == pj.CallState.DISCONNECTED:
0 ignored issues
show
Duplication introduced by Thomas Meissner
This code seems to be duplicated in your project.
Loading history...
80
            call_slot = self.call.info().conf_slot
81
82
            # If conf_slot is not greater than -1, the call has not yet been accepted
83
            if call_slot > -1:
84
                DoorPi().event_handler('OnCallStateDisconnect', __name__, {
85
                    'remote_uri': self.call.info().remote_uri
86
                })
87
                self.Lib.conf_disconnect(call_slot, 0)
88
                self.Lib.conf_disconnect(0, call_slot)
89
                logger.debug("disconneted Media from call_slot %s",str(call_slot))
90
                DoorPi().event_handler('AfterCallStateDisconnect', __name__, {
91
                    'remote_uri': self.call.info().remote_uri
92
                })
93
            else:
94
                DoorPi().event_handler('OnCallStateDismissed', __name__, {
95
                    'remote_uri': self.call.info().remote_uri
96
                })
97
98
99
    def on_dtmf_digit(self, digits):
100
        logger.debug("on_dtmf_digit (%s)",str(digits))
101
102
        self.__DTMF += str(digits)
103
        for DTMF in self.__possible_DTMF:
104
            if self.__DTMF.endswith(DTMF[1:-1]):
105
                DoorPi().event_handler('OnDTMF_'+DTMF+'', __name__, {
106
                    'remote_uri': str(self.call.info().remote_uri),
107
                    'DTMF': str(self.__DTMF)
108
                })
109