-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
107 lines (83 loc) · 3.14 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import sys
from twisted.internet.defer import inlineCallbacks
from autobahn import wamp
from autobahn.twisted.wamp import ApplicationSession
from mainProcess import MainProcess
class Imu(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
yield self.register(self)
print("Server connected")
# start imu server
self.app = MainProcess()
@inlineCallbacks
def onLeave(self, details):
print("Server disconnected")
if self.app.is_alive():
self.app.join()
@wamp.register(u'com.example.imu.test')
def test(self, arg=None):
return str("Servidor operativo")
@wamp.register(u'com.example.imu.detect')
def detectImu(self, arg=None):
resp = self.app.detectImu()
countImu = 0
if not resp:
return str("No imu detected !")
else:
for n, r in enumerate(resp):
if r is True:
countImu += 1
return str(countImu)
@wamp.register(u'com.example.imu.filename')
def setFileName(self, arg):
print arg
self.app.setFileName(str(arg))
@wamp.register(u'com.example.imu.getfilename')
def getFileName(self, arg=None):
return self.app.getFileName()
@wamp.register(u'com.example.imu.startimu')
def startImu(self, arg=None):
print("Start")
self.app.start()
@wamp.register(u'com.example.imu.stopimu')
def stoptImu(self, arg=None):
print("Stop")
self.app.stop()
self.app.join(1)
@wamp.register(u'com.example.imu.getdata')
def getImuData(self, arg=None):
data = self.app.getData()
print data
return data
if __name__ == '__main__':
import sys
import argparse
# parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", action="store_true",
help="Enable debug output.")
parser.add_argument("--web", type=int, default=8080,
help='Web port to use for embedded Web server. Use 0 to disable.')
parser.add_argument("--router", type=str, default=None,
help='If given, connect to this WAMP router. Else run an embedded router on 9000.')
args = parser.parse_args()
if args.debug:
from twisted.python import log
log.startLogging(sys.stdout)
# import Twisted reactor
from twisted.internet import reactor
# create embedded web server for static file
if args.web:
from twisted.web.server import Site
from twisted.web.static import File
reactor.listenTCP(args.web, Site(File(".")))
# run WAMP application component
from autobahn.twisted.wamp import ApplicationRunner
router = args.router or 'ws://localhost:9000'
runner = ApplicationRunner(router, u"realm1", standalone=not args.router,
debug=False, # low-level logging
debug_wamp=args.debug, # WAMP level logging
debug_app=args.debug) # app-level logging
# start the component and the Twisted reactor ..
runner.run(Imu)