def browser_page(request): """ return list of offlink node that cover by a border router """ lists_offlink_nodes = get_list_of_nodes() lists_offlink_nodes_json = json.dumps(lists_offlink_nodes, cls=DjangoJSONEncoder) if request.method == "POST": gatewayaddress = str(request.POST["gatewayaddress"]) nodes = None if gatewayaddress: management = sensornetwork.management() nodes = management.offlink_nodes(gatewayaddress) if nodes: offlink_nodes = OfflinkNodes(border_router_address=gatewayaddress, node_list=nodes) offlink_nodes.save() for node in nodes: node_object, created = Node.objects.get_or_create(ip_address=node) if created: node_object.save() return render_to_response( 'browser/browser_page.html', {'gatewayaddress': gatewayaddress, 'lastlistsofnodes': lists_offlink_nodes_json, 'nodes': nodes,}, RequestContext(request), ) else: return render_to_response( 'browser/browser_page.html', {'lastlistsofnodes': lists_offlink_nodes_json,}, RequestContext(request))
def node_page(request, node): """ provide node information. if the information not found in the db, use snmp (or any means) to retrive input: update or not, (json input from ajax) output: json data """ UPDATE = False node_infor = None if not UPDATE: try: node_object = Node.objects.filter(ip_address=node).order_by('-date')[:1][0] node_infor = Infor.objects.filter(node=node_object).order_by('-date')[0] except: print "Unexpected error:", sys.exc_info()[0] node_infor = None if UPDATE or not node_infor: try: node_object = Node.objects.filter(ip_address=node).order_by('-date')[:1][0] except: print "Node with ip=%s not in the db: %s" %(node, sys.exc_info()[0]) node_object = None management = sensornetwork.management() node_items = ast.literal_eval(management.discovery_node(node)) print node_items #save to db when node_items contain value, and node is in the Node if node_items and node_object: node_infor = Infor(node=node_object, sys_descr = node_items.get('sysdescr'), sys_name = node_items.get('sysname'), if_phys_addr = node_items.get('ifphyaddr'), ent_physical_descr = node_items.get('phydescr'), ) node_infor.save() #for consistance display between the NON Update and Update node_infor = Infor.objects.filter(node=node_object).order_by('-date')[:1][0] #prepare for json response node_items_json = None if node_infor: node_items_data = {} node_items_data['data'] = {} node_items_data['data']['ip_address'] = node node_items_data['data']['sys_descr'] = node_infor.sys_descr node_items_data['data']['sys_name'] = node_infor.sys_name node_items_data['data']['if_phys_addr'] = node_infor.if_phys_addr node_items_data['data']['ent_physical_descr'] = node_infor.ent_physical_descr node_items_data['data']['physensor_type'] = node_infor.physensor_type node_items_data['data']['physensor_unit_display'] = node_infor.physensor_unit_display node_items_data['update'] = calendar.timegm(node_infor.date.utctimetuple())*1000 #timestamp in js is in ms node_items_json = json.dumps(node_items_data, cls=DjangoJSONEncoder) return HttpResponse(node_items_json, content_type="application/json")
def _collect_node(node, command): """ start/stop collecting (collectd) with node node: node address command: START/STOP """ management = sensornetwork.management() management.collect_node(node, command) status = {} status['status'] = 'success' return json.dumps(status, cls=DjangoJSONEncoder)
def _collect_all(command): """ request to start/stop the collectd from all nodes which is manage by the gateway command = START/STOP """ offlink_nodes = get_list_of_nodes() nodes = offlink_nodes.get('nodes') if nodes != None: management = sensornetwork.management() management.collect_all(nodes, command) #command = START/STOP #TODO: check the reply for status status = {} status['status'] = 'success' return json.dumps(status, cls=DjangoJSONEncoder)