Json响应错误:响应为空?

2024-05-29 04:02:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个问题。在

我有一个页面,在那里我向传感器网络发送命令。在

当我点击这部分代码

<a href='javascript:void(send_command_to_sensor("{{net.id}}", "{{sens.id}}", "identify"));'></a>

我调用一个js函数:

^{pr2}$

这个函数构建一个url,用于调用用python编写的Tornado web服务器中的处理程序。处理程序如下:

################################################################################
# SensorSend handler. 
# Sends a packet to the sensor by writing in a file (Should work through
# MySQL database) read by Quantaserv Daemon
################################################################################
class SensorSendHandler(BaseHandler):
    # Requires authentication 
    @tornado.web.authenticated
    def get(self, nid, sid):
        command = self.get_argument('command').upper(); 

        print command

        # Retrieve the current user 
        usr = self.get_current_user()
        usr_id = usr['id']

        self.lock_tables("read", ['nets_permissions as n'])
        perm = self.db.get("SELECT n.perm FROM nets_permissions as n \
                              WHERE n.network_id=%s AND n.user_id=%s", nid, int(usr_id))
        self.unlock_tables()

        # Check whether the user has access to the network
        perms = self.check_network_access(nid, perm['perm'])

        #Check wether the sensor exists in this network
        self.check_sensor_in_network(sid, nid)

        # The dictionary to return
        ret = {}

        ############################################################################
        # The following Code is valid for ZTC networks only
        # Must be generalized
        # TODO: - get the network type:
        #       - check wether the command is allowed
        ############################################################################
        if command not in ['ON', 'OFF', 'TOGGLE', 'IDENTIFY']:
            raise tornado.web.HTTPError(404, "Unknown command: " + str(command))


        #Command OnOffCmd_SetState
        if command in ['ON', 'OFF', 'TOGGLE']:
            op_group = "70"
            op_code = "50"
            packet_meta = "%s%s%s%s02%s%s600080000%s"
            pkt_len = hextransform(16, 2)
            netid = hextransform(int(nid), 16)
            sens_id = hextransform(int(sid) >> 16, 4)
            sens_id_little = invert2bytes(sens_id,0)
            cluster_id = hextransform(int(sid) & 0x00FFFF, 4)
            end_point = "08"


            if command == 'ON':
                cmd_data = "01"
            elif command == 'OFF':
                cmd_data = "00"
            elif command == 'TOGGLE':
                cmd_data = "02"

            packet = packet_meta % (netid, op_group, op_code, pkt_len, sens_id, end_point, cmd_data)
            packet = packet.upper()

            op_group_hex=0x70
            op_code_hex=0x50

            print command

        #Command ZDP-IEEE_addr.Request            
        elif command == 'IDENTIFY':
            op_group = "A2"
            op_code = "01"
            packet_meta = "%s%s%s%s"
            pkt_len = hextransform(2, 2)

            sens_id = hextransform(int(sid) >> 16, 4)
            sens_id_little = invert2bytes(sens_id,0)

            packet = packet_meta % (op_group, op_code, pkt_len, sens_id)
            packet = packet.upper()

            op_group_hex=0xA2
            op_code_hex=0x01


        #Command ZDP-Active_EP_req.Request            
        elif command == 'HOWMANY':
            op_group = "A2"
            op_code = "05"
            packet_meta = "%s%s%s%s%s"
            pkt_len = hextransform(4, 2)
            netid = hextransform(int(nid), 16)
            sens_id = hextransform(int(sid) >> 16, 4)
            sens_id_little = invert2bytes(sens_id,0)

            packet = packet_meta % (op_group, op_code, pkt_len, sens_id, netid)
            packet = packet.upper()

            op_group_hex=0xA2
            op_code_hex=0x05



        mynet_type ="ztc"

        cmdjson = packet2json(op_group_hex,op_code_hex, packet)

        print("\t\t " + packet + "\n")

        #
        #
        #TODO : -write command into db  
        ts = datetime.datetime.now().isoformat()
        self.lock_tables("write", ['confcommands'])
        self.db.execute("INSERT INTO confcommands (network_id, ntype, timestamp, command) \
                                  VALUES (%s,%s,%s,%s)", nid, mynet_type, ts, cmdjson)
        self.unlock_tables();
        ############### ELISA ##########################################
        # TODO: - open the /tmp/iztc file in append mode
        cmdfile = open('/tmp/iztc', 'a')
        #       - acquire a lock  "only for the DB case, it's easier"
        #       - write the packet 
        cmdfile.write(nid + "\t"+ mynet_type + "\t"+ ts + "\t"+  cmdjson +"\n");
        #       - release the lock "only for the DB case, it's easier"
        #       - close the file
        cmdfile.close()


        if command == 'HOWMANY':
            opcodegroupr = "A0"
            opcoder = "85"
        elif command == 'IDENTIFY':
            opcodegroupr = "A0"
            opcoder = "81"

        print command

        #Code for retrieving the MAC address of the node
        como_url = "".join(['http://', options.como_address, ':', options.como_port,
                            '/', ztc_config, '?netid=', netid,
                            '&opcode_group=', opcodegroupr, 
                            '&opcode=', opcoder, '&start=-5m&end=-1s'])
        http_client = AsyncHTTPClient()
        response = yield tornado.gen.Task(http_client.fetch, como_url)

        ret = {}
        if response.error:
            ret['error'] = 'Error while retrieving unregistered sensors'
        else:
            for line in response.body.split("\n"):
                if line != "": 
                    value = int(line.split(" ")[6])

        ret['response'] = value
        self.write(tornado.escape.json_encode(ret))


        if command == 'HOWMANY':
            status = value[0]
            NwkAddress = value[1:2]
            ActiveEPCount = value[3]
            ActiveEPList = value[len(ActiveEPCount)]
            if status == "0":
                ret['success'] = "The %s command has been succesfully sent!" % (command.upper())
                self.write(tornado.escape.json_encode(ret))
            elif status == "80":
                ret['error'] = "Invalid Request Type"
                self.write(tornado.escape.json_encode(ret))
            elif status == "89":
                ret['error'] = "No Descriptor"
                self.write(tornado.escape.json_encode(ret))
            else:
                ret['error'] = "Device not found!"
                self.write(tornado.escape.json_encode(ret))


        if command == 'IDENTIFY':
            status = value[0]
            IEEEAddrRemoteDev = value[1:8]
            NWKAddrRemoteDev = value[9:2]
            NumOfAssociatedDevice = value[11:1]
            StartIndex = value[12:1]
            ListOfShortAddress = value[13:2*NumOfAssociatedDevice]
            if status == "0":
                ret['success'] = "Command succesfully sent! The IEEE address is: %s" % (IEEEAddrRemoteDev)
                self.write(tornado.escape.json_encode(ret))
            elif status == "80":
                ret['success'] = "Invalid Request Type"
                self.write(tornado.escape.json_encode(ret))
            else:
                ret['error'] = "Device Not Found"
                self.write(tornado.escape.json_encode(ret))

我在开发人员控制台中收到的错误是:

Uncaught TypeError: Cannot read property 'error' of null $.ajax.successwsn.js:26 jQuery.Callbacks.firejquery-1.7.2.js:1075 jQuery.Callbacks.self.fireWithjquery-1.7.2.js:1193 donejquery-1.7.2.js:7538 jQuery.ajaxTransport.send.callback

在js函数中。为什么函数不知道“错误”或“成功”?问题在哪里????我认为程序不会在处理程序中输入never,而是在js函数中被阻塞。在

非常感谢你的帮助!这篇文章很长,但读起来很简单!拜托!在


Tags: theselfidifpacketvaluegroupcode
1条回答
网友
1楼 · 发布于 2024-05-29 04:02:41

快速阅读是函数没有正确修饰。

线

 response = yield tornado.gen.Task(http_client.fetch, como_url)

意味着您应该立即声明函数:

^{pr2}$

注意添加了两个额外的装饰器。

相关问题 更多 >

    热门问题