sms-receiver/agent/agent.py
2019-07-16 15:12:57 +02:00

39 lines
900 B
Python

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()
messages_queue.extend(messages)
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)