-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcraft.py
executable file
·409 lines (341 loc) · 14.8 KB
/
craft.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
'''
Copyright 2018 Logitech Inc.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files(the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
對於要怎麼裝,請參考:https://drive.google.com/file/d/1gB5NkRc3RvGKC3EqUkwcDbgQUhW1hQt0/view
'''
import wx
import websocket
import threading
import os
import platform
import json
import uuid
import sys
from copy import deepcopy
from time import sleep
from json import JSONEncoder
from uuid import UUID
glist=[]
crownObjectList = []
toolObject = []
sessionId=""
JSONEncoder_olddefault = JSONEncoder.default
# special encoding for supporting UUID in json payload
def JSONEncoder_newdefault(self, o):
if isinstance(o, UUID): return str(o)
return JSONEncoder_olddefault(self, o)
JSONEncoder.default = JSONEncoder_newdefault
class CraftClient(object):
def __init__(self):
self.executableName=""
self.manifestPath=""
self.callback=""
def on_message(self,ws, message):
print("on_message called...")
# craft events come in as json objects
craftEventObj = json.loads(message)
wx.CallAfter(self.wrapperUpdateUI, craftEventObj)
def on_close(self,ws):
print("### closed ###")
def on_open(self,ws):
print("on_open called...")
uid = "6202f2fb-834c-4393-a95f-f5051171e3ec"
pid = os.getpid()
connectMessage = {
"message_type": "register",
"plugin_guid": uid,
"PID": pid,
"execName": self.executableName,
"manifestPath": self.manifestPath,
"application_version": "0.0.0"
}
regMsg = json.dumps(connectMessage)
ws.send(regMsg.encode('utf8'))
def connect(self, execName,manifestFilePath):
print("connect called...")
global ws
self.executableName = execName
self.manifestPath = manifestFilePath
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://127.0.0.1:10134",
on_open = self.on_open,
on_message = self.on_message,
on_close = self.on_close)
wst = threading.Thread(target=ws.run_forever)
wst.daemon = True
wst.start()
def registerEventHandler(self, cb):
self.callback = cb
def report(self, event, value):
toolId = event.get('task_options').get('current_tool')
na = event.get('task_options').get('current_tool_option')
response = {
"message_type" : "tool_update",
"session_id" : sessionId,
"show_overlay" : True,
"tool_id" : toolId,
"tool_options" : [{
"name" : na,
"value" : value
}]
};
regMsg = json.dumps(response)
ws.send(regMsg.encode('utf8'))
def wrapperUpdateUI(self,msg):
global glist,sessionId
totalDeltaValue=0
totalRatchetDeltaValue=0
count=0
listCount=0
global firstObject
if(msg['message_type'] == "crown_turn_event"):
glist.append(msg)
listCount = len(glist)
if listCount==0:
return
currentToolOption = glist[0]['task_options']['current_tool_option']
print("+++currentToolOption = ",currentToolOption)
print("listCount = ",listCount)
firstObject = glist[0]
for i in range(listCount):
if currentToolOption == glist[i]['task_options']['current_tool_option']:
totalDeltaValue = totalDeltaValue = glist[i]['delta']
totalRatchetDeltaValue = totalRatchetDeltaValue + glist[i]['ratchet_delta']
else:
break
count += 1
if listCount >= 0:
glist.clear()
print("totalDeltaValue = ",totalDeltaValue)
print("firstObject = ",firstObject['message_type'])
if firstObject['message_type'] == "deactivate_plugin":
return
try:
if firstObject['message_type'] == "crown_turn_event":
print("turn event =====")
if firstObject['task_options']['current_tool'] == 'Slider':
print("\n","selected slider")
v = slider.GetValue()
tvalue = v + totalDeltaValue
if tvalue <= 0:
tvalue = 0
if tvalue >1000:
tvalue = 1000
slider.SetValue(tvalue)
print("report called....",tvalue,msg)
self.report(msg,tvalue)
elif firstObject['task_options']['current_tool'] == 'SpinCtrl':
print("\n","selected SpinCtrl")
v = spin.GetValue()
tvalue = v + totalDeltaValue
if tvalue <= 0:
tvalue = 0
if tvalue >1000:
tvalue = 1000
spin.SetValue(tvalue)
self.report(msg,tvalue)
elif firstObject['task_options']['current_tool'] == 'Gauge':
if firstObject['task_options']['current_tool_option'] == 'gauge':
print("\n","selected Gauge")
v = gauge.GetValue()
tvalue = v + totalDeltaValue
"""if tvalue <= 0:
tvalue = 0
if tvalue >500:
tvalue = 500"""
gauge.SetValue(tvalue)
QQQ = {0:"↑fan 1",
1:"↑💡 1",
2:"↓💡 1",
3:"↑💡 2",
4:"↓💡 2",
5:"↑💡 3",
6:"↓💡 3",
7:"↑💡 4",
8:"↓💡 4",
9:"↑curtain1",
10:"↓curtain1",
11:"↑curtain2",
12:"↓curtain2",
13:"↑curtain3",
14:"↓curtain3",
15:"↑curtain4",
16:"↓curtain4",
17:"↑Faucet1",
18:"↓Faucet1",
18:"↓Screen1",
19:"↑Screen1",
20:"↓fan 1"
}
self.report(msg,QQQ[tvalue%21])
if firstObject['task_options']['current_tool_option'] == 'gaugeRatchet':
print("\n","selected gaugeRatchet")
v = gauge.GetValue()
"""tvalue = v + (totalRatchetDeltaValue * 10)
if tvalue <= 0:
tvalue = 0
if tvalue >500:
tvalue = 500"""
tvalue = v + totalRatchetDeltaValue
QQ = {0:"fan 1📖",
1:"💡 1📖",
2:"💡 2📖",
3:"💡 3📖",
4:"💡 4📖",
5:"curtain1📖",
6:"curtain2📖",
7:"curtain3📖",
8:"curtain4📖",
9:"Faucet1📖",
10:"Screen1📖",
}
gauge.SetValue(tvalue)
self.report(msg,QQ[tvalue%11])
elif firstObject['task_options']['current_tool'] == 'ComboBox':
print("\n","selected ComboBox")
v = combo.GetSelection()
tvalue = v + totalRatchetDeltaValue
if tvalue <= 0:
tvalue = 0
if tvalue >999:
tvalue = 999
combo.SetSelection(tvalue)
self.report(msg,tvalue)
elif firstObject['task_options']['current_tool'] == 'TextCtrl':
print("\n","selected TextCtrl")
v = txt.GetSize()
h = v.height + totalDeltaValue
w = v.width + totalDeltaValue
txt.SetSize(w,h)
self.report(msg,w)
elif firstObject['task_options']['current_tool'] == 'ListBox':
print("\n","selected ListBox")
v = lb.GetSelection()
v = v + totalRatchetDeltaValue
if v <= 0:
v = 0
if v > 999:
v = 999
lb.SetSelection(v)
lb.EnsureVisible(v)
self.report(msg,v)
except ValueError:
print("Error: update UI")
elif (msg['message_type'] == "register_ack"):
print("register_ack = ",msg['message_type'])
sessionId = msg['session_id']
print("Session Id = ",sessionId)
if platform.system() == 'Windows':
defaultTool = "Slider"
else:
defaultTool = "Gauge"
connectMessage = {
"message_type": "tool_change",
"session_id": sessionId,
"tool_id": defaultTool
}
regMsg = json.dumps(connectMessage)
ws.send(regMsg.encode('utf8'))
class TestFrame(wx.Frame):
def __init__(self, parent, id):
global craft,slider,spin,gauge,combo,txt,lb
wx.Frame.__init__(self, parent, id, "品質很Gold", size=(800,400))
panel = wx.Panel(self)
"""lbl = wx.StaticText(panel,-1, label="text", pos=(10,20), size=(50,-1))
lbl.SetLabel("Slider")
slider=wx.Slider(panel, -1, 0, 1, 1000, (100,20), (200,-1))
slider.Bind(wx.EVT_SET_FOCUS, self.sliderFocus)
slider.Bind(wx.EVT_LEFT_UP, self.sliderFocus)
lbl = wx.StaticText(panel, -1, label="text", pos=(10,100), size=(50,-1))
lbl.SetLabel("SpinCtrl")
spin = wx.SpinCtrl(panel, -1, "", pos=(100,100), size=(200,-1), min=0, max=1000)
spin.Bind(wx.EVT_CHILD_FOCUS, self.spinCtrlFocus)
spin.Bind(wx.EVT_LEFT_UP, self.spinCtrlFocus)"""
lbl = wx.StaticText(panel, -1, label="text", pos=(10,180), size=(50,-1))
lbl.SetLabel("Mysterious line")
gauge = wx.Gauge(panel, -1, range=500, pos=(130,180), size=(500,25))
gauge.Bind(wx.EVT_LEFT_UP, self.gaugeClick)
"""lbl = wx.StaticText(panel, -1, label="text", pos=(10,260), size=(50,-1))
lbl.SetLabel("ComboBox")
l=[]
for i in range(0, 1000):
l.append(str(i))
combo = wx.ComboBox(panel, -1, "", pos=(100,260), size=(200,25), choices=l)
combo.Bind(wx.EVT_SET_FOCUS, self.comboBoxFocus)
combo.Bind(wx.EVT_LEFT_UP, self.comboBoxFocus)
lbl = wx.StaticText(panel, -1, label="text", pos=(400,20), size=(50,-1))
lbl.SetLabel("TextCtrl")
vtxt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque semper ut felis non bibendum. " \
"Suspendisse potenti. Mauris rhoncus auctor quam, non ullamcorper elit interdum a. Aliquam erat volutpat. " \
"Ut facilisis odio a enim facilisis, at porta lectus hendrerit. Curabitur et vulputate lectus, nec sollicitudin odio. " \
"Morbi tincidunt iaculis erat, eu bibendum sapien vehicula vel. Etiam sodales malesuada mauris, " \
"quis viverra eros imperdiet at. Vestibulum maximus dui dolor, sed laoreet arcu laoreet in. "
txt = wx.TextCtrl(panel, -1, vtxt, pos=(480,20), size=(100,-1), style=wx.TE_MULTILINE|wx.TE_READONLY)
txt.Bind(wx.EVT_SET_FOCUS, self.textCtrlFocus)
txt.Bind(wx.EVT_LEFT_UP, self.textCtrlFocus)
lbl = wx.StaticText(panel, -1, label="text", pos=(400,180), size=(50,-1))
lbl.SetLabel("ListBox")
li =[]
for i in range(0, 1000):
li.append(str(i))
lb = wx.ListBox(panel, -1, pos=(480,180), size=(100,-1), choices=li)
lb.Bind(wx.EVT_SET_FOCUS, self.listBoxFocus)
lb.Bind(wx.EVT_LEFT_UP, self.listBoxFocus)"""
def listBoxFocus(self, event):
print("ListBox receives focus")
self.changeTool("ListBox")
event.Skip()
def textCtrlFocus(self, event):
print("TextCtrl receives focus")
self.changeTool("TextCtrl")
event.Skip()
def comboBoxFocus(self, event):
print("ComboBox receives focus")
self.changeTool("ComboBox")
event.Skip()
def gaugeClick(self, event):
print("Gauge clicked...")
self.changeTool("Gauge")
event.Skip()
def sliderFocus(self, event):
print("Slider receives focus")
self.changeTool("Slider")
event.Skip()
def spinCtrlFocus(self, event):
print("SpinCtrl receives focus")
self.changeTool("SpinCtrl")
event.Skip()
def changeTool(self, name):
connectMessage = {
"message_type": "tool_change",
"session_id": sessionId,
"tool_id": name
}
regMsg = json.dumps(connectMessage)
ws.send(regMsg.encode('utf8'))
if __name__ == '__main__':
global ws
global craft
app = wx.App()
frame = TestFrame(parent=None, id=-1)
frame.Show()
craft = CraftClient()
if platform.system() == 'Windows':
craft.connect("Craft.exe", "")
else:
craft.connect("craft.app", "")
app.MainLoop()