author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 22 Nov 2024 16:31:51 +0100 | |
changeset 4043 | b217fb367574 |
parent 4037 | 4127c64df073 |
parent 4033 | 208a808d1081 |
permissions | -rw-r--r-- |
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 runtime |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
6 |
# See COPYING.Runtime 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 |
import sys |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
9 |
import traceback |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
10 |
from inspect import getmembers, isfunction |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
11 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
12 |
import erpc |
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 |
# eRPC service code |
3887
2df45e4bd500
Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3885
diff
changeset
|
15 |
from erpc_interface.erpc_PLCObject.common import PSKID, PLCstatus, TraceVariables, trace_sample, PLCstatus_enum, log_message |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
16 |
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
|
17 |
from erpc_interface.erpc_PLCObject.server import BeremizPLCObjectServiceService |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
19 |
from runtime import GetPLCObjectSingleton as PLC |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
20 |
from runtime.loglevels import LogLevelsDict |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
from runtime.ServicePublisher import ServicePublisher |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
22 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
24 |
def ReturnAsLastOutput(method, args_wrapper, *args): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
25 |
args[-1].value = method(*args_wrapper(*args[:-1])) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
26 |
return 0 |
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 |
def TranslatedReturnAsLastOutput(translator): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
29 |
def wrapper(method, args_wrapper, *args): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
30 |
args[-1].value = translator(method(*args_wrapper(*args[:-1]))) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
31 |
return 0 |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
32 |
return wrapper |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
33 |
|
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 |
ReturnWrappers = { |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
36 |
"AppendChunkToBlob":ReturnAsLastOutput, |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
37 |
"GetLogMessage":TranslatedReturnAsLastOutput( |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
38 |
lambda res:log_message(*res)), |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
39 |
"GetPLCID":TranslatedReturnAsLastOutput( |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
40 |
lambda res:PSKID(*res)), |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
41 |
"GetPLCstatus":TranslatedReturnAsLastOutput( |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
42 |
lambda res:PLCstatus(getattr(PLCstatus_enum, res[0]),res[1])), |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
43 |
"GetTraceVariables":TranslatedReturnAsLastOutput( |
3885
22a009561502
Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3884
diff
changeset
|
44 |
lambda res:TraceVariables(getattr(PLCstatus_enum, res[0]),[trace_sample(*sample) for sample in res[1]])), |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
45 |
"MatchMD5":ReturnAsLastOutput, |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
46 |
"NewPLC":ReturnAsLastOutput, |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
47 |
"SeedBlob":ReturnAsLastOutput, |
3885
22a009561502
Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3884
diff
changeset
|
48 |
"SetTraceVariablesList": ReturnAsLastOutput, |
22a009561502
Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3884
diff
changeset
|
49 |
"StopPLC":ReturnAsLastOutput, |
4032
1ffff67678ad
Add ExtendedCall to PLCObject as a replacement of RemoteExec and reflect it in eRPC interface.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3905
diff
changeset
|
50 |
"ExtendedCall":ReturnAsLastOutput, |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
51 |
} |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
52 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
53 |
ArgsWrappers = { |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
54 |
"AppendChunkToBlob": |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
55 |
lambda data, blobID:(data, bytes(blobID)), |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
56 |
"NewPLC": |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
57 |
lambda md5sum, plcObjectBlobID, extrafiles: ( |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
58 |
md5sum, bytes(plcObjectBlobID), [(f.fname, bytes(f.blobID)) for f in extrafiles]), |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
59 |
"SetTraceVariablesList": |
3887
2df45e4bd500
Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3885
diff
changeset
|
60 |
lambda orders : ([(order.idx, None if len(order.force)==0 else bytes(order.force)) for order in orders],) |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
61 |
} |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
62 |
|
4033
208a808d1081
eRPC: Server closes connection on exception to prevent client to block until timeout when it happens.
Edouard Tisserant <edouard@beremiz.fr>
parents:
4032
diff
changeset
|
63 |
def rpc_wrapper(method_name, server): |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
64 |
PLCobj = PLC() |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
65 |
method=getattr(PLCobj, method_name) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
66 |
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
|
67 |
return_wrapper = ReturnWrappers.get(method_name, |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
68 |
lambda method, args_wrapper, *args: method(*args_wrapper(*args))) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
69 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
70 |
def exception_wrapper(self, *args): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
71 |
try: |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
72 |
return_wrapper(method, args_wrapper, *args) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
73 |
return 0 |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
74 |
except Exception as e: |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
75 |
print(traceback.format_exc()) |
4033
208a808d1081
eRPC: Server closes connection on exception to prevent client to block until timeout when it happens.
Edouard Tisserant <edouard@beremiz.fr>
parents:
4032
diff
changeset
|
76 |
PLCobj.LogMessage(LogLevelsDict["CRITICAL"], f'eRPC call {method_name} Exception "{str(e)}"') |
208a808d1081
eRPC: Server closes connection on exception to prevent client to block until timeout when it happens.
Edouard Tisserant <edouard@beremiz.fr>
parents:
4032
diff
changeset
|
77 |
server.transport.close() |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
78 |
raise |
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 |
return exception_wrapper |
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 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
83 |
class eRPCServer(object): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
84 |
def __init__(self, servicename, ip_addr, port): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
85 |
self.continueloop = True |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
86 |
self.server = None |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
87 |
self.transport = None |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
88 |
self.servicename = servicename |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
89 |
self.ip_addr = ip_addr |
4037
4127c64df073
python runtime: update ServicePublisher to current Zeroconf API
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3905
diff
changeset
|
90 |
self.port = int(port) |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
91 |
self.servicepublisher = None |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
92 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
93 |
def _to_be_published(self): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
94 |
return self.servicename is not None and \ |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
95 |
self.ip_addr not in ["", "localhost", "127.0.0.1"] |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
96 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
97 |
def PrintServerInfo(self): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
98 |
print(_("eRPC port :"), self.port) |
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 |
if self._to_be_published(): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
101 |
print(_("Publishing service on local network")) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
102 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
103 |
if sys.stdout: |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
104 |
sys.stdout.flush() |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
105 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
106 |
def Loop(self, when_ready): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
107 |
if self._to_be_published(): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
108 |
self.Publish() |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
109 |
|
3905
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
110 |
# service handler calls PLC object though erpc_stubs's wrappers |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
111 |
handler = type( |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
112 |
"PLCObjectServiceHandlder", |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
113 |
(IBeremizPLCObjectService,), |
4033
208a808d1081
eRPC: Server closes connection on exception to prevent client to block until timeout when it happens.
Edouard Tisserant <edouard@beremiz.fr>
parents:
4032
diff
changeset
|
114 |
{name: rpc_wrapper(name, self) |
3905
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
115 |
for name,_func in getmembers(IBeremizPLCObjectService, isfunction)})() |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
116 |
|
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
117 |
service = BeremizPLCObjectServiceService(handler) |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
118 |
|
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
119 |
# TODO initialize Serial transport layer if selected |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
120 |
# transport = erpc.transport.SerialTransport(device, baudrate) |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
121 |
|
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
122 |
# initialize TCP transport layer |
4037
4127c64df073
python runtime: update ServicePublisher to current Zeroconf API
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3905
diff
changeset
|
123 |
self.transport = erpc.transport.TCPTransport(self.ip_addr, self.port, True) |
3905
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
124 |
|
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
125 |
self.server = erpc.simple_server.SimpleServer(self.transport, erpc.basic_codec.BasicCodec) |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
126 |
self.server.add_service(service) |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
127 |
|
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
128 |
when_ready() |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
129 |
|
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
130 |
while self.continueloop: |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
131 |
|
3905
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
132 |
try: |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
133 |
self.server.run() |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
134 |
except erpc.transport.ConnectionClosed: |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
135 |
PLC().LogMessage(LogLevelsDict["DEBUG"], 'eRPC client disconnected') |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
136 |
except Exception as e: |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
137 |
self.Unpublish() |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
138 |
#TODO crash better |
d47fe28d799a
eRPC Server: fix exception at disconnect and reconnecting
Edouard Tisserant <edouard@beremiz.fr>
parents:
3898
diff
changeset
|
139 |
raise e |
3884
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
140 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
141 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
142 |
def Restart(self): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
143 |
self.server.stop() |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
144 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
145 |
def Quit(self): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
146 |
self.continueloop = False |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
147 |
self.server.stop() |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
148 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
149 |
def Publish(self): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
150 |
self.servicepublisher = ServicePublisher("ERPC") |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
151 |
self.servicepublisher.RegisterService(self.servicename, |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
152 |
self.ip_addr, self.port) |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
153 |
|
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
154 |
def Unpublish(self): |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
155 |
if self.servicepublisher is not None: |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
156 |
self.servicepublisher.UnRegisterService() |
34da877021d5
Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
157 |
self.servicepublisher = None |