sms-receiver/agent/agent.py

41 lines
935 B
Python
Raw Permalink Normal View History

2019-07-16 15:12:57 +02:00
import gsm
import logging
import requests
import json
def load_config():
with open('config/gsm.json') as config:
gsm = json.load(config)
with open('config/api.json') as config:
api = json.load(config)
config = {
"gsm": gsm,
"api": api
}
return config
config = load_config()
gsm_modules = list()
for module in config["gsm"]:
print(module)
gsm_object = gsm.Gsm(module["port"], module["msisdn"])
gsm_modules.append(gsm_object)
messages_queue = list()
while True:
for gsm_object in gsm_modules:
messages = gsm_object.fetch_messages()
2019-07-17 15:15:43 +02:00
if messages:
messages_queue.extend(messages)
messages = None
2019-07-16 15:12:57 +02:00
for message in messages_queue:
try:
r = requests.put(config["api"]["url"], headers={"key": config["api"]["key"]}, data=json.dumps(message), timeout=10)
except:
logging.error("Failed pushing messages queue to frontend")
if r:
messages_queue.delete(message)