-1

私は問題があります。

センサー ネットワークにコマンドを送信するページがあります。

コードのこの部分をクリックすると

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

私はjs関数を呼び出します。これは次のとおりです。

function send_command_to_sensor(net, sens, command) {
  $.ajax({url: "/networks/" + net + "/sensors/" + sens + "/send?command=" + command,
  type: "GET",
  async: true,
  dataType: "json",
  success: function(json_response) { 
             var err = json_response['error'];
         if (err) {
           show_alert('error', err);
           return;
         }
         var success = json_response['success'];
         if (success) {
           show_alert('success', success);
           return;
         }
         show_alert('alert', "This should not happen!");
           }
  }); 
}

この関数は、Python で記述された Tornado Web サーバーのハンドラーを呼び出す URL を作成します。ハンドラーは次のとおりです。

################################################################################
# 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))

開発者コンソールで受け取るエラーは次のとおりです。

キャッチされていない TypeError: null $.ajax.successwsn.js:26 のプロパティ 'エラー' を読み取れません1.7.2.js:7538 jQuery.ajaxTransport.send.callback

js関数で。関数が「エラー」または「成功」を認識しないのはなぜですか? 問題はどこだ????プログラムはハンドラーに決して入らず、js関数だけでブロックされていると思います。

助けてくれてどうもありがとう!長文ですが読みやすいです!お願いします!

4

1 に答える 1

1

簡単に読むと、関数が正しく装飾されていません。

この線

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

次のように関数を宣言する必要があることを意味します。

@tornado.web.asynchronous
@tornado.gen.engine
@tornado.web.authenticated
def get(self):
    ....

2 つのデコレーターが追加されていることに注意してください。

于 2013-02-22T14:22:29.867 に答える