def test_whoIs(self): request = WhoIsRequest( deviceInstanceRangeLowLimit=500, deviceInstanceRangeHighLimit=50000 ) 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 = IAmRequest() expected.pduDestination = GlobalBroadcast() expected.iAmDeviceIdentifier = 36113 expected.maxAPDULengthAccepted = 1024 expected.segmentationSupported = "segmentedBoth" expected.vendorID = 15 exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEqual(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 response(self, response_apdu, npdu, bvlpdu, address): if response_apdu is None: return apdu = APDU() response_apdu.encode(apdu) apdu.encode(npdu) npdu.encode(bvlpdu) pdu = PDU() bvlpdu.bvlciLength = len(bvlpdu.pduData) + 4 bvlpdu.encode(pdu) apdu_type = apdu_types.get(response_apdu.apduType) if isinstance(response_apdu, RejectPDU) or isinstance( response_apdu, ErrorPDU): self.datagram_server.sendto(pdu.pduData, address) logger.info('Bacnet Error or Reject response sent to %s (%s:%s)', response_apdu.pduDestination, apdu_type.__name__, self._response_service) else: if pdu.pduDestination == '*:*': # broadcast # sendto operates under lock self.datagram_server.sendto(pdu.pduData, ('', address[1])) else: # sendto operates under lock self.datagram_server.sendto(pdu.pduData, address) logger.info('Bacnet response sent to %s (%s:%s)', response_apdu.pduDestination, apdu_type.__name__, self._response_service)
def test_readProperty(self): request = ReadPropertyRequest( objectIdentifier=("analogInput", 14), propertyIdentifier=85 ) request.apduMaxResp = 1024 request.apduInvokeID = 101 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 = ReadPropertyACK() expected.pduDestination = GlobalBroadcast() expected.apduInvokeID = 101 expected.objectIdentifier = 14 expected.objectName = "AI 01" expected.propertyIdentifier = 85 expected.propertyValue = Any(Real(68.0)) exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEqual(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, ('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_readProperty(self): request = ReadPropertyRequest(objectIdentifier=('analogInput', 14), propertyIdentifier=85) request.apduMaxResp = 1024 request.apduInvokeID = 101 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 = ReadPropertyACK() expected.pduDestination = GlobalBroadcast() expected.apduInvokeID = 101 expected.objectIdentifier = 14 expected.objectName = 'AI 01' expected.propertyIdentifier = 85 expected.propertyValue = Any(Real(68.0)) exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEquals(exp_pdu.pduData, received_data)
def test_whoIs(self): request = WhoIsRequest(deviceInstanceRangeLowLimit=500, deviceInstanceRangeHighLimit=50000) 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 = IAmRequest() expected.pduDestination = GlobalBroadcast() expected.iAmDeviceIdentifier = 36113 expected.maxAPDULengthAccepted = 1024 expected.segmentationSupported = 'segmentedBoth' expected.vendorID = 15 exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEquals(exp_pdu.pduData, received_data)
def response(self, response_apdu, address): if response_apdu is None: return apdu = APDU() response_apdu.encode(apdu) pdu = PDU() apdu.encode(pdu) if isinstance(response_apdu, RejectPDU) or isinstance( response_apdu, ErrorPDU): self.datagram_server.sendto(pdu.pduData, address) else: apdu_type = apdu_types.get(response_apdu.apduType) if pdu.pduDestination == "*:*": # broadcast # sendto operates under lock self.datagram_server.sendto(pdu.pduData, ("", address[1])) else: # sendto operates under lock self.datagram_server.sendto(pdu.pduData, address) logger.info( "Bacnet response sent to %s (%s:%s)", response_apdu.pduDestination, apdu_type.__name__, self._response_service, )
def response(self, response_apdu, address): if response_apdu is None: return apdu = APDU() response_apdu.encode(apdu) pdu = PDU() apdu.encode(pdu) if isinstance(response_apdu, RejectPDU) or isinstance(response_apdu, ErrorPDU): self.datagram_server.sendto(pdu.pduData, address) else: apdu_type = apdu_types.get(response_apdu.apduType) if pdu.pduDestination == '*:*': # broadcast # sendto operates under lock self.datagram_server.sendto(pdu.pduData, ('', address[1])) else: # sendto operates under lock self.datagram_server.sendto(pdu.pduData, address) logger.info('Bacnet response sent to %s (%s:%s)', response_apdu.pduDestination, apdu_type.__name__, self._response_service)
def response(self, response_apdu, address): if response_apdu is None: return apdu = APDU() response_apdu.encode(apdu) pdu = PDU() apdu.encode(pdu) if isinstance(response_apdu, RejectPDU) or isinstance(response_apdu, ErrorPDU): self.sock.sendto(pdu.pduData, address) else: apdu_type = apdu_types.get(response_apdu.apduType) if pdu.pduDestination == '*:*': # broadcast self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self.sock.sendto(pdu.pduData, ('', address[1])) else: # unicast self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 0) self.sock.sendto(pdu.pduData, address) logger.info('Bacnet response sent to %s (%s:%s)', response_apdu.pduDestination, apdu_type.__name__, self._response_service)
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()