def send_disk_eapi(api_name, pkt_device_id, pkt_disk_index, token): r = "FAIL" pkt_send = {} thread_list = [] par = {} par['DiskIndex'] = pkt_disk_index eapi_msg = {} eapi_msg['id'] = "EAPI" eapi_msg['api'] = api_name eapi_msg['par'] = par eapi_msg['token'] = token pkt_send['id'] = "ctrl" pkt_send['message'] = [eapi_msg, ] try: jstr = json.dumps(pkt_send) Topic_db = models.Topic.objects.get(device_id=pkt_device_id) pkt_device_topic = Topic_db.control_topic # Send mqtt pub th = pub_th(pkt_device_topic, jstr, 2, 0) th.daemon = True th.start() thread_list.append(th) for i in thread_list: if i.isAlive(): i.join(10) r = "OK" except: print "send_disk_eapi error" return r
def control(request): result = [] r = "FAIL" pkt_send = {} thread_list = [] if request.method == 'POST': # send Control message to /ADLINK/SEMA/Model_Name/Device_ID/Control try: decoded = json.loads(str(request.body)) pkt_control = decoded['control'] for a in pkt_control: pkt_device_id = a.get('device_id') pkt_message = a.get('message') pkt_send['id'] = "ctrl" pkt_send['message'] = pkt_message jstr = json.dumps(pkt_send) Topic_db = models.Topic.objects.get(device_id=pkt_device_id) #pkt_device_topic = ROOT_TOPIC + Topic_db.model_name + "/" + pkt_device_id + CONTROL_TOPIC pkt_ctrl_topic = Topic_db.control_topic # Send mqtt pub th = pub_th(pkt_ctrl_topic, jstr, 2, 0) th.daemon = True th.start() thread_list.append(th) for i in thread_list: if i.isAlive(): i.join(10) r = "OK" except: print "error" #r = serializers.serialize('json', result) return HttpResponse(r, content_type="application/json")
def push_interval(request): r = "FAIL" pkt_send = {} thread_list = [] if request.method == 'POST': # send Configure message to /ADLINK/SEMA/Model_Name/Device_ID/Configure decoded = json.loads(str(request.body)) device_id = decoded.get('device_id') value = decoded.get('value') unit = decoded.get('unit') token = decoded.get('token') pkt_send['id'] = "cfg" pkt_send['message'] = [{"id":"Push_Interval","value":value,"unit":unit, "token":token}] jstr = json.dumps(pkt_send) #print "configure jstr", jstr ### Find Topic form db try: Topic_db = models.Topic.objects.get(device_id=device_id) #pkt_device_topic = ROOT_TOPIC + Topic_db.model_name + "/" + device_id + CONFIGURE_TOPIC pkt_cfg_topic = Topic_db.configure_topic ### Send mqtt pub th = pub_th(pkt_cfg_topic, jstr, 2, 0) th.daemon = True th.start() thread_list.append(th) for i in thread_list: if i.isAlive(): i.join(10) r = "OK" except: r= "FAIL" return HttpResponse(r, content_type="application/json")