def main(): parser = optparse.OptionParser() parser.add_option('-i', '--interface', dest='interface', default=None, help='Network interface to broadcast over') parser.add_option('-p', '--ip-filter', dest='fip', default=None, help='Filter devices by IP prefix') opts, args = parser.parse_args() filename = args[0] if len(args) else '-' if filename != '-': fout = open("test.json", 'wb') else: fout = open("test.json", 'wb') #fout = sys.stdout print(opts) # MUST USE default port for whois bacnet.Init(opts.interface, None) device_list = [] # Discover and store devices print(bacnet) devs = bacnet.whois(2) print >> sys.stderr, "Found", len(devs), "devices" for h_dev in sorted(devs, key=itemgetter('device_id')): # IP filter mac = '.'.join([str(i) for i in h_dev['mac']]) if not opts.fip or mac.startswith(opts.fip): # Dev filter objs = [] obj_count = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, 0) name = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_NAME, -1) try: desc = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_DESCRIPTION, -1) except IOError: desc = None device = {'props': h_dev, 'name': name, 'desc': desc, 'objs': []} if obj_count == 0: print >> sys.stderr, "No objects found:", d continue # Get object props and names for i in range(1, obj_count + 1): h_obj = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, i) if h_obj == None: print >> sys.stderr, "Object not found:", i continue try: name = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_OBJECT_NAME, -1) except IOError: name = None try: desc = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_DESCRIPTION, -1) except IOError: desc = None try: unit = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_UNITS, -1) except IOError: unit = None try: value = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_PRESENT_VALUE, -1) except IOError: value = None now = datetime.datetime.now() #date_time = now date_time = now.strftime("%Y-%m-%d %H:%M") device['objs'].append({ 'props': h_obj, 'name': name, 'desc': desc, 'unit': unit, 'data_type': process_point(device['name'], name), 'value': value, 'date_time': date_time }) print >> sys.stderr, device['name'], "has", len( device['objs']), "objects" device_list.append(device) #print("\n\n\n\n ff \n",device_list) json.dump(device_list, fout) fout.close()
default=None, help='Filter devices by IP prefix') opts, args = parser.parse_args() # MUST USE default port for whois bacnet.Init(None, None) h_dev = { u'mac': [10, 2, 24, 2, 168, 192], u'device_id': 242, u'adr': [], u'net': 0, u'max_apdu': 1024 } h_dev = bacnet.whois(5)[0] print h_dev print #h_obj = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, 11) #print bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_PRESENT_VALUE, -1) #print bacnet.write_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_PRESENT_VALUE, bacnet.BACNET_APPLICATION_TAG_REAL, '30', 16) #h_obj = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, 2) #h_obj={u'type_str': u'analog-value', u'instance': 17162, u'type': 2} h_obj = {u'type_str': u'binary-output', u'instance': 17153, u'type': 4} print h_obj #print #rint bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, 0) #print #print print bacnet.write_prop(h_dev, h_obj['type'], h_obj['instance'],
def main(): parser = optparse.OptionParser() parser.add_option('-i', '--interface', dest='interface', default=None, help='Network interface to broadcast over') parser.add_option('-p', '--ip-filter', dest='fip', default=None, help='Filter devices by IP prefix') opts, args = parser.parse_args() if len(args) != 1: print "Usage:", sys.argv[0], "[options] <output file>" parser.print_help() sys.exit(1) filename = args[0] fout = open(filename, 'wb') # MUST USE default port for whois bacnet.Init(opts.interface, None) device_list = [] # Discover and store devices devs = bacnet.whois(5) print >>sys.stderr, "Found", len(devs), "devices" for h_dev in sorted(devs, key=itemgetter('device_id')): # IP filter mac = '.'.join([str(i) for i in h_dev['mac']]) if not opts.fip or mac.startswith(opts.fip): # Dev filter objs = [] obj_count = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, 0) name = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_NAME, -1) try: desc = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_DESCRIPTION, -1) except IOError: desc = None device = { 'props': h_dev, 'name': name, 'desc': desc, 'objs': [] } if obj_count == 0: print >>sys.stderr, "No objects found:", d continue # Get object props and names for i in range(1, obj_count+1): h_obj = bacnet.read_prop(h_dev, bacnet.OBJECT_DEVICE, h_dev['device_id'], bacnet.PROP_OBJECT_LIST, i) if h_obj == None: print >>sys.stderr, "Object not found:", i continue try: name = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_OBJECT_NAME, -1) except IOError: name = None try: desc = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_DESCRIPTION, -1) except IOError: desc = None try: unit = bacnet.read_prop(h_dev, h_obj['type'], h_obj['instance'], bacnet.PROP_UNITS, -1) except IOError: unit = None device['objs'].append({ 'props': h_obj, 'name': name, 'desc': desc, 'unit': unit, 'data_type': process_point(device['name'], name), }) print >>sys.stderr, device['name'], "has", len(device['objs']), "objects" device_list.append(device) json.dump(device_list, fout) fout.close()