2013-02-22 100 views
-1

我有一個問題。Json響應錯誤:響應null?

我有一個頁面,我發送命令到傳感器網絡。

當我點擊這部分代碼

<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!"); 
      } 
    }); 
} 

此功能建立在寫在龍捲風網絡服務器召回的處理程序的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)) 

我收到開發商consolle的錯誤是這樣的:

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函數

。爲什麼函數不知道'錯誤'或'成功'?哪裏有問題????我認爲該程序不會進入處理程序,但會在js函數中被阻止。

非常感謝您的幫助!這是一篇很長的文章,但它很容易閱讀!請!

回答

1

快速閱讀功能未正確裝飾。

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

意味着你應該正是如此聲明函數:

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

注意添加了兩個附加裝飾的。

+0

是啊!而已!!謝謝! – sharkbait 2013-02-22 14:39:28