author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Tue, 11 Apr 2017 17:45:49 +0300 | |
changeset 1677 | db05cb9e0439 |
parent 1595 | b3cc68dd037d |
child 1731 | 6ebd9c40b2be |
permissions | -rwxr-xr-x |
203 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
3 |
|
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
4 |
# This file is part of Beremiz, a Integrated Development Environment for |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
203 | 6 |
# |
1455 | 7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
203 | 8 |
# |
1455 | 9 |
# See COPYING file for copyrights details. |
203 | 10 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
11 |
# This program is free software; you can redistribute it and/or |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
12 |
# modify it under the terms of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
13 |
# as published by the Free Software Foundation; either version 2 |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
203 | 15 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
16 |
# This program is distributed in the hope that it will be useful, |
1455 | 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
19 |
# GNU General Public License for more details. |
203 | 20 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
21 |
# You should have received a copy of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
22 |
# along with this program; if not, write to the Free Software |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1455
diff
changeset
|
24 |
|
1455 | 25 |
import Pyro |
26 |
import Pyro.core |
|
27 |
import Pyro.util |
|
203 | 28 |
from Pyro.errors import PyroError |
29 |
import traceback |
|
30 |
from time import sleep |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
31 |
import copy |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
32 |
import socket |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
33 |
service_type = '_PYRO._tcp.local.' |
1455 | 34 |
import os.path |
399 | 35 |
# this module attribute contains a list of DNS-SD (Zeroconf) service types |
717 | 36 |
# supported by this connector confnode. |
399 | 37 |
# |
38 |
# for connectors that do not support DNS-SD, this attribute can be omitted |
|
39 |
# or set to an empty list. |
|
40 |
||
717 | 41 |
def PYRO_connector_factory(uri, confnodesroot): |
203 | 42 |
""" |
43 |
This returns the connector to Pyro style PLCobject |
|
44 |
""" |
|
1455 | 45 |
confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri) |
203 | 46 |
|
47 |
servicetype, location = uri.split("://") |
|
1455 | 48 |
if servicetype == "PYROS": |
49 |
schemename = "PYROLOCSSL" |
|
50 |
# Protect against name->IP substitution in Pyro3 |
|
51 |
Pyro.config.PYRO_DNS_URI = True |
|
52 |
# Beware Pyro lib need str path, not unicode |
|
53 |
# don't rely on PYRO_STORAGE ! see documentation |
|
54 |
Pyro.config.PYROSSL_CERTDIR = os.path.abspath(str(confnodesroot.ProjectPath) + '/certs') |
|
55 |
if not os.path.exists(Pyro.config.PYROSSL_CERTDIR): |
|
56 |
confnodesroot.logger.write_error( |
|
57 |
'Error : the directory %s is missing for SSL certificates (certs_dir).' |
|
58 |
'Please fix it in your project.\n' % Pyro.config.PYROSSL_CERTDIR) |
|
59 |
return None |
|
60 |
else: |
|
61 |
confnodesroot.logger.write(_("PYRO using certificates in '%s' \n") |
|
62 |
% (Pyro.config.PYROSSL_CERTDIR)) |
|
63 |
Pyro.config.PYROSSL_CERT = "client.crt" |
|
64 |
Pyro.config.PYROSSL_KEY = "client.key" |
|
65 |
# Ugly Monkey Patching |
|
66 |
def _gettimeout(self): |
|
67 |
return self.timeout |
|
68 |
||
69 |
def _settimeout(self, timeout): |
|
70 |
self.timeout = timeout |
|
71 |
from M2Crypto.SSL import Connection |
|
72 |
Connection.timeout = None |
|
73 |
Connection.gettimeout = _gettimeout |
|
74 |
Connection.settimeout = _settimeout |
|
75 |
# M2Crypto.SSL.Checker.WrongHost: Peer certificate commonName does not |
|
76 |
# match host, expected 127.0.0.1, got server |
|
77 |
Connection.clientPostConnectionCheck = None |
|
78 |
else: |
|
79 |
schemename = "PYROLOC" |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
80 |
if location.find(service_type) != -1: |
1455 | 81 |
try: |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
82 |
from util.Zeroconf import Zeroconf |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
83 |
r = Zeroconf() |
1455 | 84 |
i = r.getServiceInfo(service_type, location) |
85 |
if i is None: |
|
86 |
raise Exception("'%s' not found" % location) |
|
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
87 |
ip = str(socket.inet_ntoa(i.getAddress())) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
88 |
port = str(i.getPort()) |
1455 | 89 |
newlocation = ip + ':' + port |
1581
2295fdc5c271
fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
90 |
confnodesroot.logger.write(_("'{a1}' is located at {a2}\n").format(a1 = location, a2 = newlocation)) |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
91 |
location = newlocation |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
92 |
r.close() |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
93 |
except Exception, msg: |
1455 | 94 |
confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n") % location) |
763
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
95 |
confnodesroot.logger.write_error(traceback.format_exc()) |
c1104099c151
Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents:
733
diff
changeset
|
96 |
return None |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
97 |
|
203 | 98 |
# Try to get the proxy object |
1455 | 99 |
try: |
100 |
RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject") |
|
203 | 101 |
except Exception, msg: |
1455 | 102 |
confnodesroot.logger.write_error(_("Connection to '%s' failed.\n") % location) |
717 | 103 |
confnodesroot.logger.write_error(traceback.format_exc()) |
203 | 104 |
return None |
105 |
||
106 |
def PyroCatcher(func, default=None): |
|
107 |
""" |
|
1455 | 108 |
A function that catch a Pyro exceptions, write error to logger |
109 |
and return default value when it happen |
|
203 | 110 |
""" |
1455 | 111 |
def catcher_func(*args, **kwargs): |
203 | 112 |
try: |
1455 | 113 |
return func(*args, **kwargs) |
1116
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
114 |
except Pyro.errors.ConnectionClosedError, e: |
1595
b3cc68dd037d
add internatialization support for Beremiz_service.py
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1581
diff
changeset
|
115 |
confnodesroot.logger.write_error(_("Connection lost!\n")) |
1116
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
116 |
confnodesroot._SetConnector(None) |
493
015a803301b9
Catch ProtocolError exception when connection failed
laurent
parents:
486
diff
changeset
|
117 |
except Pyro.errors.ProtocolError, e: |
1595
b3cc68dd037d
add internatialization support for Beremiz_service.py
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1581
diff
changeset
|
118 |
confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e) |
1455 | 119 |
except Exception, e: |
120 |
# confnodesroot.logger.write_error(traceback.format_exc()) |
|
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
121 |
errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
1455 | 122 |
confnodesroot.logger.write_error(errmess + "\n") |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
465
diff
changeset
|
123 |
print errmess |
1116
300f98a8d4c6
Fixed bug connector not resetted when connection is lost
Laurent Bessard
parents:
1070
diff
changeset
|
124 |
confnodesroot._SetConnector(None) |
1070
86ee833e33ef
Added exception printing on Pyro connector, tracking random connection failure...
Edouard Tisserant
parents:
1035
diff
changeset
|
125 |
return default |
218 | 126 |
return catcher_func |
203 | 127 |
|
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
128 |
# Check connection is effective. |
203 | 129 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
1455 | 130 |
if PyroCatcher(lambda: RemotePLCObjectProxy.GetPLCstatus())() is None: |
717 | 131 |
confnodesroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) |
203 | 132 |
return None |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
133 |
|
1441
826730e60407
Added auto-reconnect for runtime. Fixed Beremiz closing problem caused by remaining twisted reactor thread in IDE.
Edouard Tisserant
parents:
1440
diff
changeset
|
134 |
class PyroProxyProxy(object): |
203 | 135 |
""" |
136 |
A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
|
1455 | 137 |
And to put Pyro exception catcher in between caller and Pyro proxy |
203 | 138 |
""" |
284
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
139 |
def __init__(self): |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
140 |
# for safe use in from debug thread, must create a copy |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
141 |
self.RemotePLCObjectProxyCopy = None |
3fecc96090c8
Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents:
235
diff
changeset
|
142 |
|
203 | 143 |
def GetPyroProxy(self): |
144 |
""" |
|
145 |
This func returns the real Pyro Proxy. |
|
146 |
Use this if you musn't keep reference to it. |
|
147 |
""" |
|
148 |
return RemotePLCObjectProxy |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
149 |
|
235 | 150 |
def _PyroStartPLC(self, *args, **kwargs): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
151 |
""" |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
152 |
confnodesroot._connector.GetPyroProxy() is used |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
153 |
rather than RemotePLCObjectProxy because |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1116
diff
changeset
|
154 |
object is recreated meanwhile, |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
155 |
so we must not keep ref to it here |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
156 |
""" |
969 | 157 |
current_status, log_count = confnodesroot._connector.GetPyroProxy().GetPLCstatus() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
158 |
if current_status == "Dirty": |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
159 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
160 |
Some bad libs with static symbols may polute PLC |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
161 |
ask runtime to suicide and come back again |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
162 |
""" |
717 | 163 |
confnodesroot.logger.write(_("Force runtime reload\n")) |
164 |
confnodesroot._connector.GetPyroProxy().ForceReload() |
|
165 |
confnodesroot._Disconnect() |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
166 |
# let remote PLC time to resurect.(freeze app) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
167 |
sleep(0.5) |
717 | 168 |
confnodesroot._Connect() |
169 |
self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) |
|
170 |
return confnodesroot._connector.GetPyroProxy().StartPLC(*args, **kwargs) |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
171 |
StartPLC = PyroCatcher(_PyroStartPLC, False) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
172 |
|
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
173 |
def _PyroGetTraceVariables(self): |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
174 |
""" |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
175 |
for safe use in from debug thread, must use the copy |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
176 |
""" |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
177 |
if self.RemotePLCObjectProxyCopy is None: |
717 | 178 |
self.RemotePLCObjectProxyCopy = copy.copy(confnodesroot._connector.GetPyroProxy()) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
179 |
return self.RemotePLCObjectProxyCopy.GetTraceVariables() |
1455 | 180 |
GetTraceVariables = PyroCatcher(_PyroGetTraceVariables, ("Broken", None)) |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
181 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
182 |
def _PyroGetPLCstatus(self): |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
183 |
return RemotePLCObjectProxy.GetPLCstatus() |
1455 | 184 |
GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, ("Broken", None)) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
185 |
|
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
186 |
def _PyroRemoteExec(self, script, **kwargs): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
187 |
return RemotePLCObjectProxy.RemoteExec(script, **kwargs) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
188 |
RemoteExec = PyroCatcher(_PyroRemoteExec, (-1, "RemoteExec script failed!")) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
493
diff
changeset
|
189 |
|
203 | 190 |
def __getattr__(self, attrName): |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
191 |
member = self.__dict__.get(attrName, None) |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
192 |
if member is None: |
1455 | 193 |
def my_local_func(*args, **kwargs): |
194 |
return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs) |
|
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
195 |
member = PyroCatcher(my_local_func, None) |
203 | 196 |
self.__dict__[attrName] = member |
231
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
197 |
return member |
f1db3ce8f40a
Re-organized pyro connector proxy members mascarading
etisserant
parents:
218
diff
changeset
|
198 |
|
203 | 199 |
return PyroProxyProxy() |