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 = {_rcb_ref_from_json(i['ref']): i for i in conf['rcbs']}
  44    dataset_confs = {_dataset_ref_from_json(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_json(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_json(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_json(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_json(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[_rcb_ref_from_json(data_conf['rcb'])]
  75        ds_ref = _dataset_ref_from_json(rcb_conf['dataset'])
  76        ds_conf = dataset_confs[ds_ref]
  77        for value_conf in ds_conf['values']:
  78            value_ref = _value_ref_from_json(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_json(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_json(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',
 106            _report_id_from_rcb_conf(i))
 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_json(i['ref']) for i in conf['changes']}
 139    device._rcb_type = {
 140        _report_id_from_rcb_conf(rcb_conf): rcb_conf['ref']['type']
 141        for rcb_conf in conf['rcbs']}
 142    device._persist_dyn_datasets = set(_get_persist_dyn_datasets(conf))
 143    device._dyn_datasets_values = dict(_get_dyn_datasets_values(conf))
 144    device._report_data_refs = collections.defaultdict(collections.deque)
 145    for rcb_conf in device._conf['rcbs']:
 146        report_id = _report_id_from_rcb_conf(rcb_conf)
 147        ds_ref = _dataset_ref_from_json(rcb_conf['dataset'])
 148        for value_conf in dataset_confs[ds_ref]['values']:
 149            value_ref = _value_ref_from_json(value_conf)
 150            device._report_data_refs[report_id].append(value_ref)
 151
 152    device._dataset_change_value_types = dict(
 153        _get_dataset_change_value_types(conf, value_types_61850))
 154    device._cmd_value_types = dict(
 155        _get_cmd_value_types(conf, value_types_61850))
 156
 157    device._log = _create_logger_adapter(conf['name'])
 158
 159    device._async_group = aio.Group()
 160    device._async_group.spawn(device._connection_loop)
 161    device._loop = asyncio.get_running_loop()
 162
 163    return device
 164
 165
 166info: common.DeviceInfo = common.DeviceInfo(
 167    type="iec61850_client",
 168    create=create,
 169    json_schema_id="hat-gateway://iec61850.yaml#/$defs/client",
 170    json_schema_repo=common.json_schema_repo)
 171
 172
 173class Iec61850ClientDevice(common.Device):
 174
 175    @property
 176    def async_group(self) -> aio.Group:
 177        return self._async_group
 178
 179    async def process_events(self, events: Collection[hat.event.common.Event]):
 180        try:
 181            for event in events:
 182                suffix = event.type[len(self._event_type_prefix):]
 183
 184                if suffix[:2] == ('system', 'command'):
 185                    cmd_name, = suffix[2:]
 186                    await self._process_cmd_req(event, cmd_name)
 187
 188                elif suffix[:2] == ('system', 'change'):
 189                    val_name, = suffix[2:]
 190                    await self._process_change_req(event, val_name)
 191
 192                else:
 193                    raise Exception('unsupported event type')
 194
 195        except Exception as e:
 196            self._log.warning('error processing event %s: %s',
 197                              event.type, e, exc_info=e)
 198
 199    async def _connection_loop(self):
 200
 201        async def cleanup():
 202            await self._register_status('DISCONNECTED')
 203            if self._conn:
 204                await self._conn.async_close()
 205
 206        conn_conf = self._conf['connection']
 207        try:
 208            while True:
 209                await self._register_status('CONNECTING')
 210                try:
 211                    self._log.debug('connecting to %s:%s',
 212                                    conn_conf['host'], conn_conf['port'])
 213                    self._conn = await aio.wait_for(
 214                        iec61850.connect(
 215                            addr=tcp.Address(conn_conf['host'],
 216                                             conn_conf['port']),
 217                            data_value_types=self._dataset_change_value_types,
 218                            cmd_value_types=self._cmd_value_types,
 219                            report_data_refs=self._report_data_refs,
 220                            report_cb=self._on_report,
 221                            termination_cb=self._on_termination,
 222                            status_delay=conn_conf['status_delay'],
 223                            status_timeout=conn_conf['status_timeout'],
 224                            local_tsel=conn_conf.get('local_tsel'),
 225                            remote_tsel=conn_conf.get('remote_tsel'),
 226                            local_ssel=conn_conf.get('local_ssel'),
 227                            remote_ssel=conn_conf.get('remote_ssel'),
 228                            local_psel=conn_conf.get('local_psel'),
 229                            remote_psel=conn_conf.get('remote_psel'),
 230                            local_ap_title=conn_conf.get('local_ap_title'),
 231                            remote_ap_title=conn_conf.get('remote_ap_title'),
 232                            local_ae_qualifier=conn_conf.get(
 233                                'local_ae_qualifier'),
 234                            remote_ae_qualifier=conn_conf.get(
 235                                'remote_ae_qualifier'),
 236                            local_detail_calling=conn_conf.get(
 237                                'local_detail_calling'),
 238                            name=self._conf['name']),
 239                        conn_conf['connect_timeout'])
 240
 241                except Exception as e:
 242                    self._log.warning('connnection failed: %s', e, exc_info=e)
 243                    await self._register_status('DISCONNECTED')
 244                    await asyncio.sleep(conn_conf['reconnect_delay'])
 245                    continue
 246
 247                self._log.debug('connected')
 248                await self._register_status('CONNECTED')
 249
 250                initialized = False
 251                try:
 252                    await self._create_dynamic_datasets()
 253                    for rcb_conf in self._conf['rcbs']:
 254                        await self._init_rcb(rcb_conf)
 255                    initialized = True
 256
 257                except Exception as e:
 258                    self._log.warning(
 259                        'initialization failed: %s, closing connection',
 260                        e, exc_info=e)
 261                    self._conn.close()
 262
 263                await self._conn.wait_closing()
 264                await self._register_status('DISCONNECTED')
 265                await self._conn.wait_closed()
 266                self._conn = None
 267                self._terminations = {}
 268                self._reports_segments = {}
 269                if not initialized:
 270                    await asyncio.sleep(conn_conf['reconnect_delay'])
 271
 272        except Exception as e:
 273            self._log.error('connection loop error: %s', e, exc_info=e)
 274
 275        finally:
 276            self._log.debug('closing connection loop')
 277            self.close()
 278            await aio.uncancellable(cleanup())
 279
 280    async def _process_cmd_req(self, event, cmd_name):
 281        if not self._conn or not self._conn.is_open:
 282            raise Exception('no connection')
 283
 284        if cmd_name not in self._command_name_confs:
 285            raise Exception('unexpected command name')
 286
 287        cmd_conf = self._command_name_confs[cmd_name]
 288        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 289        action = event.payload.data['action']
 290        evt_session_id = event.payload.data['session_id']
 291        if (action == 'SELECT' and
 292                cmd_conf['model'] == 'SBO_WITH_NORMAL_SECURITY'):
 293            cmd = None
 294        else:
 295            ctl_num = self._command_name_ctl_nums[cmd_name]
 296            ctl_num = _update_ctl_num(ctl_num, action, cmd_conf['model'])
 297            self._command_name_ctl_nums[cmd_name] = ctl_num
 298            value_type = self._command_ref_value_type[cmd_ref]
 299            if value_type is None:
 300                raise Exception('value type undefined')
 301
 302            cmd = _command_from_event(event, cmd_conf, ctl_num, value_type)
 303
 304        term_future = None
 305        if (action == 'OPERATE' and
 306                cmd_conf['model'] in ['DIRECT_WITH_ENHANCED_SECURITY',
 307                                      'SBO_WITH_ENHANCED_SECURITY']):
 308            term_future = self._loop.create_future()
 309            self._conn.async_group.spawn(
 310                self._wait_cmd_term, cmd_name, cmd_ref, cmd, evt_session_id,
 311                term_future)
 312
 313        try:
 314            resp = await aio.wait_for(
 315                self._send_command(action, cmd_ref, cmd),
 316                self._conf['connection']['response_timeout'])
 317
 318        except (asyncio.TimeoutError, ConnectionError) as e:
 319            self._log.warning('send command failed: %s', e, exc_info=e)
 320            if term_future and not term_future.done():
 321                term_future.cancel()
 322            return
 323
 324        if resp is not None:
 325            if term_future and not term_future.done():
 326                term_future.cancel()
 327
 328        event = _cmd_resp_to_event(
 329            self._event_type_prefix, cmd_name, evt_session_id, action, resp)
 330        await self._register_events([event])
 331
 332    async def _send_command(self, action, cmd_ref, cmd):
 333        if action == 'SELECT':
 334            return await self._conn.select(cmd_ref, cmd)
 335
 336        if action == 'CANCEL':
 337            return await self._conn.cancel(cmd_ref, cmd)
 338
 339        if action == 'OPERATE':
 340            return await self._conn.operate(cmd_ref, cmd)
 341
 342        raise Exception('unsupported action')
 343
 344    async def _wait_cmd_term(self, cmd_name, cmd_ref, cmd, session_id, future):
 345        cmd_session_id = _get_command_session_id(cmd_ref, cmd)
 346        self._terminations[cmd_session_id] = future
 347        try:
 348            term = await aio.wait_for(future, termination_timeout)
 349            event = _cmd_resp_to_event(
 350                self._event_type_prefix, cmd_name, session_id, 'TERMINATION',
 351                term.error)
 352            await self._register_events([event])
 353
 354        except asyncio.TimeoutError:
 355            self._log.warning('command termination timeout')
 356
 357        finally:
 358            del self._terminations[cmd_session_id]
 359
 360    async def _process_change_req(self, event, value_name):
 361        if not self._conn or not self._conn.is_open:
 362            raise Exception('no connection')
 363
 364        if value_name not in self._change_name_value_refs:
 365            raise Exception('unexpected change name')
 366
 367        ref = self._change_name_value_refs[value_name]
 368        value_type = self._change_ref_value_type[ref]
 369        if value_type is None:
 370            raise Exception('value type undefined')
 371
 372        value = _value_from_json(event.payload.data['value'], value_type)
 373        try:
 374            resp = await aio.wait_for(
 375                self._conn.write_data(ref, value),
 376                self._conf['connection']['response_timeout'])
 377
 378        except asyncio.TimeoutError:
 379            self._log.warning('write data response timeout')
 380            return
 381
 382        except ConnectionError as e:
 383            self._log.warning('connection error on write data: %s',
 384                              e, exc_info=e)
 385            return
 386
 387        session_id = event.payload.data['session_id']
 388        event = _write_data_resp_to_event(
 389            self._event_type_prefix, value_name, session_id, resp)
 390        await self._register_events([event])
 391
 392    async def _on_report(self, report):
 393        try:
 394            events = list(self._events_from_report(report))
 395            if not events:
 396                return
 397
 398            await self._register_events(events)
 399            if self._rcb_type[report.report_id] == 'BUFFERED':
 400                self._rcbs_entry_ids[report.report_id] = report.entry_id
 401
 402        except Exception as e:
 403            self._log.warning('report %s ignored: %s',
 404                              report.report_id, e, exc_info=e)
 405
 406    def _events_from_report(self, report):
 407        report_id = report.report_id
 408        if report_id not in self._report_data_refs:
 409            raise Exception(f'unexpected report {report_id}')
 410
 411        segm_id = (report_id, report.sequence_number)
 412        if report.more_segments_follow:
 413            if segm_id in self._reports_segments:
 414                segment_data, timeout_timer = self._reports_segments[segm_id]
 415                timeout_timer.cancel()
 416            else:
 417                segment_data = collections.deque()
 418
 419            segment_data.extend(report.data)
 420            timeout_timer = self._loop.call_later(
 421                report_segments_timeout, self._reports_segments.pop, segm_id)
 422            self._reports_segments[segm_id] = (segment_data, timeout_timer)
 423            return
 424
 425        if segm_id in self._reports_segments:
 426            report_data, timeout_timer = self._reports_segments.pop(segm_id)
 427            timeout_timer.cancel()
 428            report_data.extend(report.data)
 429
 430        else:
 431            report_data = report.data
 432
 433        yield from self._events_from_report_data(report_data, report_id)
 434
 435        if self._rcb_type[report_id] == 'BUFFERED':
 436            yield hat.event.common.RegisterEvent(
 437                type=(*self._event_type_prefix, 'gateway',
 438                      'entry_id', report_id),
 439                source_timestamp=None,
 440                payload=hat.event.common.EventPayloadJson(
 441                    report.entry_id.hex()
 442                    if report.entry_id is not None else None))
 443
 444    def _events_from_report_data(self, report_data, report_id):
 445        data_values_json = collections.defaultdict(dict)
 446        data_reasons = collections.defaultdict(set)
 447        for rv in report_data:
 448            if rv.ref not in self._value_ref_data_names:
 449                continue
 450
 451            value_type = self._dataset_values_ref_type[rv.ref]
 452            if value_type is None:
 453                self._log.warning('report data ignored: unknown value type')
 454                continue
 455
 456            value_json = _value_to_json(rv.value, value_type)
 457            for data_name in self._value_ref_data_names[rv.ref]:
 458                value_path = [rv.ref.logical_device, rv.ref.logical_node,
 459                              rv.ref.fc, *rv.ref.names]
 460                data_values_json[data_name] = json.set_(
 461                    data_values_json[data_name], value_path, value_json)
 462                if rv.reasons:
 463                    data_reasons[data_name].update(
 464                        reason.name for reason in rv.reasons)
 465
 466        for data_name, values_json in data_values_json.items():
 467            payload = {'reasons': list(data_reasons[data_name])}
 468            data_conf = self._data_name_confs[data_name]
 469            value_path = _conf_ref_to_path(data_conf['value'])
 470            value_json = json.get(values_json, value_path)
 471            if value_json is not None:
 472                value_ref = _value_ref_from_json(data_conf['value'])
 473                value_type = self._data_value_types[value_ref]
 474                payload['value'] = _value_json_to_event_json(
 475                    value_json, value_type)
 476
 477            if 'quality' in data_conf:
 478                quality_path = _conf_ref_to_path(data_conf['quality'])
 479                quality_json = json.get(values_json, quality_path)
 480                if quality_json is not None:
 481                    payload['quality'] = quality_json
 482
 483            if 'timestamp' in data_conf:
 484                timestamp_path = _conf_ref_to_path(data_conf['timestamp'])
 485                timestamp_json = json.get(values_json, timestamp_path)
 486                if timestamp_json is not None:
 487                    payload['timestamp'] = timestamp_json
 488
 489            if 'selected' in data_conf:
 490                selected_path = _conf_ref_to_path(data_conf['selected'])
 491                selected_json = json.get(values_json, selected_path)
 492                if selected_json is not None:
 493                    payload['selected'] = selected_json
 494
 495            yield hat.event.common.RegisterEvent(
 496                type=(*self._event_type_prefix, 'gateway',
 497                      'data', data_name),
 498                source_timestamp=None,
 499                payload=hat.event.common.EventPayloadJson(payload))
 500
 501    def _on_termination(self, termination):
 502        cmd_session_id = _get_command_session_id(
 503            termination.ref, termination.cmd)
 504        if cmd_session_id not in self._terminations:
 505            self._log.warning('unexpected termination dropped')
 506            return
 507
 508        term_future = self._terminations[cmd_session_id]
 509        if not term_future.done():
 510            self._terminations[cmd_session_id].set_result(termination)
 511
 512    async def _init_rcb(self, rcb_conf):
 513        ref = iec61850.RcbRef(
 514            logical_device=rcb_conf['ref']['logical_device'],
 515            logical_node=rcb_conf['ref']['logical_node'],
 516            type=iec61850.RcbType[rcb_conf['ref']['type']],
 517            name=rcb_conf['ref']['name'])
 518        self._log.debug('initiating rcb %s', ref)
 519
 520        get_attrs = collections.deque([iec61850.RcbAttrType.REPORT_ID])
 521        dataset_ref = _dataset_ref_from_json(rcb_conf['dataset'])
 522        if dataset_ref not in self._dyn_datasets_values:
 523            get_attrs.append(iec61850.RcbAttrType.DATASET)
 524        if 'conf_revision' in rcb_conf:
 525            get_attrs.append(iec61850.RcbAttrType.CONF_REVISION)
 526
 527        get_rcb_resp = await self._conn.get_rcb_attrs(ref, get_attrs)
 528        _validate_get_rcb_response(get_rcb_resp, rcb_conf)
 529
 530        if ref.type == iec61850.RcbType.BUFFERED:
 531            if 'reservation_time' in rcb_conf:
 532                await self._set_rcb(
 533                    ref, [(iec61850.RcbAttrType.RESERVATION_TIME,
 534                           rcb_conf['reservation_time'])])
 535        elif ref.type == iec61850.RcbType.UNBUFFERED:
 536            await self._set_rcb(ref, [(iec61850.RcbAttrType.RESERVE, True)])
 537        else:
 538            raise Exception('unexpected rcb type')
 539
 540        await self._set_rcb(ref, [(iec61850.RcbAttrType.REPORT_ENABLE, False)])
 541
 542        if dataset_ref in self._dyn_datasets_values:
 543            await self._set_rcb(
 544                ref, [(iec61850.RcbAttrType.DATASET, dataset_ref)],
 545                critical=True)
 546
 547        if ref.type == iec61850.RcbType.BUFFERED:
 548            entry_id = self._rcbs_entry_ids.get(
 549                _report_id_from_rcb_conf(rcb_conf))
 550            if rcb_conf.get('purge_buffer') or entry_id is None:
 551                await self._set_rcb(
 552                    ref, [(iec61850.RcbAttrType.PURGE_BUFFER, True)])
 553
 554            else:
 555                try:
 556                    await self._set_rcb(
 557                        ref, [(iec61850.RcbAttrType.ENTRY_ID, entry_id)],
 558                        critical=True)
 559
 560                except Exception as e:
 561                    self._log.warning('%s', e, exc_info=e)
 562                    # try setting entry id to 0 in order to resynchronize
 563                    await self._set_rcb(
 564                        ref, [(iec61850.RcbAttrType.ENTRY_ID, b'\x00')])
 565
 566        attrs = collections.deque()
 567        if 'trigger_options' in rcb_conf:
 568            attrs.append((iec61850.RcbAttrType.TRIGGER_OPTIONS,
 569                          set(iec61850.TriggerCondition[i]
 570                              for i in rcb_conf['trigger_options'])))
 571        if 'optional_fields' in rcb_conf:
 572            attrs.append((iec61850.RcbAttrType.OPTIONAL_FIELDS,
 573                          set(iec61850.OptionalField[i]
 574                              for i in rcb_conf['optional_fields'])))
 575        if 'buffer_time' in rcb_conf:
 576            attrs.append((iec61850.RcbAttrType.BUFFER_TIME,
 577                          rcb_conf['buffer_time']))
 578        if 'integrity_period' in rcb_conf:
 579            attrs.append((iec61850.RcbAttrType.INTEGRITY_PERIOD,
 580                          rcb_conf['integrity_period']))
 581        if attrs:
 582            await self._set_rcb(ref, attrs)
 583
 584        await self._set_rcb(
 585            ref, [(iec61850.RcbAttrType.REPORT_ENABLE, True)], critical=True)
 586        await self._set_rcb(
 587            ref, [(iec61850.RcbAttrType.GI, True)], critical=True)
 588        self._log.debug('rcb %s initiated', ref)
 589
 590    async def _set_rcb(self, ref, attrs, critical=False):
 591        try:
 592            resp = await self._conn.set_rcb_attrs(ref, attrs)
 593            attrs_failed = set((attr, attr_res)
 594                               for attr, attr_res in resp.items()
 595                               if isinstance(attr_res, iec61850.ServiceError))
 596            if attrs_failed:
 597                raise Exception(f"set attribute errors: {attrs_failed}")
 598
 599        except Exception as e:
 600            if critical:
 601                raise Exception(f'set rcb {ref} failed') from e
 602
 603            else:
 604                self._log.warning('set rcb %s failed: %s', ref, e, exc_info=e)
 605
 606    async def _create_dynamic_datasets(self):
 607        existing_ds_refs = set()
 608        for ds_ref in self._persist_dyn_datasets:
 609            ld = ds_ref.logical_device
 610            res = await self._conn.get_persisted_dataset_refs(ld)
 611            if isinstance(res, iec61850.ServiceError):
 612                raise Exception(f'get datasets for ld {ld} failed: {res}')
 613
 614            existing_ds_refs.update(res)
 615
 616        existing_persisted_ds_refs = existing_ds_refs.intersection(
 617            self._persist_dyn_datasets)
 618        for ds_ref, ds_value_refs in self._dyn_datasets_values.items():
 619            if ds_ref in existing_persisted_ds_refs:
 620                res = await self._conn.get_dataset_data_refs(ds_ref)
 621                if isinstance(res, iec61850.ServiceError):
 622                    raise Exception(f'get ds {ds_ref} data refs failed: {res}')
 623                else:
 624                    exist_ds_value_refs = res
 625
 626                if ds_value_refs == list(exist_ds_value_refs):
 627                    self._log.debug('dataset %s already exists', ds_ref)
 628                    continue
 629
 630                raise Exception('persisted dataset changed')
 631
 632            res = await self._conn.create_dataset(ds_ref, ds_value_refs)
 633            if res is not None:
 634                raise Exception(f'create dataset {ds_ref} failed: {res}')
 635
 636            self._log.debug("dataset %s crated", ds_ref)
 637
 638    async def _register_events(self, events):
 639        try:
 640            await self._eventer_client.register(events)
 641
 642        except ConnectionError:
 643            self.close()
 644            raise
 645
 646    async def _register_status(self, status):
 647        if status == self._conn_status:
 648            return
 649
 650        event = hat.event.common.RegisterEvent(
 651            type=(*self._event_type_prefix, 'gateway', 'status'),
 652            source_timestamp=None,
 653            payload=hat.event.common.EventPayloadJson(status))
 654        await self._register_events([event])
 655        self._conn_status = status
 656        self._log.debug('registered status %s', status)
 657
 658
 659def _value_ref_from_json(value_ref_conf):
 660    return iec61850.DataRef(
 661        logical_device=value_ref_conf['logical_device'],
 662        logical_node=value_ref_conf['logical_node'],
 663        fc=value_ref_conf['fc'],
 664        names=tuple(value_ref_conf['names']))
 665
 666
 667def _dataset_ref_from_json(ds_conf):
 668    if isinstance(ds_conf, str):
 669        return iec61850.NonPersistedDatasetRef(ds_conf)
 670
 671    elif isinstance(ds_conf, dict):
 672        return iec61850.PersistedDatasetRef(**ds_conf)
 673
 674
 675def _rcb_ref_from_json(rcb_ref_conf):
 676    return iec61850.RcbRef(
 677        logical_device=rcb_ref_conf['logical_device'],
 678        logical_node=rcb_ref_conf['logical_node'],
 679        type=iec61850.RcbType[rcb_ref_conf['type']],
 680        name=rcb_ref_conf['name'])
 681
 682
 683def _refs_match(ref1, ref2):
 684    if ref1.logical_device != ref2.logical_device:
 685        return False
 686
 687    if ref1.logical_node != ref2.logical_node:
 688        return False
 689
 690    if ref1.fc != ref2.fc:
 691        return False
 692
 693    if len(ref1.names) == len(ref2.names):
 694        return ref1.names == ref2.names
 695
 696    if len(ref1.names) < len(ref2.names):
 697        names1, names2 = ref1.names, ref2.names
 698    else:
 699        names1, names2 = ref2.names, ref1.names
 700
 701    return names2[:len(names1)] == names1
 702
 703
 704def _get_persist_dyn_datasets(conf):
 705    for ds_conf in conf['datasets']:
 706        if not ds_conf['dynamic']:
 707            continue
 708
 709        ds_ref = _dataset_ref_from_json(ds_conf['ref'])
 710        if isinstance(ds_ref, iec61850.PersistedDatasetRef):
 711            yield ds_ref
 712
 713
 714def _get_dyn_datasets_values(conf):
 715    for ds_conf in conf['datasets']:
 716        if not ds_conf['dynamic']:
 717            continue
 718
 719        ds_ref = _dataset_ref_from_json(ds_conf['ref'])
 720        yield ds_ref, [_value_ref_from_json(val_conf)
 721                       for val_conf in ds_conf['values']]
 722
 723
 724def _vtype_61850_from_vtype_conf(vt_conf):
 725    if isinstance(vt_conf, str):
 726        return _value_type_from_str(vt_conf)
 727
 728    if vt_conf['type'] == 'ARRAY':
 729        return iec61850.ArrayValueType(
 730            type=_vtype_61850_from_vtype_conf(vt_conf['element_type']),
 731            length=vt_conf['length'])
 732
 733    if vt_conf['type'] == 'STRUCT':
 734        return iec61850.StructValueType(
 735            [(el_conf['name'], _vtype_61850_from_vtype_conf(el_conf['type']))
 736             for el_conf in vt_conf['elements']])
 737
 738    raise Exception('unsupported value type')
 739
 740
 741def _vtype_from_vtype_conf(vt_conf):
 742    if isinstance(vt_conf, str):
 743        return _value_type_from_str(vt_conf)
 744
 745    if vt_conf['type'] == 'ARRAY':
 746        return iec61850.ArrayValueType(
 747            type=_vtype_from_vtype_conf(vt_conf['element_type']),
 748            length=vt_conf['length'])
 749
 750    if vt_conf['type'] == 'STRUCT':
 751        return {el_conf['name']: _vtype_from_vtype_conf(el_conf['type'])
 752                for el_conf in vt_conf['elements']}
 753
 754    raise Exception('unsupported value type')
 755
 756
 757def _value_type_from_str(vt_conf):
 758    if vt_conf in ['BOOLEAN',
 759                   'INTEGER',
 760                   'UNSIGNED',
 761                   'FLOAT',
 762                   'BIT_STRING',
 763                   'OCTET_STRING',
 764                   'VISIBLE_STRING',
 765                   'MMS_STRING']:
 766        return iec61850.BasicValueType(vt_conf)
 767
 768    if vt_conf in ['QUALITY',
 769                   'TIMESTAMP',
 770                   'DOUBLE_POINT',
 771                   'DIRECTION',
 772                   'SEVERITY',
 773                   'ANALOGUE',
 774                   'VECTOR',
 775                   'STEP_POSITION',
 776                   'BINARY_CONTROL']:
 777        return iec61850.AcsiValueType(vt_conf)
 778
 779    raise Exception('unsupported value type')
 780
 781
 782def _value_type_from_ref(value_types, ref):
 783    left_names = []
 784    if isinstance(ref, iec61850.CommandRef):
 785        left_names = ['Oper', 'ctlVal']
 786        key = (ref.logical_device, ref.logical_node, 'CO', ref.name)
 787
 788    elif isinstance(ref, iec61850.DataRef):
 789        left_names = ref.names[1:]
 790        key = (ref.logical_device, ref.logical_node, ref.fc, ref.names[0])
 791
 792    else:
 793        raise Exception('unexpected reference')
 794
 795    if key not in value_types:
 796        return
 797
 798    value_type = value_types[key]
 799    while left_names:
 800        name = left_names[0]
 801        left_names = left_names[1:]
 802        if isinstance(name, str):
 803            if isinstance(value_type, iec61850.StructValueType):
 804                _, value_type = util.first(
 805                    value_type.elements, lambda i: i[0] == name)
 806
 807            elif value_type == iec61850.AcsiValueType.ANALOGUE:
 808                if name == 'i':
 809                    value_type = iec61850.BasicValueType.INTEGER
 810                elif name == 'f':
 811                    value_type = iec61850.BasicValueType.FLOAT
 812                else:
 813                    raise Exception("analogue attribute should be 'i' or 'f'")
 814
 815            elif value_type == iec61850.AcsiValueType.VECTOR:
 816                if name in ('mag', 'ang'):
 817                    value_type = iec61850.AcsiValueType.ANALOGUE
 818                else:
 819                    raise Exception(
 820                        "vector attribute should be 'mag' or 'ang'")
 821
 822            elif value_type == iec61850.AcsiValueType.STEP_POSITION:
 823                if name == 'posVal':
 824                    value_type = iec61850.BasicValueType.INTEGER
 825                elif name == 'transInd':
 826                    value_type = iec61850.BasicValueType.BOOLEAN
 827                else:
 828                    raise Exception("step position attribute should be "
 829                                    "'posVal' or 'transInd'")
 830
 831            else:
 832                value_type = value_type[name]
 833
 834        if isinstance(name, int):
 835            value_type = value_type.type
 836
 837    return value_type
 838
 839
 840def _get_dataset_change_value_types(conf, value_types):
 841    for ds_conf in conf['datasets']:
 842        for val_ref_conf in ds_conf['values']:
 843            value_ref = _value_ref_from_json(val_ref_conf)
 844            value_type = _value_type_from_ref(value_types, value_ref)
 845            yield value_ref, value_type
 846
 847    for change_conf in conf['changes']:
 848        value_ref = _value_ref_from_json(change_conf['ref'])
 849        value_type = _value_type_from_ref(value_types, value_ref)
 850        yield value_ref, value_type
 851
 852
 853def _get_cmd_value_types(conf, value_types):
 854    for cmd_conf in conf['commands']:
 855        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 856        value_type = _value_type_from_ref(value_types, cmd_ref)
 857        yield cmd_ref, value_type
 858
 859
 860_epoch_start = datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
 861
 862
 863def _command_from_event(event, cmd_conf, control_number, value_type):
 864    if cmd_conf['with_operate_time']:
 865        operate_time = iec61850.Timestamp(
 866            value=_epoch_start,
 867            leap_second=False,
 868            clock_failure=False,
 869            not_synchronized=False,
 870            accuracy=0)
 871    else:
 872        operate_time = None
 873    return iec61850.Command(
 874        value=_value_from_json(event.payload.data['value'], value_type),
 875        operate_time=operate_time,
 876        origin=iec61850.Origin(
 877            category=iec61850.OriginCategory[
 878                event.payload.data['origin']['category']],
 879            identification=event.payload.data[
 880                'origin']['identification'].encode('utf-8')),
 881        control_number=control_number,
 882        t=_timestamp_from_event_timestamp(event.timestamp),
 883        test=event.payload.data['test'],
 884        checks=set(iec61850.Check[i] for i in event.payload.data['checks']))
 885
 886
 887def _timestamp_from_event_timestamp(timestamp):
 888    return iec61850.Timestamp(
 889        value=hat.event.common.timestamp_to_datetime(timestamp),
 890        leap_second=False,
 891        clock_failure=False,
 892        not_synchronized=False,
 893        accuracy=None)
 894
 895
 896def _cmd_resp_to_event(event_type_prefix, cmd_name, event_session_id, action,
 897                       resp):
 898    success = resp is None
 899    payload = {'session_id': event_session_id,
 900               'action': action,
 901               'success': success}
 902    if not success:
 903        if resp.service_error is not None:
 904            payload['service_error'] = resp.service_error.name
 905        if resp.additional_cause is not None:
 906            payload['additional_cause'] = resp.additional_cause.name
 907        if resp.test_error is not None:
 908            payload['test_error'] = resp.test_error.name
 909
 910    return hat.event.common.RegisterEvent(
 911        type=(*event_type_prefix, 'gateway', 'command', cmd_name),
 912        source_timestamp=None,
 913        payload=hat.event.common.EventPayloadJson(payload))
 914
 915
 916def _write_data_resp_to_event(event_type_prefix, value_name, session_id, resp):
 917    success = resp is None
 918    payload = {'session_id': session_id,
 919               'success': success}
 920    if not success:
 921        payload['error'] = resp.name
 922    return hat.event.common.RegisterEvent(
 923        type=(*event_type_prefix, 'gateway', 'change', value_name),
 924        source_timestamp=None,
 925        payload=hat.event.common.EventPayloadJson(payload))
 926
 927
 928def _get_command_session_id(cmd_ref, cmd):
 929    return (cmd_ref, cmd.control_number)
 930
 931
 932def _value_from_json(event_value, value_type):
 933    if isinstance(value_type, iec61850.BasicValueType):
 934        if value_type == iec61850.BasicValueType.OCTET_STRING:
 935            return bytes.fromhex(event_value)
 936
 937        elif value_type == iec61850.BasicValueType.FLOAT:
 938            return float(event_value)
 939
 940        else:
 941            return event_value
 942
 943    if value_type == iec61850.AcsiValueType.QUALITY:
 944        return iec61850.Quality(
 945            validity=iec61850.QualityValidity[event_value['validity']],
 946            details={iec61850.QualityDetail[i]
 947                     for i in event_value['details']},
 948            source=iec61850.QualitySource[event_value['source']],
 949            test=event_value['test'],
 950            operator_blocked=event_value['operator_blocked'])
 951
 952    if value_type == iec61850.AcsiValueType.TIMESTAMP:
 953        return iec61850.Timestamp(
 954            value=datetime.datetime.fromtimestamp(event_value['value'],
 955                                                  datetime.timezone.utc),
 956            leap_second=event_value['leap_second'],
 957            clock_failure=event_value['clock_failure'],
 958            not_synchronized=event_value['not_synchronized'],
 959            accuracy=event_value.get('accuracy'))
 960
 961    if value_type == iec61850.AcsiValueType.DOUBLE_POINT:
 962        return iec61850.DoublePoint[event_value]
 963
 964    if value_type == iec61850.AcsiValueType.DIRECTION:
 965        return iec61850.Direction[event_value]
 966
 967    if value_type == iec61850.AcsiValueType.SEVERITY:
 968        return iec61850.Severity[event_value]
 969
 970    if value_type == iec61850.AcsiValueType.ANALOGUE:
 971        return iec61850.Analogue(
 972            i=event_value.get('i'),
 973            f=(float(event_value['f']) if 'f' in event_value else None))
 974
 975    if value_type == iec61850.AcsiValueType.VECTOR:
 976        return iec61850.Vector(
 977            magnitude=_value_from_json(event_value['magnitude'],
 978                                       iec61850.AcsiValueType.ANALOGUE),
 979            angle=(_value_from_json(event_value['angle'],
 980                                    iec61850.AcsiValueType.ANALOGUE)
 981                   if 'angle' in event_value else None))
 982
 983    if value_type == iec61850.AcsiValueType.STEP_POSITION:
 984        return iec61850.StepPosition(value=event_value['value'],
 985                                     transient=event_value.get('transient'))
 986
 987    if value_type == iec61850.AcsiValueType.BINARY_CONTROL:
 988        return iec61850.BinaryControl[event_value]
 989
 990    if isinstance(value_type, iec61850.ArrayValueType):
 991        return [_value_from_json(val, value_type.type)
 992                for val in event_value]
 993
 994    if isinstance(value_type, dict):
 995        return {k: _value_from_json(v, value_type[k])
 996                for k, v in event_value.items()}
 997
 998    raise Exception('unsupported value type')
 999
1000
1001def _value_to_json(data_value, value_type):
1002    if isinstance(value_type, iec61850.BasicValueType):
1003        if value_type == iec61850.BasicValueType.OCTET_STRING:
1004            return data_value.hex()
1005
1006        elif value_type == iec61850.BasicValueType.BIT_STRING:
1007            return list(data_value)
1008
1009        elif value_type == iec61850.BasicValueType.FLOAT:
1010            return data_value if math.isfinite(data_value) else str(data_value)
1011
1012        else:
1013            return data_value
1014
1015    if isinstance(value_type, iec61850.AcsiValueType):
1016        if value_type == iec61850.AcsiValueType.QUALITY:
1017            return {'validity': data_value.validity.name,
1018                    'details': [i.name for i in data_value.details],
1019                    'source': data_value.source.name,
1020                    'test': data_value.test,
1021                    'operator_blocked': data_value.operator_blocked}
1022
1023        if value_type == iec61850.AcsiValueType.TIMESTAMP:
1024            val = {'value': data_value.value.timestamp(),
1025                   'leap_second': data_value.leap_second,
1026                   'clock_failure': data_value.clock_failure,
1027                   'not_synchronized': data_value.not_synchronized}
1028            if data_value.accuracy is not None:
1029                val['accuracy'] = data_value.accuracy
1030            return val
1031
1032        if value_type in [iec61850.AcsiValueType.DOUBLE_POINT,
1033                          iec61850.AcsiValueType.DIRECTION,
1034                          iec61850.AcsiValueType.SEVERITY,
1035                          iec61850.AcsiValueType.BINARY_CONTROL]:
1036            return data_value.name
1037
1038        if value_type == iec61850.AcsiValueType.ANALOGUE:
1039            val = {}
1040            if data_value.i is not None:
1041                val['i'] = data_value.i
1042            if data_value.f is not None:
1043                val['f'] = (data_value.f if math.isfinite(data_value.f)
1044                            else str(data_value.f))
1045            return val
1046
1047        if value_type == iec61850.AcsiValueType.VECTOR:
1048            val = {'mag': _value_to_json(
1049                        data_value.magnitude, iec61850.AcsiValueType.ANALOGUE)}
1050            if data_value.angle is not None:
1051                val['ang'] = _value_to_json(
1052                    data_value.angle, iec61850.AcsiValueType.ANALOGUE)
1053            return val
1054
1055        if value_type == iec61850.AcsiValueType.STEP_POSITION:
1056            val = {'posVal': data_value.value}
1057            if data_value.transient is not None:
1058                val['transInd'] = data_value.transient
1059            return val
1060
1061    if isinstance(value_type, iec61850.ArrayValueType):
1062        return [_value_to_json(i, value_type.type) for i in data_value]
1063
1064    if isinstance(value_type, dict):
1065        return {
1066            child_name: _value_to_json(child_value, value_type[child_name])
1067            for child_name, child_value in data_value.items()}
1068
1069    raise Exception('unsupported value type')
1070
1071
1072def _value_json_to_event_json(data_value, value_type):
1073    if value_type == iec61850.AcsiValueType.VECTOR:
1074        val = {'magnitude': data_value['mag']}
1075        if 'ang' in data_value:
1076            val['angle'] = data_value['ang']
1077        return val
1078
1079    if value_type == iec61850.AcsiValueType.STEP_POSITION:
1080        val = {'value': data_value['posVal']}
1081        if 'transInd' in data_value:
1082            val['transient'] = data_value['transInd']
1083        return val
1084
1085    if isinstance(value_type, iec61850.ArrayValueType):
1086        return [_value_json_to_event_json(i, value_type.type)
1087                for i in data_value]
1088
1089    if isinstance(value_type, dict):
1090        return {
1091            child_name: _value_json_to_event_json(
1092                child_value, value_type[child_name])
1093            for child_name, child_value in data_value.items()}
1094
1095    return data_value
1096
1097
1098def _update_ctl_num(ctl_num, action, cmd_model):
1099    if action == 'SELECT' or (
1100        action == 'OPERATE' and
1101        cmd_model in ['DIRECT_WITH_NORMAL_SECURITY',
1102                      'DIRECT_WITH_ENHANCED_SECURITY']):
1103        return (ctl_num + 1) % 256
1104
1105    return ctl_num
1106
1107
1108def _validate_get_rcb_response(get_rcb_resp, rcb_conf):
1109    for k, v in get_rcb_resp.items():
1110        if isinstance(v, iec61850.ServiceError):
1111            raise Exception(f"get {k.name} failed: {v}")
1112
1113        if (k == iec61850.RcbAttrType.REPORT_ID and
1114                v != rcb_conf['report_id']):
1115            raise Exception(f"rcb report id {v} different from "
1116                            f"configured {rcb_conf['report_id']}")
1117
1118        if (k == iec61850.RcbAttrType.DATASET and
1119                v != _dataset_ref_from_json(rcb_conf['dataset'])):
1120            raise Exception(f"rcb dataset {v} different from "
1121                            f"configured {rcb_conf['dataset']}")
1122
1123        if (k == iec61850.RcbAttrType.CONF_REVISION and
1124                v != rcb_conf['conf_revision']):
1125            raise Exception(
1126                f"Conf revision {v} different from "
1127                f"the configuration defined {rcb_conf['conf_revision']}")
1128
1129
1130def _conf_ref_to_path(conf_ref):
1131    return [conf_ref['logical_device'],
1132            conf_ref['logical_node'],
1133            conf_ref['fc'],
1134            *conf_ref['names']]
1135
1136
1137def _report_id_from_rcb_conf(rcb_conf):
1138    return (rcb_conf['report_id'] if rcb_conf['report_id'] else
1139            _report_id_from_rcb_ref(rcb_conf['ref']))
1140
1141
1142def _report_id_from_rcb_ref(rcb_ref):
1143    return (f"{rcb_ref['logical_device']}/"
1144            f"{rcb_ref['logical_node']}$"
1145            f"{iec61850.RcbType[rcb_ref['type']].value}$"
1146            f"{rcb_ref['name']}")
1147
1148
1149def _create_logger_adapter(name):
1150    extra = {'meta': {'type': 'Iec61850ClientDevice',
1151                      'name': name}}
1152
1153    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 = {_rcb_ref_from_json(i['ref']): i for i in conf['rcbs']}
 45    dataset_confs = {_dataset_ref_from_json(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_json(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_json(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_json(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_json(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[_rcb_ref_from_json(data_conf['rcb'])]
 76        ds_ref = _dataset_ref_from_json(rcb_conf['dataset'])
 77        ds_conf = dataset_confs[ds_ref]
 78        for value_conf in ds_conf['values']:
 79            value_ref = _value_ref_from_json(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_json(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_json(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',
107            _report_id_from_rcb_conf(i))
108        for i in conf['rcbs'] if i['ref']['type'] == 'BUFFERED']
109    if entry_id_event_types:
110        result = await eventer_client.query(
111            hat.event.common.QueryLatestParams(entry_id_event_types))
112        rcbs_entry_ids = {
113            event.type[5]: (bytes.fromhex(event.payload.data)
114                            if event.payload.data is not None else None)
115            for event in result.events}
116    else:
117        rcbs_entry_ids = {}
118
119    device = Iec61850ClientDevice()
120
121    device._rcbs_entry_ids = rcbs_entry_ids
122    device._conf = conf
123    device._eventer_client = eventer_client
124    device._event_type_prefix = event_type_prefix
125    device._conn = None
126    device._conn_status = None
127    device._terminations = {}
128    device._reports_segments = {}
129
130    device._value_ref_data_names = value_ref_data_names
131    device._data_value_types = data_value_types
132    device._dataset_values_ref_type = dataset_values_ref_type
133    device._command_ref_value_type = command_ref_value_type
134    device._change_ref_value_type = change_ref_value_type
135    device._data_name_confs = {i['name']: i for i in conf['data']}
136    device._command_name_confs = {i['name']: i for i in conf['commands']}
137    device._command_name_ctl_nums = {i['name']: 0 for i in conf['commands']}
138    device._change_name_value_refs = {
139        i['name']: _value_ref_from_json(i['ref']) for i in conf['changes']}
140    device._rcb_type = {
141        _report_id_from_rcb_conf(rcb_conf): rcb_conf['ref']['type']
142        for rcb_conf in conf['rcbs']}
143    device._persist_dyn_datasets = set(_get_persist_dyn_datasets(conf))
144    device._dyn_datasets_values = dict(_get_dyn_datasets_values(conf))
145    device._report_data_refs = collections.defaultdict(collections.deque)
146    for rcb_conf in device._conf['rcbs']:
147        report_id = _report_id_from_rcb_conf(rcb_conf)
148        ds_ref = _dataset_ref_from_json(rcb_conf['dataset'])
149        for value_conf in dataset_confs[ds_ref]['values']:
150            value_ref = _value_ref_from_json(value_conf)
151            device._report_data_refs[report_id].append(value_ref)
152
153    device._dataset_change_value_types = dict(
154        _get_dataset_change_value_types(conf, value_types_61850))
155    device._cmd_value_types = dict(
156        _get_cmd_value_types(conf, value_types_61850))
157
158    device._log = _create_logger_adapter(conf['name'])
159
160    device._async_group = aio.Group()
161    device._async_group.spawn(device._connection_loop)
162    device._loop = asyncio.get_running_loop()
163
164    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', 'rcb', 'value'], 'properties': {'name': {'type': 'string'}, 'rcb': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/rcb'}, '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):
174class Iec61850ClientDevice(common.Device):
175
176    @property
177    def async_group(self) -> aio.Group:
178        return self._async_group
179
180    async def process_events(self, events: Collection[hat.event.common.Event]):
181        try:
182            for event in events:
183                suffix = event.type[len(self._event_type_prefix):]
184
185                if suffix[:2] == ('system', 'command'):
186                    cmd_name, = suffix[2:]
187                    await self._process_cmd_req(event, cmd_name)
188
189                elif suffix[:2] == ('system', 'change'):
190                    val_name, = suffix[2:]
191                    await self._process_change_req(event, val_name)
192
193                else:
194                    raise Exception('unsupported event type')
195
196        except Exception as e:
197            self._log.warning('error processing event %s: %s',
198                              event.type, e, exc_info=e)
199
200    async def _connection_loop(self):
201
202        async def cleanup():
203            await self._register_status('DISCONNECTED')
204            if self._conn:
205                await self._conn.async_close()
206
207        conn_conf = self._conf['connection']
208        try:
209            while True:
210                await self._register_status('CONNECTING')
211                try:
212                    self._log.debug('connecting to %s:%s',
213                                    conn_conf['host'], conn_conf['port'])
214                    self._conn = await aio.wait_for(
215                        iec61850.connect(
216                            addr=tcp.Address(conn_conf['host'],
217                                             conn_conf['port']),
218                            data_value_types=self._dataset_change_value_types,
219                            cmd_value_types=self._cmd_value_types,
220                            report_data_refs=self._report_data_refs,
221                            report_cb=self._on_report,
222                            termination_cb=self._on_termination,
223                            status_delay=conn_conf['status_delay'],
224                            status_timeout=conn_conf['status_timeout'],
225                            local_tsel=conn_conf.get('local_tsel'),
226                            remote_tsel=conn_conf.get('remote_tsel'),
227                            local_ssel=conn_conf.get('local_ssel'),
228                            remote_ssel=conn_conf.get('remote_ssel'),
229                            local_psel=conn_conf.get('local_psel'),
230                            remote_psel=conn_conf.get('remote_psel'),
231                            local_ap_title=conn_conf.get('local_ap_title'),
232                            remote_ap_title=conn_conf.get('remote_ap_title'),
233                            local_ae_qualifier=conn_conf.get(
234                                'local_ae_qualifier'),
235                            remote_ae_qualifier=conn_conf.get(
236                                'remote_ae_qualifier'),
237                            local_detail_calling=conn_conf.get(
238                                'local_detail_calling'),
239                            name=self._conf['name']),
240                        conn_conf['connect_timeout'])
241
242                except Exception as e:
243                    self._log.warning('connnection failed: %s', e, exc_info=e)
244                    await self._register_status('DISCONNECTED')
245                    await asyncio.sleep(conn_conf['reconnect_delay'])
246                    continue
247
248                self._log.debug('connected')
249                await self._register_status('CONNECTED')
250
251                initialized = False
252                try:
253                    await self._create_dynamic_datasets()
254                    for rcb_conf in self._conf['rcbs']:
255                        await self._init_rcb(rcb_conf)
256                    initialized = True
257
258                except Exception as e:
259                    self._log.warning(
260                        'initialization failed: %s, closing connection',
261                        e, exc_info=e)
262                    self._conn.close()
263
264                await self._conn.wait_closing()
265                await self._register_status('DISCONNECTED')
266                await self._conn.wait_closed()
267                self._conn = None
268                self._terminations = {}
269                self._reports_segments = {}
270                if not initialized:
271                    await asyncio.sleep(conn_conf['reconnect_delay'])
272
273        except Exception as e:
274            self._log.error('connection loop error: %s', e, exc_info=e)
275
276        finally:
277            self._log.debug('closing connection loop')
278            self.close()
279            await aio.uncancellable(cleanup())
280
281    async def _process_cmd_req(self, event, cmd_name):
282        if not self._conn or not self._conn.is_open:
283            raise Exception('no connection')
284
285        if cmd_name not in self._command_name_confs:
286            raise Exception('unexpected command name')
287
288        cmd_conf = self._command_name_confs[cmd_name]
289        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
290        action = event.payload.data['action']
291        evt_session_id = event.payload.data['session_id']
292        if (action == 'SELECT' and
293                cmd_conf['model'] == 'SBO_WITH_NORMAL_SECURITY'):
294            cmd = None
295        else:
296            ctl_num = self._command_name_ctl_nums[cmd_name]
297            ctl_num = _update_ctl_num(ctl_num, action, cmd_conf['model'])
298            self._command_name_ctl_nums[cmd_name] = ctl_num
299            value_type = self._command_ref_value_type[cmd_ref]
300            if value_type is None:
301                raise Exception('value type undefined')
302
303            cmd = _command_from_event(event, cmd_conf, ctl_num, value_type)
304
305        term_future = None
306        if (action == 'OPERATE' and
307                cmd_conf['model'] in ['DIRECT_WITH_ENHANCED_SECURITY',
308                                      'SBO_WITH_ENHANCED_SECURITY']):
309            term_future = self._loop.create_future()
310            self._conn.async_group.spawn(
311                self._wait_cmd_term, cmd_name, cmd_ref, cmd, evt_session_id,
312                term_future)
313
314        try:
315            resp = await aio.wait_for(
316                self._send_command(action, cmd_ref, cmd),
317                self._conf['connection']['response_timeout'])
318
319        except (asyncio.TimeoutError, ConnectionError) as e:
320            self._log.warning('send command failed: %s', e, exc_info=e)
321            if term_future and not term_future.done():
322                term_future.cancel()
323            return
324
325        if resp is not None:
326            if term_future and not term_future.done():
327                term_future.cancel()
328
329        event = _cmd_resp_to_event(
330            self._event_type_prefix, cmd_name, evt_session_id, action, resp)
331        await self._register_events([event])
332
333    async def _send_command(self, action, cmd_ref, cmd):
334        if action == 'SELECT':
335            return await self._conn.select(cmd_ref, cmd)
336
337        if action == 'CANCEL':
338            return await self._conn.cancel(cmd_ref, cmd)
339
340        if action == 'OPERATE':
341            return await self._conn.operate(cmd_ref, cmd)
342
343        raise Exception('unsupported action')
344
345    async def _wait_cmd_term(self, cmd_name, cmd_ref, cmd, session_id, future):
346        cmd_session_id = _get_command_session_id(cmd_ref, cmd)
347        self._terminations[cmd_session_id] = future
348        try:
349            term = await aio.wait_for(future, termination_timeout)
350            event = _cmd_resp_to_event(
351                self._event_type_prefix, cmd_name, session_id, 'TERMINATION',
352                term.error)
353            await self._register_events([event])
354
355        except asyncio.TimeoutError:
356            self._log.warning('command termination timeout')
357
358        finally:
359            del self._terminations[cmd_session_id]
360
361    async def _process_change_req(self, event, value_name):
362        if not self._conn or not self._conn.is_open:
363            raise Exception('no connection')
364
365        if value_name not in self._change_name_value_refs:
366            raise Exception('unexpected change name')
367
368        ref = self._change_name_value_refs[value_name]
369        value_type = self._change_ref_value_type[ref]
370        if value_type is None:
371            raise Exception('value type undefined')
372
373        value = _value_from_json(event.payload.data['value'], value_type)
374        try:
375            resp = await aio.wait_for(
376                self._conn.write_data(ref, value),
377                self._conf['connection']['response_timeout'])
378
379        except asyncio.TimeoutError:
380            self._log.warning('write data response timeout')
381            return
382
383        except ConnectionError as e:
384            self._log.warning('connection error on write data: %s',
385                              e, exc_info=e)
386            return
387
388        session_id = event.payload.data['session_id']
389        event = _write_data_resp_to_event(
390            self._event_type_prefix, value_name, session_id, resp)
391        await self._register_events([event])
392
393    async def _on_report(self, report):
394        try:
395            events = list(self._events_from_report(report))
396            if not events:
397                return
398
399            await self._register_events(events)
400            if self._rcb_type[report.report_id] == 'BUFFERED':
401                self._rcbs_entry_ids[report.report_id] = report.entry_id
402
403        except Exception as e:
404            self._log.warning('report %s ignored: %s',
405                              report.report_id, e, exc_info=e)
406
407    def _events_from_report(self, report):
408        report_id = report.report_id
409        if report_id not in self._report_data_refs:
410            raise Exception(f'unexpected report {report_id}')
411
412        segm_id = (report_id, report.sequence_number)
413        if report.more_segments_follow:
414            if segm_id in self._reports_segments:
415                segment_data, timeout_timer = self._reports_segments[segm_id]
416                timeout_timer.cancel()
417            else:
418                segment_data = collections.deque()
419
420            segment_data.extend(report.data)
421            timeout_timer = self._loop.call_later(
422                report_segments_timeout, self._reports_segments.pop, segm_id)
423            self._reports_segments[segm_id] = (segment_data, timeout_timer)
424            return
425
426        if segm_id in self._reports_segments:
427            report_data, timeout_timer = self._reports_segments.pop(segm_id)
428            timeout_timer.cancel()
429            report_data.extend(report.data)
430
431        else:
432            report_data = report.data
433
434        yield from self._events_from_report_data(report_data, report_id)
435
436        if self._rcb_type[report_id] == 'BUFFERED':
437            yield hat.event.common.RegisterEvent(
438                type=(*self._event_type_prefix, 'gateway',
439                      'entry_id', report_id),
440                source_timestamp=None,
441                payload=hat.event.common.EventPayloadJson(
442                    report.entry_id.hex()
443                    if report.entry_id is not None else None))
444
445    def _events_from_report_data(self, report_data, report_id):
446        data_values_json = collections.defaultdict(dict)
447        data_reasons = collections.defaultdict(set)
448        for rv in report_data:
449            if rv.ref not in self._value_ref_data_names:
450                continue
451
452            value_type = self._dataset_values_ref_type[rv.ref]
453            if value_type is None:
454                self._log.warning('report data ignored: unknown value type')
455                continue
456
457            value_json = _value_to_json(rv.value, value_type)
458            for data_name in self._value_ref_data_names[rv.ref]:
459                value_path = [rv.ref.logical_device, rv.ref.logical_node,
460                              rv.ref.fc, *rv.ref.names]
461                data_values_json[data_name] = json.set_(
462                    data_values_json[data_name], value_path, value_json)
463                if rv.reasons:
464                    data_reasons[data_name].update(
465                        reason.name for reason in rv.reasons)
466
467        for data_name, values_json in data_values_json.items():
468            payload = {'reasons': list(data_reasons[data_name])}
469            data_conf = self._data_name_confs[data_name]
470            value_path = _conf_ref_to_path(data_conf['value'])
471            value_json = json.get(values_json, value_path)
472            if value_json is not None:
473                value_ref = _value_ref_from_json(data_conf['value'])
474                value_type = self._data_value_types[value_ref]
475                payload['value'] = _value_json_to_event_json(
476                    value_json, value_type)
477
478            if 'quality' in data_conf:
479                quality_path = _conf_ref_to_path(data_conf['quality'])
480                quality_json = json.get(values_json, quality_path)
481                if quality_json is not None:
482                    payload['quality'] = quality_json
483
484            if 'timestamp' in data_conf:
485                timestamp_path = _conf_ref_to_path(data_conf['timestamp'])
486                timestamp_json = json.get(values_json, timestamp_path)
487                if timestamp_json is not None:
488                    payload['timestamp'] = timestamp_json
489
490            if 'selected' in data_conf:
491                selected_path = _conf_ref_to_path(data_conf['selected'])
492                selected_json = json.get(values_json, selected_path)
493                if selected_json is not None:
494                    payload['selected'] = selected_json
495
496            yield hat.event.common.RegisterEvent(
497                type=(*self._event_type_prefix, 'gateway',
498                      'data', data_name),
499                source_timestamp=None,
500                payload=hat.event.common.EventPayloadJson(payload))
501
502    def _on_termination(self, termination):
503        cmd_session_id = _get_command_session_id(
504            termination.ref, termination.cmd)
505        if cmd_session_id not in self._terminations:
506            self._log.warning('unexpected termination dropped')
507            return
508
509        term_future = self._terminations[cmd_session_id]
510        if not term_future.done():
511            self._terminations[cmd_session_id].set_result(termination)
512
513    async def _init_rcb(self, rcb_conf):
514        ref = iec61850.RcbRef(
515            logical_device=rcb_conf['ref']['logical_device'],
516            logical_node=rcb_conf['ref']['logical_node'],
517            type=iec61850.RcbType[rcb_conf['ref']['type']],
518            name=rcb_conf['ref']['name'])
519        self._log.debug('initiating rcb %s', ref)
520
521        get_attrs = collections.deque([iec61850.RcbAttrType.REPORT_ID])
522        dataset_ref = _dataset_ref_from_json(rcb_conf['dataset'])
523        if dataset_ref not in self._dyn_datasets_values:
524            get_attrs.append(iec61850.RcbAttrType.DATASET)
525        if 'conf_revision' in rcb_conf:
526            get_attrs.append(iec61850.RcbAttrType.CONF_REVISION)
527
528        get_rcb_resp = await self._conn.get_rcb_attrs(ref, get_attrs)
529        _validate_get_rcb_response(get_rcb_resp, rcb_conf)
530
531        if ref.type == iec61850.RcbType.BUFFERED:
532            if 'reservation_time' in rcb_conf:
533                await self._set_rcb(
534                    ref, [(iec61850.RcbAttrType.RESERVATION_TIME,
535                           rcb_conf['reservation_time'])])
536        elif ref.type == iec61850.RcbType.UNBUFFERED:
537            await self._set_rcb(ref, [(iec61850.RcbAttrType.RESERVE, True)])
538        else:
539            raise Exception('unexpected rcb type')
540
541        await self._set_rcb(ref, [(iec61850.RcbAttrType.REPORT_ENABLE, False)])
542
543        if dataset_ref in self._dyn_datasets_values:
544            await self._set_rcb(
545                ref, [(iec61850.RcbAttrType.DATASET, dataset_ref)],
546                critical=True)
547
548        if ref.type == iec61850.RcbType.BUFFERED:
549            entry_id = self._rcbs_entry_ids.get(
550                _report_id_from_rcb_conf(rcb_conf))
551            if rcb_conf.get('purge_buffer') or entry_id is None:
552                await self._set_rcb(
553                    ref, [(iec61850.RcbAttrType.PURGE_BUFFER, True)])
554
555            else:
556                try:
557                    await self._set_rcb(
558                        ref, [(iec61850.RcbAttrType.ENTRY_ID, entry_id)],
559                        critical=True)
560
561                except Exception as e:
562                    self._log.warning('%s', e, exc_info=e)
563                    # try setting entry id to 0 in order to resynchronize
564                    await self._set_rcb(
565                        ref, [(iec61850.RcbAttrType.ENTRY_ID, b'\x00')])
566
567        attrs = collections.deque()
568        if 'trigger_options' in rcb_conf:
569            attrs.append((iec61850.RcbAttrType.TRIGGER_OPTIONS,
570                          set(iec61850.TriggerCondition[i]
571                              for i in rcb_conf['trigger_options'])))
572        if 'optional_fields' in rcb_conf:
573            attrs.append((iec61850.RcbAttrType.OPTIONAL_FIELDS,
574                          set(iec61850.OptionalField[i]
575                              for i in rcb_conf['optional_fields'])))
576        if 'buffer_time' in rcb_conf:
577            attrs.append((iec61850.RcbAttrType.BUFFER_TIME,
578                          rcb_conf['buffer_time']))
579        if 'integrity_period' in rcb_conf:
580            attrs.append((iec61850.RcbAttrType.INTEGRITY_PERIOD,
581                          rcb_conf['integrity_period']))
582        if attrs:
583            await self._set_rcb(ref, attrs)
584
585        await self._set_rcb(
586            ref, [(iec61850.RcbAttrType.REPORT_ENABLE, True)], critical=True)
587        await self._set_rcb(
588            ref, [(iec61850.RcbAttrType.GI, True)], critical=True)
589        self._log.debug('rcb %s initiated', ref)
590
591    async def _set_rcb(self, ref, attrs, critical=False):
592        try:
593            resp = await self._conn.set_rcb_attrs(ref, attrs)
594            attrs_failed = set((attr, attr_res)
595                               for attr, attr_res in resp.items()
596                               if isinstance(attr_res, iec61850.ServiceError))
597            if attrs_failed:
598                raise Exception(f"set attribute errors: {attrs_failed}")
599
600        except Exception as e:
601            if critical:
602                raise Exception(f'set rcb {ref} failed') from e
603
604            else:
605                self._log.warning('set rcb %s failed: %s', ref, e, exc_info=e)
606
607    async def _create_dynamic_datasets(self):
608        existing_ds_refs = set()
609        for ds_ref in self._persist_dyn_datasets:
610            ld = ds_ref.logical_device
611            res = await self._conn.get_persisted_dataset_refs(ld)
612            if isinstance(res, iec61850.ServiceError):
613                raise Exception(f'get datasets for ld {ld} failed: {res}')
614
615            existing_ds_refs.update(res)
616
617        existing_persisted_ds_refs = existing_ds_refs.intersection(
618            self._persist_dyn_datasets)
619        for ds_ref, ds_value_refs in self._dyn_datasets_values.items():
620            if ds_ref in existing_persisted_ds_refs:
621                res = await self._conn.get_dataset_data_refs(ds_ref)
622                if isinstance(res, iec61850.ServiceError):
623                    raise Exception(f'get ds {ds_ref} data refs failed: {res}')
624                else:
625                    exist_ds_value_refs = res
626
627                if ds_value_refs == list(exist_ds_value_refs):
628                    self._log.debug('dataset %s already exists', ds_ref)
629                    continue
630
631                raise Exception('persisted dataset changed')
632
633            res = await self._conn.create_dataset(ds_ref, ds_value_refs)
634            if res is not None:
635                raise Exception(f'create dataset {ds_ref} failed: {res}')
636
637            self._log.debug("dataset %s crated", ds_ref)
638
639    async def _register_events(self, events):
640        try:
641            await self._eventer_client.register(events)
642
643        except ConnectionError:
644            self.close()
645            raise
646
647    async def _register_status(self, status):
648        if status == self._conn_status:
649            return
650
651        event = hat.event.common.RegisterEvent(
652            type=(*self._event_type_prefix, 'gateway', 'status'),
653            source_timestamp=None,
654            payload=hat.event.common.EventPayloadJson(status))
655        await self._register_events([event])
656        self._conn_status = status
657        self._log.debug('registered status %s', status)

Device interface

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

Group controlling resource's lifetime.

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

Process received events

This method can be coroutine or regular function.