Esempio n. 1
0
    def mqtt_on_message(self, mqttc, obj, msg):
        print "receive TOPIC:", msg.topic
        if msg.topic == "/ADLINK/SEMA/Discover":
            try:
                decoded = json.loads(str(msg.payload))
                pkt_id = decoded.get('id')
                if pkt_id == None:
                    return

                if pkt_id == "discover":
                    print "id : discover"

                    if MY__DEBUG == False:
                        # delet all db
                        print "delet all d"
                        Topic_db = models.Topic.objects.all()
                        Topic_db.delete()
                        Register_db = models.Register.objects.all()
                        Register_db.delete()
                        print "Register db delete"
                        ConfigureAck_db = models.ConfigureAck.objects.all()
                        ConfigureAck_db.delete()
                        print "ConfigureAck db delete"
                        ControlAck_db = models.ControlAck.objects.all()
                        ControlAck_db.delete()
                        print "ControlAck db delete"

                elif pkt_id == "register":
                    print "id : register"
                    print "PAYLOAD: ", msg.payload
                    pkt_model_name = decoded.get('model_name')
                    pkt_device_id = decoded.get('device_id')
                    pkt_ipv4 = decoded.get('ipv4')
                    pkt_item = (pkt_device_id, pkt_model_name, pkt_ipv4)
                    db = models.Register(
                        device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4)
                    db.save()
                    print "Register db add :", pkt_device_id
                    pkt_device_topic = ROOT_TOPIC + pkt_model_name + "/" + pkt_device_id
                    pkt_control_topic = pkt_device_topic + CONTROL_TOPIC
                    pkt_configure_topic = pkt_device_topic + CONFIGURE_TOPIC

                elif pkt_id == "register_ack":
                    print "id : register_ack"
                else:
                    print "Unknow id :", pkt_id

            except:
                print("not json pkt")
        elif msg.topic == "/ADLINK/SEMA/PING":
            print "rev :", str(msg.payload)

        elif msg.topic.find("/ADLINK/SEMA/") == 0:
            if msg.topic.find("-") != -1:
                return
            # only in Topic DB's topic can rev
            Topic_db = models.Topic.objects.extra(
                where=['device_topic=%s OR control_topic=%s OR configure_topic=%s'],
                params=[msg.topic, msg.topic, msg.topic])

            if Topic_db.count() <= 0:
                print "Topic Illegal"
                return
            elif Topic_db.count() > 1:
                print "Duplicate Topic"
                print 'match Topic_db count = ', Topic_db.count()
                return

            pkt_device_id = ""

            for a in Topic_db:
                pkt_device_id = a.device_id
                pkt_model_name = a.model_name
                pkt_ipv4 = a.ipv4

                try:
                    decoded = json.loads(str(msg.payload))
                    pkt_id = decoded.get('id')
                    if pkt_id == "static_information":
                        payload = str(msg.payload)
                        print "id : static_information"
                        try:
                            db = models.LastSeen(
                                device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                            db.save()
                            db = models.StaticInfo(
                                device_id=pkt_device_id, json=payload, last_modified=str(int(time.time())))
                            db.save()
                            # save to mongodb
                            conn = MongoClient(MONGO_HOSTNAME, MONGO_PORT)
                            db = conn[MONGO_DB_NAME]
                            db.authenticate(MONGO_USER, MONGO_PASSWORD)
                            try:
                                print "device_id: ", pkt_device_id
                                print "message payload: ", payload
                                info.save_msg_str(db, str(pkt_device_id), payload)
                            except:
                                print "static info error"

                            # retrieve disk information
                            # removed by ywdeng@20150914 to avoid too many request
                            #n = smomd.views.get_disknum(pkt_device_id)
                            #if n > 0:
                            #    for d in range(0, n):
                            #        smomd.views.post_diskinfo(pkt_device_id, d)
                            #        smomd.views.post_disksmart(pkt_device_id, d)
                            #        time.sleep(1)
                        except:
                            print "error : static_information"

                    elif pkt_id == "sensor_information":
                        payload = str(msg.payload)
                        #messages = filter(lambda x: x.get('result', 0)==0, decoded.get('messages'))
                        #decoded['messages'] = messages
                        #payload = json.dumps(decoded)
                        print "id : sensor_information"
                        #print "blank ", pkt_device_id + " : ", payload
                        # save to mongodb
                        try:
                            db = models.LastSeen(
                                device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                            db.save()
                            db = models.SensorInfo(
                                device_id=pkt_device_id, json=payload, last_modified=str(int(time.time())))
                            db.save()
                            conn = MongoClient(MONGO_HOSTNAME, MONGO_PORT)
                            db = conn[MONGO_DB_NAME]
                            db.authenticate(MONGO_USER, MONGO_PASSWORD)
                            try:
                                print "device_id: ", pkt_device_id
                                print "message payload: ", payload
                                info.save_msg_str(db, pkt_device_id, payload)
                            except Exception, e:
                                #traceback.print_exc()
                                print "sensor info error"

                        except:
                            print "error : sensor_information"

                    elif pkt_id == "cfg":
                        print "id : cfg"

                    elif pkt_id == "cfg_ack":
                        print "id : cfg_ack"
                        db = models.LastSeen(
                            device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                        db.save()
                        pkt_message = decoded.get('message')
                        pkt_message_str = json.dumps(pkt_message)
                        ConfigureAck_db = models.ConfigureAck(
                            device_id=pkt_device_id, message=pkt_message_str)
                        ConfigureAck_db.save()

                    elif pkt_id == "ctrl":
                        print "id : ctrl"

                    elif pkt_id == "ctrl_ack":
                        print "id : ctrl_ack"
                        db = models.LastSeen(
                            device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                        db.save()
                        pkt_message = decoded.get('message')
                        for a in pkt_message:
                            pkt_id = a.get('id')
                            if pkt_id == "EAPI":
                                api = a.get('api')
                                tok = a.get('token')
                                par = a.get('par')
                                if api == 'SemaEApiDiskInfo':
                                    pInfo = par['pInfo']
                                    print "SemaEApiDiskInfo"
                                    try:
                                        db = models.DiskInfo.objects.filter(
                                            device_id__exact=pkt_device_id, token__exact=tok)[:1]
                                        if len(db) == 1:
                                            x = db[0]
                                            x.xml = pInfo
                                            di = disk_info.DiskInfo()
                                            di.loadxml(pInfo)
                                            x.json = json.dumps(di.dumpdict())
                                            x.last_modified = str(int(time.time()))
                                            x.save()
                                        else:
                                            x = models.DiskInfo(
                                                device_id=pkt_device_id, disk_index="0", xml=pInfo, token=tok, last_modified=str(int(time.time())))
                                            x.save()
                                    except:
                                        print "models.DiskInfo save FAIL"
                                elif api == 'SemaEApiDiskSMARTAll':
                                    pInfo = par['pInfo']
                                    print "SemaEApiDiskSMARTAll"
                                    try:
                                        db = models.DiskSMARTAll.objects.filter(
                                            device_id__exact=pkt_device_id, token__exact=tok)[:1]
                                        if len(db) == 1:
                                            x = db[0]
                                            x.xml = pInfo
                                            ds = disk_smart.DiskSmart()
                                            ds.loadxml(pInfo)
                                            x.json = json.dumps(ds.dumpdict())
                                            x.last_modified = str(int(time.time()))
                                            x.save()
                                        else:
                                            x = models.DiskSMARTAll(
                                                device_id=pkt_device_id, disk_index="0", xml=pInfo, token=tok, last_modified=str(int(time.time())))
                                            x.save()
                                    except:
                                        print "models.DiskSMARTAll save FAIL"
                                else:
                                    print "Unknow api :", api
                            else:
                                pkt_message_str = json.dumps(pkt_message)
                                ControlAck_db = models.ControlAck(
                                    device_id=pkt_device_id, message=pkt_message_str)
                                ControlAck_db.save()
                    else:
                        print "Unknow id :", pkt_id
                except:
                    print("not json pkt")
Esempio n. 2
0
    def mqtt_on_message(self, mqttc, obj, msg):
        print "rev TOPIC:", msg.topic
        if msg.topic == "/ADLINK/SEMA/Discover":
            try:
                decoded = json.loads(str(msg.payload))
                pkt_id = decoded.get('id')
                if pkt_id == None:
                    return

                if pkt_id == "discover":
                    print "id : discover"

                    if MY__DEBUG == False:
                        # delet all db
                        print "delet all d"
                        Topic_db = models.Topic.objects.all()
                        Topic_db.delete()
                        Register_db = models.Register.objects.all()
                        Register_db.delete()
                        print "Register db delete"
                        ConfigureAck_db = models.ConfigureAck.objects.all()
                        ConfigureAck_db.delete()
                        print "ConfigureAck db delete"
                        ControlAck_db = models.ControlAck.objects.all()
                        ControlAck_db.delete()
                        print "ControlAck db delete"

                elif pkt_id == "register":
                    print "id : register"
                    pkt_model_name = decoded.get('model_name')
                    pkt_device_id = decoded.get('device_id')
                    pkt_ipv4 = decoded.get('ipv4')
                    pkt_item = (pkt_device_id, pkt_model_name, pkt_ipv4)
                    db = models.Register(
                        device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4)
                    db.save()
                    print "Register db add :", pkt_device_id
                    pkt_device_topic = ROOT_TOPIC + pkt_model_name + "/" + pkt_device_id
                    pkt_control_topic = pkt_device_topic + CONTROL_TOPIC
                    pkt_configure_topic = pkt_device_topic + CONFIGURE_TOPIC
                    # for test ,only register_ack can insert db
                    # db = models.Topic(device_id=pkt_device_id, model_name=pkt_model_name, \
                    #ipv4=pkt_ipv4, device_topic=pkt_device_topic, control_topic=pkt_control_topic, \
                    # configure_topic=pkt_configure_topic);
                    # db.save()
                    # print "Topic db add :", pkt_device_id
                elif pkt_id == "register_ack":
                    print "id : register_ack"
                else:
                    print "Unknow id :", pkt_id

            except:
                print("not json pkt")
        elif msg.topic == "/ADLINK/SEMA/PING":
            print "rev :", str(msg.payload)

        elif msg.topic.find("/ADLINK/SEMA/") == 0:
            if msg.topic.find("-") != -1:
                return
            # only in Topic DB's topic can rev
            Topic_db = models.Topic.objects.extra(
                where=['device_topic=%s OR control_topic=%s OR configure_topic=%s'],
                params=[msg.topic, msg.topic, msg.topic])

            if Topic_db.count() <= 0:
                print "Topic Illegal"
                return
            elif Topic_db.count() > 1:
                print "Duplicate Topic"
                print 'match Topic_db count = ', Topic_db.count()
                return

            pkt_device_id = ""

            for a in Topic_db:
                pkt_device_id = a.device_id
                pkt_model_name = a.model_name
                pkt_ipv4 = a.ipv4

            # try:
            decoded = json.loads(str(msg.payload))
            pkt_id = decoded.get('id')
            if pkt_id == "static_information":
                print "id : static_information"
                # save to mongodb
                # try:
                db = models.LastSeen(
                    device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                db.save()
                conn = MongoClient(MONGO_HOSTNAME, MONGO_PORT)
                db = conn[MONGO_DB_NAME]
                db.authenticate(MONGO_USER, MONGO_PASSWORD)
                # try:
                info.save_msg_str(db, str(pkt_device_id), str(msg.payload))
                # except:
                #    print "static info error"

                # except:
                #    print "error : static_information"

            elif pkt_id == "sensor_information":
                print "id : sensor_information"
                # save to mongodb
                # try:
                db = models.LastSeen(
                    device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                db.save()
                conn = MongoClient(MONGO_HOSTNAME, MONGO_PORT)
                db = conn[MONGO_DB_NAME]
                db.authenticate(MONGO_USER, MONGO_PASSWORD)
                # try:
                info.save_msg_str(db, pkt_device_id, str(msg.payload))
                # except:
                #    print "sensor info error"
                # except:
                #    print "error : sensor_information"

            elif pkt_id == "cfg_ack":
                print "id : cfg_ack"
                db = models.LastSeen(
                    device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                db.save()
                pkt_message = decoded.get('message')
                pkt_message_str = json.dumps(pkt_message)
                ControlAck_db = models.ConfigureAck(
                    device_id=pkt_device_id, message=pkt_message_str)
                ControlAck_db.save()

            elif pkt_id == "ctrl_ack":
                print "id : ctrl_ack"
                db = models.LastSeen(
                    device_id=pkt_device_id, model_name=pkt_model_name, ipv4=pkt_ipv4, last_seen=str(int(time.time())))
                db.save()
                pkt_message = decoded.get('message')
                pkt_message_str = json.dumps(pkt_message)
                ControlAck_db = models.ControlAck(device_id=pkt_device_id, message=pkt_message_str)
                ControlAck_db.save()
            else:
                print "Unknow id :", pkt_id