-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathserver.py
223 lines (197 loc) · 7.79 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
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
from flask import Flask, render_template, request
from flask_sock import Sock
import socket
import vgamepad as vg
import json
from pystray import Icon as icon, Menu as menu, MenuItem as item
from PIL import Image
import threading
import ctypes
import datetime
app = Flask(__name__, static_url_path='',
static_folder='static',
template_folder='templates')
sock = Sock(app)
# gamepad = None
def create_image():
# Open logo.png
image = Image.open("logo.png")
# Resize the image heigh 64 and preserve the aspect ratio
image.thumbnail((64, 64), Image.ANTIALIAS)
return image
@sock.route('/controller')
def controller(ws):
def my_callback(client, target, large_motor, small_motor, led_number, user_data):
notif = {
'lm': large_motor,
'sm': small_motor,
'led': led_number,
}
ws.send(json.dumps(notif))
# global gamepad
gamepad = None
if gamepad == None:
gamepad = vg.VX360Gamepad()
gamepad.register_notification(callback_function=my_callback)
while True:
if not ws.connected:
print("Disconnected")
gamepad.unregister_notification()
del gamepad
gamepad = None
ws.close()
return
message = ws.receive(0.1)
if message and message.startswith('{'):
message = json.loads(message)
gamepad.left_joystick(
int(message['lx']*32767), -int(message['ly']*32767))
gamepad.right_joystick(
int(message['rx']*32767), -int(message['ry']*32767))
if message['0'] == True:
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
elif message['0'] == False:
gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
if message['1'] == True:
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_B)
elif message['1'] == False:
gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_B)
if message['2'] == True:
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_X)
elif message['2'] == False:
gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_X)
if message['3'] == True:
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_Y)
elif message['3'] == False:
gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_Y)
if message['4'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_SHOULDER)
elif message['4'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_SHOULDER)
if message['5'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_SHOULDER)
elif message['5'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_SHOULDER)
gamepad.left_trigger(int(message['6']*255))
gamepad.right_trigger(int(message['7']*255))
if message['8'] == True:
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_BACK)
elif message['8'] == False:
gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_BACK)
if message['9'] == True:
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_START)
elif message['9'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_START)
if message['10'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_THUMB)
elif message['10'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_THUMB)
if message['11'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_THUMB)
elif message['11'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_THUMB)
if message['12'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP)
elif message['12'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP)
if message['13'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN)
elif message['13'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN)
if message['14'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
elif message['14'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
if message['15'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
elif message['15'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
if message['16'] == True:
gamepad.press_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_GUIDE)
elif message['16'] == False:
gamepad.release_button(
button=vg.XUSB_BUTTON.XUSB_GAMEPAD_GUIDE)
gamepad.update()
elif message and message == 'disconnect':
print("Disconnected")
gamepad.unregister_notification()
del gamepad
gamepad = None
ws.close()
return
@app.route('/')
def index():
return render_template('index.html')
hostname = socket.gethostname()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 0))
port = sock.getsockname()[1]
sock.close()
def runServer():
print("Open this webpage in your mobile: http://"+hostname+':'+str(port))
app.run(host='0.0.0.0', port=port)
class thread_with_exception(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
# target function of the thread class
try:
runServer()
finally:
print('ended')
def get_id(self):
# returns id of the respective thread
if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id
def raise_exception(self):
thread_id = self.get_id()
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
t = thread_with_exception('thread_with_exception')
t.start()
def exit(icon):
global t
t.raise_exception()
t.join()
icon.visible = False
icon.stop()
def startIcon():
import webbrowser
icon('test', Image.open('logo.ico'), menu=menu(
item(
'http://'+hostname+':'+str(port),
action=lambda: webbrowser.open('http://'+hostname+':'+str(port))
),
item('http://'+socket.gethostbyname(socket.gethostname())+':'+str(port),
action=lambda: webbrowser.open('http://'+socket.gethostbyname(socket.gethostname())+':'+str(port))),
item(
'Exit',
action=exit
)
)).run()
startIcon()