|
| 1 | +import requests |
| 2 | +import threading, time |
| 3 | +api_endpoint = "http://localhost:54235/razer/chromasdk" |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +init_obj = { |
| 8 | + "title": "Razer Chroma SDK RESTful Test Application", |
| 9 | + "description": "This is a REST interface test application", |
| 10 | + "author": { |
| 11 | + "name": "Chroma Developer", |
| 12 | + "contact": "www.razerzone.com" |
| 13 | + }, |
| 14 | + "device_supported": [ |
| 15 | + "keyboard", |
| 16 | + "mouse", |
| 17 | + "headset", |
| 18 | + "mousepad", |
| 19 | + "keypad", |
| 20 | + "chromalink"], |
| 21 | + "category": "application" |
| 22 | +} |
| 23 | + |
| 24 | +resp = requests.post(api_endpoint, json=init_obj) |
| 25 | +resp.json()['uri'] |
| 26 | + |
| 27 | +class Razer_api_session(): |
| 28 | + keepalive = True |
| 29 | + session_id = None |
| 30 | + session = None |
| 31 | + beat_count = None |
| 32 | + api_uri= None |
| 33 | + def __init__(self): |
| 34 | + init_api_response = requests.post(api_endpoint, json=init_obj) |
| 35 | + self.session_id = init_api_response.json()['sessionid'] |
| 36 | + self.api_uri = init_api_response.json()['uri'] |
| 37 | + self.session = requests.Session() |
| 38 | + self.session.post(self.api_uri) |
| 39 | + self.keepalive = True |
| 40 | + |
| 41 | + |
| 42 | + def heartbeat(self): |
| 43 | + self.heartbeat_uri = self.api_uri+"/heartbeat" |
| 44 | + print(self.heartbeat_uri) |
| 45 | + while self.keepalive is True: |
| 46 | + resp = requests.put(self.heartbeat_uri) |
| 47 | + self.beat_count = resp.json()['tick'] |
| 48 | + time.sleep(5) |
| 49 | + print("keepalive is false, loop stopped") |
| 50 | + return |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +color_test = { |
| 56 | + "effect": "CHROMA_STATIC", |
| 57 | + "param": { |
| 58 | + "color": 0 |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +color_test2 = { |
| 63 | + "effect":"CHROMA_CUSTOM_KEY", |
| 64 | + "param":{ |
| 65 | + "color":[ |
| 66 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 67 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 68 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 69 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 70 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 71 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 72 | + ], |
| 73 | + "key":[ |
| 74 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 75 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 76 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 77 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 78 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 79 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 80 | + ] |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +test = {'ESC': [0, 1], 'F1': [0, 3], 'F2': [0, 4], 'F3': [0, 5], 'F4': [0, 6], 'F5': [0, 7], 'F6': [0, 8], 'F7': [0, 9], 'F8': [0, 10], 'F9': [0, 11], 'F10': [0, 12], 'F11': [0, 13], 'F12': [0, 14], 'PRT_SC': [0, 15], 'SCR_LK': [0, 16], 'PAUSE': [0, 17], 'UNUSED': [0, 18], 'UNSD': [0, 19], 'LOGO': [0, 20], 'MACRO_1': [1, 0], 'SECTION': [1, 1], '1': [1, 2], '2': [1, 3], '3': [1, 4], '4': [1, 5], '5': [1, 6], '6': [1, 7], '7': [1, 8], '8': [1, 9], '9': [1, 10], '0': [1, 11], '+': [1, 12], 'TILDA': [1, 13], 'BACKSPACE': [1, 14], 'INSERT': [1, 15], 'HOME': [1, 16], 'PAGE_UP': [1, 17], 'NUM_LK': [1, 18], 'DIVIDE': [1, 19], 'MULTIPLY': [1, 20], 'MINUS': [1, 21], 'MACRO_2': [2, 0], 'TAB': [2, 1], 'Q': [2, 2], 'W': [2, 3], 'E': [2, 4], 'R': [2, 5], 'T': [2, 6], 'Y': [2, 7], 'U': [2, 8], 'I': [2, 9], 'O': [2, 10], 'P': [2, 11], 'Å': [2, 12], '^': [2, 13], "'": [2, 14], 'DEL': [2, 15], 'END': [2, 16], 'PAGE_DOWN': [2, 17], 'NUM_7': [2, 18], 'NUM_8': [2, 19], 'NUM_9': [2, 20], 'NUM_PLUS': [2, 21], 'MACRO_3': [3, 0], 'CAPS_LOCK': [3, 1], 'A': [3, 2], 'S': [3, 3], 'D': [3, 4], |
| 85 | + 'F': [3, 5], 'G': [3, 6], 'H': [3, 7], 'J': [3, 8], 'K': [3, 9], 'L': [3, 10], 'Ö': [3, 11], 'Ä': [3, 12], 'MYSTERY': [3, 13], 'ENTER': [3, 14], 'UNKWN': [3, 15], 'BLANK': [3, 16], 'EMPTY': [3, 17], 'NUM_4': [3, 18], 'NUM_5': [3, 19], 'NUM_6': [3, 20], 'NUM_UNKWN': [3, 21], 'MACRO_4': [4, 0], 'L_SHIFT': [4, 1], '<': [4, 2], 'Z': [4, 3], 'X': [4, 4], 'C': [4, 5], 'V': [4, 6], 'B': [4, 7], 'N': [4, 8], 'M': [4, 9], ',': [4, 10], '.': [4, 11], '_': [4, 12], 'UNKWN_s': [4, 13], 'R_SHIFT': [4, 14], 'EMPTY_2': [4, 15], 'UP': [4, 16], 'BLANK_2': [4, 17], 'NUM_1': [4, 18], 'NUM_2': [4, 19], 'NUM_3': [4, 20], 'NUM_ENTER': [4, 21], 'MACRO_5': [5, 0], 'L_CTRL': [5, 1], 'WINDOWS': [5, 2], 'L_ALT': [5, 3], 'SPACE_UNKWN': [5, 4], 'SPACE_UNKWN2': [5, 5], 'SPACE_UNKWN3': [5, 6], 'SPACEBAR': [5, 7], 'SPACE_UNKWN4': [5, 8], 'SPACE_UNKWN5': [5, 9], 'SPACE_UNKWN6': [5, 10], 'ALT_GR': [5, 11], 'FN': [5, 12], 'LIST': [5, 13], 'R_CTRL': [5, 14], 'LEFT': [5, 15], 'DOWN': [5, 16], 'RIGHT': [5, 17], 'RIGHT_2': [5, 18], 'NUM_0': [5, 19], 'NUM_DEL': [5, 20], 'NUM_UNK3': [5, 21]} |
| 86 | +from cmd import Cmd |
| 87 | + |
| 88 | + |
| 89 | +def set_WASD(color_scheme): |
| 90 | + scheme = color_scheme |
| 91 | + wasd=['W','A','S','D'] |
| 92 | + color=getIfromRGBReverse([255,0,0]) |
| 93 | + for key in wasd: |
| 94 | + scheme = set_color(key,color,scheme) |
| 95 | + return scheme |
| 96 | + |
| 97 | + |
| 98 | +def get_colors(ind): |
| 99 | + index=test[ind] |
| 100 | + keys=color_test2['param']['key'] |
| 101 | + value=keys[index[0]][index[1]] |
| 102 | + print(value) |
| 103 | + |
| 104 | + |
| 105 | +def find_key(row, column): |
| 106 | + new_color = color_test2 |
| 107 | + new_color['param']['key'][int(row)][int(column)] = ~16776960 |
| 108 | + return new_color |
| 109 | + |
| 110 | +def set_color(key, color_rgb, original_color): |
| 111 | + print(color_rgb) |
| 112 | + index=test[key] |
| 113 | + new_color = original_color |
| 114 | + new_color['param']['key'][index[0]][index[1]] = ~color_rgb |
| 115 | + return new_color |
| 116 | + |
| 117 | +def getIfromRGB(rgb): |
| 118 | + print(rgb) |
| 119 | + red = rgb[2] |
| 120 | + green = rgb[1] |
| 121 | + blue = rgb[0] |
| 122 | + RGBint = (red<<16) + (green<<8) + blue |
| 123 | + print(RGBint) |
| 124 | + return RGBint |
| 125 | + |
| 126 | +def getIfromRGBReverse(rgb): |
| 127 | + print(rgb) |
| 128 | + red = -rgb[2] - 255 |
| 129 | + green = -rgb[1] - 255 |
| 130 | + blue = -rgb[0] - 255 |
| 131 | + RGBint = (red<<16) + (green<<8) + blue |
| 132 | + print(RGBint) |
| 133 | + return RGBint |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +class MyPrompt(Cmd): |
| 138 | + api_session = None |
| 139 | + thread = None |
| 140 | + global new_color |
| 141 | + def do_init(self, inp): |
| 142 | + '''Initialize connection to Razer Synapse API.''' |
| 143 | + print("Initializing connection to api...") |
| 144 | + self.api_session = Razer_api_session() |
| 145 | + self.thread = threading.Thread(target=self.api_session.heartbeat) |
| 146 | + self.thread.start() |
| 147 | + |
| 148 | + def do_quit(self,inp): |
| 149 | + '''Disconnect and quit''' |
| 150 | + print("Stopping keepalive loop.") |
| 151 | + self.api_session.keepalive = False |
| 152 | + response = requests.delete(self.api_session.api_uri) |
| 153 | + print(response.json()) |
| 154 | + return True |
| 155 | + |
| 156 | + def do_check(self,inp): |
| 157 | + '''Check Current heartbeat status.''' |
| 158 | + print("Current heartbeat count is:") |
| 159 | + print(self.api_session.beat_count) |
| 160 | + |
| 161 | + def do_effect(self,inp): |
| 162 | + resp = requests.put(self.api_session.api_uri+"/keyboard", json=color_test2) |
| 163 | + print(resp.json()) |
| 164 | + |
| 165 | + def do_color(self,inp): |
| 166 | + rgb=list(map(int, inp.split(','))) |
| 167 | + print(rgb) |
| 168 | + color_test['param']['color'] = (getIfromRGB(rgb)) |
| 169 | + resp = requests.put(self.api_session.api_uri+"/keyboard", json=color_test) |
| 170 | + print(resp.json()) |
| 171 | + |
| 172 | + def do_wasd(self,inp): |
| 173 | + data = set_WASD(color_test2) |
| 174 | + resp = requests.put(self.api_session.api_uri+"/keyboard", json=data) |
| 175 | + print(resp.json()) |
| 176 | + |
| 177 | + def do_write(self,inp): |
| 178 | + global test |
| 179 | + File_object = open("key_map.json","a+") |
| 180 | + File_object.write(str(test)) |
| 181 | + File_object.close() |
| 182 | + |
| 183 | + def do_set(self,inp): |
| 184 | + global new_color |
| 185 | + args = inp.split(' ') |
| 186 | + new_color = color_test2 |
| 187 | + rgb = list(map(int, args[1].split(','))) |
| 188 | + new_rgb = getIfromRGBReverse(rgb) |
| 189 | + print(args) |
| 190 | + print(rgb) |
| 191 | + data = set_color(args[0],new_rgb, color_test2) |
| 192 | + print(data) |
| 193 | + resp = requests.put(self.api_session.api_uri+"/keyboard", json=data) |
| 194 | + print(resp.json()) |
| 195 | + |
| 196 | + def do_test(self,inp): |
| 197 | + print(inp) |
| 198 | + |
| 199 | + def do_map(self,inp): |
| 200 | + i=-1 |
| 201 | + for row in color_test2['param']['key']: |
| 202 | + i = i+1 |
| 203 | + if i >= 2: |
| 204 | + y=0 |
| 205 | + for column in row: |
| 206 | + if i ==2 and y >=7 or i > 2: |
| 207 | + print("new key") |
| 208 | + data=find_key(i,y) |
| 209 | + resp = requests.put(self.api_session.api_uri+"/keyboard", json=data) |
| 210 | + print(resp.json()) |
| 211 | + print("Key at " + str(i) + ',' + str(y) + " turned on") |
| 212 | + print("What is this key called?") |
| 213 | + key_name = input('Key name: ') |
| 214 | + test[key_name] = [i,y] |
| 215 | + print(str(i)+ ','+ str(y)) |
| 216 | + y=y+1 |
| 217 | + |
| 218 | + def do_find(self, inp): |
| 219 | + global test |
| 220 | + location = list(map(int, inp.split(','))) |
| 221 | + data = find_key(location[0], location[1]) |
| 222 | + resp = requests.put(self.api_session.api_uri+"/keyboard", json=data) |
| 223 | + print(resp.json()) |
| 224 | + print("Key at " + str(location) + "turned on") |
| 225 | + print("What is this key called?") |
| 226 | + key_name = input('Key name: ') |
| 227 | + test[key_name] = location |
| 228 | + |
| 229 | + def do_print(self,inp): |
| 230 | + global test |
| 231 | + print(test) |
| 232 | + |
| 233 | + |
| 234 | +def menu(): |
| 235 | + MyPrompt().cmdloop() |
| 236 | + |
| 237 | +if __name__ == '__main__': |
| 238 | + menu() |
| 239 | + |
| 240 | + |
| 241 | + |
0 commit comments