runtime/WampClient.py
changeset 2306 908471b344ba
parent 2265 2de61e2fbf14
child 2308 4d7cee25a474
equal deleted inserted replaced
2273:a0efe3d9c853 2306:908471b344ba
    30 import re
    30 import re
    31 from autobahn.twisted import wamp
    31 from autobahn.twisted import wamp
    32 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
    32 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS
    33 from autobahn.wamp import types, auth
    33 from autobahn.wamp import types, auth
    34 from autobahn.wamp.serializer import MsgPackSerializer
    34 from autobahn.wamp.serializer import MsgPackSerializer
    35 from twisted.internet.defer import inlineCallbacks
       
    36 from twisted.internet.protocol import ReconnectingClientFactory
    35 from twisted.internet.protocol import ReconnectingClientFactory
    37 from twisted.python.components import registerAdapter
    36 from twisted.python.components import registerAdapter
    38 
    37 
    39 from formless import annotate, webform
    38 from formless import annotate, webform
    40 import formless
    39 import formless
    41 from nevow import tags, url, static
    40 from nevow import tags, url, static
       
    41 from runtime import GetPLCObjectSingleton
    42 
    42 
    43 mandatoryConfigItems = ["ID", "active", "realm", "url"]
    43 mandatoryConfigItems = ["ID", "active", "realm", "url"]
    44 
    44 
    45 _transportFactory = None
    45 _transportFactory = None
    46 _WampSession = None
    46 _WampSession = None
    85 
    85 
    86 
    86 
    87 def GetCallee(name):
    87 def GetCallee(name):
    88     """ Get Callee or Subscriber corresponding to '.' spearated object path """
    88     """ Get Callee or Subscriber corresponding to '.' spearated object path """
    89     names = name.split('.')
    89     names = name.split('.')
    90     obj = _PySrv.plcobj
    90     obj = GetPLCObjectSingleton()
    91     while names:
    91     while names:
    92         obj = getattr(obj, names.pop(0))
    92         obj = getattr(obj, names.pop(0))
    93     return obj
    93     return obj
    94 
    94 
    95 
    95 
   113                 raise Exception("no secret given for authentication")
   113                 raise Exception("no secret given for authentication")
   114         else:
   114         else:
   115             raise Exception(
   115             raise Exception(
   116                 "don't know how to handle authmethod {}".format(challenge.method))
   116                 "don't know how to handle authmethod {}".format(challenge.method))
   117 
   117 
   118     @inlineCallbacks
       
   119     def onJoin(self, details):
   118     def onJoin(self, details):
   120         global _WampSession
   119         global _WampSession
   121         _WampSession = self
   120         _WampSession = self
   122         ID = self.config.extra["ID"]
   121         ID = self.config.extra["ID"]
   123 
   122 
   126                 registerOptions = types.RegisterOptions(**kwargs)
   125                 registerOptions = types.RegisterOptions(**kwargs)
   127             except TypeError as e:
   126             except TypeError as e:
   128                 registerOptions = None
   127                 registerOptions = None
   129                 print(_("TypeError register option: {}".format(e)))
   128                 print(_("TypeError register option: {}".format(e)))
   130 
   129 
   131             yield self.register(GetCallee(name), u'.'.join((ID, name)), registerOptions)
   130             self.register(GetCallee(name), u'.'.join((ID, name)), registerOptions)
   132 
   131 
   133         for name in SubscribedEvents:
   132         for name in SubscribedEvents:
   134             yield self.subscribe(GetCallee(name), unicode(name))
   133             self.subscribe(GetCallee(name), unicode(name))
   135 
   134 
   136         for func in DoOnJoin:
   135         for func in DoOnJoin:
   137             yield func(self)
   136             func(self)
   138 
   137 
   139         print(_('WAMP session joined (%s) by:' % time.ctime()), ID)
   138         print(_('WAMP session joined (%s) by:' % time.ctime()), ID)
   140 
   139 
   141     def onLeave(self, details):
   140     def onLeave(self, details):
   142         global _WampSession, _transportFactory
   141         global _WampSession, _transportFactory
   143         super(WampSession, self).onLeave(details)
   142         super(WampSession, self).onLeave(details)
   144         _WampSession = None
   143         _WampSession = None
   145         _transportFactory = None
   144         _transportFactory = None
   146         print(_('WAMP session left'))
   145         print(_('WAMP session left'))
   147 
   146 
       
   147     def publishWithOwnID(eventID, value):
       
   148         ID = self.config.extra["ID"]
       
   149         self.publish(unicode(ID+'.'+eventID), value)
   148 
   150 
   149 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
   151 class ReconnectingWampWebSocketClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):
   150 
   152 
   151     def __init__(self, config, *args, **kwargs):
   153     def __init__(self, config, *args, **kwargs):
   152         global _transportFactory
   154         global _transportFactory
   339 
   341 
   340 def SetServer(pysrv):
   342 def SetServer(pysrv):
   341     global _PySrv
   343     global _PySrv
   342     _PySrv = pysrv
   344     _PySrv = pysrv
   343 
   345 
       
   346 def PublishEvent(eventID, value):
       
   347     if getWampStatus() == "Attached":
       
   348          _WampSession.publish(eventID, value)
       
   349 
       
   350 def PublishEventWithOwnID(eventID, value):
       
   351     if getWampStatus() == "Attached":
       
   352          _WampSession.publishWithOwnID(eventID, value)
   344 
   353 
   345 # WEB CONFIGURATION INTERFACE
   354 # WEB CONFIGURATION INTERFACE
   346 WAMP_SECRET_URL = "secret"
   355 WAMP_SECRET_URL = "secret"
   347 webExposedConfigItems = ['active', 'url', 'ID']
   356 webExposedConfigItems = ['active', 'url', 'ID']
   348 
   357