hat.gateway.devices.iec61850.client

IEC 61850 client device

   1"""IEC 61850 client device"""
   2
   3from collections.abc import Collection
   4import asyncio
   5import collections
   6import datetime
   7import logging
   8import math
   9
  10from hat import aio
  11from hat import json
  12from hat import util
  13from hat.drivers import iec61850
  14from hat.drivers import tcp
  15import hat.event.common
  16
  17from hat.gateway import common
  18
  19
  20mlog: logging.Logger = logging.getLogger(__name__)
  21
  22
  23termination_timeout: int = 100
  24
  25report_segments_timeout: int = 100
  26
  27
  28async def create(conf: common.DeviceConf,
  29                 eventer_client: hat.event.eventer.Client,
  30                 event_type_prefix: common.EventTypePrefix
  31                 ) -> 'Iec61850ClientDevice':
  32
  33    value_types = {}
  34    value_types_61850 = {}
  35    for vt in conf['value_types']:
  36        ref = (vt['logical_device'],
  37               vt['logical_node'],
  38               vt['fc'],
  39               vt['name'])
  40        value_types_61850[ref] = _vtype_61850_from_vtype_conf(vt['type'])
  41        value_types[ref] = _vtype_from_vtype_conf(vt['type'])
  42
  43    rcb_confs = {i['report_id']: i for i in conf['rcbs']}
  44    dataset_confs = {_dataset_ref_from_conf(ds_conf['ref']): ds_conf
  45                     for ds_conf in conf['datasets']}
  46    value_ref_data_name = {}
  47    for data_conf in conf['data']:
  48        data_v_ref = _value_ref_from_conf(data_conf['value'])
  49
  50        q_ref = (_value_ref_from_conf(data_conf['quality'])
  51                 if data_conf.get('quality') else None)
  52        if data_conf.get('quality'):
  53            q_type = _value_type_from_ref(value_types, q_ref)
  54            if q_type != iec61850.AcsiValueType.QUALITY:
  55                raise Exception(f"invalid quality type {q_ref}")
  56
  57        t_ref = (_value_ref_from_conf(data_conf['timestamp'])
  58                 if data_conf.get('timestamp') else None)
  59        if t_ref:
  60            t_type = _value_type_from_ref(value_types, t_ref)
  61            if t_type != iec61850.AcsiValueType.TIMESTAMP:
  62                raise Exception(f"invalid timestamp type {t_ref}")
  63
  64        seld_ref = (_value_ref_from_conf(data_conf['selected'])
  65                    if data_conf.get('selected') else None)
  66        if seld_ref:
  67            seld_type = _value_type_from_ref(value_types, seld_ref)
  68            if seld_type != iec61850.BasicValueType.BOOLEAN:
  69                raise Exception(f"invalid selected type {seld_ref}")
  70
  71        rcb_conf = rcb_confs[data_conf['report_id']]
  72        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
  73        ds_conf = dataset_confs[ds_ref]
  74        for value_conf in ds_conf['values']:
  75            value_ref = _value_ref_from_conf(value_conf)
  76            if (_refs_match(data_v_ref, value_ref) or
  77                    (q_ref and _refs_match(q_ref, value_ref)) or
  78                    (t_ref and _refs_match(t_ref, value_ref)) or
  79                    (seld_ref and _refs_match(seld_ref, value_ref))):
  80                value_ref_data_name[value_ref] = data_conf['name']
  81
  82    dataset_values_ref_type = {}
  83    for ds_conf in conf['datasets']:
  84        for val_ref_conf in ds_conf['values']:
  85            value_ref = _value_ref_from_conf(val_ref_conf)
  86            value_type = _value_type_from_ref(value_types, value_ref)
  87            dataset_values_ref_type[value_ref] = value_type
  88
  89    command_ref_value_type = {}
  90    for cmd_conf in conf['commands']:
  91        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
  92        value_type = _value_type_from_ref(value_types, cmd_ref)
  93        command_ref_value_type[cmd_ref] = value_type
  94
  95    change_ref_value_type = {}
  96    for change_conf in conf['changes']:
  97        value_ref = _value_ref_from_conf(change_conf['ref'])
  98        value_type = _value_type_from_ref(value_types, value_ref)
  99        change_ref_value_type[value_ref] = value_type
 100
 101    entry_id_event_types = [
 102        (*event_type_prefix, 'gateway', 'entry_id', i['report_id'])
 103        for i in conf['rcbs'] if i['ref']['type'] == 'BUFFERED']
 104    if entry_id_event_types:
 105        result = await eventer_client.query(
 106            hat.event.common.QueryLatestParams(entry_id_event_types))
 107        rcbs_entry_ids = {
 108            event.type[5]: (bytes.fromhex(event.payload.data)
 109                            if event.payload.data is not None else None)
 110            for event in result.events}
 111    else:
 112        rcbs_entry_ids = {}
 113
 114    device = Iec61850ClientDevice()
 115
 116    device._rcbs_entry_ids = rcbs_entry_ids
 117    device._conf = conf
 118    device._eventer_client = eventer_client
 119    device._event_type_prefix = event_type_prefix
 120    device._conn = None
 121    device._conn_status = None
 122    device._terminations = {}
 123    device._reports_segments = {}
 124
 125    device._value_ref_data_name = value_ref_data_name
 126    device._dataset_values_ref_type = dataset_values_ref_type
 127    device._command_ref_value_type = command_ref_value_type
 128    device._change_ref_value_type = change_ref_value_type
 129    device._data_name_confs = {i['name']: i for i in conf['data']}
 130    device._command_name_confs = {i['name']: i for i in conf['commands']}
 131    device._command_name_ctl_nums = {i['name']: 0 for i in conf['commands']}
 132    device._change_name_value_refs = {
 133        i['name']: _value_ref_from_conf(i['ref']) for i in conf['changes']}
 134    device._rcb_type = {rcb_conf['report_id']: rcb_conf['ref']['type']
 135                        for rcb_conf in conf['rcbs']}
 136    device._persist_dyn_datasets = set(_get_persist_dyn_datasets(conf))
 137    device._dyn_datasets_values = dict(_get_dyn_datasets_values(conf))
 138    device._report_data_refs = collections.defaultdict(collections.deque)
 139    for rcb_conf in device._conf['rcbs']:
 140        report_id = rcb_conf['report_id']
 141        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
 142        for value_conf in dataset_confs[ds_ref]['values']:
 143            value_ref = _value_ref_from_conf(value_conf)
 144            device._report_data_refs[report_id].append(value_ref)
 145
 146    device._data_value_types = dict(
 147        _get_data_value_types(conf, value_types_61850))
 148    device._cmd_value_types = dict(
 149        _get_cmd_value_types(conf, value_types_61850))
 150
 151    device._async_group = aio.Group()
 152    device._async_group.spawn(device._connection_loop)
 153    device._loop = asyncio.get_running_loop()
 154
 155    return device
 156
 157
 158info: common.DeviceInfo = common.DeviceInfo(
 159    type="iec61850_client",
 160    create=create,
 161    json_schema_id="hat-gateway://iec61850.yaml#/$defs/client",
 162    json_schema_repo=common.json_schema_repo)
 163
 164
 165class Iec61850ClientDevice(common.Device):
 166
 167    @property
 168    def async_group(self) -> aio.Group:
 169        return self._async_group
 170
 171    async def process_events(self, events: Collection[hat.event.common.Event]):
 172        try:
 173            for event in events:
 174                suffix = event.type[len(self._event_type_prefix):]
 175
 176                if suffix[:2] == ('system', 'command'):
 177                    cmd_name, = suffix[2:]
 178                    await self._process_cmd_req(event, cmd_name)
 179
 180                elif suffix[:2] == ('system', 'change'):
 181                    val_name, = suffix[2:]
 182                    await self._process_change_req(event, val_name)
 183
 184                else:
 185                    raise Exception('unsupported event type')
 186
 187        except Exception as e:
 188            mlog.warning('error processing event %s: %s',
 189                         event.type, e, exc_info=e)
 190
 191    async def _connection_loop(self):
 192
 193        async def cleanup():
 194            await self._register_status('DISCONNECTED')
 195            if self._conn:
 196                await self._conn.async_close()
 197
 198        conn_conf = self._conf['connection']
 199        try:
 200            while True:
 201                await self._register_status('CONNECTING')
 202                try:
 203                    mlog.debug('connecting to %s:%s',
 204                               conn_conf['host'], conn_conf['port'])
 205                    self._conn = await aio.wait_for(
 206                        iec61850.connect(
 207                            addr=tcp.Address(conn_conf['host'],
 208                                             conn_conf['port']),
 209                            data_value_types=self._data_value_types,
 210                            cmd_value_types=self._cmd_value_types,
 211                            report_data_refs=self._report_data_refs,
 212                            report_cb=self._on_report,
 213                            termination_cb=self._on_termination,
 214                            status_delay=conn_conf['status_delay'],
 215                            status_timeout=conn_conf['status_timeout'],
 216                            local_tsel=conn_conf.get('local_tsel'),
 217                            remote_tsel=conn_conf.get('remote_tsel'),
 218                            local_ssel=conn_conf.get('local_ssel'),
 219                            remote_ssel=conn_conf.get('remote_ssel'),
 220                            local_psel=conn_conf.get('local_psel'),
 221                            remote_psel=conn_conf.get('remote_psel'),
 222                            local_ap_title=conn_conf.get('local_ap_title'),
 223                            remote_ap_title=conn_conf.get('remote_ap_title'),
 224                            local_ae_qualifier=conn_conf.get(
 225                                'local_ae_qualifier'),
 226                            remote_ae_qualifier=conn_conf.get(
 227                                'remote_ae_qualifier'),
 228                            local_detail_calling=conn_conf.get(
 229                                'local_detail_calling')),
 230                        conn_conf['connect_timeout'])
 231
 232                except Exception as e:
 233                    mlog.warning('connnection failed: %s', e, exc_info=e)
 234                    await self._register_status('DISCONNECTED')
 235                    await asyncio.sleep(conn_conf['reconnect_delay'])
 236                    continue
 237
 238                mlog.debug('connected')
 239                await self._register_status('CONNECTED')
 240
 241                initialized = False
 242                try:
 243                    await self._create_dynamic_datasets()
 244                    for rcb_conf in self._conf['rcbs']:
 245                        await self._init_rcb(rcb_conf)
 246                    initialized = True
 247
 248                except Exception as e:
 249                    mlog.warning(
 250                        'initialization failed: %s, closing connection',
 251                        e, exc_info=e)
 252                    self._conn.close()
 253
 254                await self._conn.wait_closing()
 255                await self._register_status('DISCONNECTED')
 256                await self._conn.wait_closed()
 257                self._conn = None
 258                self._terminations = {}
 259                self._reports_segments = {}
 260                if not initialized:
 261                    await asyncio.sleep(conn_conf['reconnect_delay'])
 262
 263        except Exception as e:
 264            mlog.error('connection loop error: %s', e, exc_info=e)
 265
 266        finally:
 267            mlog.debug('closing connection loop')
 268            self.close()
 269            await aio.uncancellable(cleanup())
 270
 271    async def _process_cmd_req(self, event, cmd_name):
 272        if not self._conn or not self._conn.is_open:
 273            raise Exception('no connection')
 274
 275        if cmd_name not in self._command_name_confs:
 276            raise Exception('unexpected command name')
 277
 278        cmd_conf = self._command_name_confs[cmd_name]
 279        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 280        action = event.payload.data['action']
 281        evt_session_id = event.payload.data['session_id']
 282        if (action == 'SELECT' and
 283                cmd_conf['model'] == 'SBO_WITH_NORMAL_SECURITY'):
 284            cmd = None
 285        else:
 286            ctl_num = self._command_name_ctl_nums[cmd_name]
 287            ctl_num = _update_ctl_num(ctl_num, action, cmd_conf['model'])
 288            self._command_name_ctl_nums[cmd_name] = ctl_num
 289            value_type = self._command_ref_value_type[cmd_ref]
 290            if value_type is None:
 291                raise Exception('value type undefined')
 292
 293            cmd = _command_from_event(event, cmd_conf, ctl_num, value_type)
 294
 295        term_future = None
 296        if (action == 'OPERATE' and
 297                cmd_conf['model'] in ['DIRECT_WITH_ENHANCED_SECURITY',
 298                                      'SBO_WITH_ENHANCED_SECURITY']):
 299            term_future = self._loop.create_future()
 300            self._conn.async_group.spawn(
 301                self._wait_cmd_term, cmd_name, cmd_ref, cmd, evt_session_id,
 302                term_future)
 303
 304        try:
 305            resp = await aio.wait_for(
 306                self._send_command(action, cmd_ref, cmd),
 307                self._conf['connection']['response_timeout'])
 308
 309        except (asyncio.TimeoutError, ConnectionError) as e:
 310            mlog.warning('send command failed: %s', e, exc_info=e)
 311            if term_future and not term_future.done():
 312                term_future.cancel()
 313            return
 314
 315        if resp is not None:
 316            if term_future and not term_future.done():
 317                term_future.cancel()
 318
 319        event = _cmd_resp_to_event(
 320            self._event_type_prefix, cmd_name, evt_session_id, action, resp)
 321        await self._register_events([event])
 322
 323    async def _send_command(self, action, cmd_ref, cmd):
 324        if action == 'SELECT':
 325            return await self._conn.select(cmd_ref, cmd)
 326
 327        if action == 'CANCEL':
 328            return await self._conn.cancel(cmd_ref, cmd)
 329
 330        if action == 'OPERATE':
 331            return await self._conn.operate(cmd_ref, cmd)
 332
 333        raise Exception('unsupported action')
 334
 335    async def _wait_cmd_term(self, cmd_name, cmd_ref, cmd, session_id, future):
 336        cmd_session_id = _get_command_session_id(cmd_ref, cmd)
 337        self._terminations[cmd_session_id] = future
 338        try:
 339            term = await aio.wait_for(future, termination_timeout)
 340            event = _cmd_resp_to_event(
 341                self._event_type_prefix, cmd_name, session_id, 'TERMINATION',
 342                term.error)
 343            await self._register_events([event])
 344
 345        except asyncio.TimeoutError:
 346            mlog.warning('command termination timeout')
 347
 348        finally:
 349            del self._terminations[cmd_session_id]
 350
 351    async def _process_change_req(self, event, value_name):
 352        if not self._conn or not self._conn.is_open:
 353            raise Exception('no connection')
 354
 355        if value_name not in self._change_name_value_refs:
 356            raise Exception('unexpected change name')
 357
 358        ref = self._change_name_value_refs[value_name]
 359        value_type = self._change_ref_value_type[ref]
 360        if value_type is None:
 361            raise Exception('value type undefined')
 362
 363        value = _value_from_json(event.payload.data['value'], value_type)
 364        try:
 365            resp = await aio.wait_for(
 366                self._conn.write_data(ref, value),
 367                self._conf['connection']['response_timeout'])
 368
 369        except asyncio.TimeoutError:
 370            mlog.warning('write data response timeout')
 371            return
 372
 373        except ConnectionError as e:
 374            mlog.warning('connection error on write data: %s', e, exc_info=e)
 375            return
 376
 377        session_id = event.payload.data['session_id']
 378        event = _write_data_resp_to_event(
 379            self._event_type_prefix, value_name, session_id, resp)
 380        await self._register_events([event])
 381
 382    async def _on_report(self, report):
 383        try:
 384            events = list(self._events_from_report(report))
 385            if not events:
 386                return
 387
 388            await self._register_events(events)
 389            if self._rcb_type[report.report_id] == 'BUFFERED':
 390                self._rcbs_entry_ids[report.report_id] = report.entry_id
 391
 392        except Exception as e:
 393            mlog.warning('report %s ignored: %s',
 394                         report.report_id, e, exc_info=e)
 395
 396    def _events_from_report(self, report):
 397        report_id = report.report_id
 398        if report_id not in self._report_data_refs:
 399            raise Exception(f'unexpected report {report_id}')
 400
 401        segm_id = (report_id, report.sequence_number)
 402        if report.more_segments_follow:
 403            if segm_id in self._reports_segments:
 404                segment_data, timeout_timer = self._reports_segments[segm_id]
 405                timeout_timer.cancel()
 406            else:
 407                segment_data = collections.deque()
 408
 409            segment_data.extend(report.data)
 410            timeout_timer = self._loop.call_later(
 411                report_segments_timeout, self._reports_segments.pop, segm_id)
 412            self._reports_segments[segm_id] = (segment_data, timeout_timer)
 413            return
 414
 415        if segm_id in self._reports_segments:
 416            report_data, timeout_timer = self._reports_segments.pop(segm_id)
 417            timeout_timer.cancel()
 418            report_data.extend(report.data)
 419
 420        else:
 421            report_data = report.data
 422
 423        yield from self._events_from_report_data(report_data, report_id)
 424
 425        if self._rcb_type[report_id] == 'BUFFERED':
 426            yield hat.event.common.RegisterEvent(
 427                type=(*self._event_type_prefix, 'gateway',
 428                      'entry_id', report_id),
 429                source_timestamp=None,
 430                payload=hat.event.common.EventPayloadJson(
 431                    report.entry_id.hex()
 432                    if report.entry_id is not None else None))
 433
 434    def _events_from_report_data(self, report_data, report_id):
 435        data_values_json = collections.defaultdict(dict)
 436        data_reasons = collections.defaultdict(set)
 437        for rv in report_data:
 438            if rv.ref not in self._value_ref_data_name:
 439                continue
 440
 441            data_name = self._value_ref_data_name[rv.ref]
 442            value_type = self._dataset_values_ref_type[rv.ref]
 443            if value_type is None:
 444                mlog.warning('report data ignored: unknown value type')
 445                continue
 446
 447            value_json = _value_to_json(rv.value, value_type)
 448            value_path = [rv.ref.logical_device, rv.ref.logical_node,
 449                          rv.ref.fc, *rv.ref.names]
 450            data_values_json[data_name] = json.set_(
 451                data_values_json[data_name], value_path, value_json)
 452            if rv.reasons:
 453                data_reasons[data_name].update(
 454                    reason.name for reason in rv.reasons)
 455
 456        for data_name, values_json in data_values_json.items():
 457            payload = {'reasons': list(data_reasons[data_name])}
 458            data_conf = self._data_name_confs[data_name]
 459            value_path = _conf_ref_to_path(data_conf['value'])
 460            value_json = json.get(values_json, value_path)
 461            if value_json is not None:
 462                payload['value'] = value_json
 463
 464            if 'quality' in data_conf:
 465                quality_path = _conf_ref_to_path(data_conf['quality'])
 466                quality_json = json.get(values_json, quality_path)
 467                if quality_json is not None:
 468                    payload['quality'] = quality_json
 469
 470            if 'timestamp' in data_conf:
 471                timestamp_path = _conf_ref_to_path(data_conf['timestamp'])
 472                timestamp_json = json.get(values_json, timestamp_path)
 473                if timestamp_json is not None:
 474                    payload['timestamp'] = timestamp_json
 475
 476            if 'selected' in data_conf:
 477                selected_path = _conf_ref_to_path(data_conf['selected'])
 478                selected_json = json.get(values_json, selected_path)
 479                if selected_json is not None:
 480                    payload['selected'] = selected_json
 481
 482            yield hat.event.common.RegisterEvent(
 483                type=(*self._event_type_prefix, 'gateway',
 484                      'data', data_name),
 485                source_timestamp=None,
 486                payload=hat.event.common.EventPayloadJson(payload))
 487
 488    def _on_termination(self, termination):
 489        cmd_session_id = _get_command_session_id(
 490            termination.ref, termination.cmd)
 491        if cmd_session_id not in self._terminations:
 492            mlog.warning('unexpected termination dropped')
 493            return
 494
 495        term_future = self._terminations[cmd_session_id]
 496        if not term_future.done():
 497            self._terminations[cmd_session_id].set_result(termination)
 498
 499    async def _init_rcb(self, rcb_conf):
 500        ref = iec61850.RcbRef(
 501            logical_device=rcb_conf['ref']['logical_device'],
 502            logical_node=rcb_conf['ref']['logical_node'],
 503            type=iec61850.RcbType[rcb_conf['ref']['type']],
 504            name=rcb_conf['ref']['name'])
 505        mlog.debug('initiating rcb %s', ref)
 506
 507        get_attrs = collections.deque([iec61850.RcbAttrType.REPORT_ID])
 508        dataset_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
 509        if dataset_ref not in self._dyn_datasets_values:
 510            get_attrs.append(iec61850.RcbAttrType.DATASET)
 511        if 'conf_revision' in rcb_conf:
 512            get_attrs.append(iec61850.RcbAttrType.CONF_REVISION)
 513
 514        get_rcb_resp = await self._conn.get_rcb_attrs(ref, get_attrs)
 515        _validate_get_rcb_response(get_rcb_resp, rcb_conf)
 516
 517        await self._set_rcb(ref, [(iec61850.RcbAttrType.REPORT_ENABLE, False)])
 518
 519        if dataset_ref in self._dyn_datasets_values:
 520            await self._set_rcb(
 521                ref, [(iec61850.RcbAttrType.DATASET, dataset_ref)],
 522                critical=True)
 523
 524        if ref.type == iec61850.RcbType.BUFFERED:
 525            if 'reservation_time' in rcb_conf:
 526                await self._set_rcb(
 527                    ref, [(iec61850.RcbAttrType.RESERVATION_TIME,
 528                           rcb_conf['reservation_time'])])
 529
 530            entry_id = self._rcbs_entry_ids.get(rcb_conf['report_id'])
 531            if rcb_conf.get('purge_buffer') or entry_id is None:
 532                await self._set_rcb(
 533                    ref, [(iec61850.RcbAttrType.PURGE_BUFFER, True)])
 534
 535            else:
 536                try:
 537                    await self._set_rcb(
 538                        ref, [(iec61850.RcbAttrType.ENTRY_ID, entry_id)],
 539                        critical=True)
 540
 541                except Exception as e:
 542                    mlog.warning('%s', e, exc_info=e)
 543                    # try setting entry id to 0 in order to resynchronize
 544                    await self._set_rcb(
 545                        ref, [(iec61850.RcbAttrType.ENTRY_ID, b'\x00')])
 546
 547        elif ref.type == iec61850.RcbType.UNBUFFERED:
 548            await self._set_rcb(ref, [(iec61850.RcbAttrType.RESERVE, True)])
 549
 550        attrs = collections.deque()
 551        if 'trigger_options' in rcb_conf:
 552            attrs.append((iec61850.RcbAttrType.TRIGGER_OPTIONS,
 553                          set(iec61850.TriggerCondition[i]
 554                              for i in rcb_conf['trigger_options'])))
 555        if 'optional_fields' in rcb_conf:
 556            attrs.append((iec61850.RcbAttrType.OPTIONAL_FIELDS,
 557                          set(iec61850.OptionalField[i]
 558                              for i in rcb_conf['optional_fields'])))
 559        if 'buffer_time' in rcb_conf:
 560            attrs.append((iec61850.RcbAttrType.BUFFER_TIME,
 561                          rcb_conf['buffer_time']))
 562        if 'integrity_period' in rcb_conf:
 563            attrs.append((iec61850.RcbAttrType.INTEGRITY_PERIOD,
 564                          rcb_conf['integrity_period']))
 565        if attrs:
 566            await self._set_rcb(ref, attrs)
 567
 568        await self._set_rcb(
 569            ref, [(iec61850.RcbAttrType.REPORT_ENABLE, True)], critical=True)
 570        await self._set_rcb(
 571            ref, [(iec61850.RcbAttrType.GI, True)], critical=True)
 572        mlog.debug('rcb %s initiated', ref)
 573
 574    async def _set_rcb(self, ref, attrs, critical=False):
 575        try:
 576            resp = await self._conn.set_rcb_attrs(ref, attrs)
 577            attrs_failed = set((attr, attr_res)
 578                               for attr, attr_res in resp.items()
 579                               if isinstance(attr_res, iec61850.ServiceError))
 580            if attrs_failed:
 581                raise Exception(f"set attribute errors: {attrs_failed}")
 582
 583        except Exception as e:
 584            if critical:
 585                raise Exception(f'set rcb {ref} failed') from e
 586
 587            else:
 588                mlog.warning('set rcb %s failed: %s', ref, e, exc_info=e)
 589
 590    async def _create_dynamic_datasets(self):
 591        existing_ds_refs = set()
 592        for ds_ref in self._persist_dyn_datasets:
 593            ld = ds_ref.logical_device
 594            res = await self._conn.get_persisted_dataset_refs(ld)
 595            if isinstance(res, iec61850.ServiceError):
 596                raise Exception(f'get datasets for ld {ld} failed: {res}')
 597
 598            existing_ds_refs.update(res)
 599
 600        existing_persisted_ds_refs = existing_ds_refs.intersection(
 601            self._persist_dyn_datasets)
 602        for ds_ref, ds_value_refs in self._dyn_datasets_values.items():
 603            if ds_ref in existing_persisted_ds_refs:
 604                res = await self._conn.get_dataset_data_refs(ds_ref)
 605                if isinstance(res, iec61850.ServiceError):
 606                    raise Exception(f'get ds {ds_ref} data refs failed: {res}')
 607                else:
 608                    exist_ds_value_refs = res
 609
 610                if ds_value_refs == list(exist_ds_value_refs):
 611                    mlog.debug('dataset %s already exists', ds_ref)
 612                    continue
 613
 614                mlog.debug("dataset %s exists, but different", ds_ref)
 615                res = await self._conn.delete_dataset(ds_ref)
 616                if res is not None:
 617                    raise Exception(f'delete dataset {ds_ref} failed: {res}')
 618                mlog.debug("dataset %s deleted", ds_ref)
 619
 620            res = await self._conn.create_dataset(ds_ref, ds_value_refs)
 621            if res is not None:
 622                raise Exception(f'create dataset {ds_ref} failed: {res}')
 623
 624            mlog.debug("dataset %s crated", ds_ref)
 625
 626    async def _register_events(self, events):
 627        try:
 628            await self._eventer_client.register(events)
 629
 630        except ConnectionError:
 631            self.close()
 632            raise
 633
 634    async def _register_status(self, status):
 635        if status == self._conn_status:
 636            return
 637
 638        event = hat.event.common.RegisterEvent(
 639            type=(*self._event_type_prefix, 'gateway', 'status'),
 640            source_timestamp=None,
 641            payload=hat.event.common.EventPayloadJson(status))
 642        await self._register_events([event])
 643        self._conn_status = status
 644        mlog.debug('registered status %s', status)
 645
 646
 647def _value_ref_from_conf(value_ref_conf):
 648    return iec61850.DataRef(
 649        logical_device=value_ref_conf['logical_device'],
 650        logical_node=value_ref_conf['logical_node'],
 651        fc=value_ref_conf['fc'],
 652        names=tuple(value_ref_conf['names']))
 653
 654
 655def _dataset_ref_from_conf(ds_conf):
 656    if isinstance(ds_conf, str):
 657        return iec61850.NonPersistedDatasetRef(ds_conf)
 658
 659    elif isinstance(ds_conf, dict):
 660        return iec61850.PersistedDatasetRef(**ds_conf)
 661
 662
 663def _refs_match(ref1, ref2):
 664    if ref1.logical_device != ref2.logical_device:
 665        return False
 666
 667    if ref1.logical_node != ref2.logical_node:
 668        return False
 669
 670    if ref1.fc != ref2.fc:
 671        return False
 672
 673    if len(ref1.names) == len(ref2.names):
 674        return ref1.names == ref2.names
 675
 676    if len(ref1.names) < len(ref2.names):
 677        names1, names2 = ref1.names, ref2.names
 678    else:
 679        names1, names2 = ref2.names, ref1.names
 680
 681    return names2[:len(names1)] == names1
 682
 683
 684def _get_persist_dyn_datasets(conf):
 685    for ds_conf in conf['datasets']:
 686        if not ds_conf['dynamic']:
 687            continue
 688
 689        ds_ref = _dataset_ref_from_conf(ds_conf['ref'])
 690        if isinstance(ds_ref, iec61850.PersistedDatasetRef):
 691            yield ds_ref
 692
 693
 694def _get_dyn_datasets_values(conf):
 695    for ds_conf in conf['datasets']:
 696        if not ds_conf['dynamic']:
 697            continue
 698
 699        ds_ref = _dataset_ref_from_conf(ds_conf['ref'])
 700        yield ds_ref, [_value_ref_from_conf(val_conf)
 701                       for val_conf in ds_conf['values']]
 702
 703
 704def _vtype_61850_from_vtype_conf(vt_conf):
 705    if isinstance(vt_conf, str):
 706        return _value_type_from_str(vt_conf)
 707
 708    if vt_conf['type'] == 'ARRAY':
 709        return iec61850.ArrayValueType(
 710            type=_vtype_61850_from_vtype_conf(vt_conf['element_type']),
 711            length=vt_conf['length'])
 712
 713    if vt_conf['type'] == 'STRUCT':
 714        return iec61850.StructValueType(
 715            [(el_conf['name'], _vtype_61850_from_vtype_conf(el_conf['type']))
 716             for el_conf in vt_conf['elements']])
 717
 718    raise Exception('unsupported value type')
 719
 720
 721def _vtype_from_vtype_conf(vt_conf):
 722    if isinstance(vt_conf, str):
 723        return _value_type_from_str(vt_conf)
 724
 725    if vt_conf['type'] == 'ARRAY':
 726        return iec61850.ArrayValueType(
 727            type=_vtype_from_vtype_conf(vt_conf['element_type']),
 728            length=vt_conf['length'])
 729
 730    if vt_conf['type'] == 'STRUCT':
 731        return {el_conf['name']: _vtype_from_vtype_conf(el_conf['type'])
 732                for el_conf in vt_conf['elements']}
 733
 734    raise Exception('unsupported value type')
 735
 736
 737def _value_type_from_str(vt_conf):
 738    if vt_conf in ['BOOLEAN',
 739                   'INTEGER',
 740                   'UNSIGNED',
 741                   'FLOAT',
 742                   'BIT_STRING',
 743                   'OCTET_STRING',
 744                   'VISIBLE_STRING',
 745                   'MMS_STRING']:
 746        return iec61850.BasicValueType(vt_conf)
 747
 748    if vt_conf in ['QUALITY',
 749                   'TIMESTAMP',
 750                   'DOUBLE_POINT',
 751                   'DIRECTION',
 752                   'SEVERITY',
 753                   'ANALOGUE',
 754                   'VECTOR',
 755                   'STEP_POSITION',
 756                   'BINARY_CONTROL']:
 757        return iec61850.AcsiValueType(vt_conf)
 758
 759    raise Exception('unsupported value type')
 760
 761
 762def _value_type_from_ref(value_types, ref):
 763    left_names = []
 764    if isinstance(ref, iec61850.CommandRef):
 765        left_names = []
 766        key = (ref.logical_device, ref.logical_node, 'CO', ref.name)
 767
 768    elif isinstance(ref, iec61850.DataRef):
 769        left_names = ref.names[1:]
 770        key = (ref.logical_device, ref.logical_node, ref.fc, ref.names[0])
 771
 772    else:
 773        raise Exception('unexpected reference')
 774
 775    if key not in value_types:
 776        return
 777
 778    value_type = value_types[key]
 779    while left_names:
 780        name = left_names[0]
 781        left_names = left_names[1:]
 782        if isinstance(name, str):
 783            if isinstance(value_type, iec61850.StructValueType):
 784                _, value_type = util.first(
 785                    value_type.elements, lambda i: i[0] == name)
 786            else:
 787                value_type = value_type[name]
 788
 789        if isinstance(name, int):
 790            value_type = value_type.type
 791
 792    return value_type
 793
 794
 795def _get_data_value_types(conf, value_types):
 796    for ds_conf in conf['datasets']:
 797        for val_ref_conf in ds_conf['values']:
 798            value_ref = _value_ref_from_conf(val_ref_conf)
 799            value_type = _value_type_from_ref(value_types, value_ref)
 800            yield value_ref, value_type
 801
 802    for change_conf in conf['changes']:
 803        value_ref = _value_ref_from_conf(change_conf['ref'])
 804        value_type = _value_type_from_ref(value_types, value_ref)
 805        yield value_ref, value_type
 806
 807
 808def _get_cmd_value_types(conf, value_types):
 809    for cmd_conf in conf['commands']:
 810        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 811        value_type = _value_type_from_ref(value_types, cmd_ref)
 812        yield cmd_ref, value_type
 813
 814
 815_epoch_start = datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
 816
 817
 818def _command_from_event(event, cmd_conf, control_number, value_type):
 819    if cmd_conf['with_operate_time']:
 820        operate_time = iec61850.Timestamp(
 821            value=_epoch_start,
 822            leap_second=False,
 823            clock_failure=False,
 824            not_synchronized=False,
 825            accuracy=0)
 826    else:
 827        operate_time = None
 828    return iec61850.Command(
 829        value=_value_from_json(event.payload.data['value'], value_type),
 830        operate_time=operate_time,
 831        origin=iec61850.Origin(
 832            category=iec61850.OriginCategory[
 833                event.payload.data['origin']['category']],
 834            identification=event.payload.data[
 835                'origin']['identification'].encode('utf-8')),
 836        control_number=control_number,
 837        t=_timestamp_from_event_timestamp(event.timestamp),
 838        test=event.payload.data['test'],
 839        checks=set(iec61850.Check[i] for i in event.payload.data['checks']))
 840
 841
 842def _timestamp_from_event_timestamp(timestamp):
 843    return iec61850.Timestamp(
 844        value=hat.event.common.timestamp_to_datetime(timestamp),
 845        leap_second=False,
 846        clock_failure=False,
 847        not_synchronized=False,
 848        accuracy=None)
 849
 850
 851def _cmd_resp_to_event(event_type_prefix, cmd_name, event_session_id, action,
 852                       resp):
 853    success = resp is None
 854    payload = {'session_id': event_session_id,
 855               'action': action,
 856               'success': success}
 857    if not success:
 858        if resp.service_error is not None:
 859            payload['service_error'] = resp.service_error.name
 860        if resp.additional_cause is not None:
 861            payload['additional_cause'] = resp.additional_cause.name
 862        if resp.test_error is not None:
 863            payload['test_error'] = resp.test_error.name
 864
 865    return hat.event.common.RegisterEvent(
 866        type=(*event_type_prefix, 'gateway', 'command', cmd_name),
 867        source_timestamp=None,
 868        payload=hat.event.common.EventPayloadJson(payload))
 869
 870
 871def _write_data_resp_to_event(event_type_prefix, value_name, session_id, resp):
 872    success = resp is None
 873    payload = {'session_id': session_id,
 874               'success': success}
 875    if not success:
 876        payload['error'] = resp.name
 877    return hat.event.common.RegisterEvent(
 878        type=(*event_type_prefix, 'gateway', 'change', value_name),
 879        source_timestamp=None,
 880        payload=hat.event.common.EventPayloadJson(payload))
 881
 882
 883def _get_command_session_id(cmd_ref, cmd):
 884    return (cmd_ref, cmd.control_number)
 885
 886
 887def _value_from_json(event_value, value_type):
 888    if isinstance(value_type, iec61850.BasicValueType):
 889        if value_type == iec61850.BasicValueType.OCTET_STRING:
 890            return bytes.fromhex(event_value)
 891
 892        elif value_type == iec61850.BasicValueType.FLOAT:
 893            return float(event_value)
 894
 895        else:
 896            return event_value
 897
 898    if value_type == iec61850.AcsiValueType.QUALITY:
 899        return iec61850.Quality(
 900            validity=iec61850.QualityValidity[event_value['validity']],
 901            details={iec61850.QualityDetail[i]
 902                     for i in event_value['details']},
 903            source=iec61850.QualitySource[event_value['source']],
 904            test=event_value['test'],
 905            operator_blocked=event_value['operator_blocked'])
 906
 907    if value_type == iec61850.AcsiValueType.TIMESTAMP:
 908        return iec61850.Timestamp(
 909            value=datetime.datetime.fromtimestamp(event_value['value'],
 910                                                  datetime.timezone.utc),
 911            leap_second=event_value['leap_second'],
 912            clock_failure=event_value['clock_failure'],
 913            not_synchronized=event_value['not_synchronized'],
 914            accuracy=event_value.get('accuracy'))
 915
 916    if value_type == iec61850.AcsiValueType.DOUBLE_POINT:
 917        return iec61850.DoublePoint[event_value]
 918
 919    if value_type == iec61850.AcsiValueType.DIRECTION:
 920        return iec61850.Direction[event_value]
 921
 922    if value_type == iec61850.AcsiValueType.SEVERITY:
 923        return iec61850.Severity[event_value]
 924
 925    if value_type == iec61850.AcsiValueType.ANALOGUE:
 926        return iec61850.Analogue(
 927            i=event_value.get('i'),
 928            f=(float(event_value['f']) if 'f' in event_value else None))
 929
 930    if value_type == iec61850.AcsiValueType.VECTOR:
 931        return iec61850.Vector(
 932            magnitude=_value_from_json(event_value['magnitude'],
 933                                       iec61850.AcsiValueType.ANALOGUE),
 934            angle=(_value_from_json(event_value['angle'],
 935                                    iec61850.AcsiValueType.ANALOGUE)
 936                   if 'angle' in event_value else None))
 937
 938    if value_type == iec61850.AcsiValueType.STEP_POSITION:
 939        return iec61850.StepPosition(value=event_value['value'],
 940                                     transient=event_value.get('transient'))
 941
 942    if value_type == iec61850.AcsiValueType.BINARY_CONTROL:
 943        return iec61850.BinaryControl[event_value]
 944
 945    if isinstance(value_type, iec61850.ArrayValueType):
 946        return [_value_from_json(val, value_type.type)
 947                for val in event_value]
 948
 949    if isinstance(value_type, dict):
 950        return {k: _value_from_json(v, value_type[k])
 951                for k, v in event_value.items()}
 952
 953    raise Exception('unsupported value type')
 954
 955
 956def _value_to_json(data_value, value_type):
 957    if isinstance(value_type, iec61850.BasicValueType):
 958        if value_type == iec61850.BasicValueType.OCTET_STRING:
 959            return data_value.hex()
 960
 961        elif value_type == iec61850.BasicValueType.BIT_STRING:
 962            return list(data_value)
 963
 964        elif value_type == iec61850.BasicValueType.FLOAT:
 965            return data_value if math.isfinite(data_value) else str(data_value)
 966
 967        else:
 968            return data_value
 969
 970    if isinstance(value_type, iec61850.AcsiValueType):
 971        if value_type == iec61850.AcsiValueType.QUALITY:
 972            return {'validity': data_value.validity.name,
 973                    'details': [i.name for i in data_value.details],
 974                    'source': data_value.source.name,
 975                    'test': data_value.test,
 976                    'operator_blocked': data_value.operator_blocked}
 977
 978        if value_type == iec61850.AcsiValueType.TIMESTAMP:
 979            val = {'value': data_value.value.timestamp(),
 980                   'leap_second': data_value.leap_second,
 981                   'clock_failure': data_value.clock_failure,
 982                   'not_synchronized': data_value.not_synchronized}
 983            if data_value.accuracy is not None:
 984                val['accuracy'] = data_value.accuracy
 985            return val
 986
 987        if value_type in [iec61850.AcsiValueType.DOUBLE_POINT,
 988                          iec61850.AcsiValueType.DIRECTION,
 989                          iec61850.AcsiValueType.SEVERITY,
 990                          iec61850.AcsiValueType.BINARY_CONTROL]:
 991            return data_value.name
 992
 993        if value_type == iec61850.AcsiValueType.ANALOGUE:
 994            val = {}
 995            if data_value.i is not None:
 996                val['i'] = data_value.i
 997            if data_value.f is not None:
 998                val['f'] = (data_value.f if math.isfinite(data_value.f)
 999                            else str(data_value.f))
1000            return val
1001
1002        if value_type == iec61850.AcsiValueType.VECTOR:
1003            val = {'magnitude': _value_to_json(
1004                        data_value.magnitude, iec61850.AcsiValueType.ANALOGUE)}
1005            if data_value.angle is not None:
1006                val['angle'] = _value_to_json(
1007                    data_value.angle, iec61850.AcsiValueType.ANALOGUE)
1008            return val
1009
1010        if value_type == iec61850.AcsiValueType.STEP_POSITION:
1011            val = {'value': data_value.value}
1012            if data_value.transient is not None:
1013                val['transient'] = data_value.transient
1014            return val
1015
1016    if isinstance(value_type, iec61850.ArrayValueType):
1017        return [_value_to_json(i, value_type.type) for i in data_value]
1018
1019    if isinstance(value_type, dict):
1020        return {
1021            child_name: _value_to_json(child_value, value_type[child_name])
1022            for child_name, child_value in data_value.items()}
1023
1024    raise Exception('unsupported value type')
1025
1026
1027def _update_ctl_num(ctl_num, action, cmd_model):
1028    if action == 'SELECT' or (
1029        action == 'OPERATE' and
1030        cmd_model in ['DIRECT_WITH_NORMAL_SECURITY',
1031                      'DIRECT_WITH_ENHANCED_SECURITY']):
1032        return (ctl_num + 1) % 256
1033
1034    return ctl_num
1035
1036
1037def _validate_get_rcb_response(get_rcb_resp, rcb_conf):
1038    for k, v in get_rcb_resp.items():
1039        if isinstance(v, iec61850.ServiceError):
1040            raise Exception(f"get {k.name} failed: {v}")
1041
1042        if (k == iec61850.RcbAttrType.REPORT_ID and
1043                v != rcb_conf['report_id']):
1044            raise Exception(f"rcb report id {v} different from "
1045                            f"configured {rcb_conf['report_id']}")
1046
1047        if (k == iec61850.RcbAttrType.DATASET and
1048                v != _dataset_ref_from_conf(rcb_conf['dataset'])):
1049            raise Exception(f"rcb dataset {v} different from "
1050                            f"configured {rcb_conf['report_id']}")
1051
1052        if (k == iec61850.RcbAttrType.CONF_REVISION and
1053                v != rcb_conf['conf_revision']):
1054            raise Exception(
1055                f"Conf revision {v} different from "
1056                f"the configuration defined {rcb_conf['conf_revision']}")
1057
1058
1059def _conf_ref_to_path(conf_ref):
1060    return [conf_ref['logical_device'],
1061            conf_ref['logical_node'],
1062            conf_ref['fc'],
1063            *conf_ref['names']]
mlog: logging.Logger = <Logger hat.gateway.devices.iec61850.client (WARNING)>
termination_timeout: int = 100
report_segments_timeout: int = 100
async def create( conf: Union[NoneType, bool, int, float, str, List[ForwardRef('Data')], Dict[str, ForwardRef('Data')]], eventer_client: hat.event.eventer.client.Client, event_type_prefix: tuple[str, str, str]) -> Iec61850ClientDevice:
 29async def create(conf: common.DeviceConf,
 30                 eventer_client: hat.event.eventer.Client,
 31                 event_type_prefix: common.EventTypePrefix
 32                 ) -> 'Iec61850ClientDevice':
 33
 34    value_types = {}
 35    value_types_61850 = {}
 36    for vt in conf['value_types']:
 37        ref = (vt['logical_device'],
 38               vt['logical_node'],
 39               vt['fc'],
 40               vt['name'])
 41        value_types_61850[ref] = _vtype_61850_from_vtype_conf(vt['type'])
 42        value_types[ref] = _vtype_from_vtype_conf(vt['type'])
 43
 44    rcb_confs = {i['report_id']: i for i in conf['rcbs']}
 45    dataset_confs = {_dataset_ref_from_conf(ds_conf['ref']): ds_conf
 46                     for ds_conf in conf['datasets']}
 47    value_ref_data_name = {}
 48    for data_conf in conf['data']:
 49        data_v_ref = _value_ref_from_conf(data_conf['value'])
 50
 51        q_ref = (_value_ref_from_conf(data_conf['quality'])
 52                 if data_conf.get('quality') else None)
 53        if data_conf.get('quality'):
 54            q_type = _value_type_from_ref(value_types, q_ref)
 55            if q_type != iec61850.AcsiValueType.QUALITY:
 56                raise Exception(f"invalid quality type {q_ref}")
 57
 58        t_ref = (_value_ref_from_conf(data_conf['timestamp'])
 59                 if data_conf.get('timestamp') else None)
 60        if t_ref:
 61            t_type = _value_type_from_ref(value_types, t_ref)
 62            if t_type != iec61850.AcsiValueType.TIMESTAMP:
 63                raise Exception(f"invalid timestamp type {t_ref}")
 64
 65        seld_ref = (_value_ref_from_conf(data_conf['selected'])
 66                    if data_conf.get('selected') else None)
 67        if seld_ref:
 68            seld_type = _value_type_from_ref(value_types, seld_ref)
 69            if seld_type != iec61850.BasicValueType.BOOLEAN:
 70                raise Exception(f"invalid selected type {seld_ref}")
 71
 72        rcb_conf = rcb_confs[data_conf['report_id']]
 73        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
 74        ds_conf = dataset_confs[ds_ref]
 75        for value_conf in ds_conf['values']:
 76            value_ref = _value_ref_from_conf(value_conf)
 77            if (_refs_match(data_v_ref, value_ref) or
 78                    (q_ref and _refs_match(q_ref, value_ref)) or
 79                    (t_ref and _refs_match(t_ref, value_ref)) or
 80                    (seld_ref and _refs_match(seld_ref, value_ref))):
 81                value_ref_data_name[value_ref] = data_conf['name']
 82
 83    dataset_values_ref_type = {}
 84    for ds_conf in conf['datasets']:
 85        for val_ref_conf in ds_conf['values']:
 86            value_ref = _value_ref_from_conf(val_ref_conf)
 87            value_type = _value_type_from_ref(value_types, value_ref)
 88            dataset_values_ref_type[value_ref] = value_type
 89
 90    command_ref_value_type = {}
 91    for cmd_conf in conf['commands']:
 92        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
 93        value_type = _value_type_from_ref(value_types, cmd_ref)
 94        command_ref_value_type[cmd_ref] = value_type
 95
 96    change_ref_value_type = {}
 97    for change_conf in conf['changes']:
 98        value_ref = _value_ref_from_conf(change_conf['ref'])
 99        value_type = _value_type_from_ref(value_types, value_ref)
100        change_ref_value_type[value_ref] = value_type
101
102    entry_id_event_types = [
103        (*event_type_prefix, 'gateway', 'entry_id', i['report_id'])
104        for i in conf['rcbs'] if i['ref']['type'] == 'BUFFERED']
105    if entry_id_event_types:
106        result = await eventer_client.query(
107            hat.event.common.QueryLatestParams(entry_id_event_types))
108        rcbs_entry_ids = {
109            event.type[5]: (bytes.fromhex(event.payload.data)
110                            if event.payload.data is not None else None)
111            for event in result.events}
112    else:
113        rcbs_entry_ids = {}
114
115    device = Iec61850ClientDevice()
116
117    device._rcbs_entry_ids = rcbs_entry_ids
118    device._conf = conf
119    device._eventer_client = eventer_client
120    device._event_type_prefix = event_type_prefix
121    device._conn = None
122    device._conn_status = None
123    device._terminations = {}
124    device._reports_segments = {}
125
126    device._value_ref_data_name = value_ref_data_name
127    device._dataset_values_ref_type = dataset_values_ref_type
128    device._command_ref_value_type = command_ref_value_type
129    device._change_ref_value_type = change_ref_value_type
130    device._data_name_confs = {i['name']: i for i in conf['data']}
131    device._command_name_confs = {i['name']: i for i in conf['commands']}
132    device._command_name_ctl_nums = {i['name']: 0 for i in conf['commands']}
133    device._change_name_value_refs = {
134        i['name']: _value_ref_from_conf(i['ref']) for i in conf['changes']}
135    device._rcb_type = {rcb_conf['report_id']: rcb_conf['ref']['type']
136                        for rcb_conf in conf['rcbs']}
137    device._persist_dyn_datasets = set(_get_persist_dyn_datasets(conf))
138    device._dyn_datasets_values = dict(_get_dyn_datasets_values(conf))
139    device._report_data_refs = collections.defaultdict(collections.deque)
140    for rcb_conf in device._conf['rcbs']:
141        report_id = rcb_conf['report_id']
142        ds_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
143        for value_conf in dataset_confs[ds_ref]['values']:
144            value_ref = _value_ref_from_conf(value_conf)
145            device._report_data_refs[report_id].append(value_ref)
146
147    device._data_value_types = dict(
148        _get_data_value_types(conf, value_types_61850))
149    device._cmd_value_types = dict(
150        _get_cmd_value_types(conf, value_types_61850))
151
152    device._async_group = aio.Group()
153    device._async_group.spawn(device._connection_loop)
154    device._loop = asyncio.get_running_loop()
155
156    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': ['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': {'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': ['connection', 'value_types', 'datasets', 'rcbs', 'data', 'commands', 'changes'], 'properties': {'connection': {'type': 'object', 'required': ['host', 'port', 'connect_timeout', 'reconnect_delay', 'response_timeout', 'status_delay', 'status_timeout'], 'properties': {'host': {'type': 'string'}, 'port': {'type': 'integer'}, 'connect_timeout': {'type': 'number'}, 'reconnect_delay': {'type': 'number'}, 'response_timeout': {'type': 'number'}, 'status_delay': {'type': 'number'}, 'status_timeout': {'type': 'number'}, 'local_tsel': {'type': 'integer'}, 'remote_tsel': {'type': 'integer'}, 'local_ssel': {'type': 'integer'}, 'remote_ssel': {'type': 'integer'}, 'local_psel': {'type': 'integer'}, 'remote_psel': {'type': 'integer'}, 'local_ap_title': {'type': 'array', 'items': {'type': 'integer'}}, 'remote_ap_title': {'type': 'array', 'items': {'type': 'integer'}}, 'local_ae_qualifier': {'type': 'integer'}, 'remote_ae_qualifier': {'type': 'integer'}, 'local_detail_calling': {'type': 'integer'}}}, 'value_types': {'type': 'array', 'items': {'type': 'object', 'required': ['logical_device', 'logical_node', 'fc', 'name', 'type'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'fc': {'type': 'string'}, 'name': {'type': 'string'}, 'type': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value_type'}}}}, 'datasets': {'type': 'array', 'items': {'type': 'object', 'required': ['ref', 'values', 'dynamic'], 'properties': {'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset'}, 'values': {'type': 'array', 'items': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}}, 'dynamic': {'type': 'boolean'}}}}, 'rcbs': {'type': 'array', 'items': {'type': 'object', 'required': ['ref', 'report_id', 'dataset'], 'properties': {'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/rcb'}, 'report_id': {'type': 'string'}, 'dataset': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset'}, 'trigger_options': {'type': 'array', 'items': {'enum': ['DATA_CHANGE', 'QUALITY_CHANGE', 'DATA_UPDATE', 'INTEGRITY', 'GENERAL_INTERROGATION']}}, 'optional_fields': {'type': 'array', 'items': {'enum': ['SEQUENCE_NUMBER', 'REPORT_TIME_STAMP', 'REASON_FOR_INCLUSION', 'DATA_SET_NAME', 'DATA_REFERENCE', 'BUFFER_OVERFLOW', 'ENTRY_ID', 'CONF_REVISION']}}, 'conf_revision': {'type': 'integer'}, 'buffer_time': {'type': 'integer'}, 'integrity_period': {'type': 'integer'}, 'purge_buffer': {'type': 'boolean'}, 'reservation_time': {'type': 'integer'}}}}, 'data': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'report_id', 'value'], 'properties': {'name': {'type': 'string'}, 'report_id': {'type': 'string'}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}, 'quality': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}, 'timestamp': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}, 'selected': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}}}}, 'commands': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'model', 'ref', 'with_operate_time'], 'properties': {'name': {'type': 'string'}, 'model': {'enum': ['DIRECT_WITH_NORMAL_SECURITY', 'SBO_WITH_NORMAL_SECURITY', 'DIRECT_WITH_ENHANCED_SECURITY', 'SBO_WITH_ENHANCED_SECURITY']}, 'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/command'}, 'with_operate_time': {'type': 'boolean'}}}}, 'changes': {'type': 'array', 'items': {'type': 'object', 'required': ['name', 'ref'], 'properties': {'name': {'type': 'string'}, 'ref': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/value'}}}}}}, 'events': {'client': {'gateway': {'status': {'enum': ['CONNECTING', 'CONNECTED', 'DISCONNECTED']}, 'data': {'type': 'object', 'required': ['reasons'], 'properties': {'reasons': {'type': 'array', 'items': {'enum': ['DATA_CHANGE', 'QUALITY_CHANGE', 'DATA_UPDATE', 'INTEGRITY', 'GENERAL_INTERROGATION', 'APPLICATION_TRIGGER']}}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}, 'quality': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/quality'}, 'timestamp': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/timestamp'}, 'selected': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/boolean'}}}, 'command': {'allOf': [{'type': 'object', 'required': ['session_id', 'action'], 'properties': {'session_id': {'type': 'string'}, 'action': {'enum': ['SELECT', 'CANCEL', 'OPERATE', 'TERMINATION']}}}, {'oneOf': [{'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': True}}}, {'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': False}, 'service_error': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/service_error'}, 'additional_cause': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/additional_cause'}, 'test_error': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/test_error'}}}]}]}, 'change': {'allOf': [{'type': 'object', 'required': ['session_id'], 'properties': {'session_id': {'type': 'string'}}}, {'oneOf': [{'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': True}}}, {'type': 'object', 'requried': ['success'], 'properties': {'success': {'const': False}, 'error': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/errors/service_error'}}}]}]}, 'entry_id': {'type': ['string', 'null'], 'description': 'hex encoded bytes'}}, 'system': {'command': {'type': 'object', 'required': ['session_id', 'action', 'value', 'origin', 'test', 'checks'], 'properties': {'session_id': {'type': 'string'}, 'action': {'enum': ['SELECT', 'CANCEL', 'OPERATE']}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}, 'origin': {'type': 'object', 'required': ['category', 'identification'], 'properties': {'category': {'enum': ['BAY_CONTROL', 'STATION_CONTROL', 'REMOTE_CONTROL', 'AUTOMATIC_BAY', 'AUTOMATIC_STATION', 'AUTOMATIC_REMOTE', 'MAINTENANCE', 'PROCESS']}, 'identification': {'type': 'string'}}}, 'test': {'type': 'boolean'}, 'checks': {'type': 'array', 'items': {'enum': ['SYNCHRO', 'INTERLOCK']}}}}, 'change': {'type': 'object', 'requried': ['session_id', 'value'], 'properties': {'session_id': {'type': 'string'}, 'value': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}}}}}}, 'value': {'anyOf': [{'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/boolean'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/integer'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/unsigned'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/float'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/bit_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/octet_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/visible_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/mms_string'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/array'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/struct'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/quality'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/timestamp'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/double_point'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/direction'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/severity'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/analogue'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/vector'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/step_position'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/binary_control'}], '$defs': {'boolean': {'type': 'boolean'}, 'integer': {'type': 'integer'}, 'unsigned': {'type': 'integer'}, 'float': {'oneOf': [{'type': 'number'}, {'enum': ['nan', 'inf', '-inf']}]}, 'bit_string': {'type': 'array', 'items': {'type': 'boolean'}}, 'octet_string': {'type': 'string', 'description': 'hex encoded bytes'}, 'visible_string': {'type': 'string'}, 'mms_string': {'type': 'string'}, 'array': {'type': 'array', 'items': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}}, 'struct': {'type': 'object', 'patternProperties': {'.+': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value'}}}, 'quality': {'type': 'object', 'required': ['validity', 'details', 'source', 'test', 'operator_blocked'], 'properties': {'validity': {'enum': ['GOOD', 'INVALID', 'RESERVED', 'QUESTIONABLE']}, 'details': {'type': 'array', 'items': {'enum': ['OVERFLOW', 'OUT_OF_RANGE', 'BAD_REFERENCE', 'OSCILLATORY', 'FAILURE', 'OLD_DATA', 'INCONSISTENT', 'INACCURATE']}}, 'source': {'enum': ['PROCESS', 'SUBSTITUTED']}, 'test': {'type': 'boolean'}, 'operator_blocked': {'type': 'boolean'}}}, 'timestamp': {'type': 'object', 'required': ['value', 'leap_second', 'clock_failure', 'not_synchronized'], 'properties': {'value': {'type': 'number', 'description': 'seconds since 1970-01-01'}, 'leap_second': {'type': 'boolean'}, 'clock_failure': {'type': 'boolean'}, 'not_synchronized': {'type': 'boolean'}, 'accuracy': {'type': 'integer'}}}, 'double_point': {'enum': ['INTERMEDIATE', 'OFF', 'ON', 'BAD']}, 'direction': {'enum': ['UNKNOWN', 'FORWARD', 'BACKWARD', 'BOTH']}, 'severity': {'enum': ['UNKNOWN', 'CRITICAL', 'MAJOR', 'MINOR', 'WARNING']}, 'analogue': {'type': 'object', 'properties': {'i': {'type': 'integer'}, 'f': {'oneOf': [{'type': 'number'}, {'enum': ['nan', 'inf', '-inf']}]}}}, 'vector': {'type': 'object', 'required': ['magnitude'], 'properties': {'magnitude': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/analogue'}, 'angle': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value/$defs/analogue'}}}, 'step_position': {'type': 'object', 'required': ['value'], 'properties': {'value': {'type': 'integer'}, 'transient': {'type': 'boolean'}}}, 'binary_control': {'enum': ['STOP', 'LOWER', 'HIGHER', 'RESERVED']}}}, 'value_type': {'oneOf': [{'enum': ['BOOLEAN', 'INTEGER', 'UNSIGNED', 'FLOAT', 'BIT_STRING', 'OCTET_STRING', 'VISIBLE_STRING', 'MMS_STRING', 'QUALITY', 'TIMESTAMP', 'DOUBLE_POINT', 'DIRECTION', 'SEVERITY', 'ANALOGUE', 'VECTOR', 'STEP_POSITION', 'BINARY_CONTROL']}, {'type': 'object', 'required': ['type', 'element_type', 'length'], 'properties': {'type': {'const': 'ARRAY'}, 'element_type': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value_type'}, 'length': {'type': 'integer'}}}, {'type': 'object', 'required': ['type', 'elements'], 'properties': {'type': {'const': 'STRUCT'}, 'elements': {'type': 'array', 'items': {'type': 'object', 'requried': ['name', 'type'], 'properties': {'name': {'type': 'string'}, 'type': {'$ref': 'hat-gateway://iec61850.yaml#/$defs/value_type'}}}}}}]}, 'refs': {'value': {'type': 'object', 'required': ['logical_device', 'logical_node', 'fc', 'names'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'fc': {'type': 'string'}, 'names': {'type': 'array', 'items': {'type': ['string', 'integer']}}}}, 'command': {'type': 'object', 'required': ['logical_device', 'logical_node', 'name'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'name': {'type': 'string'}}}, 'rcb': {'type': 'object', 'required': ['logical_device', 'logical_node', 'type', 'name'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'type': {'enum': ['BUFFERED', 'UNBUFFERED']}, 'name': {'type': 'string'}}}, 'dataset': {'oneOf': [{'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset/$defs/nonpersisted'}, {'$ref': 'hat-gateway://iec61850.yaml#/$defs/refs/dataset/$defs/persisted'}], '$defs': {'nonpersisted': {'type': 'string'}, 'persisted': {'type': 'object', 'required': ['logical_device', 'logical_node', 'name'], 'properties': {'logical_device': {'type': 'string'}, 'logical_node': {'type': 'string'}, 'name': {'type': 'string'}}}}}}, 'errors': {'service_error': {'enum': ['NO_ERROR', 'INSTANCE_NOT_AVAILABLE', 'INSTANCE_IN_USE', 'ACCESS_VIOLATION', 'ACCESS_NOT_ALLOWED_IN_CURRENT_STATE', 'PARAMETER_VALUE_INAPPROPRIATE', 'PARAMETER_VALUE_INCONSISTENT', 'CLASS_NOT_SUPPORTED', 'INSTANCE_LOCKED_BY_OTHER_CLIENT', 'CONTROL_MUST_BE_SELECTED', 'TYPE_CONFLICT', 'FAILED_DUE_TO_COMMUNICATIONS_CONSTRAINT', 'FAILED_DUE_TO_SERVER_CONTRAINT']}, 'additional_cause': {'enum': ['UNKNOWN', 'NOT_SUPPORTED', 'BLOCKED_BY_SWITCHING_HIERARCHY', 'SELECT_FAILED', 'INVALID_POSITION', 'POSITION_REACHED', 'PARAMETER_CHANGE_IN_EXECUTION', 'STEP_LIMIT', 'BLOCKED_BY_MODE', 'BLOCKED_BY_PROCESS', 'BLOCKED_BY_INTERLOCKING', 'BLOCKED_BY_SYNCHROCHECK', 'COMMAND_ALREADY_IN_EXECUTION', 'BLOCKED_BY_HEALTH', 'ONE_OF_N_CONTROL', 'ABORTION_BY_CANCEL', 'TIME_LIMIT_OVER', 'ABORTION_BY_TRIP', 'OBJECT_NOT_SELECTED', 'OBJECT_ALREADY_SELECTED', 'NO_ACCESS_AUTHORITY', 'ENDED_WITH_OVERSHOOT', 'ABORTION_DUE_TO_DEVIATION', 'ABORTION_BY_COMMUNICATION_LOSS', 'BLOCKED_BY_COMMAND', 'NONE', 'INCONSISTENT_PARAMETERS', 'LOCKED_BY_OTHER_CLIENT']}, 'test_error': {'enum': ['NO_ERROR', 'UNKNOWN', 'TIMEOUT_TEST_NOT_OK', 'OPERATOR_TEST_NOT_OK']}}}}, 'hat-gateway://iec104.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec104.yaml', '$defs': {'master': {'type': 'object', 'required': ['remote_addresses', 'response_timeout', 'supervisory_timeout', 'test_timeout', 'send_window_size', 'receive_window_size', 'reconnect_delay', 'time_sync_delay', 'security'], 'properties': {'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://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': ['remote_host', 'remote_port', 'connect_delay', 'request_timeout', 'request_retry_count', 'request_retry_delay', 'polling_delay', 'polling_oids', 'string_hex_oids'], 'properties': {'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': ['local_host', 'local_port', 'users', 'remote_devices'], 'properties': {'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://iec103.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec103.yaml', '$defs': {'master': {'type': 'object', 'required': ['port', 'baudrate', 'bytesize', 'parity', 'stopbits', 'flow_control', 'silent_interval', 'reconnect_delay', 'remote_devices'], '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'}, '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://iec101.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://iec101.yaml', '$defs': {'master': {'allOf': [{'type': 'object', 'required': ['port', 'baudrate', 'bytesize', 'parity', 'stopbits', 'flow_control', 'silent_interval', 'cause_size', 'asdu_address_size', 'io_address_size', 'reconnect_delay'], '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']}, '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://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': ['connection', 'remote_devices'], 'properties': {'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'}}}}}}}}, '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://ping.yaml': {'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'hat-gateway://ping.yaml', '$defs': {'device': {'type': 'object', 'required': ['remote_devices'], 'properties': {'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']}}}}})
class Iec61850ClientDevice(hat.gateway.common.Device):
166class Iec61850ClientDevice(common.Device):
167
168    @property
169    def async_group(self) -> aio.Group:
170        return self._async_group
171
172    async def process_events(self, events: Collection[hat.event.common.Event]):
173        try:
174            for event in events:
175                suffix = event.type[len(self._event_type_prefix):]
176
177                if suffix[:2] == ('system', 'command'):
178                    cmd_name, = suffix[2:]
179                    await self._process_cmd_req(event, cmd_name)
180
181                elif suffix[:2] == ('system', 'change'):
182                    val_name, = suffix[2:]
183                    await self._process_change_req(event, val_name)
184
185                else:
186                    raise Exception('unsupported event type')
187
188        except Exception as e:
189            mlog.warning('error processing event %s: %s',
190                         event.type, e, exc_info=e)
191
192    async def _connection_loop(self):
193
194        async def cleanup():
195            await self._register_status('DISCONNECTED')
196            if self._conn:
197                await self._conn.async_close()
198
199        conn_conf = self._conf['connection']
200        try:
201            while True:
202                await self._register_status('CONNECTING')
203                try:
204                    mlog.debug('connecting to %s:%s',
205                               conn_conf['host'], conn_conf['port'])
206                    self._conn = await aio.wait_for(
207                        iec61850.connect(
208                            addr=tcp.Address(conn_conf['host'],
209                                             conn_conf['port']),
210                            data_value_types=self._data_value_types,
211                            cmd_value_types=self._cmd_value_types,
212                            report_data_refs=self._report_data_refs,
213                            report_cb=self._on_report,
214                            termination_cb=self._on_termination,
215                            status_delay=conn_conf['status_delay'],
216                            status_timeout=conn_conf['status_timeout'],
217                            local_tsel=conn_conf.get('local_tsel'),
218                            remote_tsel=conn_conf.get('remote_tsel'),
219                            local_ssel=conn_conf.get('local_ssel'),
220                            remote_ssel=conn_conf.get('remote_ssel'),
221                            local_psel=conn_conf.get('local_psel'),
222                            remote_psel=conn_conf.get('remote_psel'),
223                            local_ap_title=conn_conf.get('local_ap_title'),
224                            remote_ap_title=conn_conf.get('remote_ap_title'),
225                            local_ae_qualifier=conn_conf.get(
226                                'local_ae_qualifier'),
227                            remote_ae_qualifier=conn_conf.get(
228                                'remote_ae_qualifier'),
229                            local_detail_calling=conn_conf.get(
230                                'local_detail_calling')),
231                        conn_conf['connect_timeout'])
232
233                except Exception as e:
234                    mlog.warning('connnection failed: %s', e, exc_info=e)
235                    await self._register_status('DISCONNECTED')
236                    await asyncio.sleep(conn_conf['reconnect_delay'])
237                    continue
238
239                mlog.debug('connected')
240                await self._register_status('CONNECTED')
241
242                initialized = False
243                try:
244                    await self._create_dynamic_datasets()
245                    for rcb_conf in self._conf['rcbs']:
246                        await self._init_rcb(rcb_conf)
247                    initialized = True
248
249                except Exception as e:
250                    mlog.warning(
251                        'initialization failed: %s, closing connection',
252                        e, exc_info=e)
253                    self._conn.close()
254
255                await self._conn.wait_closing()
256                await self._register_status('DISCONNECTED')
257                await self._conn.wait_closed()
258                self._conn = None
259                self._terminations = {}
260                self._reports_segments = {}
261                if not initialized:
262                    await asyncio.sleep(conn_conf['reconnect_delay'])
263
264        except Exception as e:
265            mlog.error('connection loop error: %s', e, exc_info=e)
266
267        finally:
268            mlog.debug('closing connection loop')
269            self.close()
270            await aio.uncancellable(cleanup())
271
272    async def _process_cmd_req(self, event, cmd_name):
273        if not self._conn or not self._conn.is_open:
274            raise Exception('no connection')
275
276        if cmd_name not in self._command_name_confs:
277            raise Exception('unexpected command name')
278
279        cmd_conf = self._command_name_confs[cmd_name]
280        cmd_ref = iec61850.CommandRef(**cmd_conf['ref'])
281        action = event.payload.data['action']
282        evt_session_id = event.payload.data['session_id']
283        if (action == 'SELECT' and
284                cmd_conf['model'] == 'SBO_WITH_NORMAL_SECURITY'):
285            cmd = None
286        else:
287            ctl_num = self._command_name_ctl_nums[cmd_name]
288            ctl_num = _update_ctl_num(ctl_num, action, cmd_conf['model'])
289            self._command_name_ctl_nums[cmd_name] = ctl_num
290            value_type = self._command_ref_value_type[cmd_ref]
291            if value_type is None:
292                raise Exception('value type undefined')
293
294            cmd = _command_from_event(event, cmd_conf, ctl_num, value_type)
295
296        term_future = None
297        if (action == 'OPERATE' and
298                cmd_conf['model'] in ['DIRECT_WITH_ENHANCED_SECURITY',
299                                      'SBO_WITH_ENHANCED_SECURITY']):
300            term_future = self._loop.create_future()
301            self._conn.async_group.spawn(
302                self._wait_cmd_term, cmd_name, cmd_ref, cmd, evt_session_id,
303                term_future)
304
305        try:
306            resp = await aio.wait_for(
307                self._send_command(action, cmd_ref, cmd),
308                self._conf['connection']['response_timeout'])
309
310        except (asyncio.TimeoutError, ConnectionError) as e:
311            mlog.warning('send command failed: %s', e, exc_info=e)
312            if term_future and not term_future.done():
313                term_future.cancel()
314            return
315
316        if resp is not None:
317            if term_future and not term_future.done():
318                term_future.cancel()
319
320        event = _cmd_resp_to_event(
321            self._event_type_prefix, cmd_name, evt_session_id, action, resp)
322        await self._register_events([event])
323
324    async def _send_command(self, action, cmd_ref, cmd):
325        if action == 'SELECT':
326            return await self._conn.select(cmd_ref, cmd)
327
328        if action == 'CANCEL':
329            return await self._conn.cancel(cmd_ref, cmd)
330
331        if action == 'OPERATE':
332            return await self._conn.operate(cmd_ref, cmd)
333
334        raise Exception('unsupported action')
335
336    async def _wait_cmd_term(self, cmd_name, cmd_ref, cmd, session_id, future):
337        cmd_session_id = _get_command_session_id(cmd_ref, cmd)
338        self._terminations[cmd_session_id] = future
339        try:
340            term = await aio.wait_for(future, termination_timeout)
341            event = _cmd_resp_to_event(
342                self._event_type_prefix, cmd_name, session_id, 'TERMINATION',
343                term.error)
344            await self._register_events([event])
345
346        except asyncio.TimeoutError:
347            mlog.warning('command termination timeout')
348
349        finally:
350            del self._terminations[cmd_session_id]
351
352    async def _process_change_req(self, event, value_name):
353        if not self._conn or not self._conn.is_open:
354            raise Exception('no connection')
355
356        if value_name not in self._change_name_value_refs:
357            raise Exception('unexpected change name')
358
359        ref = self._change_name_value_refs[value_name]
360        value_type = self._change_ref_value_type[ref]
361        if value_type is None:
362            raise Exception('value type undefined')
363
364        value = _value_from_json(event.payload.data['value'], value_type)
365        try:
366            resp = await aio.wait_for(
367                self._conn.write_data(ref, value),
368                self._conf['connection']['response_timeout'])
369
370        except asyncio.TimeoutError:
371            mlog.warning('write data response timeout')
372            return
373
374        except ConnectionError as e:
375            mlog.warning('connection error on write data: %s', e, exc_info=e)
376            return
377
378        session_id = event.payload.data['session_id']
379        event = _write_data_resp_to_event(
380            self._event_type_prefix, value_name, session_id, resp)
381        await self._register_events([event])
382
383    async def _on_report(self, report):
384        try:
385            events = list(self._events_from_report(report))
386            if not events:
387                return
388
389            await self._register_events(events)
390            if self._rcb_type[report.report_id] == 'BUFFERED':
391                self._rcbs_entry_ids[report.report_id] = report.entry_id
392
393        except Exception as e:
394            mlog.warning('report %s ignored: %s',
395                         report.report_id, e, exc_info=e)
396
397    def _events_from_report(self, report):
398        report_id = report.report_id
399        if report_id not in self._report_data_refs:
400            raise Exception(f'unexpected report {report_id}')
401
402        segm_id = (report_id, report.sequence_number)
403        if report.more_segments_follow:
404            if segm_id in self._reports_segments:
405                segment_data, timeout_timer = self._reports_segments[segm_id]
406                timeout_timer.cancel()
407            else:
408                segment_data = collections.deque()
409
410            segment_data.extend(report.data)
411            timeout_timer = self._loop.call_later(
412                report_segments_timeout, self._reports_segments.pop, segm_id)
413            self._reports_segments[segm_id] = (segment_data, timeout_timer)
414            return
415
416        if segm_id in self._reports_segments:
417            report_data, timeout_timer = self._reports_segments.pop(segm_id)
418            timeout_timer.cancel()
419            report_data.extend(report.data)
420
421        else:
422            report_data = report.data
423
424        yield from self._events_from_report_data(report_data, report_id)
425
426        if self._rcb_type[report_id] == 'BUFFERED':
427            yield hat.event.common.RegisterEvent(
428                type=(*self._event_type_prefix, 'gateway',
429                      'entry_id', report_id),
430                source_timestamp=None,
431                payload=hat.event.common.EventPayloadJson(
432                    report.entry_id.hex()
433                    if report.entry_id is not None else None))
434
435    def _events_from_report_data(self, report_data, report_id):
436        data_values_json = collections.defaultdict(dict)
437        data_reasons = collections.defaultdict(set)
438        for rv in report_data:
439            if rv.ref not in self._value_ref_data_name:
440                continue
441
442            data_name = self._value_ref_data_name[rv.ref]
443            value_type = self._dataset_values_ref_type[rv.ref]
444            if value_type is None:
445                mlog.warning('report data ignored: unknown value type')
446                continue
447
448            value_json = _value_to_json(rv.value, value_type)
449            value_path = [rv.ref.logical_device, rv.ref.logical_node,
450                          rv.ref.fc, *rv.ref.names]
451            data_values_json[data_name] = json.set_(
452                data_values_json[data_name], value_path, value_json)
453            if rv.reasons:
454                data_reasons[data_name].update(
455                    reason.name for reason in rv.reasons)
456
457        for data_name, values_json in data_values_json.items():
458            payload = {'reasons': list(data_reasons[data_name])}
459            data_conf = self._data_name_confs[data_name]
460            value_path = _conf_ref_to_path(data_conf['value'])
461            value_json = json.get(values_json, value_path)
462            if value_json is not None:
463                payload['value'] = value_json
464
465            if 'quality' in data_conf:
466                quality_path = _conf_ref_to_path(data_conf['quality'])
467                quality_json = json.get(values_json, quality_path)
468                if quality_json is not None:
469                    payload['quality'] = quality_json
470
471            if 'timestamp' in data_conf:
472                timestamp_path = _conf_ref_to_path(data_conf['timestamp'])
473                timestamp_json = json.get(values_json, timestamp_path)
474                if timestamp_json is not None:
475                    payload['timestamp'] = timestamp_json
476
477            if 'selected' in data_conf:
478                selected_path = _conf_ref_to_path(data_conf['selected'])
479                selected_json = json.get(values_json, selected_path)
480                if selected_json is not None:
481                    payload['selected'] = selected_json
482
483            yield hat.event.common.RegisterEvent(
484                type=(*self._event_type_prefix, 'gateway',
485                      'data', data_name),
486                source_timestamp=None,
487                payload=hat.event.common.EventPayloadJson(payload))
488
489    def _on_termination(self, termination):
490        cmd_session_id = _get_command_session_id(
491            termination.ref, termination.cmd)
492        if cmd_session_id not in self._terminations:
493            mlog.warning('unexpected termination dropped')
494            return
495
496        term_future = self._terminations[cmd_session_id]
497        if not term_future.done():
498            self._terminations[cmd_session_id].set_result(termination)
499
500    async def _init_rcb(self, rcb_conf):
501        ref = iec61850.RcbRef(
502            logical_device=rcb_conf['ref']['logical_device'],
503            logical_node=rcb_conf['ref']['logical_node'],
504            type=iec61850.RcbType[rcb_conf['ref']['type']],
505            name=rcb_conf['ref']['name'])
506        mlog.debug('initiating rcb %s', ref)
507
508        get_attrs = collections.deque([iec61850.RcbAttrType.REPORT_ID])
509        dataset_ref = _dataset_ref_from_conf(rcb_conf['dataset'])
510        if dataset_ref not in self._dyn_datasets_values:
511            get_attrs.append(iec61850.RcbAttrType.DATASET)
512        if 'conf_revision' in rcb_conf:
513            get_attrs.append(iec61850.RcbAttrType.CONF_REVISION)
514
515        get_rcb_resp = await self._conn.get_rcb_attrs(ref, get_attrs)
516        _validate_get_rcb_response(get_rcb_resp, rcb_conf)
517
518        await self._set_rcb(ref, [(iec61850.RcbAttrType.REPORT_ENABLE, False)])
519
520        if dataset_ref in self._dyn_datasets_values:
521            await self._set_rcb(
522                ref, [(iec61850.RcbAttrType.DATASET, dataset_ref)],
523                critical=True)
524
525        if ref.type == iec61850.RcbType.BUFFERED:
526            if 'reservation_time' in rcb_conf:
527                await self._set_rcb(
528                    ref, [(iec61850.RcbAttrType.RESERVATION_TIME,
529                           rcb_conf['reservation_time'])])
530
531            entry_id = self._rcbs_entry_ids.get(rcb_conf['report_id'])
532            if rcb_conf.get('purge_buffer') or entry_id is None:
533                await self._set_rcb(
534                    ref, [(iec61850.RcbAttrType.PURGE_BUFFER, True)])
535
536            else:
537                try:
538                    await self._set_rcb(
539                        ref, [(iec61850.RcbAttrType.ENTRY_ID, entry_id)],
540                        critical=True)
541
542                except Exception as e:
543                    mlog.warning('%s', e, exc_info=e)
544                    # try setting entry id to 0 in order to resynchronize
545                    await self._set_rcb(
546                        ref, [(iec61850.RcbAttrType.ENTRY_ID, b'\x00')])
547
548        elif ref.type == iec61850.RcbType.UNBUFFERED:
549            await self._set_rcb(ref, [(iec61850.RcbAttrType.RESERVE, True)])
550
551        attrs = collections.deque()
552        if 'trigger_options' in rcb_conf:
553            attrs.append((iec61850.RcbAttrType.TRIGGER_OPTIONS,
554                          set(iec61850.TriggerCondition[i]
555                              for i in rcb_conf['trigger_options'])))
556        if 'optional_fields' in rcb_conf:
557            attrs.append((iec61850.RcbAttrType.OPTIONAL_FIELDS,
558                          set(iec61850.OptionalField[i]
559                              for i in rcb_conf['optional_fields'])))
560        if 'buffer_time' in rcb_conf:
561            attrs.append((iec61850.RcbAttrType.BUFFER_TIME,
562                          rcb_conf['buffer_time']))
563        if 'integrity_period' in rcb_conf:
564            attrs.append((iec61850.RcbAttrType.INTEGRITY_PERIOD,
565                          rcb_conf['integrity_period']))
566        if attrs:
567            await self._set_rcb(ref, attrs)
568
569        await self._set_rcb(
570            ref, [(iec61850.RcbAttrType.REPORT_ENABLE, True)], critical=True)
571        await self._set_rcb(
572            ref, [(iec61850.RcbAttrType.GI, True)], critical=True)
573        mlog.debug('rcb %s initiated', ref)
574
575    async def _set_rcb(self, ref, attrs, critical=False):
576        try:
577            resp = await self._conn.set_rcb_attrs(ref, attrs)
578            attrs_failed = set((attr, attr_res)
579                               for attr, attr_res in resp.items()
580                               if isinstance(attr_res, iec61850.ServiceError))
581            if attrs_failed:
582                raise Exception(f"set attribute errors: {attrs_failed}")
583
584        except Exception as e:
585            if critical:
586                raise Exception(f'set rcb {ref} failed') from e
587
588            else:
589                mlog.warning('set rcb %s failed: %s', ref, e, exc_info=e)
590
591    async def _create_dynamic_datasets(self):
592        existing_ds_refs = set()
593        for ds_ref in self._persist_dyn_datasets:
594            ld = ds_ref.logical_device
595            res = await self._conn.get_persisted_dataset_refs(ld)
596            if isinstance(res, iec61850.ServiceError):
597                raise Exception(f'get datasets for ld {ld} failed: {res}')
598
599            existing_ds_refs.update(res)
600
601        existing_persisted_ds_refs = existing_ds_refs.intersection(
602            self._persist_dyn_datasets)
603        for ds_ref, ds_value_refs in self._dyn_datasets_values.items():
604            if ds_ref in existing_persisted_ds_refs:
605                res = await self._conn.get_dataset_data_refs(ds_ref)
606                if isinstance(res, iec61850.ServiceError):
607                    raise Exception(f'get ds {ds_ref} data refs failed: {res}')
608                else:
609                    exist_ds_value_refs = res
610
611                if ds_value_refs == list(exist_ds_value_refs):
612                    mlog.debug('dataset %s already exists', ds_ref)
613                    continue
614
615                mlog.debug("dataset %s exists, but different", ds_ref)
616                res = await self._conn.delete_dataset(ds_ref)
617                if res is not None:
618                    raise Exception(f'delete dataset {ds_ref} failed: {res}')
619                mlog.debug("dataset %s deleted", ds_ref)
620
621            res = await self._conn.create_dataset(ds_ref, ds_value_refs)
622            if res is not None:
623                raise Exception(f'create dataset {ds_ref} failed: {res}')
624
625            mlog.debug("dataset %s crated", ds_ref)
626
627    async def _register_events(self, events):
628        try:
629            await self._eventer_client.register(events)
630
631        except ConnectionError:
632            self.close()
633            raise
634
635    async def _register_status(self, status):
636        if status == self._conn_status:
637            return
638
639        event = hat.event.common.RegisterEvent(
640            type=(*self._event_type_prefix, 'gateway', 'status'),
641            source_timestamp=None,
642            payload=hat.event.common.EventPayloadJson(status))
643        await self._register_events([event])
644        self._conn_status = status
645        mlog.debug('registered status %s', status)

Device interface

async_group: hat.aio.group.Group
168    @property
169    def async_group(self) -> aio.Group:
170        return self._async_group

Group controlling resource's lifetime.

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

Process received events

This method can be coroutine or regular function.