def removeClient(clientname): try: if (check_special(clientname) == "reject@wronginput"): raise ParameterError(1001) os._exit(0) if clientname == "": raise ParameterError(1011) os._exit(0) url = "http://" + server + ":" + port + "/api/removeclient" headers = {'content-type': 'application/json'} data = {'cl_name': clientname} params = "{}" response = requests.post(url, params=data, headers=headers) return json.loads(response.text) except requests.exceptions.ConnectionError: print "Unable to connect with Port " + port + " . Please restart the simulator" os._exit(0) except ParameterError as e: if (e.args[0] == 1011): return "%s \n%d: %s" % (e.bm[0], e.args[0], e.pm[e.args[0]]) else: return "%s %s \n%d: %s" % (e.bm[0], e.em[0], e.args[0], e.pm[e.args[0]]) except Exception as e: return e.args[0]
def getPublishDetails(clientname): try: if (check_special(clientname) == "reject@wronginput"): raise ParameterError(1001) os._exit(0) if clientname == "": raise ParameterError(1011) os._exit(0) url = "http://" + server + ":" + port + "/api/get/publishdetails?client_name=" + clientname response = requests.get(url) return json.loads(response.text) except requests.exceptions.ConnectionError: print "Unable to connect with Port " + port + " . Please restart the simulator" os._exit(0) except ParameterError as e: if (e.args[0] == 1011): return "%s \n%d: %s" % (e.bm[0], e.args[0], e.pm[e.args[0]]) else: return "%s %s \n%d: %s" % (e.bm[0], e.em[0], e.args[0], e.pm[e.args[0]]) except Exception as e: pass
def addClient(clientname, desc="description", clean_session=0, will_flag=0, willtopic="", willmessage="", willqos=1, willretain=0): try: if (check_special(clientname) == "reject@wronginput"): raise ParameterError(1001) os._exit(0) if (check_special(desc) == "reject@wronginput"): raise ParameterError(1101) os._exit(0) if clientname == "": raise ParameterError(1011) os._exit(0) if (will_flag == 1): if (willtopic == "" or willmessage == ""): raise ParameterError(1103) elif (check_special(willtopic) == "reject@wronginput") or (check_special(willmessage) == "reject@wronginput"): raise ParameterError(1013) url = "http://" + server + ":" + port + "/api/addclient" headers = {'content-type': 'application/json'} data = { 'cl_name': clientname, 'description': desc, 'clean_session': clean_session, 'will_flag': will_flag, 'willtopic': willtopic, 'willmessage': willmessage, 'willqos': willqos, 'willretain': willretain } params = "{}" response = requests.post(url, params=data, headers=headers) return json.loads(response.text) except requests.exceptions.ConnectionError: print "Unable to connect with Port " + port + " . Please restart the simulator" os._exit(0) except ParameterError as e: if (e.args[0] == 1011): return "%s \n%d : %s" % (e.bm[0], e.args[0], e.pm[e.args[0]]) else: return "%s %d : %s" % (e.bm[0], e.args[0], e.pm[e.args[0]]) except Exception as e: return e.args[0]
def subscribe(clientname="", topic="", qos=0, addtodb="false"): try: if (clientname == "" and topic == "" and qos == 0 and addtodb == "false"): url = "http://" + server + ":" + port + "/api/subscribe" headers = {'content-type': 'application/json'} data = {'cl_name': clientname, 't': topic} response = requests.post(url, params=data, headers=headers) else: if (check_special(clientname) == "reject@wronginput"): raise ParameterError(1001) os._exit(0) if (check_special(topic) == "reject@wronginput"): raise ParameterError(1002) os._exit(0) try: qos = int(qos) if (qos < 0 or qos > 2): raise ParameterError(1104) os._exit(0) except: raise ParameterError(1004) os._exit(0) if not (addtodb.upper() == "TRUE" or addtodb.upper() == "FALSE"): print "Addtodb: Expecting true or false" os._exit(0) url = "http://" + server + ":" + port + "/api/subscribeclient" headers = {'content-type': 'application/json'} data = { 'cl_name': clientname, 't': topic, 'q': qos, 'adddb': addtodb } params = "{}" response = requests.post(url, params=data, headers=headers) return json.loads(response.text) except requests.exceptions.ConnectionError: print "Unable to connect with Port " + port + " . Please restart the simulator" os._exit(0) except ParameterError as e: return "%s %s \n%d: %s" % (e.bm[0], e.em[0], e.args[0], e.pm[e.args[0]]) except Exception as e: return e.args[0]
def addNewPublishMessage(clientname, topic, message, qos=0, retain=0): try: if (check_special(clientname) == "reject@wronginput"): raise ParameterError(1001) os._exit(0) if (check_special(topic) == "reject@wronginput"): raise ParameterError(1002) os._exit(0) if (check_special(message) == "reject@wronginput"): raise ParameterError(1003) os._exit(0) try: qos = int(qos) if (qos < 0 or qos > 2): raise ParameterError(1004) os._exit(0) except: raise ParameterError(1104) os._exit(0) try: retain = int(retain) if (retain < 0 or qos > 1): raise ParameterError(1105) os._exit(0) except: raise ParameterError(1005) os._exit(0) if clientname == "" or topic == "" or message == "": raise ParameterError(1011) os._exit(0) url = "http://" + server + ":" + port + "/api/addpublish" headers = {'content-type': 'application/json'} data = { 'cl_name': clientname, 't': topic, 'm': message, 'q': qos, 'r': retain } params = "{}" response = requests.post(url, params=data, headers=headers) return json.loads(response.text) except requests.exceptions.ConnectionError: print "Unable to connect with Port " + port + " . Please restart the simulator" os._exit(0) except ParameterError as e: if (e.args[0] == 1011): return "%s \n%d: %s" % (e.bm[0], e.args[0], e.pm[e.args[0]]) else: return "%s %s \n%d: %s" % (e.bm[0], e.em[0], e.args[0], e.pm[e.args[0]]) except Exception as e: return 'BW-IoT 1012: Parameter Type mismatch'
def publish(clientname="", topic="", message="", qos=0, retain=1, addtodb="false"): try: if (clientname == "" and topic == "" and message == "" and qos == 0 and retain == 1 and addtodb == "false"): url = "http://" + server + ":" + port + "/api/publish" headers = {'content-type': 'application/json'} response = requests.post(url, headers=headers) else: if (check_special(clientname) == "reject@wronginput"): raise ParameterError(1001) os._exit(0) if (check_special(topic) == "reject@wronginput"): raise ParameterError(1002) os._exit(0) if (check_special(message) == "reject@wronginput"): raise ParameterError(1003) os._exit(0) if (clientname != ""): if (topic != ""): if (message == ""): return "Message to be published is missing" os._exit(0) else: return "Topic cannot be empty" os._exit(0) try: qos = int(qos) if (qos < 0 or qos > 2): raise ParameterError(1104) os._exit(0) except: raise ParameterError(1004) os._exit(0) try: retain = int(retain) if (retain < 0 or retain > 1): raise ParameterError(1105) os._exit(0) except: raise ParameterError(1005) os._exit(0) if not (addtodb.upper() == "TRUE" or addtodb.upper() == "FALSE"): return "Addtodb: Expecting true or false" os._exit(0) url = "http://" + server + ":" + port + "/api/publishclient" headers = {'content-type': 'application/json'} data = { 'cl_name': clientname, 't': topic, 'm': message, 'q': qos, 'r': retain, 'adddb': addtodb } params = "{}" response = requests.post(url, params=data, headers=headers) return json.loads(response.text) except requests.exceptions.ConnectionError: print "Unable to connect with Port " + port + " . Please restart the simulator" os._exit(0) except ParameterError as e: return "%s %s \n%d: %s" % (e.bm[0], e.am[0], e.args[0], e.pm[e.args[0]]) except Exception as e: return e.args[0]