forked from libmapper/webmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmapper.py
executable file
·268 lines (226 loc) · 9.83 KB
/
webmapper.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
import webmapper_http_server as server
import mapper
import mapperstorage
import netifaces # a library to find available network interfaces
import sys, os, os.path, threading, json, re, pdb
from random import randint
networkInterfaces = {'active': '', 'available': []}
dirname = os.path.dirname(__file__)
if dirname:
os.chdir(os.path.dirname(__file__))
if 'tracing' in sys.argv[1:]:
server.tracing = True
def open_gui(port):
url = 'http://localhost:%d'%port
apps = ['~\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe --app=%s',
'/usr/bin/chromium-browser --app=%s',
]
if 'darwin' in sys.platform:
# Dangerous to run 'open' on platforms other than OS X, so
# check for OS explicitly in this case.
apps = ['open -n -a "Google Chrome" --args --app=%s']
def launch():
try:
import webbrowser, time
time.sleep(0.2)
for a in apps:
a = os.path.expanduser(a)
a = a.replace('\\','\\\\')
if webbrowser.get(a).open(url):
return
webbrowser.open(url)
except:
print 'Error opening web browser, continuing anyway.'
launcher = threading.Thread(target=launch)
launcher.start()
monitor = mapper.monitor(autosubscribe_flags=mapper.SUB_DEVICE | mapper.SUB_DEVICE_LINKS_OUT)
def on_device(dev, action):
if action == mapper.MDB_NEW:
server.send_command("new_device", dev)
if action == mapper.MDB_MODIFY:
server.send_command("mod_device", dev)
if action == mapper.MDB_REMOVE:
server.send_command("del_device", dev)
def on_signal(sig, action):
if action == mapper.MDB_NEW:
server.send_command("new_signal", sig)
if action == mapper.MDB_MODIFY:
server.send_command("mod_signal", sig)
if action == mapper.MDB_REMOVE:
server.send_command("del_signal", sig)
def on_link(link, action):
if action == mapper.MDB_NEW:
server.send_command("new_link", link)
if action == mapper.MDB_MODIFY:
server.send_command("mod_link", link)
if action == mapper.MDB_REMOVE:
server.send_command("del_link", link)
def on_connection(con, action):
if action == mapper.MDB_NEW:
server.send_command("new_connection", con)
if action == mapper.MDB_MODIFY:
server.send_command("mod_connection", con)
if action == mapper.MDB_REMOVE:
server.send_command("del_connection", con)
def set_connection(con):
if con.has_key('mode'):
con['mode'] = {'bypass': mapper.MO_BYPASS,
'reverse': mapper.MO_REVERSE,
'linear': mapper.MO_LINEAR,
'calibrate': mapper.MO_CALIBRATE,
'expression': mapper.MO_EXPRESSION}[con['mode']]
if (con.has_key('src_min')):
if (type(con['src_min']) is int or type(con['src_min']) is float):
con['src_min'] = float(con['src_min'])
numargs = 1;
else:
if (type(con['src_min']) is str):
con['src_min'] = con['src_min'].replace(',',' ').split()
numargs = len(con['src_min'])
for i in range(numargs):
con['src_min'][i] = float(con['src_min'][i])
if numargs == 1:
con['src_min'] = con['src_min'][0]
con['src_type'] = 'f'
if (con.has_key('src_max')):
if (type(con['src_max']) is int or type(con['src_max']) is float):
con['src_max'] = float(con['src_max'])
numargs = 1;
else:
if (type(con['src_max']) is str):
con['src_max'] = con['src_max'].replace(',',' ').split()
numargs = len(con['src_max'])
for i in range(numargs):
con['src_max'][i] = float(con['src_max'][i])
if numargs == 1:
con['src_max'] = con['src_max'][0]
con['src_type'] = 'f'
if (con.has_key('dest_min')):
if (type(con['dest_min']) is int or type(con['dest_min']) is float):
con['dest_min'] = float(con['dest_min'])
numargs = 1;
else:
if (type(con['dest_min']) is str):
con['dest_min'] = con['dest_min'].replace(',',' ').split()
numargs = len(con['dest_min'])
for i in range(numargs):
con['dest_min'][i] = float(con['dest_min'][i])
if numargs == 1:
con['dest_min'] = con['dest_min'][0]
con['src_type'] = 'f'
if (con.has_key('dest_max')):
if (type(con['dest_max']) is int or type(con['dest_max']) is float):
con['dest_max'] = float(con['dest_max'])
numargs = 1;
else:
if (type(con['dest_max']) is str):
con['dest_max'] = con['dest_max'].replace(',',' ').split()
numargs = len(con['dest_max'])
for i in range(numargs):
con['dest_max'][i] = float(con['dest_max'][i])
if numargs == 1:
con['dest_max'] = con['dest_max'][0]
con['src_type'] = 'f'
monitor.modify_connection(con['src_name'], con['dest_name'], con)
def on_refresh(arg):
global monitor
del monitor
admin = mapper.admin(networkInterfaces['active'])
monitor = mapper.monitor(admin, autosubscribe_flags=mapper.SUB_DEVICE | mapper.SUB_DEVICE_LINKS_OUT)
init_monitor()
def on_save(arg):
ds = list(monitor.db.match_devices_by_name(arg['dev']))
fn = '/'.join(ds[0]['name'].split('/')[1:])
fn.replace('/','_')
fn = '.'.join(fn.split('.')[:-1]+['json'])
return fn, mapperstorage.serialise(monitor, arg['dev'])
def on_load(mapping_json, devices):
# pdb.set_trace()
mapperstorage.deserialise(monitor, mapping_json, devices)
def select_network(newNetwork):
# print 'select_network', newNetwork
networkInterfaces['active'] = newNetwork
server.send_command('set_network', newNetwork)
def get_networks(arg):
location = netifaces.AF_INET # A computer specific integer for internet addresses
totalInterfaces = netifaces.interfaces() # A list of all possible interfaces
connectedInterfaces = []
for i in totalInterfaces:
addrs = netifaces.ifaddresses(i)
if location in addrs: # Test to see if the interface is actually connected
connectedInterfaces.append(i)
server.send_command("available_networks", connectedInterfaces)
networkInterfaces['available'] = connectedInterfaces
server.send_command("active_network", networkInterfaces['active'])
def get_active_network(arg):
print networkInterfaces['active']
server.send_command("active_network", networkInterfaces['active'])
def init_monitor():
monitor.db.add_device_callback(on_device)
monitor.db.add_signal_callback(on_signal)
monitor.db.add_link_callback(on_link)
monitor.db.add_connection_callback(on_connection)
init_monitor()
server.add_command_handler("all_devices",
lambda x: ("all_devices",
list(monitor.db.all_devices())))
def select_tab(src_dev):
# TODO:
# if src_dev != focus_dev and focus_dev != "All Devices":
# # revert device subscription back to only device and link metadata
# monitor.subscribe(focus_dev, mapper.SUB_DEVICE | mapper.SUB_DEVICE_LINKS_OUT, -1)
if src_dev != "All Devices":
monitor.subscribe(src_dev, mapper.SUB_DEVICE_OUTPUTS | mapper.SUB_DEVICE_CONNECTIONS_OUT, -1)
links = monitor.db.links_by_src_device_name(src_dev)
for i in links:
monitor.subscribe(i["dest_name"], mapper.SUB_DEVICE_INPUTS, -1)
def new_connection(args):
source = str(args[0])
dest = str(args[1])
options = {}
if( len(args) > 2 ): # See if the connection message has been supplied with options
if( type(args[2]) is dict ): # Make sure they are the proper format
options = args[2]
monitor.connect(source, dest, options)
server.add_command_handler("tab", lambda x: select_tab(x))
server.add_command_handler("all_signals",
lambda x: ("all_signals",
list(monitor.db.all_inputs())
+ list(monitor.db.all_outputs())))
server.add_command_handler("all_links",
lambda x: ("all_links",
list(monitor.db.all_links())))
server.add_command_handler("all_connections",
lambda x: ("all_connections",
list(monitor.db.all_connections())))
server.add_command_handler("set_connection", set_connection)
server.add_command_handler("link",
lambda x: monitor.link(*map(str,x)))
server.add_command_handler("unlink",
lambda x: monitor.unlink(*map(str,x)))
server.add_command_handler("connect", lambda x: new_connection(x))
server.add_command_handler("disconnect",
lambda x: monitor.disconnect(*map(str,x)))
server.add_command_handler("refresh", on_refresh)
server.add_command_handler("save", on_save)
server.add_command_handler("load", on_load)
server.add_command_handler("select_network", select_network)
server.add_command_handler("get_networks", get_networks)
get_networks(False)
if ( 'en1' in networkInterfaces['available'] ) :
networkInterfaces['active'] = 'en1'
elif ( 'en0' in networkInterfaces['available'] ):
networkInterfaces['active'] = 'en0'
elif ( 'lo0' in networkInterfaces['available'] ):
networkInterfaces['active'] = 'lo0'
try:
port = int(sys.argv[sys.argv.index('--port'):][1])
except:
#port = randint(49152,65535)
port = 50000
on_open = lambda: ()
if not '--no-browser' in sys.argv and not '-n' in sys.argv:
on_open = lambda: open_gui(port)
server.serve(port=port, poll=lambda: monitor.poll(100), on_open=on_open,
quit_on_disconnect=not '--stay-alive' in sys.argv)