forked from ToninoTarsi/swpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
76 lines (64 loc) · 2.84 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
###########################################################################
# Sint Wind PI
# Copyright 2012 by Tonino Tarsi <[email protected]>
# Modem comunication based on Slawek Ligus pyhumod-0.03 module
#
# Please refer to the LICENSE file for conditions
# Visit http://www.vololiberomontecucco.it
#
##########################################################################
"""Action functions to be taken in response to events."""
import re
# pylint: disable-msg=W0613
def call_notification(modem, message):
"""Execute when someone is calling."""
print('Someone is calling')
def call_notificationclip(modem, message):
"""Execute when someone is calling."""
print('Someone is calling')
def null_action(modem, message):
"""Take no action."""
pass
def rssi_update(modem, message):
"""Handle RSSI level change."""
modem.status.rssi = modem.get_rssi()
def flow_report_update(modem, message):
"""Update connection report."""
hex2dec = lambda h: int(h, 16)
flow_rpt = message[11:].rstrip()
values = [hex2dec(item) for item in flow_rpt.split(',', 7)]
sts = modem.status
(sts.link_uptime, sts.uplink, sts.downlink, sts.byte_tx,
sts.byte_rx) = values[0:5]
def mode_update(modem, message):
"""Update connection mode."""
# Info taken from:
# https://forge.betavine.net/pipermail/vodafonemobilec-devel/
# 2007-November/000044.html
mode_dict = {'0': 'No service', '1': 'AMPS', '2': 'CDMA', '3': 'GSM/GPRS',
'4': 'HDR', '5': 'WCDMA', '6': 'GPS'}
submode_dict = {'0': 'None', '1': 'GSM', '2': 'GPRS', '3': 'EDEG',
'4': 'WCDMA', '5': 'HSDPA', '6': 'HSUPA', '7': 'HSDPA'}
mode, submode = message[6:].strip().split(',', 1)
modem.status.mode = '%s/%s' % (mode_dict[mode], submode_dict[submode])
def new_message(modem, message):
"""New message action."""
print('New message arrived.')
PATTERN = {'incoming call': re.compile(r'^RING\r\n'),
'incoming callclip': re.compile(r'^\+CLIP:.*'),
'new sms': re.compile(r'^\+CMTI:.*'),
'rssi update': re.compile(r'^\^RSSI:.*'),
'flow report': re.compile(r'^\^DSFLOWRPT:'),
'mode update': re.compile(r'^\^MODE:.*'),
'boot update': re.compile(r'^\^BOOT:.*$'),
'new line': re.compile(r'^\r\n$'),
'empty line': re.compile(r'^$')}
STANDARD_ACTIONS = [(PATTERN['incoming call'], call_notification),
(PATTERN['incoming callclip'], call_notificationclip),
(PATTERN['new line'], null_action),
(PATTERN['empty line'], null_action),
(PATTERN['boot update'], null_action),
(PATTERN['new sms'], new_message),
(PATTERN['mode update'], mode_update),
(PATTERN['rssi update'], rssi_update),
(PATTERN['flow report'], flow_report_update)]