-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_messages.py
More file actions
174 lines (137 loc) · 5.51 KB
/
server_messages.py
File metadata and controls
174 lines (137 loc) · 5.51 KB
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
import json
import colorsys
from logger import log
import preferences
from PyQt4 import QtGui
Preferences = preferences.Preferences.Instance
class NetMessageType:
MSG_INVALID = 0
MSG_GENERIC_REQUEST = 1
MSG_GENERIC_REPONSE = 2
MSG_FRAME_BUFFER = 3
MSG_PREFERENCES = 4
MSG_SET_BOUNDS = 5
MSG_SET_COLOR_SETTINGS = 6
CLASS_LOOKUP = {}
class NetMessage(object):
HEADER_LENGTH = 15
def serialize(self):
body = json.dumps(self.__dict__)
data = '%4i,%10i%s\0' % (self.__class__.TYPE, len(body)+1, body)
return data
def autoDeserialize(self, strData):
log.debug("NetMessage.autoDeserialize: ")
my_attributes = self.__dict__.keys()
json_data = json.loads(strData)
for attr in json_data:
if attr in my_attributes:
value = json_data[attr]
log.debug(" - %s = %s", attr, value)
self.__setattr__(attr, value)
def write(self):
return None
def read(self, strData):
pass
class MsgGenericRequest(NetMessage):
TYPE = NetMessageType.MSG_GENERIC_REQUEST
class REQUEST_STR:
getframebuffer = "getframebuffer"
getprefs = "getprefs"
def __init__(self, requestStr):
self.request = requestStr
def write(self):
return self.serialize()
class MsgGenericResponse(NetMessage):
TYPE = NetMessageType.MSG_GENERIC_REPONSE
def __init__(self):
self.error = None
self.info = None
self.warning = None
def read(self, strData):
self.autoDeserialize(strData)
if self.error is not None:
log.error(self.error)
if self.info is not None:
log.info(self.info)
if self.warning is not None:
log.warn(self.warning)
class MsgFrameBuffer(NetMessage):
TYPE = NetMessageType.MSG_FRAME_BUFFER
def __init__(self):
self.width = 0
self.height = 0
self.imageSize = 0
self.channelSeq = ""
self.imageData = []
self.colorModel = ""
def read(self, strData):
self.autoDeserialize(strData)
if not self.imageData:
return
# Decode colors to (r, g, b) tuple
pixels = []
for pixel in self.imageData:
r = (pixel >> 16) & 0b11111111
g = (pixel >> 8) & 0b11111111
b = pixel & 0b11111111
pixels.append((r, g, b))
# Update sampler
from window import MainWindow
MainWindow.Instance.sampler.setFrame(pixels, int(self.width), int(self.height))
class MsgPreferences(NetMessage):
TYPE = NetMessageType.MSG_PREFERENCES
def read(self, strData):
json_data = json.loads(strData)
Preferences.boundsTopLeft = json_data['boundsTopLeft']
Preferences.boundsTopRight = json_data['boundsTopRight']
Preferences.boundsBottomRight = json_data['boundsBottomRight']
Preferences.boundsBottomLeft = json_data['boundsBottomLeft']
Preferences.totalFadeTimeMS = json_data['totalFadeTimeMS']
Preferences.fixedColorEnabled = json_data['fixedColorEnabled']
rgb = json_data['fixedColor']
hsv = colorsys.rgb_to_hsv(rgb[0], rgb[0], rgb[0])
Preferences.colorHue = int(hsv[0]*100)
Preferences.colorSaturation = int(hsv[1]*100)
Preferences.colorBrightness = int(hsv[2]*100)
Preferences.camSaturation = json_data['camSaturation']
Preferences.camBrightness = json_data['camBrightness']
Preferences.camContrast = json_data['camContrast']
Preferences.camGain = json_data['camGain']
preferences.savePreferences()
# Update interface
from window import MainWindow
MainWindow.Instance.updateColorTabFromSettings()
MainWindow.Instance.sampler.setBounds(Preferences.boundsTopLeft, Preferences.boundsTopRight,
Preferences.boundsBottomRight, Preferences.boundsBottomLeft)
log.info("Updated to server's preferences")
class MsgSetBounds(NetMessage):
TYPE = NetMessageType.MSG_SET_BOUNDS
def __init__(self):
self.boundsTopLeft = Preferences.boundsTopLeft
self.boundsTopRight = Preferences.boundsTopRight
self.boundsBottomRight = Preferences.boundsBottomRight
self.boundsBottomLeft = Preferences.boundsBottomLeft
def write(self):
return self.serialize()
class MsgSetColorSettings(NetMessage):
TYPE = NetMessageType.MSG_SET_COLOR_SETTINGS
def __init__(self):
self.totalFadeTimeMS = Preferences.totalFadeTimeMS
self.fixedColorEnabled = Preferences.fixedColorEnabled
rgbColor = colorsys.hsv_to_rgb(Preferences.colorHue/100.0, Preferences.colorSaturation/100.0, Preferences.colorBrightness/100.0)
self.fixedColor = (rgbColor[0], rgbColor[1], rgbColor[2])
self.camSaturation = Preferences.camSaturation
self.camBrightness = Preferences.camBrightness
self.camContrast = Preferences.camContrast
self.camGain = Preferences.camGain
def write(self):
return self.serialize()
NetMessageType.CLASS_LOOKUP = {
NetMessageType.MSG_INVALID : None,
NetMessageType.MSG_GENERIC_REQUEST : MsgGenericRequest,
NetMessageType.MSG_GENERIC_REPONSE : MsgGenericResponse,
NetMessageType.MSG_FRAME_BUFFER : MsgFrameBuffer,
NetMessageType.MSG_PREFERENCES : MsgPreferences,
NetMessageType.MSG_SET_BOUNDS : MsgSetBounds,
NetMessageType.MSG_SET_COLOR_SETTINGS : MsgSetColorSettings
}