-
Notifications
You must be signed in to change notification settings - Fork 6
/
kb_sse.cs
173 lines (155 loc) · 4.45 KB
/
kb_sse.cs
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
using System;
using System.Windows.Forms;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class SSE {
string server_name = "GUARDIAN_SERVER";
string event_name = "RGB_ZONE_";
string address;
System.Timers.Timer timer;
int number_of_zones;
class coreProps {
public string address { get; set; }
}
void get_address() {
string name = Environment.GetEnvironmentVariable("PROGRAMDATA");
name += "/SteelSeries/SteelSeries Engine 3/coreProps.json";
if (File.Exists(name)) {
address = JsonConvert.DeserializeObject<coreProps>(File.ReadAllText(name)).address;
} else {
MessageBox.Show("SteelSeries Engine not found");
Application.Exit();
}
}
string http_post(string command, string data) {
string url = "http://" + address + command;
try {
var client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
return client.UploadString(url, data);
} catch (WebException ex) {
return new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
}
void remove_game() {
var j = new JObject();
j.Add("game", server_name);
http_post("/remove_game", j.ToString());
}
void register_game() {
var j = new JObject();
j.Add("game", server_name);
j.Add("game_display_name", server_name);
j.Add("icon_color_id", 0);
http_post("/game_metadata", j.ToString());
}
JObject json_color(int r, int g, int b) {
JObject j = new JObject();
j.Add("red", r);
j.Add("green", g);
j.Add("blue", b);
return j;
}
JObject json_gradient(int r, int g, int b, int r1, int g1, int b1) {
JObject color = new JObject();
JObject gradient = new JObject();
gradient.Add("zero", json_color(r, g, b));
gradient.Add("hundred", json_color(r1, g1, b1));
color.Add("gradient", gradient);
return color;
}
JObject json_range(int low, int high, JObject color) {
JObject j = new JObject();
j.Add("low", low);
j.Add("high", high);
j.Add("color", color);
return j;
}
void bind_event(string name, string zone, dynamic color) {
var j = new JObject();
var handlers = new JArray();
var handler = new JObject();
j.Add("game", server_name);
j.Add("event", name);
j.Add("min_value", 0);
j.Add("max_value", 511);
j.Add("icon_id", 0);
if (number_of_zones == 5) {
handler.Add("device-type", "rgb-5-zone");
} else if (number_of_zones == 4) {
handler.Add("device-type", "rgb-4-zone");
} else {
handler.Add("device-type", "rgb-3-zone");
}
handler.Add("zone", zone);
handler.Add("mode", "color");
handler.Add("color", color);
handlers.Add(handler);
j.Add("handlers", handlers);
http_post("/bind_game_event", j.ToString());
}
public void set_rgb_zone(int i, string zone) {
var j = new JArray();
int index = 0;
for (int b = 0; b < 8; b += 1) {
for (int g = 0; g < 8; g += 1) {
for (int r = 0; r < 8; r += 1) {
j.Add(json_range(index, index, json_color(r << 5, g << 5, b << 5)));
index += 1;
}
}
}
bind_event(event_name + i, zone, j);
}
public string send_event(string name, int value) {
var j = new JObject();
var data = new JObject();
j.Add("game", server_name);
j.Add("event", name);
data.Add("value", value);
j.Add("data", data);
return http_post("/game_event", j.ToString());
}
public int get_number_of_zones() {
return number_of_zones;
}
public void set_zone_color(int zone, int r, int g, int b) {
r >>= 5;
g >>= 5;
b >>= 5;
send_event(event_name + zone, r | (g << 3) | (b << 6));
}
public void send_ping() {
var j = new JObject();
j.Add("game", server_name);
http_post("/game_heartbeat", j.ToString());
}
public void timer_event(Object source, System.Timers.ElapsedEventArgs e) {
send_ping();
}
public void set_timer() {
timer = new System.Timers.Timer();
timer.Interval = 10000;
timer.Elapsed += timer_event;
timer.AutoReset = true;
timer.Enabled = true;
}
public SSE(int z) {
number_of_zones = z;
get_address();
remove_game();
register_game();
set_rgb_zone(0, "one");
set_rgb_zone(1, "two");
set_rgb_zone(2, "three");
if (number_of_zones == 4) {
set_rgb_zone(3, "four");
} else if (number_of_zones == 5) {
set_rgb_zone(3, "four");
set_rgb_zone(4, "five");
}
set_timer();
}
}