3 # Copyright (c) 2012, Intel Corporation.
5 # This program is licensed under the terms and conditions of the
6 # Apache License, version 2.0. The full text of the Apache License is at
7 # http://www.apache.org/licenses/LICENSE-2.0
17 import simplejson as json
21 import dbus.mainloop.glib
23 class RemoteControlService(dbus.service.Object):
25 def __init__(self, conn, object_path='/com/intel/RemoteControl/server'):
26 dbus.service.Object.__init__(self, conn, object_path)
28 @dbus.service.signal(dbus_interface='com.intel.RemoteControl', \
30 def notification(self, mtype, mvalue):
31 print 'Triggering notification: ', mtype, mvalue
33 class DBusThread(threading.Thread):
36 threading.Thread.__init__ ( self )
38 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
40 session_bus = dbus.SessionBus()
41 name = dbus.service.BusName('com.intel.RemoteControl', \
43 self.remoteserver = RemoteControlService(session_bus)
45 loop = gobject.MainLoop()
46 gobject.threads_init()
48 print 'Dbus thread started...'
50 print 'Dbus thread stopped...'
52 def emitMessage(self, mtype, mvalue):
53 self.remoteserver.notification(mtype, mvalue)
55 from autobahn.websocket import WebSocketServerFactory, \
56 WebSocketServerProtocol, \
59 from api.videoplayer import VideoPlayerAPI
60 from api.dialer import DialerAPI
61 from api.voicecommand import VoiceCommandAPI
62 from api.texttospeech import TextToSpeechAPI
63 from api.remotecontrol import RemoteControlAPI
65 class RemoteServerProtocol(WebSocketServerProtocol):
68 self.videoplayerAPI = VideoPlayerAPI(self)
69 self.dialerAPI = DialerAPI(self)
70 self.voicecommandAPI = VoiceCommandAPI(self)
71 self.ttsAPI = TextToSpeechAPI(self)
72 self.remotecontrolAPI = RemoteControlAPI(self)
74 def onMessage(self, data, isBinary):
75 jsonData = json.loads(data)
76 print'data received', jsonData
78 if jsonData.get('type') == 'connect':
79 print 'client connected'
81 if not 'api_namespace' in jsonData:
82 raise Exception('method_namespace missing')
84 if jsonData.get('api_namespace') == 'tizen.ivi.videoplayer':
85 self.videoplayerAPI.handleMessage(jsonData)
86 elif jsonData.get('api_namespace') == 'tizen.ivi.dialer':
87 self.dialerAPI.handleMessage(jsonData)
88 elif jsonData.get('api_namespace') == 'tizen.ivi.voicecommand':
89 self.voicecommandAPI.handleMessage(jsonData)
90 elif jsonData.get('api_namespace') == 'tizen.ivi.texttospeech':
91 self.ttsAPI.handleMessage(jsonData)
92 elif jsonData.get('api_namespace') == 'tizen.ivi.remotecontrol':
93 self.remotecontrolAPI.handleMessage(jsonData)
95 print 'Error, unsupported api'
97 print 'Error in onMessage() ', e
100 'api_error_msg': e.__str__()
102 self.sendMessage(reply)
104 from netifaces import interfaces, ifaddresses, AF_INET
105 from twisted.internet import reactor
106 from twisted.python import log
107 from twisted.web import static
108 from twisted.web.server import Site
111 #log.startLogging(sys.stdout)
115 for interface in interfaces():
117 if AF_INET not in ifaddresses(interface):
121 for link in ifaddresses(interface)[AF_INET]:
122 address = link['addr']
123 if address != '127.0.0.1':
128 web_url = 'http://' + ip + ':' + str(port)
129 ws_url = 'ws://' + ip + ':' + str(ws_port)
131 # search and replace websocket ip addresses in masterControl.js
132 directory = os.path.abspath(os.path.dirname(sys.modules['remotecontrol'].__file__))
133 filename = os.path.join(directory, 'www/js/masterControl.js')
134 print 'filename ', filename
136 finput = open(filename, 'r')
137 content = finput.read()
138 for match in re.findall( r'ws://[0-9|a-z|A-Z|.]+:[0-9]+', content):
139 print 'Replacing existing websocket ip address', match, 'with new ip', ws_url
140 content = content.replace(match, ws_url)
142 tempfile = filename + '~'
143 foutput = open(tempfile, "w")
144 foutput.write(content)
148 os.rename(tempfile, filename)
150 factory = WebSocketServerFactory(ws_url,
152 debugCodePaths = True)
156 #dthread = DBusThread()
157 #dthread.setDaemon(True)
161 factory.protocol = RemoteServerProtocol
162 factory.protocol.dservice = dthread
164 print 'WebSocket server running on ', ws_url, '...'
166 # this is the webserver serving remotecontrol webapp on port 8080
167 webdir = static.File(os.path.join(directory, 'www'))
169 reactor.listenTCP(port, web)
170 print 'Http server running on ', web_url, '...'
173 print 'Http server stopping...'
175 if __name__ == '__main__':