def test_whoHas(self): request_object = WhoHasObject() request_object.objectIdentifier = ('binaryInput', 12) request = WhoHasRequest(object=request_object) apdu = APDU() request.encode(apdu) pdu = PDU() apdu.encode(pdu) buf_size = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(pdu.pduData, ('127.0.0.1', self.bacnet_server.server.server_port)) data = s.recvfrom(buf_size) received_data = data[0] expected = IHaveRequest() expected.pduDestination = GlobalBroadcast() expected.deviceIdentifier = 36113 expected.objectIdentifier = 12 expected.objectName = 'BI 01' exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEquals(exp_pdu.pduData, received_data)
def test_whoHas(self): request_object = WhoHasObject() request_object.objectIdentifier = ("binaryInput", 12) request = WhoHasRequest(object=request_object) apdu = APDU() request.encode(apdu) pdu = PDU() apdu.encode(pdu) buf_size = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(pdu.pduData, self.address) data = s.recvfrom(buf_size) s.close() received_data = data[0] expected = IHaveRequest() expected.pduDestination = GlobalBroadcast() expected.deviceIdentifier = 36113 expected.objectIdentifier = 12 expected.objectName = "BI 01" exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEqual(exp_pdu.pduData, received_data)
def whohas( self, object_id=None, object_name=None, instance_range_low_limit=0, instance_range_high_limit=4194303, destination=None, global_broadcast=False, ): """ Object ID : analogInput:1 Object Name : string Instance Range Low Limit : 0 Instance Range High Limit : 4194303 destination (optional) : If empty, local broadcast will be used. global_broadcast : False """ obj_id = ObjectIdentifier(object_id) if object_name and not object_id: obj_name = CharacterString(object_name) obj = WhoHasObject(objectName=obj_name) elif object_id and not object_name: obj = WhoHasObject(objectIdentifier=obj_id) else: obj = WhoHasObject(objectIdentifier=obj_id, objectName=obj_name) limits = WhoHasLimits( deviceInstanceRangeLowLimit=instance_range_low_limit, deviceInstanceRangeHighLimit=instance_range_high_limit, ) request = WhoHasRequest(object=obj, limits=limits) if destination: request.pduDestination = Address(destination) else: if global_broadcast: request.pduDestination = GlobalBroadcast() else: request.pduDestination = LocalBroadcast() iocb = IOCB(request) # make an IOCB iocb.set_timeout(2) deferred(self.this_application.request_io, iocb) iocb.wait() iocb = IOCB(request) # make an IOCB self.this_application._last_i_have_received = [] if iocb.ioResponse: # successful response apdu = iocb.ioResponse if iocb.ioError: # unsuccessful: error/reject/abort pass time.sleep(3) # self.discoveredObjects = self.this_application.i_am_counter return self.this_application._last_i_have_received
def test_who_has_object_by_id(self): """Test a Who-Has for an object by identifier.""" if _debug: TestWhoIsIAm._debug("test_who_has_object_by_id") # create a network anet = ApplicationNetwork("test_who_has_object_by_id") # add the service capability to the IUT anet.iut.add_capability(WhoHasIHaveServices) # send the Who-Has, get back a response anet.td.start_state.doc("6-1-0") \ .send(WhoHasRequest( destination=anet.vlan.broadcast_address, object=WhoHasObject(objectIdentifier=('device', 20)), )).doc("6-1-1") \ .receive(IHaveRequest, pduSource=anet.iut.address).doc("6-1-2") \ .success() # no IUT application layer matching anet.iut.start_state.success() # run the group anet.run()
def test_who_has_object_by_name(self): """Test an unconstrained WhoIs, all devices respond.""" if _debug: TestWhoIsIAm._debug("test_who_has_object_by_name") # create a network anet = ApplicationNetwork() # add the service capability to the IUT anet.iut.add_capability(WhoHasIHaveServices) # all start states are successful anet.td.start_state.doc("5-1-0") \ .send(WhoHasRequest( destination=anet.vlan.broadcast_address, object=WhoHasObject(objectName="iut"), )).doc("5-1-1") \ .receive(IHaveRequest, pduSource=anet.iut.address).doc("5-1-2") \ .success() # no IUT application layer matching anet.iut.start_state.success() # run the group anet.run()
def whohas_request(args, console=None): # pylint: disable=too-many-statements, too-many-branches """ This function creates a whohas request. Usage: whohas [ <address> ] ( <name> | <type> <instance> ) [ <lowlimit> <highlimit> ] :param args: list of parameters :param console: console object :return: request """ # check if too few arguments were passed if len(args) == 0: raise ValueError('too few arguments') # initialize position and result result = {} address = None # check if limits were defined if len(args) > 2: if args[-1].isdigit() and args[-2].isdigit(): result['low'] = int(args[-2]) result['high'] = int(args[-1]) args = args[:-2] if len(args) == 0: raise ValueError('name not found') elif len(args) == 3: if args[2].isdigit(): address, result['type'], result['inst'] = args else: result['type'], result['inst'], result['name'] = args elif len(args) == 2: if args[1].isdigit(): result['type'], result['inst'] = args else: address, result['name'] = args elif len(args) == 1: result['name'] = args[0] else: raise ValueError('too many arguments') # check if type is correct if 'inst' in result: if result['inst'].isdigit(): result['inst'] = int(result['inst']) else: raise ValueError('object instance invalid') # check if instance is correct if 'type' in result: if result['type'].isdigit(): result['type'] = int(result['type']) elif not get_object_class(result['type']): raise ValueError('object type invalid') obj_id = None if 'inst' in result and 'type' in result: obj_id = (result['type'], result['inst']) # create whohas object console = WhoHasObject() console.objectName = result.get('name', None) console.objectIdentifier = obj_id if 'low' in result: limits = WhoHasLimits() limits.deviceInstanceRangeLowLimit = result['low'] limits.deviceInstanceRangeHighLimit = result['high'] else: limits = None # create request request = WhoHasRequest() # check if address was specified if address is None: # send broadcast request.pduDestination = GlobalBroadcast() else: # send to specified address request.pduDestination = Address(address) request.object = console request.limits = limits # return created request return request
from bacpypes.apdu import WhoIsRequest, WhoHasObject, WhoHasRequest # code for generating adpu - who-is # request = WhoIsRequest(deviceInstanceRangeLowLimit=500, deviceInstanceRangeHighLimit=50000) # test_pdu = PDU() # test_apdu = APDU() # request.encode(test_apdu) # test_apdu.encode(test_pdu) # bacnet_app = BACnetApp(test.thisDevice, test) # bacnet_app.get_objects_and_properties(test.dom) # bacnet_app.indication(test_apdu, ('127.0.0.1', 9999), test.thisDevice) # print(bacnet_app._response) # bacnet_app.response(bacnet_app._response, ('127.0.0.1', 9999)) # # logger.debug('Starting BACnet Server! at {}:{}'.format('localhost', 9999)) # # test.start('127.0.0.1', 9999) # testing who-has request_object = WhoHasObject() request_object.objectIdentifier = ('binaryInput', 12) request = WhoHasRequest(object=request_object) test_apdu = APDU() request.encode(test_apdu) test_pdu = PDU() test_apdu.encode(test_pdu) bacnet_app = BACnetApp(test.thisDevice, test) bacnet_app.get_objects_and_properties(test.dom) bacnet_app.indication(test_apdu, ('127.0.0.1', 9999), test.thisDevice) print(bacnet_app._response) bacnet_app.response(bacnet_app._response, ('127.0.0.1', 9999)) except KeyboardInterrupt: logger.debug('Stopping BACnet server') test.stop()