From ec9ba6c903a449dc9d455122871e0318fbe5245b Mon Sep 17 00:00:00 2001 From: Lucas Mendes Date: Tue, 5 Mar 2019 15:42:03 +0100 Subject: [PATCH] SH-332: dialplan starts notification --- cacofonisk/channel.py | 2 +- cacofonisk/handlers.py | 3 +- cacofonisk/reporters.py | 10 +++---- setup.py | 2 +- .../fixtures/simple/a_created_no_answer.json | 12 ++++++++ tests/test_reporters.py | 4 +-- tests/test_simple.py | 28 +++++++++++++++++++ tests/test_var_set.py | 2 +- 8 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 tests/fixtures/simple/a_created_no_answer.json diff --git a/cacofonisk/channel.py b/cacofonisk/channel.py index 382a3c9..b81126e 100644 --- a/cacofonisk/channel.py +++ b/cacofonisk/channel.py @@ -46,7 +46,7 @@ def __init__(self, event): self.fwd_dials = [] self.bridge = None self.is_originated = False - self.is_calling = False + self.is_calling = self.uniqueid == self.linkedid # The Custom dict can be used to store custom data. self.custom = {} diff --git a/cacofonisk/handlers.py b/cacofonisk/handlers.py index cf9eaff..b1473b7 100644 --- a/cacofonisk/handlers.py +++ b/cacofonisk/handlers.py @@ -610,6 +610,7 @@ def on_bridge_enter(self, channel, bridge): # The rest are will be marked as targets. for non_caller in sorted_callers: targets.add(non_caller) + non_caller.is_calling = False elif len(callers) < 1: # A call should always have a caller. self._logger.warning('Call {} has too few callers: {}'.format( @@ -832,7 +833,7 @@ def on_user_event(self, event): Args: event (dict): Dict-like object with all attributes of the event. """ - self._reporter.on_user_event(event) + self._reporter.on_user_event(self._channels[event['Uniqueid']].as_namedtuple(), event) def on_hangup(self, channel, event): """ diff --git a/cacofonisk/reporters.py b/cacofonisk/reporters.py index 85b1d25..9214024 100644 --- a/cacofonisk/reporters.py +++ b/cacofonisk/reporters.py @@ -94,7 +94,7 @@ def on_blind_transfer(self, caller, transferer, targets): """ pass - def on_user_event(self, event): + def on_user_event(self, caller, event): """ Handle custom UserEvent messages from Asterisk. @@ -233,7 +233,7 @@ def on_blind_transfer(self, caller, transferer, targets): self._logger.info('{} bld xfer: {} <--> {} (through {})'.format( caller.linkedid, caller, targets, transferer)) - def on_user_event(self, event): + def on_user_event(self, caller, event): """ Handle custom UserEvent messages from Asterisk. @@ -287,11 +287,11 @@ def on_event(self, event): for reporter in self.reporters: reporter.on_event(event) - def on_user_event(self, event): - super(MultiReporter, self).on_user_event(event) + def on_user_event(self, caller, event): + super(MultiReporter, self).on_user_event(caller, event) for reporter in self.reporters: - reporter.on_user_event(event) + reporter.on_user_event(caller, event) def on_b_dial(self, caller, targets): super(MultiReporter, self).on_b_dial(caller, targets) diff --git a/setup.py b/setup.py index e3a0d35..910d227 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ with open(path.join(here, 'README.md'), encoding='utf-8') as f: long_description = f.read() -version = '0.6.0' +version = '0.7.0' setup( name='cacofonisk', diff --git a/tests/fixtures/simple/a_created_no_answer.json b/tests/fixtures/simple/a_created_no_answer.json new file mode 100644 index 0000000..9435571 --- /dev/null +++ b/tests/fixtures/simple/a_created_no_answer.json @@ -0,0 +1,12 @@ +[ + {"AccountCode": "", "CallerIDName": "", "CallerIDNum": "+31150010001", "Channel": "SIP/voipgrid-siproute-docker-00000009", "ChannelState": "0", "ChannelStateDesc": "Down", "ConnectedLineName": "", "ConnectedLineNum": "", "Context": "voipgrid_in", "Event": "Newchannel", "Exten": "+31150010004", "Language": "en", "Linkedid": "e25ad522d444-1553503669.94", "Priority": "1", "Privilege": "call,all", "SystemName": "e25ad522d444", "Uniqueid": "e25ad522d444-1553503669.94", "content": ""}, + {"AccountCode": "", "CallerIDName": "", "CallerIDNum": "+31150010001", "Channel": "SIP/voipgrid-siproute-docker-00000009", "ChannelState": "4", "ChannelStateDesc": "Ring", "ConnectedLineName": "", "ConnectedLineNum": "", "Context": "voipgrid_in", "Event": "Newstate", "Exten": "+31150010004", "Language": "vg_en_UK_F", "Linkedid": "e25ad522d444-1553503669.94", "Priority": "1", "Privilege": "call,all", "SystemName": "e25ad522d444", "Uniqueid": "e25ad522d444-1553503669.94", "content": ""}, + {"AccountCode": "15001", "CallerIDName": "", "CallerIDNum": "+31150010001", "Channel": "SIP/voipgrid-siproute-docker-00000009", "ChannelState": "4", "ChannelStateDesc": "Ring", "ConnectedLineName": "", "ConnectedLineNum": "", "Context": "osvpi_world", "Event": "NewAccountCode", "Exten": "+31150010004", "Language": "vg_en_UK_F", "Linkedid": "e25ad522d444-1553503669.94", "Priority": "11", "Privilege": "call,all", "SystemName": "e25ad522d444", "Uniqueid": "e25ad522d444-1553503669.94", "content": ""}, + {"AccountCode": "15001", "CallerIDName": "", "CallerIDNum": "+31150010001", "Channel": "SIP/voipgrid-siproute-docker-00000009", "ChannelState": "4", "ChannelStateDesc": "Ring", "ConnectedLineName": "", "ConnectedLineNum": "", "Context": "osvpi_world", "Direction": "inbound", "Event": "UserEvent", "Exten": "+31150010004", "Language": "vg_en_UK_F", "Linkedid": "e25ad522d444-1553503669.94", "Priority": "16", "Privilege": "user,all", "Provider": "webhook", "SystemName": "e25ad522d444", "Uniqueid": "e25ad522d444-1553503669.94", "UserEvent": "NotifyCallstate", "WebhookUrls": "http://notifications.example.com/data", "content": ""}, + {"AccountCode": "15001", "CallerIDName": "", "CallerIDNum": "+31150010001", "Channel": "SIP/voipgrid-siproute-docker-00000009", "ChannelState": "6", "ChannelStateDesc": "Up", "ConnectedLineName": "", "ConnectedLineNum": "", "Context": "osvpi_proc_lazyanswer", "Event": "Newstate", "Exten": "+31150010004", "Language": "vg_en_UK_F", "Linkedid": "e25ad522d444-1553503669.94", "Priority": "2", "Privilege": "call,all", "SystemName": "e25ad522d444", "Uniqueid": "e25ad522d444-1553503669.94", "content": ""}, + {"BridgeCreator": "", "BridgeName": "", "BridgeNumChannels": "0", "BridgeTechnology": "simple_bridge", "BridgeType": "basic", "BridgeUniqueid": "42ef5a4b-dfa1-4a83-a29d-bd5ffc3c6ef4", "BridgeVideoSourceMode": "none", "Event": "BridgeCreate", "Privilege": "call,all", "SystemName": "e25ad522d444", "content": ""}, + {"BridgeCreator": "", "BridgeName": "", "BridgeNumChannels": "0", "BridgeTechnology": "simple_bridge", "BridgeType": "basic", "BridgeUniqueid": "597a84a3-2db7-4344-b6c4-899360e00ea0", "BridgeVideoSourceMode": "none", "Event": "BridgeCreate", "Privilege": "call,all", "SystemName": "e25ad522d444", "content": ""}, + {"BridgeCreator": "", "BridgeName": "", "BridgeNumChannels": "0", "BridgeTechnology": "simple_bridge", "BridgeType": "basic", "BridgeUniqueid": "597a84a3-2db7-4344-b6c4-899360e00ea0", "BridgeVideoSourceMode": "none", "Event": "BridgeDestroy", "Privilege": "call,all", "SystemName": "e25ad522d444", "content": ""}, + {"BridgeCreator": "", "BridgeName": "", "BridgeNumChannels": "0", "BridgeTechnology": "simple_bridge", "BridgeType": "basic", "BridgeUniqueid": "42ef5a4b-dfa1-4a83-a29d-bd5ffc3c6ef4", "BridgeVideoSourceMode": "none", "Event": "BridgeDestroy", "Privilege": "call,all", "SystemName": "e25ad522d444", "content": ""}, + {"AccountCode": "15001", "CallerIDName": "", "CallerIDNum": "+31150010001", "Cause": "0", "Cause-txt": "Unknown", "Channel": "SIP/voipgrid-siproute-docker-00000009", "ChannelState": "6", "ChannelStateDesc": "Up", "ConnectedLineName": "", "ConnectedLineNum": "", "Context": "handlers", "Event": "Hangup", "Exten": "onhangup", "Language": "vg_en_UK_F", "Linkedid": "e25ad522d444-1553503669.94", "Priority": "4", "Privilege": "call,all", "SystemName": "e25ad522d444", "Uniqueid": "e25ad522d444-1553503669.94", "content": ""} +] \ No newline at end of file diff --git a/tests/test_reporters.py b/tests/test_reporters.py index 0832a5d..126cc53 100644 --- a/tests/test_reporters.py +++ b/tests/test_reporters.py @@ -60,8 +60,8 @@ def test_on_event(self): def test_on_user_event(self): event_dict = {'Linkedid': '195176c06ab8-1529936170.42'} - self.multi_reporter.on_user_event(event_dict) - self.mock_reporter.on_user_event.assert_called_once_with(event_dict) + self.multi_reporter.on_user_event(self.a_party, event_dict) + self.mock_reporter.on_user_event.assert_called_once_with(self.a_party, event_dict) def test_on_b_dial(self): targets = [self.b_party, self.c_party] diff --git a/tests/test_simple.py b/tests/test_simple.py index aedba05..868179d 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -270,3 +270,31 @@ def test_ab_out_of_order_dial(self): ] self.assertEqual(expected_events, events) + + def test_a_created_no_answer(self): + """ + Test a simple AB call where the state is changed before the dial. + """ + fixture_file = 'fixtures/simple/a_created_no_answer.json' + events = self.run_and_get_events(fixture_file) + + calling_chan = SimpleChannel( + name='SIP/voipgrid-siproute-docker-00000009', + uniqueid='e25ad522d444-1553503669.94', + linkedid='e25ad522d444-1553503669.94', + account_code='15001', + caller_id=CallerId(name='', num='+31150010001'), + cid_calling_pres=None, + connected_line=CallerId(), + exten='+31150010004', + state=6, + ) + + expected_events = [ + ('on_hangup', { + 'caller': calling_chan, + 'reason': 'completed', + }), + ] + + self.assertEqual(expected_events, events) diff --git a/tests/test_var_set.py b/tests/test_var_set.py index aac4475..90192e6 100644 --- a/tests/test_var_set.py +++ b/tests/test_var_set.py @@ -17,7 +17,7 @@ def __init__(self): super(UserEventReporter, self).__init__() self.events = [] - def on_user_event(self, event): + def on_user_event(self, caller, event): desired = ('UserEvent', 'Provider', 'AccountCode', 'WebhookUrls', 'Direction', 'ClientId', 'AccountInternalNumber', 'UserInternalNumbers')