some improvements while network was down.....

This commit is contained in:
Evgeny Zinoviev 2021-03-23 04:14:34 +03:00
parent b8c7b0b6ef
commit bb955f2546
3 changed files with 61 additions and 12 deletions

View File

@ -1,10 +1,25 @@
# e3372-py
E3372 SMS handler written in Python. Uses the hilink web interface API. Allows
you to do something when SMS arrives.
## Requirements
* lxml (python3-lxml)
* lxml (`apt install python3-lxml` or something like it)
* see `requirements.txt`
## Usage
You need at least Python 3.6 or so.
See `main.py` and adjust the `sms_handler` function to your needs.
The script should be launched periodically by cron. This line in crontab would
launch the script every 10 minutes:
```cron
*/10 * * * * python3 /path/to/e3372-py/main.py --trusted-phone 79001234567
```
## License
BSD-2c

View File

@ -72,7 +72,7 @@ class WebAPI:
sms = SMS(
index=int(message.find('Index').get_text()),
phone=message.find('Phone').get_text(),
content=message.find('Content').get_text(),
text=message.find('Content').get_text(),
date=message.find('Date').get_text()
)
sms_list.append(sms)
@ -156,9 +156,11 @@ class SMSHandler:
def process(self, handler: Callable):
state = self.read_state()
messages = self.api.get_sms(10, 1)
max_ts = state['last_timestamp']
for sms in messages:
# loop backwards
messages = self.api.get_sms(10, 1)
for sms in reversed(messages):
ts = sms.timestamp()
if state['last_timestamp'] > ts:
continue
@ -167,7 +169,7 @@ class SMSHandler:
max_ts = ts
try:
handler(sms)
handler(sms, self.api)
except:
traceback.print_exc()
continue
@ -178,10 +180,10 @@ class SMSHandler:
class SMS:
def __init__(self, index=None, phone=None, content=None, date=None):
def __init__(self, index=None, phone=None, text=None, date=None):
self.index = index
self.phone = phone
self.content = content
self.text = text
self.date = date
def timestamp(self):

42
main.py
View File

@ -2,30 +2,62 @@ import pathlib
import os
from argparse import ArgumentParser
from pprint import pprint
from e3372 import WebAPI, SMSHandler, SMS
config_dir = os.path.join(pathlib.Path.home(), '.e3372-sms-handler')
trusted_phone = ''
def sms_handler(sms: SMS):
def sms_handler(sms: SMS, api: WebAPI):
global trusted_phone
print(f'from: {sms.phone}')
print(f'text: {sms.content}')
print(f'text: {sms.text}')
if sms.phone == trusted_phone:
text = sms.text.lower().strip()
if text == 'you shall reboot':
api.reboot()
elif text == 'show me some status':
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 != '':
api.send_sms(phone=trusted_phone, content=buf)
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('--phone')
parser.add_argument('--content')
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)