def __init__(self, text=""): if _debug: ExtendedTagList._debug("__init__ %r", text) TagList.__init__(self) # if it was given, load it if text: self.loads(text)
def test_missing_element(self): if _debug: TestCompoundSequence1._debug("test_missing_element") # create a sequence with a missing required element seq = CompoundSequence1() if _debug: TestSimpleSequence._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() with self.assertRaises(MissingRequiredParameter): seq.encode(tag_list) # create a sequence with a missing required element seq = CompoundSequence1(hydrogen=True) if _debug: TestSimpleSequence._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() with self.assertRaises(MissingRequiredParameter): seq.encode(tag_list) # create a sequence with a missing required element seq = CompoundSequence1(helium=2) if _debug: TestSimpleSequence._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() with self.assertRaises(MissingRequiredParameter): seq.encode(tag_list)
def test_get_context(self): """Test extracting specific context encoded content. """ if _debug: TestTagList._debug("test_get_context") tag_list_data = [ ContextTag(0, xtob('00')), ContextTag(1, xtob('01')), OpeningTag(2), IntegerTag(3), OpeningTag(0), IntegerTag(4), ClosingTag(0), ClosingTag(2), ] taglist = TagList(tag_list_data) # known to be a simple context encoded element context_0 = taglist.get_context(0) if _debug: TestTagList._debug(" - context_0: %r", context_0) assert context_0 == tag_list_data[0] # known to be a simple context encoded list of element(s) context_2 = taglist.get_context(2) if _debug: TestTagList._debug(" - context_2: %r", context_2) assert context_2.tagList == tag_list_data[3:7] # known missing context context_3 = taglist.get_context(3) if _debug: TestTagList._debug(" - context_3: %r", context_3) assert taglist.get_context(3) is None
def do_save(self, args): """save <addr> <device instance>""" args = args.split() if _debug: SaveToFlashConsoleCmd._debug("do_save %r", args) try: addr, obj_inst = args[:2] # object type = 8 (device). property = 1151 (SaveToFlash) obj_type = 8 obj_inst = int(obj_inst) prop_id = 1151 # build a request request = WritePropertyRequest( objectIdentifier=(obj_type, obj_inst), propertyIdentifier=prop_id, ) request.pduDestination = Address(addr) if len(args) == 5: request.propertyArrayIndex = int(args[4]) # send an Enumerated value of 1 means SaveToFlash tag_list = TagList([ApplicationTag(9, xtob('01'))]) if _debug: SaveToFlashConsoleCmd._debug(" - tag_list: %r", tag_list) # stuff the tag list into an Any request.propertyValue = Any() request.propertyValue.decode(tag_list) if _debug: SaveToFlashConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: SaveToFlashConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) # wait for it to complete iocb.wait() # do something for success if iocb.ioResponse: # should be an ack if not isinstance(iocb.ioResponse, SimpleAckPDU): if _debug: SaveToFlashConsoleCmd._debug(" - not an ack") return sys.stdout.write("ack\n") # do something for error/reject/abort if iocb.ioError: sys.stdout.write(str(iocb.ioError) + '\n') except Exception as error: SaveToFlashConsoleCmd._exception("exception: %r", error)
def test_codec(self): if _debug: TestSimpleSequenceArray._debug("test_codec") # test array contents ary_value = [ SimpleSequence(hydrogen=True), SimpleSequence(hydrogen=False), SimpleSequence(hydrogen=True), ] # create an array ary = SimpleSequenceArray(ary_value) if _debug: TestSimpleSequenceArray._debug(" - ary: %r", ary) # encode it in a tag list tag_list = TagList() ary.encode(tag_list) if _debug: TestSimpleSequenceArray._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list ary = SimpleSequenceArray() ary.decode(tag_list) if _debug: TestSimpleSequenceArray._debug(" - ary %r", ary) # value matches assert ary.value[1:] == ary_value
def test_peek(self): if _debug: TestTagList._debug("test_peek") tag0 = IntegerTag(0) taglist = TagList([tag0]) # peek at the first tag assert tag0 == taglist.Peek() # pop of the front tag1 = taglist.Pop() assert taglist.tagList == [] # push it back on the front taglist.push(tag1) assert taglist.tagList == [tag1]
def test_wrong_type(self): if _debug: TestSimpleSequence._debug("test_wrong_type") # create a sequence with wrong element value type seq = SimpleSequence(hydrogen=12) with self.assertRaises(TypeError): tag_list = TagList() seq.encode(tag_list)
def name_value_encode(obj): """Encode a NameValue object into a tag list.""" if _debug: name_value_encode._debug("name_value_encode %r", obj) tag_list = TagList() obj.encode(tag_list) if _debug: name_value_encode._debug(" - tag_list: %r", tag_list) return tag_list
def test_missing_element(self): if _debug: TestSimpleSequence._debug("test_missing_element") # create a sequence with a missing required element seq = SimpleSequence() if _debug: TestSimpleSequence._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() with self.assertRaises(MissingRequiredParameter): seq.encode(tag_list)
def test_endec_0(self): """Test empty tag list encoding and decoding. """ if _debug: TestTagList._debug("test_endec_0") taglist = TagList([]) data = PDUData() taglist.encode(data) assert data.pduData == xtob('') taglist = TagList() taglist.decode(data) assert taglist.tagList == []
def test_endec_3(self): """Test bracketed application tagged integer encoding and decoding.""" if _debug: TestTagList._debug("test_endec_2") tag0 = OpeningTag(0) tag1 = IntegerTag(0x0102) tag2 = ClosingTag(0) taglist = TagList([tag0, tag1, tag2]) data = PDUData() taglist.encode(data) assert data.pduData == xtob('0E3201020F') taglist = TagList() taglist.decode(data) assert taglist.tagList == [tag0, tag1, tag2]
def test_endec_2(self): """Test short tag list encoding and decoding, context tags. """ if _debug: TestTagList._debug("test_endec_2") tag0 = ContextTag(0, xtob('00')) tag1 = ContextTag(1, xtob('01')) taglist = TagList([tag0, tag1]) data = PDUData() taglist.encode(data) assert data.pduData == xtob('09001901') taglist = TagList() taglist.decode(data) assert taglist.tagList == [tag0, tag1]
def test_endec_1(self): """Test short tag list encoding and decoding, application tags. """ if _debug: TestTagList._debug("test_endec_1") tag0 = IntegerTag(0x00) tag1 = IntegerTag(0x01) taglist = TagList([tag0, tag1]) data = PDUData() taglist.encode(data) assert data.pduData == xtob('31003101') taglist = TagList() taglist.decode(data) assert taglist.tagList == [tag0, tag1]
def test_empty_sequence(self): if _debug: TestEmptySequence._debug("test_empty_sequence") # create a sequence seq = EmptySequence() if _debug: TestEmptySequence._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() seq.encode(tag_list) if _debug: TestEmptySequence._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list seq = EmptySequence() seq.decode(tag_list) if _debug: TestEmptySequence._debug(" - seq: %r", seq)
def test_codec(self): if _debug: TestCompoundSequence1._debug("test_codec") # create a sequence seq = CompoundSequence1(hydrogen=True, helium=2) if _debug: TestCompoundSequence1._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() seq.encode(tag_list) if _debug: TestCompoundSequence1._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list seq = CompoundSequence1() seq.decode(tag_list) if _debug: TestCompoundSequence1._debug(" - seq: %r", seq)
def test_codec(self): if _debug: TestSimpleSequence._debug("test_codec") # create a sequence seq = SimpleSequence(hydrogen=False) if _debug: TestSimpleSequence._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() seq.encode(tag_list) if _debug: TestSimpleSequence._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list seq = SimpleSequence() seq.decode(tag_list) if _debug: TestSimpleSequence._debug(" - seq: %r", seq)
def test_codec_2(self): if _debug: TestCompoundSequence2._debug("test_codec_2") # create a sequence seq = CompoundSequence2(lithium=True, beryllium=3) if _debug: TestCompoundSequence2._debug(" - seq: %r", seq) # encode it in a tag list tag_list = TagList() seq.encode(tag_list) if _debug: TestCompoundSequence2._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list seq = CompoundSequence2() seq.decode(tag_list) if _debug: TestCompoundSequence2._debug(" - seq: %r", seq)
def test_empty_array(self): if _debug: TestIntegerArray._debug("test_empty_array") # create an empty array ary = IntegerArray() if _debug: TestIntegerArray._debug(" - ary: %r", ary) # array sematics assert len(ary) == 0 assert ary[0] == 0 # encode it in a tag list tag_list = TagList() ary.encode(tag_list) if _debug: TestIntegerArray._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list ary = IntegerArray() ary.decode(tag_list) if _debug: TestIntegerArray._debug(" - seq: %r", seq)
def test_codec(self): if _debug: TestIntegerArray._debug("test_codec") # test array contents ary_value = [1, 2, 3] # create an array ary = IntegerArray(ary_value) if _debug: TestIntegerArray._debug(" - ary: %r", ary) # encode it in a tag list tag_list = TagList() ary.encode(tag_list) if _debug: TestIntegerArray._debug(" - tag_list: %r", tag_list) # create another sequence and decode the tag list ary = IntegerArray() ary.decode(tag_list) if _debug: TestIntegerArray._debug(" - ary %r", ary) # value matches assert ary.value[1:] == ary_value
def do_write(self, args): """write <addr> <objid> <prop> [ <indx> ]""" args = args.split() if _debug: WriteSomethingConsoleCmd._debug("do_write %r", args) try: addr, obj_id, prop_id = args[:3] obj_id = ObjectIdentifier(obj_id).value if prop_id.isdigit(): prop_id = int(prop_id) # build a request request = WritePropertyRequest( objectIdentifier=obj_id, propertyIdentifier=prop_id, ) request.pduDestination = Address(addr) if len(args) == 4: request.propertyArrayIndex = int(args[3]) # build a custom datastructure tag_list = TagList([ OpeningTag(1), ContextTag(0, xtob('9c40')), ContextTag(1, xtob('02')), ContextTag(2, xtob('02')), ClosingTag(1) ]) if _debug: WriteSomethingConsoleCmd._debug(" - tag_list: %r", tag_list) # stuff the tag list into an Any request.propertyValue = Any() request.propertyValue.decode(tag_list) if _debug: WriteSomethingConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: WriteSomethingConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application deferred(this_application.request_io, iocb) # wait for it to complete iocb.wait() # do something for success if iocb.ioResponse: # should be an ack if not isinstance(iocb.ioResponse, SimpleAckPDU): if _debug: WriteSomethingConsoleCmd._debug(" - not an ack") return sys.stdout.write("ack\n") # do something for error/reject/abort if iocb.ioError: sys.stdout.write(str(iocb.ioError) + '\n') except Exception as error: WriteSomethingConsoleCmd._exception("exception: %r", error)
def do_write(self, args): """write <addr> <type> <inst> <prop> [ <indx> ]""" args = args.split() if _debug: WriteSomethingConsoleCmd._debug("do_write %r", args) try: addr, obj_type, obj_inst, prop_id = args[:4] obj_type = int(obj_type) obj_inst = int(obj_inst) prop_id = int(prop_id) # build a request request = WritePropertyRequest( objectIdentifier=(obj_type, obj_inst), propertyIdentifier=prop_id, ) request.pduDestination = Address(addr) if len(args) >= 5: request.propertyArrayIndex = int(args[4]) if len(args) == 6: #convert ip to byte array and then to hex string proxy = bytearray(args[5]) proxy_hex = str(proxy).encode('hex') # build a custom datastructure... BACnet settings IP BBMD Foreign port DSC 3.40 tag_list = TagList([ OpeningTag(1), ContextTag(0, xtob('9ca4')), ContextTag(1, xtob('02')), ContextTag(2, xtob('02')), ContextTag(3, xtob('003030302e3030302e3030302e303030')), ContextTag(4, xtob('3c')), ContextTag(5, xtob("00" + proxy_hex)), ContextTag(6, xtob('00')), ContextTag(7, xtob('00')), ContextTag(8, xtob('00')), ContextTag(9, xtob('bac0')), ContextTag(10, xtob('00')), ContextTag(11, xtob('00')), ContextTag(12, xtob('00')), ContextTag(13, xtob('ffffffff')), ContextTag(14, xtob('00')), ContextTag(15, xtob('00')), ContextTag(16, xtob('00')), ContextTag(17, xtob('00')), ClosingTag(1) ]) if _debug: WriteSomethingConsoleCmd._debug(" - tag_list: %r", tag_list) # stuff the tag list into an Any request.propertyValue = Any() request.propertyValue.decode(tag_list) if _debug: WriteSomethingConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: WriteSomethingConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) # wait for it to complete iocb.wait() # do something for success if iocb.ioResponse: # should be an ack if not isinstance(iocb.ioResponse, SimpleAckPDU): if _debug: WriteSomethingConsoleCmd._debug(" - not an ack") return sys.stdout.write("ack\n") # do something for error/reject/abort if iocb.ioError: sys.stdout.write(str(iocb.ioError) + '\n') except Exception as error: WriteSomethingConsoleCmd._exception("exception: %r", error)
def test_scale_choice(self): if _debug: TestScaleChoice._debug("test_scale_choice") taglist = TagList([Tag(1, 1, 1, bytearray(b'\x00'))]) scale = Scale() scale.decode(taglist) self.assertDictEqual(scale.dict_contents(), {'integerScale': 0})
def do_write(self, args): """write <addr> <type> <inst> <prop> [ <indx> ]""" args = args.split() if _debug: WriteSomethingConsoleCmd._debug("do_write %r", args) try: addr = args[0] obj_type = 162 #int(obj_type) obj_inst = 1 #int(obj_inst) prop_id = 1034 #int(prop_id) idx = 2 # build a request request = WritePropertyRequest( objectIdentifier=(obj_type, obj_inst), propertyIdentifier=prop_id, ) request.pduDestination = Address(addr) if len(args) >= 5: request.propertyArrayIndex = 2 #int(args[4]) request.propertyArrayIndex = idx if len(args) == 6: #convert ip to byte array and then to hex string proxy = bytearray(args[5]) proxy_hex = str(proxy).encode('hex') # build a custom data structure... BACnet settings BCP object IP BBMD Foreign port eTCH 3.40 # Context #0 inside Opening tag #9 is the IP Type 00=Regular, 01=Foreign, 02=BBMD # Context #2 inside Opening tag #9 is the Foreign IP in hex # Context #4 inside Opening tag #9 is the Proxy IP in hex tag_list = TagList([ OpeningTag(0), ContextTag(0, xtob('19')), ContextTag(1, xtob('01')), ClosingTag(0), ContextTag(1, xtob('01')), ContextTag(2, xtob('00')), ContextTag(3, xtob('9c40')), ContextTag(4, xtob('00')), OpeningTag(5), ContextTag(0, xtob('00')), ContextTag(1, xtob('00')), ClosingTag(5), ContextTag(6, xtob('00')), ContextTag(7, xtob('00')), OpeningTag(8), ContextTag(0, xtob('00')), ContextTag(1, xtob('00')), ContextTag(2, xtob('00')), ContextTag(3, xtob('00')), ContextTag(4, xtob('00')), ContextTag(5, xtob('00')), ContextTag(6, xtob('00')), ContextTag(7, xtob('00')), ContextTag(8, xtob('ffffffff')), ClosingTag(8), OpeningTag(9), ContextTag(0, xtob('02')), ContextTag(1, xtob('bac0')), ContextTag(2, xtob('00')), ContextTag(3, xtob('3c')), ContextTag(4, xtob('480c600c')), ContextTag(5, xtob('00')), ContextTag(6, xtob('ffffffff')), ClosingTag(9), ContextTag(10, xtob('00')), ContextTag(11, xtob('00')), ContextTag(12, xtob('00')), ContextTag(13, xtob('00000000')) ]) if _debug: WriteSomethingConsoleCmd._debug(" - tag_list: %r", tag_list) # stuff the tag list into an Any request.propertyValue = Any() request.propertyValue.decode(tag_list) if _debug: WriteSomethingConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: WriteSomethingConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) # wait for it to complete iocb.wait() # do something for success if iocb.ioResponse: # should be an ack if not isinstance(iocb.ioResponse, SimpleAckPDU): if _debug: WriteSomethingConsoleCmd._debug(" - not an ack") return sys.stdout.write("ack\n") # do something for error/reject/abort if iocb.ioError: sys.stdout.write(str(iocb.ioError) + '\n') except Exception as error: WriteSomethingConsoleCmd._exception("exception: %r", error)
def test_tag_list(self): if _debug: TestTagList._debug("test_tag_list") # test tag list construction tag_list = TagList() tag_list = TagList([])