Ejemplo n.º 1
0
 def setup(self, opts):
     bacnet.Init(opts.get('iface', 'eth0'), '47900')
     with open(opts.get('db'), 'r') as fp:
         self.db = json.load(fp)
     self.rate = int(opts.get('rate', 60))
     self.devices = map(re.compile, opts.get('devices', ['.*']))
     self.points = map(re.compile, opts.get('points', ['.*']))
     self.ffilter = _get_class(
         opts.get('filter')) if opts.get('filter') else None
     self.pathnamer = _get_class(
         opts.get('pathnamer')) if opts.get('pathnamer') else None
     for (dev, obj, path) in self._iter_points():
         unit = str(obj['unit']).strip()
         if unit.isdigit():
             unit = str(bacnet.type_str(int(unit)))
         self.add_timeseries(path, unit, data_type='double')
Ejemplo n.º 2
0
    def setup(self, opts):
        bacnet.Init(opts.get('iface', 'eth0'), '47900')
        with open(opts.get('db'), 'r') as fp:
            self.db = json.load(fp)
        self.rate = int(opts.get('rate', 60))
        self.devices = map(re.compile, opts.get('devices', ['.*']))
        self.points = map(re.compile, opts.get('points', ['.*']))
        self.ffilter = _get_class(opts.get('filter')) if opts.get('filter') else None
        self.pathnamer = _get_class(opts.get('pathnamer')) if opts.get('pathnamer') else None
        self.actuators = _get_class(opts.get('actuators')) if opts.get('actuators') else None

        if self.actuators:
            act_names = [a['name'] for a in self.actuators]
        for (dev, obj, path) in self._iter_points():
            unit = str(obj['unit']).strip()
            if unit.isdigit():
                unit = str(bacnet.type_str(int(unit)))
            self.add_timeseries(path, unit, data_type='double')

            # Add actuators
            if self.actuators and obj['name'] in act_names:
                actuator = find(lambda a: a['name'] == obj['name'], self.actuators)
                setup = {'obj': obj, 'dev': dev}
                if obj['props']['type'] in [bacnet.OBJECT_ANALOG_INPUT, 
                                            bacnet.OBJECT_ANALOG_OUTPUT, 
                                            bacnet.OBJECT_ANALOG_VALUE]:
                    setup['range'] = actuator['range']
                    setup['application_tag'] = bacnet.BACNET_APPLICATION_TAG_REAL
                    act = ContinuousActuator(**setup)
                    data_type = 'double'
                elif obj['props']['type'] in [bacnet.OBJECT_BINARY_INPUT,
                                              bacnet.OBJECT_BINARY_OUTPUT,
                                              bacnet.OBJECT_BINARY_VALUE]:
                    setup['application_tag'] = bacnet.BACNET_APPLICATION_TAG_ENUMERATED
                    act = BinaryActuator(**setup)
                    data_type = 'long'
                elif obj['props']['type'] in [bacnet.OBJECT_MULTI_STATE_INPUT,
                                              bacnet.OBJECT_MULTI_STATE_OUTPUT,
                                              bacnet.OBJECT_MULTI_STATE_VALUE]:
                    setup['application_tag'] = bacnet.BACNET_APPLICATION_TAG_ENUMERATED
                    setup['states'] = actuator['states']
                    act = DiscreteActuator(**setup)
                    data_type = 'long'
                if act:
                    print "adding actuator:", path, unit, act
                    self.add_actuator(path + "_act", unit, act, data_type=data_type, write_limit=5)
Ejemplo n.º 3
0
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()

# 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)
Ejemplo n.º 4
0
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()