1 #!/usr/bin/env python |
1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- |
2 # -*- coding: utf-8 -*- |
3 |
3 |
4 import sys |
4 import sys |
5 from twisted.python import log |
5 #from twisted.python import log |
6 |
|
7 from twisted.internet import reactor, ssl |
|
8 from autobahn.twisted import wamp |
6 from autobahn.twisted import wamp |
9 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
7 from autobahn.twisted.websocket import WampWebSocketClientFactory, connectWS |
|
8 from twisted.internet.defer import inlineCallbacks |
10 from autobahn.wamp import types |
9 from autobahn.wamp import types |
|
10 from autobahn.wamp.serializer import MsgPackSerializer |
11 import json |
11 import json |
12 |
12 |
13 _WampSession = None |
13 _WampSession = None |
14 _PySrv = None |
14 _PySrv = None |
15 |
15 |
|
16 ExposedCalls = ["StartPLC", |
|
17 "StopPLC", |
|
18 "ForceReload", |
|
19 "GetPLCstatus", |
|
20 "NewPLC", |
|
21 "MatchMD5", |
|
22 "SetTraceVariablesList", |
|
23 "GetTraceVariables", |
|
24 "RemoteExec", |
|
25 "GetLogMessage", |
|
26 ] |
|
27 |
|
28 def MakeCallee(name): |
|
29 global _PySrv |
|
30 def Callee(*args,**kwargs): |
|
31 return getattr(_PySrv.plcobj, name)(*args,**kwargs) |
|
32 return Callee |
|
33 |
|
34 |
16 class WampSession(wamp.ApplicationSession): |
35 class WampSession(wamp.ApplicationSession): |
|
36 |
|
37 @inlineCallbacks |
17 def onJoin(self, details): |
38 def onJoin(self, details): |
18 global _WampSession |
39 global _WampSession |
19 _WampSession = self |
40 _WampSession = self |
20 print 'WAMP session joined by :', self.config.extra["ID"] |
41 print 'WAMP session joined by :', self.config.extra["ID"] |
|
42 for name in ExposedCalls: |
|
43 reg = yield self.register(MakeCallee(name), name) |
21 |
44 |
22 def onLeave(self, details): |
45 def onLeave(self, details): |
23 global _WampSession |
46 global _WampSession |
24 _WampSession = None |
47 _WampSession = None |
25 print 'WAMP session left' |
48 print 'WAMP session left' |
26 |
49 |
27 |
|
28 def RegisterWampClient(wampconf): |
50 def RegisterWampClient(wampconf): |
29 |
51 |
30 WSClientConf = json.load(open(wampconf)) |
52 WSClientConf = json.load(open(wampconf)) |
31 |
53 |
32 ## TODO log to PLC console instead |
54 ## start logging to console |
33 ## 0) start logging to console |
55 # log.startLogging(sys.stdout) |
34 log.startLogging(sys.stdout) |
|
35 |
56 |
36 ## 1) create a WAMP application session factory |
57 # create a WAMP application session factory |
37 component_config = types.ComponentConfig( |
58 component_config = types.ComponentConfig( |
38 realm = WSClientConf["realm"], |
59 realm = WSClientConf["realm"], |
39 extra = {"ID":WSClientConf["ID"]}) |
60 extra = {"ID":WSClientConf["ID"]}) |
40 session_factory = wamp.ApplicationSessionFactory( |
61 session_factory = wamp.ApplicationSessionFactory( |
41 config = component_config) |
62 config = component_config) |
42 session_factory.session = WampSession |
63 session_factory.session = WampSession |
43 |
64 |
44 ## TODO select optimum serializer for passing session lists |
65 # create a WAMP-over-WebSocket transport client factory |
45 ## optional: use specific set of serializers |
|
46 #from autobahn.wamp.serializer import * |
|
47 #serializers = [] |
|
48 ##serializers.append(JsonSerializer(batched = True)) |
|
49 ##serializers.append(MsgPackSerializer(batched = True)) |
|
50 #serializers.append(JsonSerializer()) |
|
51 ##serializers.append(MsgPackSerializer()) |
|
52 serializers = None |
|
53 |
|
54 ## 2) create a WAMP-over-WebSocket transport client factory |
|
55 transport_factory = WampWebSocketClientFactory( |
66 transport_factory = WampWebSocketClientFactory( |
56 session_factory, |
67 session_factory, |
57 url = WSClientConf["url"], |
68 url = WSClientConf["url"], |
58 serializers = serializers, |
69 serializers = [MsgPackSerializer()], |
59 debug = False, |
70 debug = False, |
60 debug_wamp = False) |
71 debug_wamp = False) |
61 |
72 |
62 ## 3) start the client from a Twisted endpoint |
73 # start the client from a Twisted endpoint |
63 conn = connectWS(transport_factory) |
74 conn = connectWS(transport_factory) |
64 print "WAMP clien connecting to :",WSClientConf["url"] |
75 print "WAMP client connecting to :",WSClientConf["url"] |
65 return conn |
76 return conn |
66 |
77 |
67 def GetSession(): |
78 def GetSession(): |
68 global _WampSession |
79 global _WampSession |
69 return _WampSession |
80 return _WampSession |