diff --git a/README.md b/README.md index c1580f6..f66f290 100644 --- a/README.md +++ b/README.md @@ -33,20 +33,20 @@ Numbers needs to be specified because most of the times they aren't written on t ``` [ { - "dev":"/dev/ttyUSB0", + "port":"/dev/ttyUSB0", "msisdn":"+393409821322", "imei":"990000862471854" }, { - "dev":"/dev/ttyUSB1", + "port":"/dev/ttyUSB1", "msisdn":"+443312123452" }, { - "dev":"/dev/ttyUSB2", + "port":"/dev/ttyUSB2", "msisdn":"+913446435634" }, { - "dev":"/dev/ttyUSB3", + "port":"/dev/ttyUSB3", "msisdn":"+12456345344" } ] diff --git a/agent/agent.py b/agent/agent.py index e69de29..5d8194d 100644 --- a/agent/agent.py +++ b/agent/agent.py @@ -0,0 +1,39 @@ +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) \ No newline at end of file diff --git a/agent/config/gsm.json b/agent/config/gsm.json index 8590d79..5752979 100644 --- a/agent/config/gsm.json +++ b/agent/config/gsm.json @@ -1,20 +1,20 @@ [ { - "dev":"/dev/ttyUSB0", + "port":"/dev/ttyUSB0", "msisdn":"+393409821322", "imei":"990000862471854" }, { - "dev":"/dev/ttyUSB1", + "port":"/dev/ttyUSB1", "msisdn":"+443312123452", "pin": "12345" }, { - "dev":"/dev/ttyUSB2", + "port":"/dev/ttyUSB2", "msisdn":"+913446435634" }, { - "dev":"/dev/ttyUSB3", + "port":"/dev/ttyUSB3", "msisdn":"+12456345344" } ] \ No newline at end of file diff --git a/agent/gsm/__init__.py b/agent/gsm/__init__.py index aa49d02..b7ac94b 100644 --- a/agent/gsm/__init__.py +++ b/agent/gsm/__init__.py @@ -28,7 +28,7 @@ class Gsm: r = self.cmd("ATI") if "OK" in r: return True - else + else: return False def unlock_sim(self, pin): @@ -52,10 +52,10 @@ class Gsm: r = self.cmd('AT+EMGR=1,7,"' + imei + '"') if "OK" in r: return True - else + else: return False - def check_messages(self): + def fetch_messages(self): r = self.cmd("AT+CMGL=REC UNREAD") print(r) diff --git a/agent/gsm/__init__.pyc b/agent/gsm/__init__.pyc new file mode 100644 index 0000000..b77bbfc Binary files /dev/null and b/agent/gsm/__init__.pyc differ diff --git a/agent/gsm/__pycache__/__init__.cpython-36.pyc b/agent/gsm/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..6bb47d5 Binary files /dev/null and b/agent/gsm/__pycache__/__init__.cpython-36.pyc differ