|
| 1 | +import csv |
| 2 | +import logging |
| 3 | +from datetime import datetime, timedelta, timezone |
| 4 | +from pprint import pprint |
| 5 | +from time import sleep |
| 6 | + |
| 7 | +from dateutil import parser as dateparser |
| 8 | + |
| 9 | +from dlms_cosem.utils import parse_as_dlms_data |
| 10 | +from dlms_cosem import a_xdr, cosem, enumerations |
| 11 | +from dlms_cosem.security import ( |
| 12 | + NoSecurityAuthentication, |
| 13 | + HighLevelSecurityGmacAuthentication, |
| 14 | +) |
| 15 | +from dlms_cosem.client import DlmsClient |
| 16 | +from dlms_cosem.io import BlockingTcpIO, TcpTransport |
| 17 | +from dlms_cosem.cosem import selective_access |
| 18 | +from dlms_cosem.cosem.selective_access import RangeDescriptor |
| 19 | +from dlms_cosem.parsers import ProfileGenericBufferParser |
| 20 | +from dlms_cosem.protocol.xdlms.conformance import Conformance |
| 21 | + |
| 22 | +# set up logging so you get a bit nicer printout of what is happening. |
| 23 | +logging.basicConfig( |
| 24 | + level=logging.DEBUG, |
| 25 | + format="%(asctime)s,%(msecs)d : %(levelname)s : %(message)s", |
| 26 | + datefmt="%H:%M:%S", |
| 27 | +) |
| 28 | + |
| 29 | +c = Conformance( |
| 30 | + general_protection=False, |
| 31 | + general_block_transfer=False, |
| 32 | + delta_value_encoding=False, |
| 33 | + attribute_0_supported_with_set=False, |
| 34 | + priority_management_supported=False, |
| 35 | + attribute_0_supported_with_get=False, |
| 36 | + block_transfer_with_get_or_read=True, |
| 37 | + block_transfer_with_set_or_write=False, |
| 38 | + block_transfer_with_action=True, |
| 39 | + multiple_references=True, |
| 40 | + data_notification=False, |
| 41 | + access=False, |
| 42 | + get=True, |
| 43 | + set=True, |
| 44 | + selective_access=True, |
| 45 | + event_notification=False, |
| 46 | + action=True, |
| 47 | +) |
| 48 | + |
| 49 | +encryption_key = bytes.fromhex("990EB3136F283EDB44A79F15F0BFCC21") |
| 50 | +authentication_key = bytes.fromhex("EC29E2F4BD7D697394B190827CE3DD9A") |
| 51 | +auth = enumerations.AuthenticationMechanism.HLS_GMAC |
| 52 | +host = "100.119.108.3" |
| 53 | +port = 4059 |
| 54 | + |
| 55 | + |
| 56 | +tcp_io = BlockingTcpIO(host=host, port=port) |
| 57 | +public_tcp_transport = TcpTransport( |
| 58 | + client_logical_address=16, |
| 59 | + server_logical_address=1, |
| 60 | + io=tcp_io, |
| 61 | +) |
| 62 | +public_client = DlmsClient( |
| 63 | + transport=public_tcp_transport, authentication=NoSecurityAuthentication() |
| 64 | +) |
| 65 | + |
| 66 | + |
| 67 | +with public_client.session() as client: |
| 68 | + |
| 69 | + response_data = client.get( |
| 70 | + cosem.CosemAttribute( |
| 71 | + interface=enumerations.CosemInterface.DATA, |
| 72 | + instance=cosem.Obis(0, 0, 0x2B, 1, 0), |
| 73 | + attribute=2, |
| 74 | + ) |
| 75 | + ) |
| 76 | + data_decoder = a_xdr.AXdrDecoder( |
| 77 | + encoding_conf=a_xdr.EncodingConf( |
| 78 | + attributes=[a_xdr.Sequence(attribute_name="data")] |
| 79 | + ) |
| 80 | + ) |
| 81 | + invocation_counter = data_decoder.decode(response_data)["data"] |
| 82 | + print(f"meter_initial_invocation_counter = {invocation_counter}") |
| 83 | + |
| 84 | +# we are not reusing the socket as of now. We just need to give the meter some time to |
| 85 | +# close the connection on its side |
| 86 | +sleep(2) |
| 87 | + |
| 88 | +tcp_io = BlockingTcpIO(host=host, port=port) |
| 89 | +management_tcp_transport = TcpTransport( |
| 90 | + client_logical_address=1, |
| 91 | + server_logical_address=1, |
| 92 | + io=tcp_io, |
| 93 | +) |
| 94 | + |
| 95 | +management_client = DlmsClient( |
| 96 | + transport=management_tcp_transport, |
| 97 | + authentication=HighLevelSecurityGmacAuthentication(challenge_length=32), |
| 98 | + encryption_key=encryption_key, |
| 99 | + authentication_key=authentication_key, |
| 100 | + client_initial_invocation_counter=invocation_counter + 1, |
| 101 | +) |
| 102 | + |
| 103 | + |
| 104 | +with management_client.session() as client: |
| 105 | + read_to = dateparser.parse("2024-08-15T00:00:00-01:00") |
| 106 | + read_from = read_to - timedelta(days=500) |
| 107 | + profile = client.get( |
| 108 | + cosem.CosemAttribute( |
| 109 | + interface=enumerations.CosemInterface.PROFILE_GENERIC, |
| 110 | + instance=cosem.Obis(1, 0, 99, 2, 0), |
| 111 | + attribute=2, |
| 112 | + ), |
| 113 | + access_descriptor=RangeDescriptor( |
| 114 | + restricting_object=selective_access.CaptureObject( |
| 115 | + cosem_attribute=cosem.CosemAttribute( |
| 116 | + interface=enumerations.CosemInterface.CLOCK, |
| 117 | + instance=cosem.Obis.from_string("0.0.1.0.0.255"), |
| 118 | + attribute=2, |
| 119 | + ), |
| 120 | + data_index=0, |
| 121 | + ), |
| 122 | + from_value=read_from, |
| 123 | + to_value=read_to, |
| 124 | + ), |
| 125 | + ) |
| 126 | + |
| 127 | + time_error = datetime.now(tz=timezone.utc) - read_to |
| 128 | + |
| 129 | + |
| 130 | + parser = ProfileGenericBufferParser( |
| 131 | + capture_objects=[ |
| 132 | + cosem.CosemAttribute( |
| 133 | + interface=enumerations.CosemInterface.CLOCK, |
| 134 | + instance=cosem.Obis(0, 0, 1, 0, 0, 255), |
| 135 | + attribute=2, |
| 136 | + ), |
| 137 | + cosem.CosemAttribute( |
| 138 | + interface=enumerations.CosemInterface.DATA, |
| 139 | + instance=cosem.Obis(0, 0, 96, 10, 1, 255), |
| 140 | + attribute=2, |
| 141 | + ), |
| 142 | + cosem.CosemAttribute( |
| 143 | + interface=enumerations.CosemInterface.REGISTER, |
| 144 | + instance=cosem.Obis(1, 0, 1, 8, 0, 255), |
| 145 | + attribute=2, |
| 146 | + ), |
| 147 | + cosem.CosemAttribute( |
| 148 | + interface=enumerations.CosemInterface.REGISTER, |
| 149 | + instance=cosem.Obis(1, 0, 2, 8, 0, 255), |
| 150 | + attribute=2, |
| 151 | + ), |
| 152 | + ], |
| 153 | + capture_period=60, |
| 154 | + ) |
| 155 | + #result = parser.parse_bytes(profile) |
| 156 | + result=None |
| 157 | + pprint(profile) |
| 158 | + data = parse_as_dlms_data(profile) |
| 159 | + |
| 160 | + |
| 161 | + with open("data_output.csv", "w", newline="") as csvfile: |
| 162 | + writer = csv.DictWriter(csvfile, fieldnames=["value"]) |
| 163 | + writer.writeheader() |
| 164 | + for row in data: |
| 165 | + writer.writerow({"value": row[2]}) |
| 166 | + |
0 commit comments