connectors/ERPC/__init__.py
author Edouard Tisserant <edouard@beremiz.fr>
Thu, 07 Mar 2024 16:37:02 +0100
changeset 3905 d47fe28d799a
parent 3898 de4f17d737a9
child 3908 32eb6e05008a
permissions -rw-r--r--
eRPC Server: fix exception at disconnect and reconnecting
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
# Written by Edouard TISSERANT (C) 2024
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
# This file is part of Beremiz IDE
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
# See COPYING file for copyrights details.
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
import os.path
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
import re
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
import traceback
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
from inspect import getmembers, isfunction
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
import erpc
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
# eRPC service code
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
from erpc_interface.erpc_PLCObject.interface import IBeremizPLCObjectService
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
from erpc_interface.erpc_PLCObject.client import BeremizPLCObjectServiceClient
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    20
from erpc_interface.erpc_PLCObject.common import trace_order, extra_file, PLCstatus_enum
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
import PSKManagement as PSK
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
from connectors.ERPC.PSK_Adapter import SSLPSKClientTransport
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
from connectors.ConnectorBase import ConnectorBase
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
enum_to_PLCstatus = dict(map(lambda t:(t[1],t[0]),getmembers(PLCstatus_enum, lambda x:type(x)==int)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
class MissingCallException(Exception):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
    pass
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
def ExceptionFromERPCReturn(ret):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
    return {1:Exception,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
            2:MissingCallException}.get(ret,ValueError)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
def ReturnAsLastOutput(client_method, obj, args_wrapper, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
    retval = erpc.Reference()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
    ret = client_method(obj, *args_wrapper(*args), retval)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
    if ret != 0:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
        raise ExceptionFromERPCReturn(ret)(client_method.__name__)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
    return retval.value
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
def TranslatedReturnAsLastOutput(translator):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
    def wrapper(client_method, obj, args_wrapper, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
        res = ReturnAsLastOutput(client_method, obj, args_wrapper, *args)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
        return translator(res)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
    return wrapper
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
ReturnWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
    "AppendChunkToBlob":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
    "GetLogMessage":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
        lambda res:(res.msg, res.tick, res.sec, res.nsec)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
    "GetPLCID":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
        lambda res:(res.ID, res.PSK)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
    "GetPLCstatus":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
        lambda res:(enum_to_PLCstatus[res.PLCstatus], res.logcounts)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
    "GetTraceVariables":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
        lambda res:(enum_to_PLCstatus[res.PLCstatus],
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    58
                    [(sample.tick, bytes(sample.TraceBuffer)) for sample in res.traces])),
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
    "MatchMD5":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
    "NewPLC":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
    "SeedBlob":ReturnAsLastOutput,
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    62
    "SetTraceVariablesList": ReturnAsLastOutput,
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    63
    "StopPLC":ReturnAsLastOutput,
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
ArgsWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
    "NewPLC":
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
        lambda md5sum, plcObjectBlobID, extrafiles: (
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
            md5sum, plcObjectBlobID, [extra_file(*f) for f in extrafiles]),
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    70
    "SetTraceVariablesList":
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    71
        lambda orders : ([
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    72
            trace_order(idx, b"" if force is None else force) 
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    73
            for idx, force in orders],)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
def ERPC_connector_factory(uri, confnodesroot):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
    """
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
    returns the ERPC connector
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
    """
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
    confnodesroot.logger.write(_("ERPC connecting to URI : %s\n") % uri)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    81
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    82
    # TODO add parsing for serial URI
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
    # ERPC:///dev/ttyXX:baudrate or ERPC://:COM4:baudrate
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    85
    try:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    86
        _scheme, location = uri.split("://",1)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    87
        locator, *IDhash = location.split('#',1)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    88
        x = re.match(r'(?P<host>[^\s:]+):?(?P<port>\d+)?', locator)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    89
        host = x.group('host')
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    90
        port = x.group('port')
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    91
        if port:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    92
            port = int(port)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    93
        else:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    94
            port = 3000
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    95
    except Exception as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    96
        confnodesroot.logger.write_error(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    97
            'Malformed URI "%s": %s\n' % (uri, str(e)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    98
        return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    99
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   100
    def rpc_wrapper(method_name):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   101
        client_method = getattr(BeremizPLCObjectServiceClient, method_name)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   102
        return_wrapper = ReturnWrappers.get(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   103
            method_name, 
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   104
            lambda client_method, obj, args_wrapper, *args: client_method(obj, *args_wrapper(*args)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   105
        args_wrapper = ArgsWrappers.get(method_name, lambda *x:x)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   106
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   107
        def exception_wrapper(self, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   108
            try:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   109
                return return_wrapper(client_method, self, args_wrapper, *args)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   110
            except erpc.transport.ConnectionClosed as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   111
                confnodesroot._SetConnector(None)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   112
                confnodesroot.logger.write_error(_("Connection lost!\n"))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   113
            except erpc.codec.CodecError as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   114
                confnodesroot.logger.write_warning(_("ERPC codec error: %s\n") % e)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   115
            except erpc.client.RequestError as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   116
                confnodesroot.logger.write_error(_("ERPC request error: %s\n") % e)                
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   117
            except MissingCallException as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   118
                confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e.message)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   119
            except Exception as e:
3898
de4f17d737a9 Remove forgotten debug prints
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3887
diff changeset
   120
                errmess = _("Exception calling remote PLC object fucntion %s:\n") % method_name \
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   121
                          + traceback.format_exc()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   122
                confnodesroot.logger.write_error(errmess + "\n")
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   123
                confnodesroot._SetConnector(None)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   124
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   125
            return self.PLCObjDefaults.get(method_name)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
        return exception_wrapper
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
    PLCObjectERPCProxy = type(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
        "PLCObjectERPCProxy",
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   131
        (ConnectorBase, BeremizPLCObjectServiceClient),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   132
        {name: rpc_wrapper(name)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   133
            for name,_func in getmembers(IBeremizPLCObjectService, isfunction)})
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   134
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
    try:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
        if IDhash:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   137
            ID = IDhash[0]
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
            # load PSK from project
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
            secpath = os.path.join(str(confnodesroot.ProjectPath), 'psk', ID + '.secret')
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
            if not os.path.exists(secpath):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
                confnodesroot.logger.write_error(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
                    'Error: Pre-Shared-Key Secret in %s is missing!\n' % secpath)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
                return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
            secret = open(secpath).read().partition(':')[2].rstrip('\n\r')
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
            transport = SSLPSKClientTransport(host, port, (secret, ID))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
        else:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
            # TODO if serial URI then 
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
            # transport = erpc.transport.SerialTransport(device, baudrate)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
            transport = erpc.transport.TCPTransport(host, port, False)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
        clientManager = erpc.client.ClientManager(transport, erpc.basic_codec.BasicCodec)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
        client = PLCObjectERPCProxy(clientManager)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
    except Exception as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   156
        confnodesroot.logger.write_error(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   157
            _("Connection to {loc} failed with exception {ex}\n").format(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   158
                loc=locator, ex=str(e)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   159
        return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   160
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   161
    # Check connection is effective.
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
    IDPSK = client.GetPLCID()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
    if IDPSK:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   164
        ID, secret = IDPSK
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   165
        PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   166
    else:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
        confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n"))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
    return client