def init(self, ser): s = SMS(PORT, BAUD, loglevel=logging.DEBUG) s.setup() if not s.turnOn(): exit(1) if not s.setEchoOff(): exit(1) print("SIM800 module initalized succesfully!")
def taskWorker(): _redis=Redis() _redis.set("sim800NetworkStatus", "Unknown") _redis.set("sim800Balance","0") _redis.set("sim800RSSI",0) logger=logging.getLogger(LOGGER) balanceRegExp=re.compile(r"£(\d){1,2}\.(\d){2}") try: sms=SMS(PORT,BAUD,logger=logger) sms.setup() if not sms.turnOn(): logger.critical("Failed to turn on SMS!") return if not sms.setEchoOff(): logger.critical("Failed to set SMS echo off!") return sms.setTime(datetime.now()) netStat="Unknown" while netStat!="Good": netStat=sms.getNetworkStatus() if netStat is not None: if netStat in (NetworkStatus.RegisteredHome, NetworkStatus.RegisteredRoaming): netStat="Good" elif netStat in (NetworkStatus.Searching,): netStat="Searching" else: netStat="Bad" else: netStat="Unknown" _redis.set("sim800NetworkStatus", netStat) checkBalance=True statusCheckTime=0. while True: if taskQueue.empty(): if checkBalance: checkBalance=False balanceMsg=sms.sendUSSD(BALANCE_USSD) logger.info("Balance message: {}".format(balanceMsg)) match=balanceRegExp.search(balanceMsg) if match is not None: balance=match.group(0) logger.info("Balance amount: {}".format(balance)) _redis.set("sim800Balance",balance) if (time.time()-statusCheckTime)>FIVE_MINUTES: rssi=sms.getRSSI() if rssi is not None: rssi=(rssi.value/4.)*100 else: rssi=0 _redis.set("sim800RSSI",rssi) netStat=sms.getNetworkStatus() if netStat is not None: if netStat in (NetworkStatus.RegisteredHome, NetworkStatus.RegisteredRoaming): netStat="Good" elif netStat in (NetworkStatus.Searching,): netStat="Searching" else: netStat="Bad" else: netStat="Unknown" _redis.set("sim800NetworkStatus", netStat) statusCheckTime=time.time() try: task=taskQueue.get(timeout=60) except Empty: continue if task is None: continue phoneNumber=task.get('phoneNumber') message=task.get('message') if phoneNumber and message: logger.info("Sending SMS: {}, {}".format(phoneNumber, message)) if sms.sendSMS(phoneNumber, message): logger.info("SMS sent successfully") checkBalance=True else: logger.error("Failed to send SMS! {}, {}".format(phoneNumber, message)) else: logger.error("Task is not valid: {}".format(task)) taskQueue.task_done() except Exception as e: logger.critical("Exception in task thread: {}".format(e)) return
def taskWorker(): _redis = Redis() _redis.set("sim800NetworkStatus", "Unknown") _redis.set("sim800Balance", "0") _redis.set("sim800RSSI", 0) logger = logging.getLogger(LOGGER) balanceRegExp = re.compile(r"£(\d){1,2}\.(\d){2}") try: sms = SMS(PORT, BAUD, logger=logger) sms.setup() if not sms.turnOn(): logger.critical("Failed to turn on SMS!") return if not sms.setEchoOff(): logger.critical("Failed to set SMS echo off!") return sms.setTime(datetime.now()) netStat = "Unknown" while netStat != "Good": netStat = sms.getNetworkStatus() if netStat is not None: if netStat in (NetworkStatus.RegisteredHome, NetworkStatus.RegisteredRoaming): netStat = "Good" elif netStat in (NetworkStatus.Searching, ): netStat = "Searching" else: netStat = "Bad" else: netStat = "Unknown" _redis.set("sim800NetworkStatus", netStat) checkBalance = True statusCheckTime = 0. while True: if taskQueue.empty(): if checkBalance: checkBalance = False balanceMsg = sms.sendUSSD(BALANCE_USSD) logger.info("Balance message: {}".format(balanceMsg)) match = balanceRegExp.search(balanceMsg) if match is not None: balance = match.group(0) logger.info("Balance amount: {}".format(balance)) _redis.set("sim800Balance", balance) if (time.time() - statusCheckTime) > FIVE_MINUTES: rssi = sms.getRSSI() if rssi is not None: rssi = (rssi.value / 4.) * 100 else: rssi = 0 _redis.set("sim800RSSI", rssi) netStat = sms.getNetworkStatus() if netStat is not None: if netStat in (NetworkStatus.RegisteredHome, NetworkStatus.RegisteredRoaming): netStat = "Good" elif netStat in (NetworkStatus.Searching, ): netStat = "Searching" else: netStat = "Bad" else: netStat = "Unknown" _redis.set("sim800NetworkStatus", netStat) statusCheckTime = time.time() try: task = taskQueue.get(timeout=60) except Empty: continue if task is None: continue phoneNumber = task.get('phoneNumber') message = task.get('message') if phoneNumber and message: logger.info("Sending SMS: {}, {}".format(phoneNumber, message)) if sms.sendSMS(phoneNumber, message): logger.info("SMS sent successfully") checkBalance = True else: logger.error("Failed to send SMS! {}, {}".format( phoneNumber, message)) else: logger.error("Task is not valid: {}".format(task)) taskQueue.task_done() except Exception as e: logger.critical("Exception in task thread: {}".format(e)) return