hat.gateway.devices.iec61850.client

IEC 61850 client device

   1"""IEC 61850 client device"""
   2
   3from collections.abc import Collection
   4import asyncio
   5import collections
   6import datetime
   7import logging
   8import math
   9
  10from hat import aio
  11from hat import json
  12from hat import util
  13from hat.drivers import iec61850
  14from hat.drivers import tcp
  15import hat.event.common
  16
  17from hat.gateway import common
  18
  19
  20mlog: logging.Logger = logging.getLogger(__name__)
  21
  22
  23termination_timeout: int = 100
  24
  25report_segments_timeout: int = 100
  26
  27
  28async def create(conf: common.DeviceConf,
  29                 eventer_client: hat.event.eventer.Client,
  30                 event_type_prefix: common.EventTypePrefix
  31                 ) -> 'Iec61850ClientDevice':
  32
  33    value_types = {}
  34    value_types_61850 = {}
  35    for vt in conf['value_types']:
  36        ref = (vt['logical_device'],
  37               vt['logical_node'],
  38               vt['fc'],
  39               vt['name'])
  40        value_types_61850[ref] = _vtype_61850_from_vtype_conf(vt['type'])
  41        value_types[ref] = _vtype_from_vtype_conf(vt['type'])
  42
  43    rcb_confs = {i['report_id']: i for i in conf['rcbs']}
  44    dataset_confs = {_dataset_ref_from_conf(ds_conf['ref']): ds_conf
  45                     for ds_conf in conf['datasets']}
  46    value_ref_data_names = collections.defaultdict(collections.deque)
  47    data_value_types = {}
  48    for data_conf in conf['data']:
  49        data_v_ref = _value_ref_from_conf(data_conf['value'])
  50        data_value_types[data_v_ref] = _value_type_from_ref(
  51            value_types, data_v_ref)
  52
  53        q_ref = (_value_ref_from_conf(data_conf['quality'])
  54                 if data_conf.get('quality') else None)
  55        if data_conf.get('quality'):
  56            q_type = _value_type_from_ref(value_types, q_ref)
  57            if q_type != iec61850.AcsiValueType.QUALITY:
  58                raise Exception(f"invalid quality type {q_ref}")
  59
  60        t_ref = (_value_ref_from_conf(data_conf['timestamp'])
  61                 if data_conf.get('timestamp') else None)
  62        if t_ref:
  63            t_type = _value_type_from_ref(value_types, t_ref)
  64            if t_type != iec61850.AcsiValueType.TIMESTAMP:
  65                raise Exception(f"invalid timestamp type {t_ref}")
  66
  67        seld_ref = (_value_ref_from_conf(data_conf['selected'])
  68                    if data_conf.get('selected') else None)
  69        if seld_ref:
  70            seld_type = _value_type_from_ref(value_types, seld_ref)
  71            if seld_type != iec61850.BasicValueType.BOOLEAN:
  72                raise Exception(f"invalid selected type {seld_ref}")
  73
  74        rcb_conf = rcb_confs[data_conf['report_id']]
  75        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
  76        ds_conf = dataset_confs[ds_ref]
  77        for value_conf in ds_conf['values']:
  78            value_ref = _value_ref_from_conf(value_conf)
  79            if (_refs_match(data_v_ref, value_ref) or
  80                    (q_ref and _refs_match(q_ref, value_ref)) or
  81                    (t_ref and _refs_match(t_ref, value_ref)) or
  82                    (seld_ref and _refs_match(seld_ref, value_ref))):
  83                value_ref_data_names[value_ref].append(data_conf['name'])
  84
  85    dataset_values_ref_type = {}
  86    for ds_conf in conf['datasets']:
  87        for val_ref_conf in ds_conf['values']:
  88            value_ref = _value_ref_from_conf(val_ref_conf)
  89            value_type = _value_type_from_ref(value_types, value_ref)
  90            dataset_values_ref_type[value_ref] = value_type
  91
  92    command_ref_value_type = {}
  93    for cmd_conf in conf['commands']:
  94        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
  95        value_type = _value_type_from_ref(value_types, cmd_ref)
  96        command_ref_value_type[cmd_ref] = value_type
  97
  98    change_ref_value_type = {}
  99    for change_conf in conf['changes']:
 100        value_ref = _value_ref_from_conf(change_conf['ref'])
 101        value_type = _value_type_from_ref(value_types, value_ref)
 102        change_ref_value_type[value_ref] = value_type
 103
 104    entry_id_event_types = [
 105        (*event_type_prefix, 'gateway', 'entry_id', i['report_id'])
 106        for i in conf['rcbs'] if i['ref']['type'] == 'BUFFERED']
 107    if entry_id_event_types:
 108        result = await eventer_client.query(
 109            hat.event.common.QueryLatestParams(entry_id_event_types))
 110        rcbs_entry_ids = {
 111            event.type[5]: (bytes.fromhex(event.payload.data)
 112                            if event.payload.data is not None else None)
 113            for event in result.events}
 114    else:
 115        rcbs_entry_ids = {}
 116
 117    device = Iec61850ClientDevice()
 118
 119    device._rcbs_entry_ids = rcbs_entry_ids
 120    device._conf = conf
 121    device._eventer_client = eventer_client
 122    device._event_type_prefix = event_type_prefix
 123    device._conn = None
 124    device._conn_status = None
 125    device._terminations = {}
 126    device._reports_segments = {}
 127
 128    device._value_ref_data_names = value_ref_data_names
 129    device._data_value_types = data_value_types
 130    device._dataset_values_ref_type = dataset_values_ref_type
 131    device._command_ref_value_type = command_ref_value_type
 132    device._change_ref_value_type = change_ref_value_type
 133    device._data_name_confs = {i['name']: i for i in conf['data']}
 134    device._command_name_confs = {i['name']: i for i in conf['commands']}
 135    device._command_name_ctl_nums = {i['name']: 0 for i in conf['commands']}
 136    device._change_name_value_refs = {
 137        i['name']: _value_ref_from_conf(i['ref']) for i in conf['changes']}
 138    device._rcb_type = {rcb_conf['report_id']: rcb_conf['ref']['type']
 139                        for rcb_conf in conf['rcbs']}
 140    device._persist_dyn_datasets = set(_get_persist_dyn_datasets(conf))
 141    device._dyn_datasets_values = dict(_get_dyn_datasets_values(conf))
 142    device._report_data_refs = collections.defaultdict(collections.deque)
 143    for rcb_conf in device._conf['rcbs']:
 144        report_id = rcb_conf['report_id']
 145        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
 146        for value_conf in dataset_confs[ds_ref]['values']:
 147            value_ref = _value_ref_from_conf(value_conf)
 148            device._report_data_refs[report_id].append(value_ref)
 149
 150    device._dataset_change_value_types = dict(
 151        _get_dataset_change_value_types(conf, value_types_61850))
 152    device._cmd_value_types = dict(
 153        _get_cmd_value_types(conf, value_types_61850))
 154
 155    device._log = _create_logger_adapter(conf['name'])
 156
 157    device._async_group = aio.Group()
 158    device._async_group.spawn(device._connection_loop)
 159    device._loop = asyncio.get_running_loop()
 160
 161    return device
 162
 163
 164info: common.DeviceInfo = common.DeviceInfo(
 165    type="iec61850_client",
 166    create=create,
 167    json_schema_id="hat-gateway://iec61850.yaml#/$defs/client",
 168    json_schema_repo=common.json_schema_repo)
 169
 170
 171class Iec61850ClientDevice(common.Device):
 172
 173    @property
 174    def async_group(self) -> aio.Group:
 175        return self._async_group
 176
 177    async def process_events(self, events: Collection[hat.event.common.Event]):
 178        try:
 179            for event in events:
 180                suffix = event.type[len(self._event_type_prefix):]
 181
 182                if suffix[:2] == ('system', 'command'):
 183                    cmd_name, = suffix[2:]
 184                    await self._process_cmd_req(event, cmd_name)
 185
 186                elif suffix[:2] == ('system', 'change'):
 187                    val_name, = suffix[2:]
 188                    await self._process_change_req(event, val_name)
 189
 190                else:
 191                    raise Exception('unsupported event type')
 192
 193        except Exception as e:
 194            self._log.warning('error processing event %s: %s',
 195                              event.type, e, exc_info=e)
 196
 197    async def _connection_loop(self):
 198
 199        async def cleanup():
 200            await self._register_status('DISCONNECTED')
 201            if self._conn:
 202                await self._conn.async_close()
 203
 204        conn_conf = self._conf['connection']
 205        try:
 206            while True:
 207                await self._register_status('CONNECTING')
 208                try:
 209                    self._log.debug('connecting to %s:%s',
 210                                    conn_conf['host'], conn_conf['port'])
 211                    self._conn = await aio.wait_for(
 212                        iec61850.connect(
 213                            addr=tcp.Address(conn_conf['host'],
 214                                             conn_conf['port']),
 215                            data_value_types=self._dataset_change_value_types,
 216                            cmd_value_types=self._cmd_value_types,
 217                            report_data_refs=self._report_data_refs,
 218                            report_cb=self._on_report,
 219                            termination_cb=self._on_termination,
 220                            status_delay=conn_conf['status_delay'],
 221                            status_timeout=conn_conf['status_timeout'],
 222                            local_tsel=conn_conf.get('local_tsel'),
 223                            remote_tsel=conn_conf.get('remote_tsel'),
 224                            local_ssel=conn_conf.get('local_ssel'),
 225                            remote_ssel=conn_conf.get('remote_ssel'),
 226                            local_psel=conn_conf.get('local_psel'),
 227                            remote_psel=conn_conf.get('remote_psel'),
 228                            local_ap_title=conn_conf.get('local_ap_title'),
 229                            remote_ap_title=conn_conf.get('remote_ap_title'),
 230                            local_ae_qualifier=conn_conf.get(
 231                                'local_ae_qualifier'),
 232                            remote_ae_qualifier=conn_conf.get(
 233                                'remote_ae_qualifier'),
 234                            local_detail_calling=conn_conf.get(
 235                                'local_detail_calling'),
 236                            name=self._conf['name']),
 237                        conn_conf['connect_timeout'])
 238
 239                except Exception as e:
 240                    self._log.warning('connnection failed: %s', e, exc_info=e)
 241                    await self._register_status('DISCONNECTED')
 242                    await asyncio.sleep(conn_conf['reconnect_delay'])
 243                    continue
 244
 245                self._log.debug('connected')
 246                await self._register_status('CONNECTED')
 247
 248                initialized = False
 249                try:
 250                    await self._create_dynamic_datasets()
 251                    for rcb_conf in self._conf['rcbs']:
 252                        await self._init_rcb(rcb_conf)
 253                    initialized = True
 254
 255                except Exception as e:
 256                    self._log.warning(
 257                        'initialization failed: %s, closing connection',
 258                        e, exc_info=e)
 259                    self._conn.close()
 260
 261                await self._conn.wait_closing()
 262                await self._register_status('DISCONNECTED')
 263                await self._conn.wait_closed()
 264                self._conn = None
 265                self._terminations = {}
 266                self._reports_segments = {}
 267                if not initialized:
 268                    await asyncio.sleep(conn_conf['reconnect_delay'])
 269
 270        except Exception as e:
 271            self._log.error('connection loop error: %s', e, exc_info=e)
 272
 273        finally:
 274            self._log.debug('closing connection loop')
 275            self.close()
 276            await aio.uncancellable(cleanup())
 277
 278    async def _process_cmd_req(self, event, cmd_name):
 279        if not self._conn or not self._conn.is_open:
 280            raise Exception('no connection')
 281
 282        if cmd_name not in self._command_name_confs:
 283            raise Exception('unexpected command name')
 284
 285        cmd_conf = self._command_name_confs[cmd_name]
 286        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 287        action = event.payload.data['action']
 288        evt_session_id = event.payload.data['session_id']
 289        if (action == 'SELECT' and
 290                cmd_conf['model'] == 'SBO_WITH_NORMAL_SECURITY'):
 291            cmd = None
 292        else:
 293            ctl_num = self._command_name_ctl_nums[cmd_name]
 294            ctl_num = _update_ctl_num(ctl_num, action, cmd_conf['model'])
 295            self._command_name_ctl_nums[cmd_name] = ctl_num
 296            value_type = self._command_ref_value_type[cmd_ref]
 297            if value_type is None:
 298                raise Exception('value type undefined')
 299
 300            cmd = _command_from_event(event, cmd_conf, ctl_num, value_type)
 301
 302        term_future = None
 303        if (action == 'OPERATE' and
 304                cmd_conf['model'] in ['DIRECT_WITH_ENHANCED_SECURITY',
 305                                      'SBO_WITH_ENHANCED_SECURITY']):
 306            term_future = self._loop.create_future()
 307            self._conn.async_group.spawn(
 308                self._wait_cmd_term, cmd_name, cmd_ref, cmd, evt_session_id,
 309                term_future)
 310
 311        try:
 312            resp = await aio.wait_for(
 313                self._send_command(action, cmd_ref, cmd),
 314                self._conf['connection']['response_timeout'])
 315
 316        except (asyncio.TimeoutError, ConnectionError) as e:
 317            self._log.warning('send command failed: %s', e, exc_info=e)
 318            if term_future and not term_future.done():
 319                term_future.cancel()
 320            return
 321
 322        if resp is not None:
 323            if term_future and not term_future.done():
 324                term_future.cancel()
 325
 326        event = _cmd_resp_to_event(
 327            self._event_type_prefix, cmd_name, evt_session_id, action, resp)
 328        await self._register_events([event])
 329
 330    async def _send_command(self, action, cmd_ref, cmd):
 331        if action == 'SELECT':
 332            return await self._conn.select(cmd_ref, cmd)
 333
 334        if action == 'CANCEL':
 335            return await self._conn.cancel(cmd_ref, cmd)
 336
 337        if action == 'OPERATE':
 338            return await self._conn.operate(cmd_ref, cmd)
 339
 340        raise Exception('unsupported action')
 341
 342    async def _wait_cmd_term(self, cmd_name, cmd_ref, cmd, session_id, future):
 343        cmd_session_id = _get_command_session_id(cmd_ref, cmd)
 344        self._terminations[cmd_session_id] = future
 345        try:
 346            term = await aio.wait_for(future, termination_timeout)
 347            event = _cmd_resp_to_event(
 348                self._event_type_prefix, cmd_name, session_id, 'TERMINATION',
 349                term.error)
 350            await self._register_events([event])
 351
 352        except asyncio.TimeoutError:
 353            self._log.warning('command termination timeout')
 354
 355        finally:
 356            del self._terminations[cmd_session_id]
 357
 358    async def _process_change_req(self, event, value_name):
 359        if not self._conn or not self._conn.is_open:
 360            raise Exception('no connection')
 361
 362        if value_name not in self._change_name_value_refs:
 363            raise Exception('unexpected change name')
 364
 365        ref = self._change_name_value_refs[value_name]
 366        value_type = self._change_ref_value_type[ref]
 367        if value_type is None:
 368            raise Exception('value type undefined')
 369
 370        value = _value_from_json(event.payload.data['value'], value_type)
 371        try:
 372            resp = await aio.wait_for(
 373                self._conn.write_data(ref, value),
 374                self._conf['connection']['response_timeout'])
 375
 376        except asyncio.TimeoutError:
 377            self._log.warning('write data response timeout')
 378            return
 379
 380        except ConnectionError as e:
 381            self._log.warning('connection error on write data: %s',
 382                              e, exc_info=e)
 383            return
 384
 385        session_id = event.payload.data['session_id']
 386        event = _write_data_resp_to_event(
 387            self._event_type_prefix, value_name, session_id, resp)
 388        await self._register_events([event])
 389
 390    async def _on_report(self, report):
 391        try:
 392            events = list(self._events_from_report(report))
 393            if not events:
 394                return
 395
 396            await self._register_events(events)
 397            if self._rcb_type[report.report_id] == 'BUFFERED':
 398                self._rcbs_entry_ids[report.report_id] = report.entry_id
 399
 400        except Exception as e:
 401            self._log.warning('report %s ignored: %s',
 402                              report.report_id, e, exc_info=e)
 403
 404    def _events_from_report(self, report):
 405        report_id = report.report_id
 406        if report_id not in self._report_data_refs:
 407            raise Exception(f'unexpected report {report_id}')
 408
 409        segm_id = (report_id, report.sequence_number)
 410        if report.more_segments_follow:
 411            if segm_id in self._reports_segments:
 412                segment_data, timeout_timer = self._reports_segments[segm_id]
 413                timeout_timer.cancel()
 414            else:
 415                segment_data = collections.deque()
 416
 417            segment_data.extend(report.data)
 418            timeout_timer = self._loop.call_later(
 419                report_segments_timeout, self._reports_segments.pop, segm_id)
 420            self._reports_segments[segm_id] = (segment_data, timeout_timer)
 421            return
 422
 423        if segm_id in self._reports_segments:
 424            report_data, timeout_timer = self._reports_segments.pop(segm_id)
 425            timeout_timer.cancel()
 426            report_data.extend(report.data)
 427
 428        else:
 429            report_data = report.data
 430
 431        yield from self._events_from_report_data(report_data, report_id)
 432
 433        if self._rcb_type[report_id] == 'BUFFERED':
 434            yield hat.event.common.RegisterEvent(
 435                type=(*self._event_type_prefix, 'gateway',
 436                      'entry_id', report_id),
 437                source_timestamp=None,
 438                payload=hat.event.common.EventPayloadJson(
 439                    report.entry_id.hex()
 440                    if report.entry_id is not None else None))
 441
 442    def _events_from_report_data(self, report_data, report_id):
 443        data_values_json = collections.defaultdict(dict)
 444        data_reasons = collections.defaultdict(set)
 445        for rv in report_data:
 446            if rv.ref not in self._value_ref_data_names:
 447                continue
 448
 449            value_type = self._dataset_values_ref_type[rv.ref]
 450            if value_type is None:
 451                self._log.warning('report data ignored: unknown value type')
 452                continue
 453
 454            value_json = _value_to_json(rv.value, value_type)
 455            for data_name in self._value_ref_data_names[rv.ref]:
 456                value_path = [rv.ref.logical_device, rv.ref.logical_node,
 457                              rv.ref.fc, *rv.ref.names]
 458                data_values_json[data_name] = json.set_(
 459                    data_values_json[data_name], value_path, value_json)
 460                if rv.reasons:
 461                    data_reasons[data_name].update(
 462                        reason.name for reason in rv.reasons)
 463
 464        for data_name, values_json in data_values_json.items():
 465            payload = {'reasons': list(data_reasons[data_name])}
 466            data_conf = self._data_name_confs[data_name]
 467            value_path = _conf_ref_to_path(data_conf['value'])
 468            value_json = json.get(values_json, value_path)
 469            if value_json is not None:
 470                value_ref = _value_ref_from_conf(data_conf['value'])
 471                value_type = self._data_value_types[value_ref]
 472                payload['value'] = _value_json_to_event_json(
 473                    value_json, value_type)
 474
 475            if 'quality' in data_conf:
 476                quality_path = _conf_ref_to_path(data_conf['quality'])
 477                quality_json = json.get(values_json, quality_path)
 478                if quality_json is not None:
 479                    payload['quality'] = quality_json
 480
 481            if 'timestamp' in data_conf:
 482                timestamp_path = _conf_ref_to_path(data_conf['timestamp'])
 483                timestamp_json = json.get(values_json, timestamp_path)
 484                if timestamp_json is not None:
 485                    payload['timestamp'] = timestamp_json
 486
 487            if 'selected' in data_conf:
 488                selected_path = _conf_ref_to_path(data_conf['selected'])
 489                selected_json = json.get(values_json, selected_path)
 490                if selected_json is not None:
 491                    payload['selected'] = selected_json
 492
 493            yield hat.event.common.RegisterEvent(
 494                type=(*self._event_type_prefix, 'gateway',
 495                      'data', data_name),
 496                source_timestamp=None,
 497                payload=hat.event.common.EventPayloadJson(payload))
 498
 499    def _on_termination(self, termination):
 500        cmd_session_id = _get_command_session_id(
 501            termination.ref, termination.cmd)
 502        if cmd_session_id not in self._terminations:
 503            self._log.warning('unexpected termination dropped')
 504            return
 505
 506        term_future = self._terminations[cmd_session_id]
 507        if not term_future.done():
 508            self._terminations[cmd_session_id].set_result(termination)
 509
 510    async def _init_rcb(self, rcb_conf):
 511        ref = iec61850.RcbRef(
 512            logical_device=rcb_conf['ref']['logical_device'],
 513            logical_node=rcb_conf['ref']['logical_node'],
 514            type=iec61850.RcbType[rcb_conf['ref']['type']],
 515            name=rcb_conf['ref']['name'])
 516        self._log.debug('initiating rcb %s', ref)
 517
 518        get_attrs = collections.deque([iec61850.RcbAttrType.REPORT_ID])
 519        dataset_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
 520        if dataset_ref not in self._dyn_datasets_values:
 521            get_attrs.append(iec61850.RcbAttrType.DATASET)
 522        if 'conf_revision' in rcb_conf:
 523            get_attrs.append(iec61850.RcbAttrType.CONF_REVISION)
 524
 525        get_rcb_resp = await self._conn.get_rcb_attrs(ref, get_attrs)
 526        _validate_get_rcb_response(get_rcb_resp, rcb_conf)
 527
 528        if ref.type == iec61850.RcbType.BUFFERED:
 529            if 'reservation_time' in rcb_conf:
 530                await self._set_rcb(
 531                    ref, [(iec61850.RcbAttrType.RESERVATION_TIME,
 532                           rcb_conf['reservation_time'])])
 533        elif ref.type == iec61850.RcbType.UNBUFFERED:
 534            await self._set_rcb(ref, [(iec61850.RcbAttrType.RESERVE, True)])
 535        else:
 536            raise Exception('unexpected rcb type')
 537
 538        await self._set_rcb(ref, [(iec61850.RcbAttrType.REPORT_ENABLE, False)])
 539
 540        if dataset_ref in self._dyn_datasets_values:
 541            await self._set_rcb(
 542                ref, [(iec61850.RcbAttrType.DATASET, dataset_ref)],
 543                critical=True)
 544
 545        if ref.type == iec61850.RcbType.BUFFERED:
 546            entry_id = self._rcbs_entry_ids.get(rcb_conf['report_id'])
 547            if rcb_conf.get('purge_buffer') or entry_id is None:
 548                await self._set_rcb(
 549                    ref, [(iec61850.RcbAttrType.PURGE_BUFFER, True)])
 550
 551            else:
 552                try:
 553                    await self._set_rcb(
 554                        ref, [(iec61850.RcbAttrType.ENTRY_ID, entry_id)],
 555                        critical=True)
 556
 557                except Exception as e:
 558                    self._log.warning('%s', e, exc_info=e)
 559                    # try setting entry id to 0 in order to resynchronize
 560                    await self._set_rcb(
 561                        ref, [(iec61850.RcbAttrType.ENTRY_ID, b'\x00')])
 562
 563        attrs = collections.deque()
 564        if 'trigger_options' in rcb_conf:
 565            attrs.append((iec61850.RcbAttrType.TRIGGER_OPTIONS,
 566                          set(iec61850.TriggerCondition[i]
 567                              for i in rcb_conf['trigger_options'])))
 568        if 'optional_fields' in rcb_conf:
 569            attrs.append((iec61850.RcbAttrType.OPTIONAL_FIELDS,
 570                          set(iec61850.OptionalField[i]
 571                              for i in rcb_conf['optional_fields'])))
 572        if 'buffer_time' in rcb_conf:
 573            attrs.append((iec61850.RcbAttrType.BUFFER_TIME,
 574                          rcb_conf['buffer_time']))
 575        if 'integrity_period' in rcb_conf:
 576            attrs.append((iec61850.RcbAttrType.INTEGRITY_PERIOD,
 577                          rcb_conf['integrity_period']))
 578        if attrs:
 579            await self._set_rcb(ref, attrs)
 580
 581        await self._set_rcb(
 582            ref, [(iec61850.RcbAttrType.REPORT_ENABLE, True)], critical=True)
 583        await self._set_rcb(
 584            ref, [(iec61850.RcbAttrType.GI, True)], critical=True)
 585        self._log.debug('rcb %s initiated', ref)
 586
 587    async def _set_rcb(self, ref, attrs, critical=False):
 588        try:
 589            resp = await self._conn.set_rcb_attrs(ref, attrs)
 590            attrs_failed = set((attr, attr_res)
 591                               for attr, attr_res in resp.items()
 592                               if isinstance(attr_res, iec61850.ServiceError))
 593            if attrs_failed:
 594                raise Exception(f"set attribute errors: {attrs_failed}")
 595
 596        except Exception as e:
 597            if critical:
 598                raise Exception(f'set rcb {ref} failed') from e
 599
 600            else:
 601                self._log.warning('set rcb %s failed: %s', ref, e, exc_info=e)
 602
 603    async def _create_dynamic_datasets(self):
 604        existing_ds_refs = set()
 605        for ds_ref in self._persist_dyn_datasets:
 606            ld = ds_ref.logical_device
 607            res = await self._conn.get_persisted_dataset_refs(ld)
 608            if isinstance(res, iec61850.ServiceError):
 609                raise Exception(f'get datasets for ld {ld} failed: {res}')
 610
 611            existing_ds_refs.update(res)
 612
 613        existing_persisted_ds_refs = existing_ds_refs.intersection(
 614            self._persist_dyn_datasets)
 615        for ds_ref, ds_value_refs in self._dyn_datasets_values.items():
 616            if ds_ref in existing_persisted_ds_refs:
 617                res = await self._conn.get_dataset_data_refs(ds_ref)
 618                if isinstance(res, iec61850.ServiceError):
 619                    raise Exception(f'get ds {ds_ref} data refs failed: {res}')
 620                else:
 621                    exist_ds_value_refs = res
 622
 623                if ds_value_refs == list(exist_ds_value_refs):
 624                    self._log.debug('dataset %s already exists', ds_ref)
 625                    continue
 626
 627                raise Exception('persisted dataset changed')
 628
 629            res = await self._conn.create_dataset(ds_ref, ds_value_refs)
 630            if res is not None:
 631                raise Exception(f'create dataset {ds_ref} failed: {res}')
 632
 633            self._log.debug("dataset %s crated", ds_ref)
 634
 635    async def _register_events(self, events):
 636        try:
 637            await self._eventer_client.register(events)
 638
 639        except ConnectionError:
 640            self.close()
 641            raise
 642
 643    async def _register_status(self, status):
 644        if status == self._conn_status:
 645            return
 646
 647        event = hat.event.common.RegisterEvent(
 648            type=(*self._event_type_prefix, 'gateway', 'status'),
 649            source_timestamp=None,
 650            payload=hat.event.common.EventPayloadJson(status))
 651        await self._register_events([event])
 652        self._conn_status = status
 653        self._log.debug('registered status %s', status)
 654
 655
 656def _value_ref_from_conf(value_ref_conf):
 657    return iec61850.DataRef(
 658        logical_device=value_ref_conf['logical_device'],
 659        logical_node=value_ref_conf['logical_node'],
 660        fc=value_ref_conf['fc'],
 661        names=tuple(value_ref_conf['names']))
 662
 663
 664def _dataset_ref_from_conf(ds_conf):
 665    if isinstance(ds_conf, str):
 666        return iec61850.NonPersistedDatasetRef(ds_conf)
 667
 668    elif isinstance(ds_conf, dict):
 669        return iec61850.PersistedDatasetRef(**ds_conf)
 670
 671
 672def _refs_match(ref1, ref2):
 673    if ref1.logical_device != ref2.logical_device:
 674        return False
 675
 676    if ref1.logical_node != ref2.logical_node:
 677        return False
 678
 679    if ref1.fc != ref2.fc:
 680        return False
 681
 682    if len(ref1.names) == len(ref2.names):
 683        return ref1.names == ref2.names
 684
 685    if len(ref1.names) < len(ref2.names):
 686        names1, names2 = ref1.names, ref2.names
 687    else:
 688        names1, names2 = ref2.names, ref1.names
 689
 690    return names2[:len(names1)] == names1
 691
 692
 693def _get_persist_dyn_datasets(conf):
 694    for ds_conf in conf['datasets']:
 695        if not ds_conf['dynamic']:
 696            continue
 697
 698        ds_ref = _dataset_ref_from_conf(ds_conf['ref'])
 699        if isinstance(ds_ref, iec61850.PersistedDatasetRef):
 700            yield ds_ref
 701
 702
 703def _get_dyn_datasets_values(conf):
 704    for ds_conf in conf['datasets']:
 705        if not ds_conf['dynamic']:
 706            continue
 707
 708        ds_ref = _dataset_ref_from_conf(ds_conf['ref'])
 709        yield ds_ref, [_value_ref_from_conf(val_conf)
 710                       for val_conf in ds_conf['values']]
 711
 712
 713def _vtype_61850_from_vtype_conf(vt_conf):
 714    if isinstance(vt_conf, str):
 715        return _value_type_from_str(vt_conf)
 716
 717    if vt_conf['type'] == 'ARRAY':
 718        return iec61850.ArrayValueType(
 719            type=_vtype_61850_from_vtype_conf(vt_conf['element_type']),
 720            length=vt_conf['length'])
 721
 722    if vt_conf['type'] == 'STRUCT':
 723        return iec61850.StructValueType(
 724            [(el_conf['name'], _vtype_61850_from_vtype_conf(el_conf['type']))
 725             for el_conf in vt_conf['elements']])
 726
 727    raise Exception('unsupported value type')
 728
 729
 730def _vtype_from_vtype_conf(vt_conf):
 731    if isinstance(vt_conf, str):
 732        return _value_type_from_str(vt_conf)
 733
 734    if vt_conf['type'] == 'ARRAY':
 735        return iec61850.ArrayValueType(
 736            type=_vtype_from_vtype_conf(vt_conf['element_type']),
 737            length=vt_conf['length'])
 738
 739    if vt_conf['type'] == 'STRUCT':
 740        return {el_conf['name']: _vtype_from_vtype_conf(el_conf['type'])
 741                for el_conf in vt_conf['elements']}
 742
 743    raise Exception('unsupported value type')
 744
 745
 746def _value_type_from_str(vt_conf):
 747    if vt_conf in ['BOOLEAN',
 748                   'INTEGER',
 749                   'UNSIGNED',
 750                   'FLOAT',
 751                   'BIT_STRING',
 752                   'OCTET_STRING',
 753                   'VISIBLE_STRING',
 754                   'MMS_STRING']:
 755        return iec61850.BasicValueType(vt_conf)
 756
 757    if vt_conf in ['QUALITY',
 758                   'TIMESTAMP',
 759                   'DOUBLE_POINT',
 760                   'DIRECTION',
 761                   'SEVERITY',
 762                   'ANALOGUE',
 763                   'VECTOR',
 764                   'STEP_POSITION',
 765                   'BINARY_CONTROL']:
 766        return iec61850.AcsiValueType(vt_conf)
 767
 768    raise Exception('unsupported value type')
 769
 770
 771def _value_type_from_ref(value_types, ref):
 772    left_names = []
 773    if isinstance(ref, iec61850.CommandRef):
 774        left_names = ['Oper', 'ctlVal']
 775        key = (ref.logical_device, ref.logical_node, 'CO', ref.name)
 776
 777    elif isinstance(ref, iec61850.DataRef):
 778        left_names = ref.names[1:]
 779        key = (ref.logical_device, ref.logical_node, ref.fc, ref.names[0])
 780
 781    else:
 782        raise Exception('unexpected reference')
 783
 784    if key not in value_types:
 785        return
 786
 787    value_type = value_types[key]
 788    while left_names:
 789        name = left_names[0]
 790        left_names = left_names[1:]
 791        if isinstance(name, str):
 792            if isinstance(value_type, iec61850.StructValueType):
 793                _, value_type = util.first(
 794                    value_type.elements, lambda i: i[0] == name)
 795
 796            elif value_type == iec61850.AcsiValueType.ANALOGUE:
 797                if name == 'i':
 798                    value_type = iec61850.BasicValueType.INTEGER
 799                elif name == 'f':
 800                    value_type = iec61850.BasicValueType.FLOAT
 801                else:
 802                    raise Exception("analogue attribute should be 'i' or 'f'")
 803
 804            elif value_type == iec61850.AcsiValueType.VECTOR:
 805                if name in ('mag', 'ang'):
 806                    value_type = iec61850.AcsiValueType.ANALOGUE
 807                else:
 808                    raise Exception(
 809                        "vector attribute should be 'mag' or 'ang'")
 810
 811            elif value_type == iec61850.AcsiValueType.STEP_POSITION:
 812                if name == 'posVal':
 813                    value_type = iec61850.BasicValueType.INTEGER
 814                elif name == 'transInd':
 815                    value_type = iec61850.BasicValueType.BOOLEAN
 816                else:
 817                    raise Exception("step position attribute should be "
 818                                    "'posVal' or 'transInd'")
 819
 820            else:
 821                value_type = value_type[name]
 822
 823        if isinstance(name, int):
 824            value_type = value_type.type
 825
 826    return value_type
 827
 828
 829def _get_dataset_change_value_types(conf, value_types):
 830    for ds_conf in conf['datasets']:
 831        for val_ref_conf in ds_conf['values']:
 832            value_ref = _value_ref_from_conf(val_ref_conf)
 833            value_type = _value_type_from_ref(value_types, value_ref)
 834            yield value_ref, value_type
 835
 836    for change_conf in conf['changes']:
 837        value_ref = _value_ref_from_conf(change_conf['ref'])
 838        value_type = _value_type_from_ref(value_types, value_ref)
 839        yield value_ref, value_type
 840
 841
 842def _get_cmd_value_types(conf, value_types):
 843    for cmd_conf in conf['commands']:
 844        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 845        value_type = _value_type_from_ref(value_types, cmd_ref)
 846        yield cmd_ref, value_type
 847
 848
 849_epoch_start = datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
 850
 851
 852def _command_from_event(event, cmd_conf, control_number, value_type):
 853    if cmd_conf['with_operate_time']:
 854        operate_time = iec61850.Timestamp(
 855            value=_epoch_start,
 856            leap_second=False,
 857            clock_failure=False,
 858            not_synchronized=False,
 859            accuracy=0)
 860    else:
 861        operate_time = None
 862    return iec61850.Command(
 863        value=_value_from_json(event.payload.data['value'], value_type),
 864        operate_time=operate_time,
 865        origin=iec61850.Origin(
 866            category=iec61850.OriginCategory[
 867                event.payload.data['origin']['category']],
 868            identification=event.payload.data[
 869                'origin']['identification'].encode('utf-8')),
 870        control_number=control_number,
 871        t=_timestamp_from_event_timestamp(event.timestamp),
 872        test=event.payload.data['test'],
 873        checks=set(iec61850.Check[i] for i in event.payload.data['checks']))
 874
 875
 876def _timestamp_from_event_timestamp(timestamp):
 877    return iec61850.Timestamp(
 878        value=hat.event.common.timestamp_to_datetime(timestamp),
 879        leap_second=False,
 880        clock_failure=False,
 881        not_synchronized=False,
 882        accuracy=None)
 883
 884
 885def _cmd_resp_to_event(event_type_prefix, cmd_name, event_session_id, action,
 886                       resp):
 887    success = resp is None
 888    payload = {'session_id': event_session_id,
 889               'action': action,
 890               'success': success}
 891    if not success:
 892        if resp.service_error is not None:
 893            payload['service_error'] = resp.service_error.name
 894        if resp.additional_cause is not None:
 895            payload['additional_cause'] = resp.additional_cause.name
 896        if resp.test_error is not None:
 897            payload['test_error'] = resp.test_error.name
 898
 899    return hat.event.common.RegisterEvent(
 900        type=(*event_type_prefix, 'gateway', 'command', cmd_name),
 901        source_timestamp=None,
 902        payload=hat.event.common.EventPayloadJson(payload))
 903
 904
 905def _write_data_resp_to_event(event_type_prefix, value_name, session_id, resp):
 906    success = resp is None
 907    payload = {'session_id': session_id,
 908               'success': success}
 909    if not success:
 910        payload['error'] = resp.name
 911    return hat.event.common.RegisterEvent(
 912        type=(*event_type_prefix, 'gateway', 'change', value_name),
 913        source_timestamp=None,
 914        payload=hat.event.common.EventPayloadJson(payload))
 915
 916
 917def _get_command_session_id(cmd_ref, cmd):
 918    return (cmd_ref, cmd.control_number)
 919
 920
 921def _value_from_json(event_value, value_type):
 922    if isinstance(value_type, iec61850.BasicValueType):
 923        if value_type == iec61850.BasicValueType.OCTET_STRING:
 924            return bytes.fromhex(event_value)
 925
 926        elif value_type == iec61850.BasicValueType.FLOAT:
 927            return float(event_value)
 928
 929        else:
 930            return event_value
 931
 932    if value_type == iec61850.AcsiValueType.QUALITY:
 933        return iec61850.Quality(
 934            validity=iec61850.QualityValidity[event_value['validity']],
 935            details={iec61850.QualityDetail[i]
 936                     for i in event_value['details']},
 937            source=iec61850.QualitySource[event_value['source']],
 938            test=event_value['test'],
 939            operator_blocked=event_value['operator_blocked'])
 940
 941    if value_type == iec61850.AcsiValueType.TIMESTAMP:
 942        return iec61850.Timestamp(
 943            value=datetime.datetime.fromtimestamp(event_value['value'],
 944                                                  datetime.timezone.utc),
 945            leap_second=event_value['leap_second'],
 946            clock_failure=event_value['clock_failure'],
 947            not_synchronized=event_value['not_synchronized'],
 948            accuracy=event_value.get('accuracy'))
 949
 950    if value_type == iec61850.AcsiValueType.DOUBLE_POINT:
 951        return iec61850.DoublePoint[event_value]
 952
 953    if value_type == iec61850.AcsiValueType.DIRECTION:
 954        return iec61850.Direction[event_value]
 955
 956    if value_type == iec61850.AcsiValueType.SEVERITY:
 957        return iec61850.Severity[event_value]
 958
 959    if value_type == iec61850.AcsiValueType.ANALOGUE:
 960        return iec61850.Analogue(
 961            i=event_value.get('i'),
 962            f=(float(event_value['f']) if 'f' in event_value else None))
 963
 964    if value_type == iec61850.AcsiValueType.VECTOR:
 965        return iec61850.Vector(
 966            magnitude=_value_from_json(event_value['magnitude'],
 967                                       iec61850.AcsiValueType.ANALOGUE),
 968            angle=(_value_from_json(event_value['angle'],
 969                                    iec61850.AcsiValueType.ANALOGUE)
 970                   if 'angle' in event_value else None))
 971
 972    if value_type == iec61850.AcsiValueType.STEP_POSITION:
 973        return iec61850.StepPosition(value=event_value['value'],
 974                                     transient=event_value.get('transient'))
 975
 976    if value_type == iec61850.AcsiValueType.BINARY_CONTROL:
 977        return iec61850.BinaryControl[event_value]
 978
 979    if isinstance(value_type, iec61850.ArrayValueType):
 980        return [_value_from_json(val, value_type.type)
 981                for val in event_value]
 982
 983    if isinstance(value_type, dict):
 984        return {k: _value_from_json(v, value_type[k])
 985                for k, v in event_value.items()}
 986
 987    raise Exception('unsupported value type')
 988
 989
 990def _value_to_json(data_value, value_type):
 991    if isinstance(value_type, iec61850.BasicValueType):
 992        if value_type == iec61850.BasicValueType.OCTET_STRING:
 993            return data_value.hex()
 994
 995        elif value_type == iec61850.BasicValueType.BIT_STRING:
 996            return list(data_value)
 997
 998        elif value_type == iec61850.BasicValueType.FLOAT:
 999            return data_value if math.isfinite(data_value) else str(data_value)
1000
1001        else:
1002            return data_value
1003
1004    if isinstance(value_type, iec61850.AcsiValueType):
1005        if value_type == iec61850.AcsiValueType.QUALITY:
1006            return {'validity': data_value.validity.name,
1007                    'details': [i.name for i in data_value.details],
1008                    'source': data_value.source.name,
1009                    'test': data_value.test,
1010                    'operator_blocked': data_value.operator_blocked}
1011
1012        if value_type == iec61850.AcsiValueType.TIMESTAMP:
1013            val = {'value': data_value.value.timestamp(),
1014                   'leap_second': data_value.leap_second,
1015                   'clock_failure': data_value.clock_failure,
1016                   'not_synchronized': data_value.not_synchronized}
1017            if data_value.accuracy is not None:
1018                val['accuracy'] = data_value.accuracy
1019            return val
1020
1021        if value_type in [iec61850.AcsiValueType.DOUBLE_POINT,
1022                          iec61850.AcsiValueType.DIRECTION,
1023                          iec61850.AcsiValueType.SEVERITY,
1024                          iec61850.AcsiValueType.BINARY_CONTROL]:
1025            return data_value.name
1026
1027        if value_type == iec61850.AcsiValueType.ANALOGUE:
1028            val = {}
1029            if data_value.i is not None:
1030                val['i'] = data_value.i
1031            if data_value.f is not None:
1032                val['f'] = (data_value.f if math.isfinite(data_value.f)
1033                            else str(data_value.f))
1034            return val
1035
1036        if value_type == iec61850.AcsiValueType.VECTOR:
1037            val = {'mag': _value_to_json(
1038                        data_value.magnitude, iec61850.AcsiValueType.ANALOGUE)}
1039            if data_value.angle is not None:
1040                val['ang'] = _value_to_json(
1041                    data_value.angle, iec61850.AcsiValueType.ANALOGUE)
1042            return val
1043
1044        if value_type == iec61850.AcsiValueType.STEP_POSITION:
1045            val = {'posVal': data_value.value}
1046            if data_value.transient is not None:
1047                val['transInd'] = data_value.transient
1048            return val
1049
1050    if isinstance(value_type, iec61850.ArrayValueType):
1051        return [_value_to_json(i, value_type.type) for i in data_value]
1052
1053    if isinstance(value_type, dict):
1054        return {
1055            child_name: _value_to_json(child_value, value_type[child_name])
1056            for child_name, child_value in data_value.items()}
1057
1058    raise Exception('unsupported value type')
1059
1060
1061def _value_json_to_event_json(data_value, value_type):
1062    if value_type == iec61850.AcsiValueType.VECTOR:
1063        val = {'magnitude': data_value['mag']}
1064        if 'ang' in data_value:
1065            val['angle'] = data_value['ang']
1066        return val
1067
1068    if value_type == iec61850.AcsiValueType.STEP_POSITION:
1069        val = {'value': data_value['posVal']}
1070        if 'transInd' in data_value:
1071            val['transient'] = data_value['transInd']
1072        return val
1073
1074    if isinstance(value_type, iec61850.ArrayValueType):
1075        return [_value_json_to_event_json(i, value_type.type)
1076                for i in data_value]
1077
1078    if isinstance(value_type, dict):
1079        return {
1080            child_name: _value_json_to_event_json(
1081                child_value, value_type[child_name])
1082            for child_name, child_value in data_value.items()}
1083
1084    return data_value
1085
1086
1087def _update_ctl_num(ctl_num, action, cmd_model):
1088    if action == 'SELECT' or (
1089        action == 'OPERATE' and
1090        cmd_model in ['DIRECT_WITH_NORMAL_SECURITY',
1091                      'DIRECT_WITH_ENHANCED_SECURITY']):
1092        return (ctl_num + 1) % 256
1093
1094    return ctl_num
1095
1096
1097def _validate_get_rcb_response(get_rcb_resp, rcb_conf):
1098    for k, v in get_rcb_resp.items():
1099        if isinstance(v, iec61850.ServiceError):
1100            raise Exception(f"get {k.name} failed: {v}")
1101
1102        if (k == iec61850.RcbAttrType.REPORT_ID and
1103                v != rcb_conf['report_id']):
1104            raise Exception(f"rcb report id {v} different from "
1105                            f"configured {rcb_conf['report_id']}")
1106
1107        if (k == iec61850.RcbAttrType.DATASET and
1108                v != _dataset_ref_from_conf(rcb_conf['dataset'])):
1109            raise Exception(f"rcb dataset {v} different from "
1110                            f"configured {rcb_conf['report_id']}")
1111
1112        if (k == iec61850.RcbAttrType.CONF_REVISION and
1113                v != rcb_conf['conf_revision']):
1114            raise Exception(
1115                f"Conf revision {v} different from "
1116                f"the configuration defined {rcb_conf['conf_revision']}")
1117
1118
1119def _conf_ref_to_path(conf_ref):
1120    return [conf_ref['logical_device'],
1121            conf_ref['logical_node'],
1122            conf_ref['fc'],
1123            *conf_ref['names']]
1124
1125
1126def _create_logger_adapter(name):
1127    extra = {'meta': {'type': 'Iec61850ClientDevice',
1128                      'name': name}}
1129
1130    return logging.LoggerAdapter(mlog, extra)
mlog: logging.Logger = <Logger hat.gateway.devices.iec61850.client (WARNING)>
termination_timeout: int = 100
report_segments_timeout: int = 100
async def create( conf: Union[NoneType, bool, int, float, str, List[ForwardRef('Data')], Dict[str, ForwardRef('Data')]], eventer_client: hat.event.eventer.client.Client, event_type_prefix: tuple[str, str, str]) -> Iec61850ClientDevice:
 29async def create(conf: common.DeviceConf,
 30                 eventer_client: hat.event.eventer.Client,
 31                 event_type_prefix: common.EventTypePrefix
 32                 ) -> 'Iec61850ClientDevice':
 33
 34    value_types = {}
 35    value_types_61850 = {}
 36    for vt in conf['value_types']:
 37        ref = (vt['logical_device'],
 38               vt['logical_node'],
 39               vt['fc'],
 40               vt['name'])
 41        value_types_61850[ref] = _vtype_61850_from_vtype_conf(vt['type'])
 42        value_types[ref] = _vtype_from_vtype_conf(vt['type'])
 43
 44    rcb_confs = {i['report_id']: i for i in conf['rcbs']}
 45    dataset_confs = {_dataset_ref_from_conf(ds_conf['ref']): ds_conf
 46                     for ds_conf in conf['datasets']}
 47    value_ref_data_names = collections.defaultdict(collections.deque)
 48    data_value_types = {}
 49    for data_conf in conf['data']:
 50        data_v_ref = _value_ref_from_conf(data_conf['value'])
 51        data_value_types[data_v_ref] = _value_type_from_ref(
 52            value_types, data_v_ref)
 53
 54        q_ref = (_value_ref_from_conf(data_conf['quality'])
 55                 if data_conf.get('quality') else None)
 56        if data_conf.get('quality'):
 57            q_type = _value_type_from_ref(value_types, q_ref)
 58            if q_type != iec61850.AcsiValueType.QUALITY:
 59                raise Exception(f"invalid quality type {q_ref}")
 60
 61        t_ref = (_value_ref_from_conf(data_conf['timestamp'])
 62                 if data_conf.get('timestamp') else None)
 63        if t_ref:
 64            t_type = _value_type_from_ref(value_types, t_ref)
 65            if t_type != iec61850.AcsiValueType.TIMESTAMP:
 66                raise Exception(f"invalid timestamp type {t_ref}")
 67
 68        seld_ref = (_value_ref_from_conf(data_conf['selected'])
 69                    if data_conf.get('selected') else None)
 70        if seld_ref:
 71            seld_type = _value_type_from_ref(value_types, seld_ref)
 72            if seld_type != iec61850.BasicValueType.BOOLEAN:
 73                raise Exception(f"invalid selected type {seld_ref}")
 74
 75        rcb_conf = rcb_confs[data_conf['report_id']]
 76        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
 77        ds_conf = dataset_confs[ds_ref]
 78        for value_conf in ds_conf['values']:
 79            value_ref = _value_ref_from_conf(value_conf)
 80            if (_refs_match(data_v_ref, value_ref) or
 81                    (q_ref and _refs_match(q_ref, value_ref)) or
 82                    (t_ref and _refs_match(t_ref, value_ref)) or
 83                    (seld_ref and _refs_match(seld_ref, value_ref))):
 84                value_ref_data_names[value_ref].append(data_conf['name'])
 85
 86    dataset_values_ref_type = {}
 87    for ds_conf in conf['datasets']:
 88        for val_ref_conf in ds_conf['values']:
 89            value_ref = _value_ref_from_conf(val_ref_conf)
 90            value_type = _value_type_from_ref(value_types, value_ref)
 91            dataset_values_ref_type[value_ref] = value_type
 92
 93    command_ref_value_type = {}
 94    for cmd_conf in conf['commands']:
 95        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 96        value_type = _value_type_from_ref(value_types, cmd_ref)
 97        command_ref_value_type[cmd_ref] = value_type
 98
 99    change_ref_value_type = {}
100    for change_conf in conf['changes']:
101        value_ref = _value_ref_from_conf(change_conf['ref'])
102        value_type = _value_type_from_ref(value_types, value_ref)
103        change_ref_value_type[value_ref] = value_type
104
105    entry_id_event_types = [
106        (*event_type_prefix, 'gateway', 'entry_id', i['report_id'])
107        for i in conf['rcbs'] if i['ref']['type'] == 'BUFFERED']
108    if entry_id_event_types:
109        result = await eventer_client.query(
110            hat.event.common.QueryLatestParams(entry_id_event_types))
111        rcbs_entry_ids = {
112            event.type[5]: (bytes.fromhex(event.payload.data)
113                            if event.payload.data is not None else None)
114            for event in result.events}
115    else:
116        rcbs_entry_ids = {}
117
118    device = Iec61850ClientDevice()
119
120    device._rcbs_entry_ids = rcbs_entry_ids
121    device._conf = conf
122    device._eventer_client = eventer_client
123    device._event_type_prefix = event_type_prefix
124    device._conn = None
125    device._conn_status = None
126    device._terminations = {}
127    device._reports_segments = {}
128
129    device._value_ref_data_names = value_ref_data_names
130    device._data_value_types = data_value_types
131    device._dataset_values_ref_type = dataset_values_ref_type
132    device._command_ref_value_type = command_ref_value_type
133    device._change_ref_value_type = change_ref_value_type
134    device._data_name_confs = {i['name']: i for i in conf['data']}
135    device._command_name_confs = {i['name']: i for i in conf['commands']}
136    device._command_name_ctl_nums = {i['name']: 0 for i in conf['commands']}
137    device._change_name_value_refs = {
138        i['name']: _value_ref_from_conf(i['ref']) for i in conf['changes']}
139    device._rcb_type = {rcb_conf['report_id']: rcb_conf['ref']['type']
140                        for rcb_conf in conf['rcbs']}
141    device._persist_dyn_datasets = set(_get_persist_dyn_datasets(conf))
142    device._dyn_datasets_values = dict(_get_dyn_datasets_values(conf))
143    device._report_data_refs = collections.defaultdict(collections.deque)
144    for rcb_conf in device._conf['rcbs']:
145        report_id = rcb_conf['report_id']
146        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
147        for value_conf in dataset_confs[ds_ref]['values']:
148            value_ref = _value_ref_from_conf(value_conf)
149            device._report_data_refs[report_id].append(value_ref)
150
151    device._dataset_change_value_types = dict(
152        _get_dataset_change_value_types(conf, value_types_61850))
153    device._cmd_value_types = dict(
154        _get_cmd_value_types(conf, value_types_61850))
155
156    device._log = _create_logger_adapter(conf['name'])
157
158    device._async_group = aio.Group()
159    device._async_group.spawn(device._connection_loop)
160    device._loop = asyncio.get_running_loop()
161
162    return device
info: hat.gateway.common.DeviceInfo = DeviceInfo(type='iec61850_client', create=<function create>, json_schema_id='hat-gateway://iec61850.yaml#/$defs/client', json_schema_repo={'hat-json://path.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-json://path.yaml', 'title': 'JSON Path', 'oneOf': [{'type': 'string'}, {'type': 'integer'}, {'type': 'array', 'items': {'$ref': 'hat-json://path.yaml'}}]}, 'hat-json://logging.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-json://logging.yaml', 'title': 'Logging', 'description': 'Logging configuration', 'type': 'object', 'required': ['version'], 'properties': {'version': {'title': 'Version', 'type': 'integer', 'default': 1}, 'formatters': {'title': 'Formatters', 'type': 'object', 'patternProperties': {'.+': {'title': 'Formatter', 'type': 'object', 'properties': {'format': {'title': 'Format', 'type': 'string', 'default': None}, 'datefmt': {'title': 'Date format', 'type': 'string', 'default': None}}}}}, 'filters': {'title': 'Filters', 'type': 'object', 'patternProperties': {'.+': {'title': 'Filter', 'type': 'object', 'properties': {'name': {'title': 'Logger name', 'type': 'string', 'default': ''}}}}}, 'handlers': {'title': 'Handlers', 'type': 'object', 'patternProperties': {'.+': {'title': 'Handler', 'type': 'object', 'description': 'Additional properties are passed as keyword arguments to\nconstructor\n', 'required': ['class'], 'properties': {'class': {'title': 'Class', 'type': 'string'}, 'level': {'title': 'Level', 'type': 'string'}, 'formatter': {'title': 'Formatter', 'type': 'string'}, 'filters': {'title': 'Filters', 'type': 'array', 'items': {'title': 'Filter id', 'type': 'string'}}}}}}, 'loggers': {'title': 'Loggers', 'type': 'object', 'patternProperties': {'.+': {'title': 'Logger', 'type': 'object', 'properties': {'level': {'title': 'Level', 'type': 'string'}, 'propagate': {'title': 'Propagate', 'type': 'boolean'}, 'filters': {'title': 'Filters', 'type': 'array', 'items': {'title': 'Filter id', 'type': 'string'}}, 'handlers': {'title': 'Handlers', 'type': 'array', 'items': {'title': 'Handler id', 'type': 'string'}}}}}}, 'root': {'title': 'Root logger', 'type': 'object', 'properties': {'level': {'title': 'Level', 'type': 'string'}, 'filters': {'title': 'Filters', 'type': 'array', 'items': {'title': 'Filter id', 'type': 'string'}}, 'handlers': {'title': 'Handlers', 'type': 'array', 'items': {'title': 'Handler id', 'type': 'string'}}}}, 'incremental': {'title': 'Incremental configuration', 'type': 'boolean', 'default': False}, 'disable_existing_loggers': {'title': 'Disable existing loggers', 'type': 'boolean', 'default': True}}}, 'hat-gateway://smpp.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://smpp.yaml', '$defs': {'client': {'type': 'object', 'required': ['name', 'remote_address', 'ssl', 'system_id', 'password', 'enquire_link_delay', 'enquire_link_timeout', 'connect_timeout', 'reconnect_delay', 'short_message', 'priority', 'data_coding', 'message_encoding', 'message_timeout'], 'properties': {'name': {'type': 'string'}, 'remote_address': {'type': 'object', 'required': ['host', 'port'], 'properties': {'host': {'type': 'string'}, 'port': {'type': 'integer'}}}, 'ssl': {'type': 'boolean'}, 'system_id': {'type': 'string'}, 'password': {'type': 'string'}, 'enquire_link_delay': {'type': ['null', 'number']}, 'enquire_link_timeout': {'type': 'number'}, 'connect_timeout': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}, 'short_message': {'type': 'boolean'}, 'priority': {'enum': ['BULK', 'NORMAL', 'URGENT', 'VERY_URGENT']}, 'data_coding': {'enum': ['DEFAULT', 'ASCII', 'UNSPECIFIED_1', 'LATIN_1', 'UNSPECIFIED_2', 'JIS', 'CYRLLIC', 'LATIN_HEBREW', 'UCS2', 'PICTOGRAM', 'MUSIC', 'EXTENDED_KANJI', 'KS']}, 'message_encoding': {'type': 'string'}, 'message_timeout': {'type': 'number'}}}, 'events': {'client': {'gateway': {'status': {'enum': ['CONNECTING', 'CONNECTED', 'DISCONNECTED']}}, 'system': {'message': {'type': 'object', 'required': ['address', 'message'], 'properties': {'address': {'type': 'string'}, 'message': {'type': 'string'}}}}}}}}, 'hat-gateway://iec61850.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec61850.yaml', '$defs': {'client': {'type': 'object', 'required': ['name', 'connection', 'value_types', 'datasets', 'rcbs', 'data', 'commands', 'changes'], 'properties': {'name': {'type': 'string'}, 'connection': {'type': 'object', 'required': ['host', 'port', 'connect_timeout', 'reconnect_delay', 'response_timeout', 'status_delay', 'status_timeout'], 'properties': {'host': {'type': 'string'}, 'port': {'type': 'integer'}, 'connect_timeout': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}, 'response_timeout': {'type': 'number'}, 'status_delay': {'type': 'number'}, 'status_timeout': {'type': 'number'}, 'local_tsel': {'type': 'integer'}, 'remote_tsel': {'type': 'integer'}, 'local_ssel': {'type': 'integer'}, 'remote_ssel': {'type': 'integer'}, 'local_psel': {'type': 'integer'}, 'remote_psel': {'type': 'integer'}, 'local_ap_title': {'type': 'array', 'items': {'type': 'integer'}}, 'remote_ap_title': {'type': 'array', 'items': {'type': 'integer'}}, 'local_ae_qualifier': {'type': 'integer'}, 'remote_ae_qualifier': {'type': 'integer'}, 'local_detail_calling': {'type': 'integer'}}}, 'value_types': {'type': 'array', 'items': {'type': 'object', 'required': ['logical_device', 'logical_node', 'fc', 'name', 'type'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'fc': {'type': 'string'}, 'name': {'type': 'string'}, 'type': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value_type'}}}}, 'datasets': {'type': 'array', 'items': {'type': 'object', 'required': ['ref', 'values', 'dynamic'], 'properties': {'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset'}, 'values': {'type': 'array', 'items': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}}, 'dynamic': {'type': 'boolean'}}}}, 'rcbs': {'type': 'array', 'items': {'type': 'object', 'required': ['ref', 'report_id', 'dataset'], 'properties': {'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/rcb'}, 'report_id': {'type': 'string'}, 'dataset': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset'}, 'trigger_options': {'type': 'array', 'items': {'enum': ['DATA_CHANGE', 'QUALITY_CHANGE', 'DATA_UPDATE', 'INTEGRITY', 'GENERAL_INTERROGATION']}}, 'optional_fields': {'type': 'array', 'items': {'enum': ['SEQUENCE_NUMBER', 'REPORT_TIME_STAMP', 'REASON_FOR_INCLUSION', 'DATA_SET_NAME', 'DATA_REFERENCE', 'BUFFER_OVERFLOW', 'ENTRY_ID', 'CONF_REVISION']}}, 'conf_revision': {'type': 'integer'}, 'buffer_time': {'type': 'integer'}, 'integrity_period': {'type': 'integer'}, 'purge_buffer': {'type': 'boolean'}, 'reservation_time': {'type': 'integer'}}}}, 'data': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'report_id', 'value'], 'properties': {'name': {'type': 'string'}, 'report_id': {'type': 'string'}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}, 'quality': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}, 'timestamp': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}, 'selected': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}}}}, 'commands': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'model', 'ref', 'with_operate_time'], 'properties': {'name': {'type': 'string'}, 'model': {'enum': ['DIRECT_WITH_NORMAL_SECURITY', 'SBO_WITH_NORMAL_SECURITY', 'DIRECT_WITH_ENHANCED_SECURITY', 'SBO_WITH_ENHANCED_SECURITY']}, 'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/command'}, 'with_operate_time': {'type': 'boolean'}}}}, 'changes': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'ref'], 'properties': {'name': {'type': 'string'}, 'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}}}}}}, 'events': {'client': {'gateway': {'status': {'enum': ['CONNECTING', 'CONNECTED', 'DISCONNECTED']}, 'data': {'type': 'object', 'required': ['reasons'], 'properties': {'reasons': {'type': 'array', 'items': {'enum': ['DATA_CHANGE', 'QUALITY_CHANGE', 'DATA_UPDATE', 'INTEGRITY', 'GENERAL_INTERROGATION', 'APPLICATION_TRIGGER']}}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}, 'quality': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/quality'}, 'timestamp': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/timestamp'}, 'selected': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/boolean'}}}, 'command': {'allOf': [{'type': 'object', 'required': ['session_id', 'action'], 'properties': {'session_id': {'type': 'string'}, 'action': {'enum': ['SELECT', 'CANCEL', 'OPERATE', 'TERMINATION']}}}, {'oneOf': [{'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': True}}}, {'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': False}, 'service_error': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/service_error'}, 'additional_cause': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/additional_cause'}, 'test_error': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/test_error'}}}]}]}, 'change': {'allOf': [{'type': 'object', 'required': ['session_id'], 'properties': {'session_id': {'type': 'string'}}}, {'oneOf': [{'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': True}}}, {'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': False}, 'error': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/service_error'}}}]}]}, 'entry_id': {'type': ['string', 'null'], 'description': 'hex encoded bytes'}}, 'system': {'command': {'type': 'object', 'required': ['session_id', 'action', 'value', 'origin', 'test', 'checks'], 'properties': {'session_id': {'type': 'string'}, 'action': {'enum': ['SELECT', 'CANCEL', 'OPERATE']}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}, 'origin': {'type': 'object', 'required': ['category', 'identification'], 'properties': {'category': {'enum': ['BAY_CONTROL', 'STATION_CONTROL', 'REMOTE_CONTROL', 'AUTOMATIC_BAY', 'AUTOMATIC_STATION', 'AUTOMATIC_REMOTE', 'MAINTENANCE', 'PROCESS']}, 'identification': {'type': 'string'}}}, 'test': {'type': 'boolean'}, 'checks': {'type': 'array', 'items': {'enum': ['SYNCHRO', 'INTERLOCK']}}}}, 'change': {'type': 'object', 'requried': ['session_id', 'value'], 'properties': {'session_id': {'type': 'string'}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}}}}}}, 'value': {'anyOf': [{'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/boolean'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/integer'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/unsigned'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/float'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/bit_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/octet_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/visible_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/mms_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/array'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/struct'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/quality'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/timestamp'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/double_point'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/direction'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/severity'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/analogue'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/vector'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/step_position'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/binary_control'}], '$defs': {'boolean': {'type': 'boolean'}, 'integer': {'type': 'integer'}, 'unsigned': {'type': 'integer'}, 'float': {'oneOf': [{'type': 'number'}, {'enum': ['nan', 'inf', '-inf']}]}, 'bit_string': {'type': 'array', 'items': {'type': 'boolean'}}, 'octet_string': {'type': 'string', 'description': 'hex encoded bytes'}, 'visible_string': {'type': 'string'}, 'mms_string': {'type': 'string'}, 'array': {'type': 'array', 'items': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}}, 'struct': {'type': 'object', 'patternProperties': {'.+': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}}}, 'quality': {'type': 'object', 'required': ['validity', 'details', 'source', 'test', 'operator_blocked'], 'properties': {'validity': {'enum': ['GOOD', 'INVALID', 'RESERVED', 'QUESTIONABLE']}, 'details': {'type': 'array', 'items': {'enum': ['OVERFLOW', 'OUT_OF_RANGE', 'BAD_REFERENCE', 'OSCILLATORY', 'FAILURE', 'OLD_DATA', 'INCONSISTENT', 'INACCURATE']}}, 'source': {'enum': ['PROCESS', 'SUBSTITUTED']}, 'test': {'type': 'boolean'}, 'operator_blocked': {'type': 'boolean'}}}, 'timestamp': {'type': 'object', 'required': ['value', 'leap_second', 'clock_failure', 'not_synchronized'], 'properties': {'value': {'type': 'number', 'description': 'seconds since 1970-01-01'}, 'leap_second': {'type': 'boolean'}, 'clock_failure': {'type': 'boolean'}, 'not_synchronized': {'type': 'boolean'}, 'accuracy': {'type': 'integer'}}}, 'double_point': {'enum': ['INTERMEDIATE', 'OFF', 'ON', 'BAD']}, 'direction': {'enum': ['UNKNOWN', 'FORWARD', 'BACKWARD', 'BOTH']}, 'severity': {'enum': ['UNKNOWN', 'CRITICAL', 'MAJOR', 'MINOR', 'WARNING']}, 'analogue': {'type': 'object', 'properties': {'i': {'type': 'integer'}, 'f': {'oneOf': [{'type': 'number'}, {'enum': ['nan', 'inf', '-inf']}]}}}, 'vector': {'type': 'object', 'required': ['magnitude'], 'properties': {'magnitude': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/analogue'}, 'angle': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/analogue'}}}, 'step_position': {'type': 'object', 'required': ['value'], 'properties': {'value': {'type': 'integer'}, 'transient': {'type': 'boolean'}}}, 'binary_control': {'enum': ['STOP', 'LOWER', 'HIGHER', 'RESERVED']}}}, 'value_type': {'oneOf': [{'enum': ['BOOLEAN', 'INTEGER', 'UNSIGNED', 'FLOAT', 'BIT_STRING', 'OCTET_STRING', 'VISIBLE_STRING', 'MMS_STRING', 'QUALITY', 'TIMESTAMP', 'DOUBLE_POINT', 'DIRECTION', 'SEVERITY', 'ANALOGUE', 'VECTOR', 'STEP_POSITION', 'BINARY_CONTROL']}, {'type': 'object', 'required': ['type', 'element_type', 'length'], 'properties': {'type': {'const': 'ARRAY'}, 'element_type': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value_type'}, 'length': {'type': 'integer'}}}, {'type': 'object', 'required': ['type', 'elements'], 'properties': {'type': {'const': 'STRUCT'}, 'elements': {'type': 'array', 'items': {'type': 'object', 'requried': ['name', 'type'], 'properties': {'name': {'type': 'string'}, 'type': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value_type'}}}}}}]}, 'refs': {'value': {'type': 'object', 'required': ['logical_device', 'logical_node', 'fc', 'names'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'fc': {'type': 'string'}, 'names': {'type': 'array', 'items': {'type': ['string', 'integer']}}}}, 'command': {'type': 'object', 'required': ['logical_device', 'logical_node', 'name'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'name': {'type': 'string'}}}, 'rcb': {'type': 'object', 'required': ['logical_device', 'logical_node', 'type', 'name'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'type': {'enum': ['BUFFERED', 'UNBUFFERED']}, 'name': {'type': 'string'}}}, 'dataset': {'oneOf': [{'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset/$defs/nonpersisted'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset/$defs/persisted'}], '$defs': {'nonpersisted': {'type': 'string'}, 'persisted': {'type': 'object', 'required': ['logical_device', 'logical_node', 'name'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'name': {'type': 'string'}}}}}}, 'errors': {'service_error': {'enum': ['NO_ERROR', 'INSTANCE_NOT_AVAILABLE', 'INSTANCE_IN_USE', 'ACCESS_VIOLATION', 'ACCESS_NOT_ALLOWED_IN_CURRENT_STATE', 'PARAMETER_VALUE_INAPPROPRIATE', 'PARAMETER_VALUE_INCONSISTENT', 'CLASS_NOT_SUPPORTED', 'INSTANCE_LOCKED_BY_OTHER_CLIENT', 'CONTROL_MUST_BE_SELECTED', 'TYPE_CONFLICT', 'FAILED_DUE_TO_COMMUNICATIONS_CONSTRAINT', 'FAILED_DUE_TO_SERVER_CONTRAINT']}, 'additional_cause': {'enum': ['UNKNOWN', 'NOT_SUPPORTED', 'BLOCKED_BY_SWITCHING_HIERARCHY', 'SELECT_FAILED', 'INVALID_POSITION', 'POSITION_REACHED', 'PARAMETER_CHANGE_IN_EXECUTION', 'STEP_LIMIT', 'BLOCKED_BY_MODE', 'BLOCKED_BY_PROCESS', 'BLOCKED_BY_INTERLOCKING', 'BLOCKED_BY_SYNCHROCHECK', 'COMMAND_ALREADY_IN_EXECUTION', 'BLOCKED_BY_HEALTH', 'ONE_OF_N_CONTROL', 'ABORTION_BY_CANCEL', 'TIME_LIMIT_OVER', 'ABORTION_BY_TRIP', 'OBJECT_NOT_SELECTED', 'OBJECT_ALREADY_SELECTED', 'NO_ACCESS_AUTHORITY', 'ENDED_WITH_OVERSHOOT', 'ABORTION_DUE_TO_DEVIATION', 'ABORTION_BY_COMMUNICATION_LOSS', 'BLOCKED_BY_COMMAND', 'NONE', 'INCONSISTENT_PARAMETERS', 'LOCKED_BY_OTHER_CLIENT']}, 'test_error': {'enum': ['NO_ERROR', 'UNKNOWN', 'TIMEOUT_TEST_NOT_OK', 'OPERATOR_TEST_NOT_OK']}}}}, 'hat-gateway://iec103.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec103.yaml', '$defs': {'master': {'type': 'object', 'required': ['name', 'port', 'baudrate', 'bytesize', 'parity', 'stopbits', 'flow_control', 'silent_interval', 'reconnect_delay', 'remote_devices'], 'properties': {'name': {'type': 'string'}, 'port': {'type': 'string'}, 'baudrate': {'type': 'integer'}, 'bytesize': {'enum': ['FIVEBITS', 'SIXBITS', 'SEVENBITS', 'EIGHTBITS']}, 'parity': {'enum': ['NONE', 'EVEN', 'ODD', 'MARK', 'SPACE']}, 'stopbits': {'enum': ['ONE', 'ONE_POINT_FIVE', 'TWO']}, 'flow_control': {'type': 'object', 'required': ['xonxoff', 'rtscts', 'dsrdtr'], 'properties': {'xonxoff': {'type': 'boolean'}, 'rtscts': {'type': 'boolean'}, 'dsrdtr': {'type': 'boolean'}}}, 'silent_interval': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}, 'remote_devices': {'type': 'array', 'items': {'type': 'object', 'required': ['address', 'response_timeout', 'send_retry_count', 'poll_class1_delay', 'poll_class2_delay', 'reconnect_delay', 'time_sync_delay'], 'properties': {'address': {'type': 'integer'}, 'response_timeout': {'type': 'number'}, 'send_retry_count': {'type': 'integer'}, 'poll_class1_delay': {'type': ['null', 'number']}, 'poll_class2_delay': {'type': ['null', 'number']}, 'reconnect_delay': {'type': 'number'}, 'time_sync_delay': {'type': ['null', 'number']}}}}}}, 'events': {'master': {'gateway': {'status': {'enum': ['CONNECTING', 'CONNECTED', 'DISCONNECTED']}, 'data': {'type': 'object', 'required': ['cause', 'value'], 'properties': {'cause': {'oneOf': [{'enum': ['SPONTANEOUS', 'CYCLIC', 'TEST_MODE', 'GENERAL_INTERROGATION', 'LOCAL_OPERATION', 'REMOTE_OPERATION']}, {'type': 'integer', 'description': 'other cause in range [0, 255]\n'}]}, 'value': {'oneOf': [{'$ref': 'hat-gateway://iec103.yaml#/$defs/values/double'}, {'$ref': 'hat-gateway://iec103.yaml#/$defs/values/measurand'}]}}}, 'command': {'type': 'object', 'required': ['session_id', 'success'], 'properties': {'success': {'type': 'boolean'}}}}, 'system': {'enable': {'type': 'boolean'}, 'command': {'type': 'object', 'required': ['session_id', 'value'], 'properties': {'value': {'$ref': 'hat-gateway://iec103.yaml#/$defs/values/double'}}}}}}, 'values': {'double': {'enum': ['TRANSIENT', 'OFF', 'ON', 'ERROR']}, 'measurand': {'type': 'object', 'required': ['overflow', 'invalid', 'value'], 'properties': {'overflow': {'type': 'boolean'}, 'invalid': {'type': 'boolean'}, 'value': {'type': 'number'}}}}}}, 'hat-gateway://main.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://main.yaml', 'title': 'Gateway', 'description': "Gateway's configuration", 'type': 'object', 'required': ['name', 'event_server', 'devices'], 'properties': {'type': {'const': 'gateway', 'description': 'configuration type identification'}, 'version': {'type': 'string', 'description': 'component version'}, 'log': {'$ref': 'hat-json://logging.yaml'}, 'name': {'type': 'string', 'description': 'component name'}, 'event_server': {'allOf': [{'type': 'object', 'properties': {'require_operational': {'type': 'boolean'}}}, {'oneOf': [{'type': 'object', 'required': ['monitor_component'], 'properties': {'monitor_component': {'type': 'object', 'required': ['host', 'port', 'gateway_group', 'event_server_group'], 'properties': {'host': {'type': 'string', 'default': '127.0.0.1'}, 'port': {'type': 'integer', 'default': 23010}, 'gateway_group': {'type': 'string'}, 'event_server_group': {'type': 'string'}}}}}, {'type': 'object', 'required': ['eventer_server'], 'properties': {'eventer_server': {'type': 'object', 'required': ['host', 'port'], 'properties': {'host': {'type': 'string', 'default': '127.0.0.1'}, 'port': {'type': 'integer', 'default': 23012}}}}}]}]}, 'devices': {'type': 'array', 'items': {'$ref': 'hat-gateway://main.yaml#/$defs/device'}}, 'adminer_server': {'type': 'object', 'required': ['host', 'port'], 'properties': {'host': {'type': 'string', 'default': '127.0.0.1'}, 'port': {'type': 'integer', 'default': 23016}}}}, '$defs': {'device': {'type': 'object', 'description': 'structure of device configuration depends on device type\n', 'required': ['module', 'name'], 'properties': {'module': {'type': 'string', 'description': 'full python module name that implements device\n'}, 'name': {'type': 'string'}}}}}, 'hat-gateway://snmp.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://snmp.yaml', '$defs': {'manager': {'allOf': [{'oneOf': [{'$ref': 'hat-gateway://snmp.yaml#/$defs/managers/v1'}, {'$ref': 'hat-gateway://snmp.yaml#/$defs/managers/v2c'}, {'$ref': 'hat-gateway://snmp.yaml#/$defs/managers/v3'}]}, {'type': 'object', 'required': ['name', 'remote_host', 'remote_port', 'connect_delay', 'request_timeout', 'request_retry_count', 'request_retry_delay', 'polling_delay', 'polling_oids', 'string_hex_oids'], 'properties': {'name': {'type': 'string', 'description': 'Device name\n'}, 'remote_host': {'type': 'string', 'description': 'Remote hostname or IP address\n'}, 'remote_port': {'type': 'integer', 'description': 'Remote UDP port\n'}, 'connect_delay': {'type': 'number', 'description': 'Delay (in seconds) between two consecutive connection\nestablishment attempts\n'}, 'request_timeout': {'type': 'number', 'description': 'Maximum duration (in seconds) of request/response\nexchange\n'}, 'request_retry_count': {'type': 'integer', 'description': 'Number of request retries before remote data is\nconsidered unavailable\n'}, 'request_retry_delay': {'type': 'number', 'description': 'Delay (in seconds) between two consecutive request\nretries\n'}, 'polling_delay': {'type': 'number', 'description': 'Delay (in seconds) between two consecutive polling\ncycles\n'}, 'polling_oids': {'type': 'array', 'items': {'type': 'string', 'description': "OID read during polling cycle formated as integers\nseparated by '.'\n"}}, 'string_hex_oids': {'type': 'array', 'items': {'type': 'string', 'description': "OID associated to string hex value formated as\nintegers separated by '.'\n"}}}}]}, 'trap_listener': {'type': 'object', 'required': ['name', 'local_host', 'local_port', 'users', 'remote_devices'], 'properties': {'name': {'type': 'string', 'description': 'Device name\n'}, 'local_host': {'type': 'string', 'description': 'Local listening hostname or IP address\n'}, 'local_port': {'type': 'integer', 'description': 'Local listening UDP port\n'}, 'users': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'authentication', 'privacy'], 'properties': {'name': {'type': 'string'}, 'authentication': {'oneOf': [{'type': 'null'}, {'type': 'object', 'required': ['type', 'password'], 'properties': {'type': {'enum': ['MD5', 'SHA']}, 'password': {'type': 'string'}}}]}, 'privacy': {'oneOf': [{'type': 'null'}, {'type': 'object', 'required': ['type', 'password'], 'properties': {'type': {'const': 'DES'}, 'password': {'type': 'string'}}}]}}}}, 'remote_devices': {'type': 'array', 'items': {'allOf': [{'oneOf': [{'type': 'object', 'required': ['version', 'community'], 'properties': {'version': {'enum': ['V1', 'V2C']}, 'community': {'type': ['null', 'string']}}}, {'type': 'object', 'required': ['version', 'context'], 'properties': {'version': {'const': 'V3'}, 'context': {'oneOf': [{'type': 'null'}, {'type': 'object', 'required': ['engine_id', 'name'], 'properties': {'engine_id': {'type': 'string', 'description': 'sequence of hexadecimal\ndigits\n'}, 'name': {'type': 'string'}}}]}}}]}, {'type': 'object', 'required': ['name', 'oids', 'string_hex_oids'], 'properties': {'name': {'type': 'string', 'description': 'remote device name\n'}, 'oids': {'type': 'array', 'items': {'type': 'string', 'description': "data OID formated as integers separated\nby '.'\n"}}, 'string_hex_oids': {'type': 'array', 'items': {'type': 'string', 'description': "OID associated to string hex value\nformated as integers separated by '.'\n"}}}}]}}}}, 'managers': {'v1': {'type': 'object', 'required': ['version', 'community'], 'properties': {'version': {'const': 'V1'}, 'community': {'type': 'string'}}}, 'v2c': {'type': 'object', 'required': ['version', 'community'], 'properties': {'version': {'const': 'V2C'}, 'community': {'type': 'string'}}}, 'v3': {'type': 'object', 'required': ['version', 'context', 'user', 'authentication', 'privacy'], 'properties': {'version': {'const': 'V3'}, 'context': {'oneOf': [{'type': 'null'}, {'type': 'object', 'required': ['engine_id', 'name'], 'properties': {'engine_id': {'type': 'string', 'description': 'sequence of hexadecimal digits\n'}, 'name': {'type': 'string'}}}]}, 'user': {'type': 'string'}, 'authentication': {'oneOf': [{'type': 'null'}, {'type': 'object', 'required': ['type', 'password'], 'properties': {'type': {'enum': ['MD5', 'SHA']}, 'password': {'type': 'string'}}}]}, 'privacy': {'oneOf': [{'type': 'null'}, {'type': 'object', 'required': ['type', 'password'], 'properties': {'type': {'const': 'DES'}, 'password': {'type': 'string'}}}]}}}}, 'events': {'manager': {'gateway': {'status': {'enum': ['CONNECTING', 'CONNECTED', 'DISCONNECTED']}, 'read': {'type': 'object', 'required': ['session_id', 'cause', 'data'], 'properties': {'session_id': {'oneOf': [{'type': 'null', 'description': 'In case of INTERROGATE or CHANGE cause\n'}, {'description': 'In case of REQUESTED cause\n'}]}, 'cause': ['INTERROGATE', 'CHANGE', 'REQUESTED'], 'data': {'$ref': 'hat-gateway://snmp.yaml#/$defs/data'}}}, 'write': {'type': 'object', 'required': ['session_id', 'success'], 'properties': {'success': {'type': 'boolean'}}}}, 'system': {'read': {'type': 'object', 'required': ['session_id']}, 'write': {'type': 'object', 'required': ['session_id', 'data'], 'properties': {'data': {'$ref': 'hat-gateway://snmp.yaml#/$defs/data'}}}}}, 'trap_listener': {'gateway': {'data': {'$ref': 'hat-gateway://snmp.yaml#/$defs/data'}}}}, 'data': {'oneOf': [{'type': 'object', 'required': ['type', 'value'], 'properties': {'type': {'enum': ['INTEGER', 'UNSIGNED', 'COUNTER', 'BIG_COUNTER', 'TIME_TICKS']}, 'value': {'type': 'integer'}}}, {'type': 'object', 'required': ['type', 'value'], 'properties': {'type': {'enum': ['STRING', 'STRING_HEX', 'OBJECT_ID', 'IP_ADDRESS', 'ARBITRARY']}, 'value': {'type': 'string'}}}, {'type': 'object', 'required': ['type', 'value'], 'properties': {'type': {'const': 'ERROR'}, 'value': {'enum': ['TOO_BIG', 'NO_SUCH_NAME', 'BAD_VALUE', 'READ_ONLY', 'GEN_ERR', 'NO_ACCESS', 'WRONG_TYPE', 'WRONG_LENGTH', 'WRONG_ENCODING', 'WRONG_VALUE', 'NO_CREATION', 'INCONSISTENT_VALUE', 'RESOURCE_UNAVAILABLE', 'COMMIT_FAILED', 'UNDO_FAILED', 'AUTHORIZATION_ERROR', 'NOT_WRITABLE', 'INCONSISTENT_NAME', 'EMPTY', 'UNSPECIFIED', 'NO_SUCH_OBJECT', 'NO_SUCH_INSTANCE', 'END_OF_MIB_VIEW', 'NOT_IN_TIME_WINDOWS', 'UNKNOWN_USER_NAMES', 'UNKNOWN_ENGINE_IDS', 'WRONG_DIGESTS', 'DECRYPTION_ERRORS']}}}]}}}, 'hat-gateway://iec104.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec104.yaml', '$defs': {'master': {'type': 'object', 'required': ['name', 'remote_addresses', 'response_timeout', 'supervisory_timeout', 'test_timeout', 'send_window_size', 'receive_window_size', 'reconnect_delay', 'time_sync_delay', 'security'], 'properties': {'name': {'type': 'string'}, 'remote_addresses': {'type': 'array', 'items': {'type': 'object', 'required': ['host', 'port'], 'properties': {'host': {'type': 'string'}, 'port': {'type': 'integer'}}}}, 'response_timeout': {'type': 'number'}, 'supervisory_timeout': {'type': 'number'}, 'test_timeout': {'type': 'number'}, 'send_window_size': {'type': 'integer'}, 'receive_window_size': {'type': 'integer'}, 'reconnect_delay': {'type': 'number'}, 'time_sync_delay': {'type': ['null', 'number']}, 'security': {'oneOf': [{'type': 'null'}, {'$ref': 'hat-gateway://iec104.yaml#/$defs/security'}]}}}, 'slave': {'type': 'object', 'required': ['local_host', 'local_port', 'remote_hosts', 'max_connections', 'response_timeout', 'supervisory_timeout', 'test_timeout', 'send_window_size', 'receive_window_size', 'security', 'buffers', 'data'], 'properties': {'local_host': {'type': 'string'}, 'local_port': {'type': 'integer'}, 'remote_hosts': {'type': ['array', 'null'], 'description': 'if null, all remote hosts are allowed\n', 'items': {'type': 'string'}}, 'max_connections': {'type': ['null', 'integer']}, 'response_timeout': {'type': 'number'}, 'supervisory_timeout': {'type': 'number'}, 'test_timeout': {'type': 'number'}, 'send_window_size': {'type': 'integer'}, 'receive_window_size': {'type': 'integer'}, 'security': {'oneOf': [{'type': 'null'}, {'$ref': 'hat-gateway://iec104.yaml#/$defs/security'}]}, 'buffers': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'size'], 'properties': {'name': {'type': 'string'}, 'size': {'type': 'integer'}}}}, 'data': {'type': 'array', 'items': {'type': 'object', 'required': ['data_type', 'asdu_address', 'io_address', 'buffer'], 'properties': {'data_type': {'enum': ['SINGLE', 'DOUBLE', 'STEP_POSITION', 'BITSTRING', 'NORMALIZED', 'SCALED', 'FLOATING', 'BINARY_COUNTER', 'PROTECTION', 'PROTECTION_START', 'PROTECTION_COMMAND', 'STATUS']}, 'asdu_address': {'type': 'integer'}, 'io_address': {'type': 'integer'}, 'buffer': {'type': ['null', 'string']}}}}}}, 'events': {'master': {'gateway': {'status': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/gateway/status'}, 'data': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/gateway/data'}, 'command': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/gateway/command'}, 'interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/gateway/interrogation'}, 'counter_interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/gateway/counter_interrogation'}}, 'system': {'command': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/system/command'}, 'interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/system/interrogation'}, 'counter_interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/master/system/counter_interrogation'}}}, 'slave': {'gateway': {'connections': {'$ref': 'hat-gateway://iec104.yaml#/$defs/messages/connections'}, 'command': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/slave/gateway/command'}}, 'system': {'data': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/slave/system/data'}, 'command': {'$ref': 'hat-gateway://iec101.yaml#/$defs/events/slave/system/command'}}}}, 'messages': {'connections': {'type': 'array', 'items': {'type': 'object', 'required': ['connection_id', 'local', 'remote'], 'properties': {'connection_id': {'type': 'integer'}, 'local': {'type': 'object', 'required': ['host', 'port'], 'properties': {'host': {'type': 'string'}, 'port': {'type': 'integer'}}}, 'remote': {'type': 'object', 'required': ['host', 'port'], 'properties': {'host': {'type': 'string'}, 'port': {'type': 'integer'}}}}}}}, 'security': {'type': 'object', 'required': ['cert_path', 'key_path', 'verify_cert', 'ca_path'], 'properties': {'cert_path': {'type': 'string'}, 'key_path': {'type': ['null', 'string']}, 'verify_cert': {'type': 'boolean'}, 'ca_path': {'type': ['null', 'string']}, 'strict_mode': {'type': 'boolean'}, 'renegotiate_delay': {'type': ['null', 'number']}}}}}, 'hat-gateway://iec101.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec101.yaml', '$defs': {'master': {'allOf': [{'type': 'object', 'required': ['name', 'port', 'baudrate', 'bytesize', 'parity', 'stopbits', 'flow_control', 'silent_interval', 'cause_size', 'asdu_address_size', 'io_address_size', 'reconnect_delay'], 'properties': {'name': {'type': 'string'}, 'port': {'type': 'string'}, 'baudrate': {'type': 'integer'}, 'bytesize': {'enum': ['FIVEBITS', 'SIXBITS', 'SEVENBITS', 'EIGHTBITS']}, 'parity': {'enum': ['NONE', 'EVEN', 'ODD', 'MARK', 'SPACE']}, 'stopbits': {'enum': ['ONE', 'ONE_POINT_FIVE', 'TWO']}, 'flow_control': {'type': 'object', 'required': ['xonxoff', 'rtscts', 'dsrdtr'], 'properties': {'xonxoff': {'type': 'boolean'}, 'rtscts': {'type': 'boolean'}, 'dsrdtr': {'type': 'boolean'}}}, 'silent_interval': {'type': 'number'}, 'cause_size': {'enum': ['ONE', 'TWO']}, 'asdu_address_size': {'enum': ['ONE', 'TWO']}, 'io_address_size': {'enum': ['ONE', 'TWO', 'THREE']}, 'reconnect_delay': {'type': 'number'}}}, {'oneOf': [{'type': 'object', 'required': ['link_type', 'device_address_size', 'remote_devices'], 'properties': {'link_type': {'const': 'BALANCED'}, 'device_address_size': {'enum': ['ZERO', 'ONE', 'TWO']}, 'remote_devices': {'type': 'array', 'items': {'type': 'object', 'required': ['direction', 'address', 'response_timeout', 'send_retry_count', 'status_delay', 'reconnect_delay', 'time_sync_delay'], 'properties': {'direction': {'enum': ['A_TO_B', 'B_TO_A']}, 'address': {'type': 'integer'}, 'response_timeout': {'type': 'number'}, 'send_retry_count': {'type': 'integer'}, 'status_delay': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}, 'time_sync_delay': {'type': ['null', 'number']}}}}}}, {'type': 'object', 'required': ['link_type', 'device_address_size', 'remote_devices'], 'properties': {'link_type': {'const': 'UNBALANCED'}, 'device_address_size': {'enum': ['ONE', 'TWO']}, 'remote_devices': {'type': 'array', 'items': {'type': 'object', 'required': ['address', 'response_timeout', 'send_retry_count', 'poll_class1_delay', 'poll_class2_delay', 'reconnect_delay', 'time_sync_delay'], 'properties': {'address': {'type': 'integer'}, 'response_timeout': {'type': 'number'}, 'send_retry_count': {'type': 'integer'}, 'poll_class1_delay': {'type': ['null', 'number']}, 'poll_class2_delay': {'type': ['null', 'number']}, 'reconnect_delay': {'type': 'number'}, 'time_sync_delay': {'type': ['null', 'number']}}}}}}]}]}, 'slave': {'allOf': [{'type': 'object', 'required': ['port', 'baudrate', 'bytesize', 'parity', 'stopbits', 'flow_control', 'silent_interval', 'cause_size', 'asdu_address_size', 'io_address_size', 'buffers', 'data'], 'properties': {'port': {'type': 'string'}, 'baudrate': {'type': 'integer'}, 'bytesize': {'enum': ['FIVEBITS', 'SIXBITS', 'SEVENBITS', 'EIGHTBITS']}, 'parity': {'enum': ['NONE', 'EVEN', 'ODD', 'MARK', 'SPACE']}, 'stopbits': {'enum': ['ONE', 'ONE_POINT_FIVE', 'TWO']}, 'flow_control': {'type': 'object', 'required': ['xonxoff', 'rtscts', 'dsrdtr'], 'properties': {'xonxoff': {'type': 'boolean'}, 'rtscts': {'type': 'boolean'}, 'dsrdtr': {'type': 'boolean'}}}, 'silent_interval': {'type': 'number'}, 'cause_size': {'enum': ['ONE', 'TWO']}, 'asdu_address_size': {'enum': ['ONE', 'TWO']}, 'io_address_size': {'enum': ['ONE', 'TWO', 'THREE']}, 'buffers': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'size'], 'properties': {'name': {'type': 'string'}, 'size': {'type': 'integer'}}}}, 'data': {'type': 'array', 'items': {'type': 'object', 'required': ['data_type', 'asdu_address', 'io_address', 'buffer'], 'properties': {'data_type': {'enum': ['SINGLE', 'DOUBLE', 'STEP_POSITION', 'BITSTRING', 'NORMALIZED', 'SCALED', 'FLOATING', 'BINARY_COUNTER', 'PROTECTION', 'PROTECTION_START', 'PROTECTION_COMMAND', 'STATUS']}, 'asdu_address': {'type': 'integer'}, 'io_address': {'type': 'integer'}, 'buffer': {'type': ['null', 'string']}}}}}}, {'oneOf': [{'type': 'object', 'required': ['link_type', 'device_address_size', 'devices'], 'properties': {'link_type': {'const': 'BALANCED'}, 'device_address_size': {'enum': ['ZERO', 'ONE', 'TWO']}, 'devices': {'type': 'array', 'items': {'type': 'object', 'required': ['direction', 'address', 'response_timeout', 'send_retry_count', 'status_delay', 'reconnect_delay'], 'properties': {'direction': {'enum': ['A_TO_B', 'B_TO_A']}, 'address': {'type': 'integer'}, 'response_timeout': {'type': 'number'}, 'send_retry_count': {'type': 'integer'}, 'status_delay': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}}}}}}, {'type': 'object', 'required': ['link_type', 'device_address_size', 'devices'], 'properties': {'link_type': {'const': 'UNBALANCED'}, 'device_address_size': {'enum': ['ONE', 'TWO']}, 'devices': {'type': 'array', 'items': {'type': 'object', 'required': ['address', 'keep_alive_timeout', 'reconnect_delay'], 'properties': {'address': {'type': 'integer'}, 'keep_alive_timeout': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}}}}}}]}]}, 'events': {'master': {'gateway': {'status': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/status'}, 'data': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/data/res'}, 'command': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/command/res'}, 'interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/interrogation/res'}, 'counter_interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/counter_interrogation/res'}}, 'system': {'enable': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/enable'}, 'command': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/command/req'}, 'interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/interrogation/req'}, 'counter_interrogation': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/counter_interrogation/req'}}}, 'slave': {'gateway': {'connections': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/connections'}, 'command': {'allOf': [{'type': 'object', 'required': ['connection_id'], 'properties': {'connection_id': {'type': 'integer'}}}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/command/req'}]}}, 'system': {'data': {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/data/res'}, 'command': {'allOf': [{'type': 'object', 'required': ['connection_id'], 'properties': {'connection_id': {'type': 'integer'}}}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/command/res'}]}}}}, 'messages': {'enable': {'type': 'boolean'}, 'status': {'enum': ['CONNECTING', 'CONNECTED', 'DISCONNECTED']}, 'connections': {'type': 'array', 'items': {'type': 'object', 'required': ['connection_id', 'address'], 'properties': {'connection_id': {'type': 'integer'}, 'address': {'type': 'integer'}}}}, 'data': {'res': {'type': 'object', 'required': ['is_test', 'cause', 'data'], 'properties': {'is_test': {'type': 'boolean'}, 'cause': {'$ref': 'hat-gateway://iec101.yaml#/$defs/causes/data/res'}, 'data': {'oneOf': [{'$ref': 'hat-gateway://iec101.yaml#/$defs/data/single'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/double'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/step_position'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/bitstring'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/normalized'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/scaled'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/floating'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/binary_counter'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/protection'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/protection_start'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/protection_command'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/data/status'}]}}}}, 'command': {'req': {'type': 'object', 'required': ['is_test', 'cause', 'command'], 'properties': {'is_test': {'type': 'boolean'}, 'cause': {'$ref': 'hat-gateway://iec101.yaml#/$defs/causes/command/req'}, 'command': {'oneOf': [{'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/single'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/double'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/regulating'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/normalized'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/scaled'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/floating'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/bitstring'}]}}}, 'res': {'type': 'object', 'required': ['is_test', 'is_negative_confirm', 'cause', 'command'], 'properties': {'is_test': {'type': 'boolean'}, 'is_negative_confirm': {'type': 'boolean'}, 'cause': {'$ref': 'hat-gateway://iec101.yaml#/$defs/causes/command/res'}, 'command': {'oneOf': [{'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/single'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/double'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/regulating'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/normalized'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/scaled'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/floating'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/commands/bitstring'}]}}}}, 'interrogation': {'req': {'type': 'object', 'required': ['is_test', 'request', 'cause'], 'properties': {'is_test': {'type': 'boolean'}, 'request': {'type': 'integer', 'description': 'request in range [0, 255]\n'}, 'cause': {'$ref': 'hat-gateway://iec101.yaml#/$defs/causes/command/req'}}}, 'res': {'type': 'object', 'required': ['is_test', 'is_negative_confirm', 'request', 'cause'], 'properties': {'is_test': {'type': 'boolean'}, 'is_negative_confirm': {'type': 'boolean'}, 'request': {'type': 'integer', 'description': 'request in range [0, 255]\n'}, 'cause': {'$ref': 'hat-gateway://iec101.yaml#/$defs/causes/command/res'}}}}, 'counter_interrogation': {'req': {'allOf': [{'type': 'object', 'required': ['freeze'], 'properties': {'freeze': {'enum': ['READ', 'FREEZE', 'FREEZE_AND_RESET', 'RESET']}}}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/interrogation/req'}]}, 'res': {'allOf': [{'type': 'object', 'required': ['freeze'], 'properties': {'freeze': {'enum': ['READ', 'FREEZE', 'FREEZE_AND_RESET', 'RESET']}}}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/messages/interrogation/res'}]}}}, 'data': {'single': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/single'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/indication'}}}, 'double': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/double'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/indication'}}}, 'step_position': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/step_position'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/measurement'}}}, 'bitstring': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/bitstring'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/measurement'}}}, 'normalized': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/normalized'}, 'quality': {'oneOf': [{'type': 'null'}, {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/measurement'}]}}}, 'scaled': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/scaled'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/measurement'}}}, 'floating': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/floating'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/measurement'}}}, 'binary_counter': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/binary_counter'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/counter'}}}, 'protection': {'type': 'object', 'required': ['value', 'quality', 'elapsed_time'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/protection'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/protection'}, 'elapsed_time': {'type': 'integer', 'description': 'elapsed_time in range [0, 65535]\n'}}}, 'protection_start': {'type': 'object', 'required': ['value', 'quality', 'duration_time'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/protection_start'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/protection'}, 'duration_time': {'type': 'integer', 'description': 'duration_time in range [0, 65535]\n'}}}, 'protection_command': {'type': 'object', 'required': ['value', 'quality', 'operating_time'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/protection_command'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/protection'}, 'operating_time': {'type': 'integer', 'description': 'operating_time in range [0, 65535]\n'}}}, 'status': {'type': 'object', 'required': ['value', 'quality'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/status'}, 'quality': {'$ref': 'hat-gateway://iec101.yaml#/$defs/qualities/measurement'}}}}, 'commands': {'single': {'type': 'object', 'required': ['value', 'select', 'qualifier'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/single'}, 'select': {'type': 'boolean'}, 'qualifier': {'type': 'integer', 'description': 'qualifier in range [0, 31]\n'}}}, 'double': {'type': 'object', 'required': ['value', 'select', 'qualifier'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/double'}, 'select': {'type': 'boolean'}, 'qualifier': {'type': 'integer', 'description': 'qualifier in range [0, 31]\n'}}}, 'regulating': {'type': 'object', 'required': ['value', 'select', 'qualifier'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/regulating'}, 'select': {'type': 'boolean'}, 'qualifier': {'type': 'integer', 'description': 'qualifier in range [0, 31]\n'}}}, 'normalized': {'type': 'object', 'required': ['value', 'select'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/normalized'}, 'select': {'type': 'boolean'}}}, 'scaled': {'type': 'object', 'required': ['value', 'select'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/scaled'}, 'select': {'type': 'boolean'}}}, 'floating': {'type': 'object', 'required': ['value', 'select'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/floating'}, 'select': {'type': 'boolean'}}}, 'bitstring': {'type': 'object', 'required': ['value'], 'properties': {'value': {'$ref': 'hat-gateway://iec101.yaml#/$defs/values/bitstring'}}}}, 'values': {'single': {'enum': ['OFF', 'ON']}, 'double': {'enum': ['INTERMEDIATE', 'OFF', 'ON', 'FAULT']}, 'regulating': {'enum': ['LOWER', 'HIGHER']}, 'step_position': {'type': 'object', 'required': ['value', 'transient'], 'properties': {'value': {'type': 'integer', 'description': 'value in range [-64, 63]\n'}, 'transient': {'type': 'boolean'}}}, 'bitstring': {'type': 'array', 'description': 'bitstring encoded as 4 bytes\n', 'items': {'type': 'integer'}}, 'normalized': {'type': 'number', 'description': 'value in range [-1.0, 1.0)\n'}, 'scaled': {'type': 'integer', 'description': 'value in range [-2^15, 2^15-1]\n'}, 'floating': {'oneOf': [{'type': 'number'}, {'enum': ['nan', 'inf', '-inf']}]}, 'binary_counter': {'type': 'integer', 'description': 'value in range [-2^31, 2^31-1]\n'}, 'protection': {'enum': ['OFF', 'ON']}, 'protection_start': {'type': 'object', 'required': ['general', 'l1', 'l2', 'l3', 'ie', 'reverse'], 'properties': {'general': {'type': 'boolean'}, 'l1': {'type': 'boolean'}, 'l2': {'type': 'boolean'}, 'l3': {'type': 'boolean'}, 'ie': {'type': 'boolean'}, 'reverse': {'type': 'boolean'}}}, 'protection_command': {'type': 'object', 'required': ['general', 'l1', 'l2', 'l3'], 'properties': {'general': {'type': 'boolean'}, 'l1': {'type': 'boolean'}, 'l2': {'type': 'boolean'}, 'l3': {'type': 'boolean'}}}, 'status': {'type': 'object', 'required': ['value', 'change'], 'properties': {'value': {'type': 'array', 'description': 'value length is 16\n', 'items': {'type': 'boolean'}}, 'change': {'type': 'array', 'description': 'change length is 16\n', 'items': {'type': 'boolean'}}}}}, 'qualities': {'indication': {'type': 'object', 'required': ['invalid', 'not_topical', 'substituted', 'blocked'], 'properties': {'invalid': {'type': 'boolean'}, 'not_topical': {'type': 'boolean'}, 'substituted': {'type': 'boolean'}, 'blocked': {'type': 'boolean'}}}, 'measurement': {'type': 'object', 'required': ['invalid', 'not_topical', 'substituted', 'blocked', 'overflow'], 'properties': {'invalid': {'type': 'boolean'}, 'not_topical': {'type': 'boolean'}, 'substituted': {'type': 'boolean'}, 'blocked': {'type': 'boolean'}, 'overflow': {'type': 'boolean'}}}, 'counter': {'type': 'object', 'required': ['invalid', 'adjusted', 'overflow', 'sequence'], 'properties': {'invalid': {'type': 'boolean'}, 'adjusted': {'type': 'boolean'}, 'overflow': {'type': 'boolean'}, 'sequence': {'type': 'boolean'}}}, 'protection': {'type': 'object', 'required': ['invalid', 'not_topical', 'substituted', 'blocked', 'time_invalid'], 'properties': {'invalid': {'type': 'boolean'}, 'not_topical': {'type': 'boolean'}, 'substituted': {'type': 'boolean'}, 'blocked': {'type': 'boolean'}, 'time_invalid': {'type': 'boolean'}}}}, 'causes': {'data': {'res': {'oneOf': [{'enum': ['PERIODIC', 'BACKGROUND_SCAN', 'SPONTANEOUS', 'REQUEST', 'REMOTE_COMMAND', 'LOCAL_COMMAND', 'INTERROGATED_STATION', 'INTERROGATED_GROUP01', 'INTERROGATED_GROUP02', 'INTERROGATED_GROUP03', 'INTERROGATED_GROUP04', 'INTERROGATED_GROUP05', 'INTERROGATED_GROUP06', 'INTERROGATED_GROUP07', 'INTERROGATED_GROUP08', 'INTERROGATED_GROUP09', 'INTERROGATED_GROUP10', 'INTERROGATED_GROUP11', 'INTERROGATED_GROUP12', 'INTERROGATED_GROUP13', 'INTERROGATED_GROUP14', 'INTERROGATED_GROUP15', 'INTERROGATED_GROUP16', 'INTERROGATED_COUNTER', 'INTERROGATED_COUNTER01', 'INTERROGATED_COUNTER02', 'INTERROGATED_COUNTER03', 'INTERROGATED_COUNTER04']}, {'type': 'integer', 'description': 'other cause in range [0, 63]\n'}]}}, 'command': {'req': {'oneOf': [{'enum': ['ACTIVATION', 'DEACTIVATION']}, {'type': 'integer', 'description': 'other cause in range [0, 63]\n'}]}, 'res': {'oneOf': [{'enum': ['ACTIVATION_CONFIRMATION', 'DEACTIVATION_CONFIRMATION', 'ACTIVATION_TERMINATION', 'UNKNOWN_TYPE', 'UNKNOWN_CAUSE', 'UNKNOWN_ASDU_ADDRESS', 'UNKNOWN_IO_ADDRESS']}, {'type': 'integer', 'description': 'other cause in range [0, 63]\n'}]}}}}}, 'hat-gateway://ping.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://ping.yaml', '$defs': {'device': {'type': 'object', 'required': ['name', 'remote_devices'], 'properties': {'name': {'type': 'string'}, 'remote_devices': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'host', 'ping_delay', 'ping_timeout', 'retry_count', 'retry_delay'], 'properties': {'name': {'type': 'string'}, 'host': {'type': 'string'}, 'ping_delay': {'type': 'number'}, 'ping_timeout': {'type': 'number'}, 'retry_count': {'type': 'number'}, 'retry_delay': {'type': 'number'}}}}}}, 'events': {'status': {'enum': ['AVAILABLE', 'NOT_AVAILABLE']}}}}, 'hat-gateway://modbus.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://modbus.yaml', 'title': 'Modbus devices', '$defs': {'master': {'type': 'object', 'title': 'Modbus master', 'required': ['name', 'connection', 'remote_devices'], 'properties': {'name': {'type': 'string'}, 'connection': {'type': 'object', 'required': ['modbus_type', 'transport', 'connect_timeout', 'connect_delay', 'request_timeout', 'request_delay', 'request_retry_immediate_count', 'request_retry_delayed_count', 'request_retry_delay'], 'properties': {'modbus_type': {'description': 'Modbus message encoding type\n', 'enum': ['TCP', 'RTU', 'ASCII']}, 'transport': {'oneOf': [{'type': 'object', 'required': ['type', 'host', 'port'], 'properties': {'type': {'const': 'TCP'}, 'host': {'type': 'string', 'description': 'Remote host name\n'}, 'port': {'type': 'integer', 'description': 'Remote host TCP port\n', 'default': 502}}}, {'type': 'object', 'required': ['type', 'port', 'baudrate', 'bytesize', 'parity', 'stopbits', 'flow_control', 'silent_interval'], 'properties': {'type': {'const': 'SERIAL'}, 'port': {'type': 'string', 'description': 'Serial port name (e.g. /dev/ttyS0)\n'}, 'baudrate': {'type': 'integer', 'description': 'Baud rate (e.g. 9600)\n'}, 'bytesize': {'description': 'Number of data bits\n', 'enum': ['FIVEBITS', 'SIXBITS', 'SEVENBITS', 'EIGHTBITS']}, 'parity': {'description': 'Parity checking\n', 'enum': ['NONE', 'EVEN', 'ODD', 'MARK', 'SPACE']}, 'stopbits': {'description': 'Number of stop bits\n', 'enum': ['ONE', 'ONE_POINT_FIVE', 'TWO']}, 'flow_control': {'type': 'object', 'required': ['xonxoff', 'rtscts', 'dsrdtr'], 'properties': {'xonxoff': {'type': 'boolean', 'description': 'Enable software flow control\n'}, 'rtscts': {'type': 'boolean', 'description': 'Enable hardware (RTS/CTS) flow control\n'}, 'dsrdtr': {'type': 'boolean', 'description': 'Enable hardware (DSR/DTR) flow control\n'}}}, 'silent_interval': {'type': 'number', 'description': 'Serial communication silet interval\n'}}}]}, 'connect_timeout': {'type': 'number', 'description': 'Maximum number of seconds available to single connection\nattempt\n'}, 'connect_delay': {'type': 'number', 'description': 'Delay (in seconds) between two consecutive connection\nestablishment attempts\n'}, 'request_timeout': {'type': 'number', 'description': 'Maximum duration (in seconds) of read or write\nrequest/response exchange.\n'}, 'request_delay': {'type': 'number', 'description': 'Delay (in seconds) between two consecutive requests\n(minimal duration between response and next request)\n'}, 'request_retry_immediate_count': {'type': 'integer', 'description': 'Number of immediate request retries before remote\ndata is considered unavailable. Total number\nof retries is request_retry_immediate_count *\nrequest_retry_delayed_count.\n'}, 'request_retry_delayed_count': {'type': 'integer', 'description': 'Number of delayed request retries before remote data\nis considered unavailable. Total number\nof retries is request_retry_immediate_count *\nrequest_retry_delayed_count.\n'}, 'request_retry_delay': {'type': 'number', 'description': 'Delay (in seconds) between two consecutive delayed\nrequest retries\n'}}}, 'remote_devices': {'type': 'array', 'items': {'type': 'object', 'required': ['device_id', 'timeout_poll_delay', 'data'], 'properties': {'device_id': {'type': 'integer', 'description': 'Modbus device identifier\n'}, 'timeout_poll_delay': {'type': 'number', 'description': 'Delay (in seconds) after read timeout and\nbefore device polling is resumed\n'}, 'data': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'interval', 'data_type', 'start_address', 'bit_offset', 'bit_count'], 'properties': {'name': {'type': 'string', 'description': 'Data point name\n'}, 'interval': {'type': ['number', 'null'], 'description': 'Polling interval in seconds or\nnull if polling is disabled\n'}, 'data_type': {'description': 'Modbus register type\n', 'enum': ['COIL', 'DISCRETE_INPUT', 'HOLDING_REGISTER', 'INPUT_REGISTER', 'QUEUE']}, 'start_address': {'type': 'integer', 'description': 'Starting address of modbus register\n'}, 'bit_offset': {'type': 'integer', 'description': 'Bit offset (number of bits skipped)\n'}, 'bit_count': {'type': 'integer', 'description': 'Number of bits used for\nencoding/decoding value (not\nincluding offset bits)\n'}}}}}}}}}, 'events': {'master': {'gateway': {'status': {'enum': ['DISCONNECTED', 'CONNECTING', 'CONNECTED']}, 'remote_device_status': {'enum': ['DISABLED', 'CONNECTING', 'CONNECTED', 'DISCONNECTED']}, 'read': {'type': 'object', 'required': ['result'], 'properties': {'result': {'enum': ['SUCCESS', 'INVALID_FUNCTION_CODE', 'INVALID_DATA_ADDRESS', 'INVALID_DATA_VALUE', 'FUNCTION_ERROR', 'GATEWAY_PATH_UNAVAILABLE', 'GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND']}, 'value': {'type': 'integer'}, 'cause': {'enum': ['INTERROGATE', 'CHANGE']}}}, 'write': {'type': 'object', 'required': ['request_id', 'result'], 'properties': {'request_id': {'type': 'string'}, 'result': {'enum': ['SUCCESS', 'INVALID_FUNCTION_CODE', 'INVALID_DATA_ADDRESS', 'INVALID_DATA_VALUE', 'FUNCTION_ERROR', 'GATEWAY_PATH_UNAVAILABLE', 'GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND', 'TIMEOUT']}}}}, 'system': {'enable': {'type': 'boolean'}, 'write': {'type': 'object', 'required': ['request_id', 'value'], 'properties': {'request_id': {'type': 'string'}, 'value': {'type': 'integer'}}}}}}}}})
class Iec61850ClientDevice(hat.gateway.common.Device):
172class Iec61850ClientDevice(common.Device):
173
174    @property
175    def async_group(self) -> aio.Group:
176        return self._async_group
177
178    async def process_events(self, events: Collection[hat.event.common.Event]):
179        try:
180            for event in events:
181                suffix = event.type[len(self._event_type_prefix):]
182
183                if suffix[:2] == ('system', 'command'):
184                    cmd_name, = suffix[2:]
185                    await self._process_cmd_req(event, cmd_name)
186
187                elif suffix[:2] == ('system', 'change'):
188                    val_name, = suffix[2:]
189                    await self._process_change_req(event, val_name)
190
191                else:
192                    raise Exception('unsupported event type')
193
194        except Exception as e:
195            self._log.warning('error processing event %s: %s',
196                              event.type, e, exc_info=e)
197
198    async def _connection_loop(self):
199
200        async def cleanup():
201            await self._register_status('DISCONNECTED')
202            if self._conn:
203                await self._conn.async_close()
204
205        conn_conf = self._conf['connection']
206        try:
207            while True:
208                await self._register_status('CONNECTING')
209                try:
210                    self._log.debug('connecting to %s:%s',
211                                    conn_conf['host'], conn_conf['port'])
212                    self._conn = await aio.wait_for(
213                        iec61850.connect(
214                            addr=tcp.Address(conn_conf['host'],
215                                             conn_conf['port']),
216                            data_value_types=self._dataset_change_value_types,
217                            cmd_value_types=self._cmd_value_types,
218                            report_data_refs=self._report_data_refs,
219                            report_cb=self._on_report,
220                            termination_cb=self._on_termination,
221                            status_delay=conn_conf['status_delay'],
222                            status_timeout=conn_conf['status_timeout'],
223                            local_tsel=conn_conf.get('local_tsel'),
224                            remote_tsel=conn_conf.get('remote_tsel'),
225                            local_ssel=conn_conf.get('local_ssel'),
226                            remote_ssel=conn_conf.get('remote_ssel'),
227                            local_psel=conn_conf.get('local_psel'),
228                            remote_psel=conn_conf.get('remote_psel'),
229                            local_ap_title=conn_conf.get('local_ap_title'),
230                            remote_ap_title=conn_conf.get('remote_ap_title'),
231                            local_ae_qualifier=conn_conf.get(
232                                'local_ae_qualifier'),
233                            remote_ae_qualifier=conn_conf.get(
234                                'remote_ae_qualifier'),
235                            local_detail_calling=conn_conf.get(
236                                'local_detail_calling'),
237                            name=self._conf['name']),
238                        conn_conf['connect_timeout'])
239
240                except Exception as e:
241                    self._log.warning('connnection failed: %s', e, exc_info=e)
242                    await self._register_status('DISCONNECTED')
243                    await asyncio.sleep(conn_conf['reconnect_delay'])
244                    continue
245
246                self._log.debug('connected')
247                await self._register_status('CONNECTED')
248
249                initialized = False
250                try:
251                    await self._create_dynamic_datasets()
252                    for rcb_conf in self._conf['rcbs']:
253                        await self._init_rcb(rcb_conf)
254                    initialized = True
255
256                except Exception as e:
257                    self._log.warning(
258                        'initialization failed: %s, closing connection',
259                        e, exc_info=e)
260                    self._conn.close()
261
262                await self._conn.wait_closing()
263                await self._register_status('DISCONNECTED')
264                await self._conn.wait_closed()
265                self._conn = None
266                self._terminations = {}
267                self._reports_segments = {}
268                if not initialized:
269                    await asyncio.sleep(conn_conf['reconnect_delay'])
270
271        except Exception as e:
272            self._log.error('connection loop error: %s', e, exc_info=e)
273
274        finally:
275            self._log.debug('closing connection loop')
276            self.close()
277            await aio.uncancellable(cleanup())
278
279    async def _process_cmd_req(self, event, cmd_name):
280        if not self._conn or not self._conn.is_open:
281            raise Exception('no connection')
282
283        if cmd_name not in self._command_name_confs:
284            raise Exception('unexpected command name')
285
286        cmd_conf = self._command_name_confs[cmd_name]
287        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
288        action = event.payload.data['action']
289        evt_session_id = event.payload.data['session_id']
290        if (action == 'SELECT' and
291                cmd_conf['model'] == 'SBO_WITH_NORMAL_SECURITY'):
292            cmd = None
293        else:
294            ctl_num = self._command_name_ctl_nums[cmd_name]
295            ctl_num = _update_ctl_num(ctl_num, action, cmd_conf['model'])
296            self._command_name_ctl_nums[cmd_name] = ctl_num
297            value_type = self._command_ref_value_type[cmd_ref]
298            if value_type is None:
299                raise Exception('value type undefined')
300
301            cmd = _command_from_event(event, cmd_conf, ctl_num, value_type)
302
303        term_future = None
304        if (action == 'OPERATE' and
305                cmd_conf['model'] in ['DIRECT_WITH_ENHANCED_SECURITY',
306                                      'SBO_WITH_ENHANCED_SECURITY']):
307            term_future = self._loop.create_future()
308            self._conn.async_group.spawn(
309                self._wait_cmd_term, cmd_name, cmd_ref, cmd, evt_session_id,
310                term_future)
311
312        try:
313            resp = await aio.wait_for(
314                self._send_command(action, cmd_ref, cmd),
315                self._conf['connection']['response_timeout'])
316
317        except (asyncio.TimeoutError, ConnectionError) as e:
318            self._log.warning('send command failed: %s', e, exc_info=e)
319            if term_future and not term_future.done():
320                term_future.cancel()
321            return
322
323        if resp is not None:
324            if term_future and not term_future.done():
325                term_future.cancel()
326
327        event = _cmd_resp_to_event(
328            self._event_type_prefix, cmd_name, evt_session_id, action, resp)
329        await self._register_events([event])
330
331    async def _send_command(self, action, cmd_ref, cmd):
332        if action == 'SELECT':
333            return await self._conn.select(cmd_ref, cmd)
334
335        if action == 'CANCEL':
336            return await self._conn.cancel(cmd_ref, cmd)
337
338        if action == 'OPERATE':
339            return await self._conn.operate(cmd_ref, cmd)
340
341        raise Exception('unsupported action')
342
343    async def _wait_cmd_term(self, cmd_name, cmd_ref, cmd, session_id, future):
344        cmd_session_id = _get_command_session_id(cmd_ref, cmd)
345        self._terminations[cmd_session_id] = future
346        try:
347            term = await aio.wait_for(future, termination_timeout)
348            event = _cmd_resp_to_event(
349                self._event_type_prefix, cmd_name, session_id, 'TERMINATION',
350                term.error)
351            await self._register_events([event])
352
353        except asyncio.TimeoutError:
354            self._log.warning('command termination timeout')
355
356        finally:
357            del self._terminations[cmd_session_id]
358
359    async def _process_change_req(self, event, value_name):
360        if not self._conn or not self._conn.is_open:
361            raise Exception('no connection')
362
363        if value_name not in self._change_name_value_refs:
364            raise Exception('unexpected change name')
365
366        ref = self._change_name_value_refs[value_name]
367        value_type = self._change_ref_value_type[ref]
368        if value_type is None:
369            raise Exception('value type undefined')
370
371        value = _value_from_json(event.payload.data['value'], value_type)
372        try:
373            resp = await aio.wait_for(
374                self._conn.write_data(ref, value),
375                self._conf['connection']['response_timeout'])
376
377        except asyncio.TimeoutError:
378            self._log.warning('write data response timeout')
379            return
380
381        except ConnectionError as e:
382            self._log.warning('connection error on write data: %s',
383                              e, exc_info=e)
384            return
385
386        session_id = event.payload.data['session_id']
387        event = _write_data_resp_to_event(
388            self._event_type_prefix, value_name, session_id, resp)
389        await self._register_events([event])
390
391    async def _on_report(self, report):
392        try:
393            events = list(self._events_from_report(report))
394            if not events:
395                return
396
397            await self._register_events(events)
398            if self._rcb_type[report.report_id] == 'BUFFERED':
399                self._rcbs_entry_ids[report.report_id] = report.entry_id
400
401        except Exception as e:
402            self._log.warning('report %s ignored: %s',
403                              report.report_id, e, exc_info=e)
404
405    def _events_from_report(self, report):
406        report_id = report.report_id
407        if report_id not in self._report_data_refs:
408            raise Exception(f'unexpected report {report_id}')
409
410        segm_id = (report_id, report.sequence_number)
411        if report.more_segments_follow:
412            if segm_id in self._reports_segments:
413                segment_data, timeout_timer = self._reports_segments[segm_id]
414                timeout_timer.cancel()
415            else:
416                segment_data = collections.deque()
417
418            segment_data.extend(report.data)
419            timeout_timer = self._loop.call_later(
420                report_segments_timeout, self._reports_segments.pop, segm_id)
421            self._reports_segments[segm_id] = (segment_data, timeout_timer)
422            return
423
424        if segm_id in self._reports_segments:
425            report_data, timeout_timer = self._reports_segments.pop(segm_id)
426            timeout_timer.cancel()
427            report_data.extend(report.data)
428
429        else:
430            report_data = report.data
431
432        yield from self._events_from_report_data(report_data, report_id)
433
434        if self._rcb_type[report_id] == 'BUFFERED':
435            yield hat.event.common.RegisterEvent(
436                type=(*self._event_type_prefix, 'gateway',
437                      'entry_id', report_id),
438                source_timestamp=None,
439                payload=hat.event.common.EventPayloadJson(
440                    report.entry_id.hex()
441                    if report.entry_id is not None else None))
442
443    def _events_from_report_data(self, report_data, report_id):
444        data_values_json = collections.defaultdict(dict)
445        data_reasons = collections.defaultdict(set)
446        for rv in report_data:
447            if rv.ref not in self._value_ref_data_names:
448                continue
449
450            value_type = self._dataset_values_ref_type[rv.ref]
451            if value_type is None:
452                self._log.warning('report data ignored: unknown value type')
453                continue
454
455            value_json = _value_to_json(rv.value, value_type)
456            for data_name in self._value_ref_data_names[rv.ref]:
457                value_path = [rv.ref.logical_device, rv.ref.logical_node,
458                              rv.ref.fc, *rv.ref.names]
459                data_values_json[data_name] = json.set_(
460                    data_values_json[data_name], value_path, value_json)
461                if rv.reasons:
462                    data_reasons[data_name].update(
463                        reason.name for reason in rv.reasons)
464
465        for data_name, values_json in data_values_json.items():
466            payload = {'reasons': list(data_reasons[data_name])}
467            data_conf = self._data_name_confs[data_name]
468            value_path = _conf_ref_to_path(data_conf['value'])
469            value_json = json.get(values_json, value_path)
470            if value_json is not None:
471                value_ref = _value_ref_from_conf(data_conf['value'])
472                value_type = self._data_value_types[value_ref]
473                payload['value'] = _value_json_to_event_json(
474                    value_json, value_type)
475
476            if 'quality' in data_conf:
477                quality_path = _conf_ref_to_path(data_conf['quality'])
478                quality_json = json.get(values_json, quality_path)
479                if quality_json is not None:
480                    payload['quality'] = quality_json
481
482            if 'timestamp' in data_conf:
483                timestamp_path = _conf_ref_to_path(data_conf['timestamp'])
484                timestamp_json = json.get(values_json, timestamp_path)
485                if timestamp_json is not None:
486                    payload['timestamp'] = timestamp_json
487
488            if 'selected' in data_conf:
489                selected_path = _conf_ref_to_path(data_conf['selected'])
490                selected_json = json.get(values_json, selected_path)
491                if selected_json is not None:
492                    payload['selected'] = selected_json
493
494            yield hat.event.common.RegisterEvent(
495                type=(*self._event_type_prefix, 'gateway',
496                      'data', data_name),
497                source_timestamp=None,
498                payload=hat.event.common.EventPayloadJson(payload))
499
500    def _on_termination(self, termination):
501        cmd_session_id = _get_command_session_id(
502            termination.ref, termination.cmd)
503        if cmd_session_id not in self._terminations:
504            self._log.warning('unexpected termination dropped')
505            return
506
507        term_future = self._terminations[cmd_session_id]
508        if not term_future.done():
509            self._terminations[cmd_session_id].set_result(termination)
510
511    async def _init_rcb(self, rcb_conf):
512        ref = iec61850.RcbRef(
513            logical_device=rcb_conf['ref']['logical_device'],
514            logical_node=rcb_conf['ref']['logical_node'],
515            type=iec61850.RcbType[rcb_conf['ref']['type']],
516            name=rcb_conf['ref']['name'])
517        self._log.debug('initiating rcb %s', ref)
518
519        get_attrs = collections.deque([iec61850.RcbAttrType.REPORT_ID])
520        dataset_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
521        if dataset_ref not in self._dyn_datasets_values:
522            get_attrs.append(iec61850.RcbAttrType.DATASET)
523        if 'conf_revision' in rcb_conf:
524            get_attrs.append(iec61850.RcbAttrType.CONF_REVISION)
525
526        get_rcb_resp = await self._conn.get_rcb_attrs(ref, get_attrs)
527        _validate_get_rcb_response(get_rcb_resp, rcb_conf)
528
529        if ref.type == iec61850.RcbType.BUFFERED:
530            if 'reservation_time' in rcb_conf:
531                await self._set_rcb(
532                    ref, [(iec61850.RcbAttrType.RESERVATION_TIME,
533                           rcb_conf['reservation_time'])])
534        elif ref.type == iec61850.RcbType.UNBUFFERED:
535            await self._set_rcb(ref, [(iec61850.RcbAttrType.RESERVE, True)])
536        else:
537            raise Exception('unexpected rcb type')
538
539        await self._set_rcb(ref, [(iec61850.RcbAttrType.REPORT_ENABLE, False)])
540
541        if dataset_ref in self._dyn_datasets_values:
542            await self._set_rcb(
543                ref, [(iec61850.RcbAttrType.DATASET, dataset_ref)],
544                critical=True)
545
546        if ref.type == iec61850.RcbType.BUFFERED:
547            entry_id = self._rcbs_entry_ids.get(rcb_conf['report_id'])
548            if rcb_conf.get('purge_buffer') or entry_id is None:
549                await self._set_rcb(
550                    ref, [(iec61850.RcbAttrType.PURGE_BUFFER, True)])
551
552            else:
553                try:
554                    await self._set_rcb(
555                        ref, [(iec61850.RcbAttrType.ENTRY_ID, entry_id)],
556                        critical=True)
557
558                except Exception as e:
559                    self._log.warning('%s', e, exc_info=e)
560                    # try setting entry id to 0 in order to resynchronize
561                    await self._set_rcb(
562                        ref, [(iec61850.RcbAttrType.ENTRY_ID, b'\x00')])
563
564        attrs = collections.deque()
565        if 'trigger_options' in rcb_conf:
566            attrs.append((iec61850.RcbAttrType.TRIGGER_OPTIONS,
567                          set(iec61850.TriggerCondition[i]
568                              for i in rcb_conf['trigger_options'])))
569        if 'optional_fields' in rcb_conf:
570            attrs.append((iec61850.RcbAttrType.OPTIONAL_FIELDS,
571                          set(iec61850.OptionalField[i]
572                              for i in rcb_conf['optional_fields'])))
573        if 'buffer_time' in rcb_conf:
574            attrs.append((iec61850.RcbAttrType.BUFFER_TIME,
575                          rcb_conf['buffer_time']))
576        if 'integrity_period' in rcb_conf:
577            attrs.append((iec61850.RcbAttrType.INTEGRITY_PERIOD,
578                          rcb_conf['integrity_period']))
579        if attrs:
580            await self._set_rcb(ref, attrs)
581
582        await self._set_rcb(
583            ref, [(iec61850.RcbAttrType.REPORT_ENABLE, True)], critical=True)
584        await self._set_rcb(
585            ref, [(iec61850.RcbAttrType.GI, True)], critical=True)
586        self._log.debug('rcb %s initiated', ref)
587
588    async def _set_rcb(self, ref, attrs, critical=False):
589        try:
590            resp = await self._conn.set_rcb_attrs(ref, attrs)
591            attrs_failed = set((attr, attr_res)
592                               for attr, attr_res in resp.items()
593                               if isinstance(attr_res, iec61850.ServiceError))
594            if attrs_failed:
595                raise Exception(f"set attribute errors: {attrs_failed}")
596
597        except Exception as e:
598            if critical:
599                raise Exception(f'set rcb {ref} failed') from e
600
601            else:
602                self._log.warning('set rcb %s failed: %s', ref, e, exc_info=e)
603
604    async def _create_dynamic_datasets(self):
605        existing_ds_refs = set()
606        for ds_ref in self._persist_dyn_datasets:
607            ld = ds_ref.logical_device
608            res = await self._conn.get_persisted_dataset_refs(ld)
609            if isinstance(res, iec61850.ServiceError):
610                raise Exception(f'get datasets for ld {ld} failed: {res}')
611
612            existing_ds_refs.update(res)
613
614        existing_persisted_ds_refs = existing_ds_refs.intersection(
615            self._persist_dyn_datasets)
616        for ds_ref, ds_value_refs in self._dyn_datasets_values.items():
617            if ds_ref in existing_persisted_ds_refs:
618                res = await self._conn.get_dataset_data_refs(ds_ref)
619                if isinstance(res, iec61850.ServiceError):
620                    raise Exception(f'get ds {ds_ref} data refs failed: {res}')
621                else:
622                    exist_ds_value_refs = res
623
624                if ds_value_refs == list(exist_ds_value_refs):
625                    self._log.debug('dataset %s already exists', ds_ref)
626                    continue
627
628                raise Exception('persisted dataset changed')
629
630            res = await self._conn.create_dataset(ds_ref, ds_value_refs)
631            if res is not None:
632                raise Exception(f'create dataset {ds_ref} failed: {res}')
633
634            self._log.debug("dataset %s crated", ds_ref)
635
636    async def _register_events(self, events):
637        try:
638            await self._eventer_client.register(events)
639
640        except ConnectionError:
641            self.close()
642            raise
643
644    async def _register_status(self, status):
645        if status == self._conn_status:
646            return
647
648        event = hat.event.common.RegisterEvent(
649            type=(*self._event_type_prefix, 'gateway', 'status'),
650            source_timestamp=None,
651            payload=hat.event.common.EventPayloadJson(status))
652        await self._register_events([event])
653        self._conn_status = status
654        self._log.debug('registered status %s', status)

Device interface

async_group: hat.aio.group.Group
174    @property
175    def async_group(self) -> aio.Group:
176        return self._async_group

Group controlling resource's lifetime.

async def process_events(self, events: Collection[hat.event.common.common.Event]):
178    async def process_events(self, events: Collection[hat.event.common.Event]):
179        try:
180            for event in events:
181                suffix = event.type[len(self._event_type_prefix):]
182
183                if suffix[:2] == ('system', 'command'):
184                    cmd_name, = suffix[2:]
185                    await self._process_cmd_req(event, cmd_name)
186
187                elif suffix[:2] == ('system', 'change'):
188                    val_name, = suffix[2:]
189                    await self._process_change_req(event, val_name)
190
191                else:
192                    raise Exception('unsupported event type')
193
194        except Exception as e:
195            self._log.warning('error processing event %s: %s',
196                              event.type, e, exc_info=e)

Process received events

This method can be coroutine or regular function.