def sendmsg(num, mensaje, firebase_key): if (num != ''): nums = num.split(',') for n in nums: print("numero dispositivo: " + num) log.debug("Numero de dispositivo: " + num) m = mensaje_class() m.destination = str(n) m.contents = mensaje m.status = "SENT" m.command_id = str(uuid.uuid4()) m.firebase_key = firebase_key p.storeCommand(m) sendToGateway(n, mensaje) timer = threading.Timer(config.SMS_RESPONSE_TIMEOUT, timer_expired) timer.setName(m.command_id) timer.start() print("sendmsg::Timer started for message to: " + num) log.debug("sendmsg::Timer started for message to: " + num)
def main(argv): print("Add all arguments to command queue") print("argv length: " + str(len(argv))) x = 0 while x < len(argv): m = message() m.destination = argv[x] m.contents = argv[x + 1] m.command_id = str(uuid.uuid4()) # if number foun in db, create new record wuth command and status = PENDING # else add command to queue record = p.getRecordByNumber(m.destination) if (record.count() > 0): print("Storing PENDING command for number: " + m.destination) m.status = "PENDING" p.storeCommand(m) else: command_queue.put(m) x += 2 send_mesage()
def send_mesage(): while not command_queue.empty(): # Get message from queue msg = command_queue.get() print("Getting message from queue") print("Destination: " + msg.destination) print("Message: " + msg.contents) msg.status = "SENT" print("Status: " + msg.status) print("Id: " + str(msg.command_id)) print("Sending message") print("Wait for response timeout in seconds: " + str(config.SMS_RESPONSE_TIMEOUT)) print("-----------------------------------------------") # Store message to database p.storeCommand(msg) sleep(10) print("Message to " + msg.destination) timer = threading.Timer(config.SMS_RESPONSE_TIMEOUT, timer_expired) timer.setName(msg.command_id) timer.start() #sleep(config.SMS_RESPONSE_TIMEOUT) print("Sent all messages from queue, wait for responses")
def stream_handler(message): print('event={m[event]}; path={m[path]}; data={m[data]}'.format(m=message)) if (message["event"] == "put" and message["path"] == "/"): for command in message["data"]: print("Command JEMC1: " + command) log.debug("Command JEMC1: " + command) data = message["data"][command] for key, val in data.items(): print(key, "=>", val) numero = data["number"] comando = data["command"] status = data["status"] firebase_key = command print('comando:' + comando + 'status: ' + status) log.debug('comando:' + comando + 'status: ' + status) # if number found in db, create new record with command and status = PENDING # else add command to queue record = p.getRecordByNumber(numero) if (status == "ALERT"): sendAlert(numero, comando) data = {"status": "ANSWERED"} update_firebase(firebase_key, data) elif (record.count() > 0): print("Storing PENDING command for number: " + numero) log.debug("Storing PENDING command for number: " + numero) msg = mensaje_class() msg.destination = str(numero) msg.contents = comando msg.command_id = str(uuid.uuid4()) msg.firebase_key = command msg.status = "PENDING" p.storeCommand(msg) elif (status == 'INITIAL'): sendMessage(numero, comando, firebase_key) elif (message["event"] == "put" and message["data"]): numero = message["data"]["number"] comando = message["data"]["command"] status = message["data"]["status"] record = p.getRecordByNumber(numero) # Get firebase key from path, remove the "/" path = message["path"] firebase_key = path[1:] print("JEMC2 firebase_key: " + firebase_key) log.debug("JEMC2 firebase_key: " + firebase_key) if (status == "ALERT"): sendAlert(numero, comando) data = {"status": "ANSWERED"} update_firebase(firebase_key, data) elif (record.count() > 0): print("Storing PENDING command for number: " + numero) log.debug("Storing PENDING command for number: " + numero) msg = mensaje_class() msg.destination = str(numero) msg.contents = comando msg.command_id = str(uuid.uuid4()) msg.firebase_key = firebase_key msg.status = "PENDING" p.storeCommand(msg) elif (status == 'INITIAL'): print('Command JEMC2:' + comando) log.debug('Command JEMC2:' + comando) print("Status from firebase: " + status) log.debug("Status from firebase: " + status) sendmsg(numero, comando, firebase_key) if (comando.endswith("lock")): sleep(3)