aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 3c60739e26fab952cc84de177b4859e9260ad762 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
import pathlib
import os

from argparse import ArgumentParser
from e3372 import WebAPI, SMSHandler, SMS


config_dir = os.path.join(pathlib.Path.home(), '.e3372-sms-handler')
trusted_phone = ''


def sms_handler(sms: SMS, api: WebAPI):
    global trusted_phone

    print(f'from={sms.phone}, date={sms.date}, text={sms.text}')

    # just in case
    phone = sms.phone
    if phone.startswith('8') and len(phone) == 11:
        phone = '+7' + phone[1:]
    elif phone.startswith('7') and len(phone) == 11:
        phone = '+' + phone

    if phone == trusted_phone:
        print('this is a trusted phone, processing...')

        text = sms.text.lower().strip()
        if text == 'you shall reboot!':
            print('bye bye...')
            api.reboot()

        elif text == 'yo, get me some status':
            print('gathering status')
            api.auth()
            info = api.device_information()
            signal = api.device_signal()
            buf = []

            for key, value in info.items():
                if key in ('workmode', 'WanIPAddress'):
                    buf.append(f'{key}={value}')

            for key, value in signal.items():
                if key in ('cell_id', 'rssi', 'rscp', 'ecio', 'mode'):
                    buf.append(f'{key}={value}')

            buf = ' '.join(buf)
            if buf != '':
                print('going to send this: ' + buf)
                # we need new api key it seems :O
                api.auth()
                api.send_sms(phone=trusted_phone, content=buf)

        elif text == 'switch it off':
            print('switching it off')
            api.dataswitch(False)

        elif text == 'switch it on':
            print('switching it on')
            api.dataswitch(True)


def main():
    global trusted_phone

    # parse arguments
    parser = ArgumentParser()
    parser.add_argument('--ip',
                        default='192.168.8.1',
                        help='Modem IP address')
    parser.add_argument('--trusted-phone',
                        help='Trusted phone number')
    args = parser.parse_args()

    # set global trusted_phone
    trusted_phone = args.trusted_phone

    # webapi client
    client = WebAPI(args.ip)
    client.auth()

    # sms handler
    smshandler = SMSHandler(api=client, config_dir=config_dir)
    smshandler.process(sms_handler)


if __name__ == '__main__':
    main()