lpcmanager

74e1cf35fc20
Parents b13b3255569c
Children b2cbefc12073
Change WAMP default connector for this.
--- a/LPCconnector/WAMP/__init__.py Thu Jun 01 10:23:17 2017 +0200
+++ b/LPCconnector/WAMP/__init__.py Thu Jun 01 10:24:03 2017 +0200
@@ -4,15 +4,25 @@
#
# Copyright (c) 2017 Smarteh d.o.o. <info@smarteh.si>
# THIS PROGRAM COMES WITH NO WARRANTY
-
+import traceback
from autobahn.twisted import wamp
from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
from autobahn.wamp import types
from autobahn.wamp import auth
from autobahn.wamp.serializer import MsgPackSerializer
-import connectors.WAMP as WAMP
-from connectors.WAMP import WAMP_connector_factory, WampSession
-from threading import _sleep
+import connectors
+from twisted.internet import reactor, threads
+from autobahn.wamp.exception import TransportLost
+from threading import Thread, Event
+
+# import connectors.WAMP as WAMP
+# from connectors.WAMP import WAMP_connector_factory, WampSession
+# from threading import _sleep
+
+PLCObjDefaults = { "StartPLC": False,
+ "GetTraceVariables" : ("Broken",None),
+ "GetPLCstatus" : ("Broken",None),
+ "RemoteExec" : (-1, "RemoteExec script failed!")}
PASSWORDS = {
u'smarteh': u'smarteh'
@@ -20,11 +30,21 @@
USER = u'smarteh'
-class MWWampSession(WampSession):
+_WampSession = None
+_WampConnection = None
+_WampSessionEvent = Event()
+
+class MWWampSession(wamp.ApplicationSession):
def onConnect(self):
self.join(u"Automation", [u"wampcra"], USER)
print "CRA checking init ..."
+ def onJoin(self, details):
+ global _WampSession, _WampSessionEvent
+ _WampSession = self
+ _WampSessionEvent.set()
+ print 'WAMP session joined for :', self.config.extra["ID"]
+
def onChallenge(self, challenge):
if challenge.method == u"wampcra":
key = PASSWORDS[USER].encode('utf8')
@@ -42,12 +62,9 @@
_WampSession = None
print 'WAMP session left'
-DefalutWAMP_connector_factory = WAMP_connector_factory
+# DefalutWAMP_connector_factory = WAMP_connector_factory
def MWRegisterWampClient(realm, ID, url, confnodesroot):
- ## start logging to console
- # log.startLogging(sys.stdout)
-
# create a WAMP application session factory
component_config = types.ComponentConfig(
realm = unicode(realm),
@@ -72,17 +89,89 @@
def MWWAMP_connector_factory(uri, confnodesroot):
- def CustomFunctionCall(self, funcname, args=None):
- member = self.__dict__.get(funcname, None)
+ # def CustomFunctionCall(self, funcname, args=None):
+ # member = self.__dict__.get(funcname, None)
+ # if member is None:
+ # member = WampSessionProcMapper(funcname)
+ # self.__dict__[funcname] = member
+ # return member
+ #
+ # WAMP.RegisterWampClient = MWRegisterWampClient
+ # WAMP.WampSession = MWWampSession
+ #
+ # wamp_connector = DefalutWAMP_connector_factory(uri, confnodesroot)
+ # wamp_connector.CustomFunctionCall = CustomFunctionCall
+ # _sleep(1)
+ # return wamp_connector
+
+ servicetype, location = uri.split("://")
+ urlpath, realm, ID = location.split('#')
+ urlprefix = {"WAMP": "ws",
+ "WAMPS": "wss"}[servicetype]
+ url = urlprefix + "://" + urlpath
+
+ AddToDoBeforeQuit = confnodesroot.AppFrame.AddToDoBeforeQuit
+
+ def ThreadProc():
+ global _WampConnection
+ _WampConnection = MWRegisterWampClient(realm, ID, url, confnodesroot)
+ AddToDoBeforeQuit(reactor.stop)
+ reactor.run(installSignalHandlers=False)
+
+ def WampSessionProcMapper(funcname):
+ wampfuncname = unicode('.'.join((ID, funcname)))
+
+ def catcher_func(*args, **kwargs):
+ global _WampSession
+ if _WampSession is not None:
+ try:
+ return threads.blockingCallFromThread(
+ reactor, _WampSession.call, wampfuncname,
+ *args, **kwargs)
+ except TransportLost, e:
+ confnodesroot.logger.write_error(_("Connection lost!\n"))
+ confnodesroot._SetConnector(None)
+ except Exception, e:
+ errmess = traceback.format_exc()
+ confnodesroot.logger.write_error(errmess + "\n")
+ print errmess
+ # confnodesroot._SetConnector(None)
+ return PLCObjDefaults.get(funcname)
+
+ return catcher_func
+
+ class WampPLCObjectProxy(object):
+ def __init__(self):
+ global _WampSessionEvent, _WampConnection
+ if not reactor.running:
+ Thread(target=ThreadProc).start()
+ else:
+ _WampConnection = threads.blockingCallFromThread(
+ reactor, MWRegisterWampClient, realm, ID, url, confnodesroot)
+ if not _WampSessionEvent.wait(5):
+ _WampConnection = stopConnecting()
+ raise Exception, _("WAMP connection timeout")
+
+ def __del__(self):
+ global _WampConnection
+ _WampConnection.disconnect()
+ #
+ # reactor.stop()
+
+ def __getattr__(self, attrName):
+ member = self.__dict__.get(attrName, None)
if member is None:
- member = WampSessionProcMapper(funcname)
- self.__dict__[funcname] = member
+ member = WampSessionProcMapper(attrName)
+ self.__dict__[attrName] = member
return member
- WAMP.RegisterWampClient = MWRegisterWampClient
- WAMP.WampSession = MWWampSession
+ # Try to get the proxy object
+ try:
+ return WampPLCObjectProxy()
+ except Exception, msg:
+ confnodesroot.logger.write_error(_("WAMP connection to '%s' failed.\n") % location)
+ confnodesroot.logger.write_error(traceback.format_exc())
+ return None
- wamp_connector = DefalutWAMP_connector_factory(uri, confnodesroot)
- wamp_connector.CustomFunctionCall = CustomFunctionCall
- _sleep(1)
- return wamp_connector
\ No newline at end of file
+
+connectors.connectors["WAMP"] = lambda: MWWAMP_connector_factory
\ No newline at end of file