agent.py 935 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import gsm
  2. import logging
  3. import requests
  4. import json
  5. def load_config():
  6. with open('config/gsm.json') as config:
  7. gsm = json.load(config)
  8. with open('config/api.json') as config:
  9. api = json.load(config)
  10. config = {
  11. "gsm": gsm,
  12. "api": api
  13. }
  14. return config
  15. config = load_config()
  16. gsm_modules = list()
  17. for module in config["gsm"]:
  18. print(module)
  19. gsm_object = gsm.Gsm(module["port"], module["msisdn"])
  20. gsm_modules.append(gsm_object)
  21. messages_queue = list()
  22. while True:
  23. for gsm_object in gsm_modules:
  24. messages = gsm_object.fetch_messages()
  25. if messages:
  26. messages_queue.extend(messages)
  27. messages = None
  28. for message in messages_queue:
  29. try:
  30. r = requests.put(config["api"]["url"], headers={"key": config["api"]["key"]}, data=json.dumps(message), timeout=10)
  31. except:
  32. logging.error("Failed pushing messages queue to frontend")
  33. if r:
  34. messages_queue.delete(message)