-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
330 lines (289 loc) · 12.8 KB
/
Form1.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
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
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using Brushes = System.Windows.Media.Brushes;
namespace TraegerMon
{
public partial class Form1 : Form
{
List<TraegerDevice> Devices = null;
AccountInfo Account = null;
MqttClient client = null;
TraegerDevice CurrentDevice = null;
LineSeries AmbientTempSeries = null;
LineSeries ProbeTempSeries = null;
public Form1()
{
InitializeComponent();
InitChart();
}
private void BtnLogin_Click(object sender, EventArgs e)
{
Account = new AccountInfo();
WebClient wc = new WebClient();
wc.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(txtUsername.Text + ":" + txtPassword.Text));
wc.Headers["Content-Type"] = "application/json; charset=UTF-8";
wc.Headers["User-Agent"] = "Traeger/android";
wc.Headers["x-dw-client-id"] = System.Environment.GetEnvironmentVariable("TRAEGER_CLIENT_ID",EnvironmentVariableTarget.Machine);
var authResp = wc.UploadString("https://production-us-traegergrills.demandware.net/s/Sites-Traeger-Site/dw/shop/v17_1/customers/auth", "{\"type\": \"credentials\"}");
var resp = JObject.Parse(authResp);
Account.CustomerId = resp["customer_id"].ToString();
Account.Email = resp["email"].ToString();
Account.FirstName = resp["first_name"].ToString();
Account.LastName = resp["last_name"].ToString();
/* Create traeger.io piece */
var body = $"{{\"demandwareId\":\"{Account.CustomerId}\",\"email\":\"{Account.Email}\",\"firstname\":\"{Account.FirstName}\",\"lastname\":\"{Account.LastName}\"}}";
/* generate hmac */
StringBuilder sb = new StringBuilder();
sb.Append("POST\n");
sb.Append("https\n");
sb.Append("api.traegergrills.io:443\n");
sb.Append("/v1/user/authenticate\n");
sb.Append("application/json; charset=UTF-8\n");
sb.Append("user\n");
sb.Append(body);
sb.Append("\n");
var hmacBody = sb.ToString();
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(System.Environment.GetEnvironmentVariable("TRAEGER_HMAC_KEY",EnvironmentVariableTarget.Machine)));
var hmacString = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(hmacBody)));
wc = new WebClient();
wc.Headers["Authorization"] = "HmacSHA256 user:" + hmacString;
wc.Headers["Content-Type"] = "application/json; charset=UTF-8";
wc.Headers["User-Agent"] = "okhttp/3.6.0";
var ioResp = wc.UploadString("https://api.traegergrills.io/v1/user/authenticate",body);
var resp2 = JObject.Parse(ioResp);
Account.AccountId = resp2["accountId"].ToString();
Account.Password = resp2["password"].ToString();
/* Xively JWT login */
/* https://id.xively.com/api/v1/auth/login-user */
wc = new WebClient();
wc.Headers["Content-Type"] = "application/json; charset=UTF-8";
wc.Headers["User-Agent"] = "okhttp/3.6.0";
body = $"{{\"accountId\": \"{Account.AccountId}\",\"emailAddress\": \"{Account.Email}\", \"password\": \"{Account.Password}\"}}";
var xivelyAuth = wc.UploadString("https://id.xively.com/api/v1/auth/login-user", body);
var resp3 = JObject.Parse(xivelyAuth);
Account.JWT = resp3["jwt"].ToString();
/* Device List */
var deviceAPI = $"https://blueprint.xively.com/api/v1/devices?accountId={Account.AccountId}&meta=true&results=true&page=1&pageSize=999";
wc = new WebClient();
wc.Headers["Authorization"] = "Bearer " + Account.JWT;
wc.Headers["Content-Type"] = "application/json; charset=UTF-8";
wc.Headers["User-Agent"] = "okhttp/3.6.0";
var deviceJSON = wc.DownloadString(deviceAPI);
var deviceResp = JObject.Parse(deviceJSON);
JArray deviceList = deviceResp["devices"].Value<JArray>("results");
if (deviceList.Count > 0)
{
Devices = new List<TraegerDevice>();
for (var x = 0; x < deviceList.Count; x++)
{
Devices.Add(new TraegerDevice() { ID = deviceList[x]["id"].ToString(), Name = deviceList[x]["name"].ToString() });
}
}
UpdateUI();
}
private void UpdateUI()
{
if (Account != null)
{
lblCustID.Text = Account.CustomerId;
lblEmail.Text = Account.Email;
lblFirstName.Text = Account.FirstName;
lblLastName.Text = Account.LastName;
}
cmbDevices.Items.Clear();
if (Devices != null)
{
foreach(var d in Devices)
{
cmbDevices.Items.Add(d.Name);
}
}
}
private async void ConnectDevice()
{
if (cmbDevices.SelectedIndex >= 0)
{
CurrentDevice = Devices[cmbDevices.SelectedIndex];
client = new MqttClient("broker.xively.com", 8883, true, MqttSslProtocols.TLSv1_2, null, null);
client.Connect(CurrentDevice.ID, "Auth:JWT", Account.JWT);
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
client.Subscribe(new string[] { $"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/grill_data" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { $"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/_log" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { $"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/_set/fields" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { $"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/_updates/fields" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
}
private void Client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
{
var topic = e.Topic;
var msgData = Encoding.UTF8.GetString(e.Message);
if (e.Topic.EndsWith("_log") || e.Topic.EndsWith("fields"))
{
int i = 3;
Console.WriteLine(msgData);
return;
}
JObject msg = JObject.Parse(msgData);
JArray elems = msg.Value<JArray>("e");
JObject data = new JObject();
for(var x = 0; x < elems.Count; x++)
{
data[elems[x].Value<string>("n")] = elems[x].Value<string>("v");
}
CurrentDevice.CurrentTemp = data.Value<int>("grill");
CurrentDevice.SetTemp = data.Value<int>("set");
CurrentDevice.ProbeTemp = data.Value<int>("probe");
CurrentDevice.FanLevel = data.Value<int>("fan");
CurrentDevice.CookTimerEnd = data.Value<long>("cook_timer_end");
CurrentDevice.SystemTimerEnd = data.Value<long>("sys_timer_end");
CurrentDevice.SystemStatus = data.Value<int>("system_status");
AmbientTempSeries.Values.Add(new DataFrame() { Time = DateTime.Now, CurrentTemp = CurrentDevice.CurrentTemp });
ProbeTempSeries.Values.Add(new DataFrame() { Time = DateTime.Now, CurrentTemp = CurrentDevice.ProbeTemp });
this.BeginInvoke((MethodInvoker)delegate { UpdateDeviceInfo(); });
}
private void UpdateDeviceInfo()
{
lblCurrent.Text = CurrentDevice.CurrentTemp + " F";
lblTarget.Text = CurrentDevice.SetTemp + " F";
lblProbe.Text = CurrentDevice.ProbeTemp + " F";
prgFan.Value = CurrentDevice.FanLevel;
lblSmoke.Text = CurrentDevice.Smoke == 1 ? "Yes" : "No";
if (CurrentDevice.SystemTimerEnd > 0)
{
/* figure out when it is... */
var timeLeft = DateTimeOffset.FromUnixTimeSeconds(CurrentDevice.SystemTimerEnd).LocalDateTime.Subtract(DateTime.Now);
lblSystemTimer.Text = timeLeft.ToString(@"dd\.hh\:mm\:ss");
} else
{
lblSystemTimer.Text = "";
}
if (CurrentDevice.CookTimerEnd > 0)
{
/* figure out when it is... */
var timeLeft = DateTimeOffset.FromUnixTimeSeconds(CurrentDevice.CookTimerEnd).LocalDateTime.Subtract(DateTime.Now);
lblCookTimer.Text = timeLeft.ToString(@"dd\.hh\:mm\:ss");
} else
{
lblCookTimer.Text = "";
}
switch(CurrentDevice.SystemStatus)
{
case 2:
lblStatus.Text = "Sleeping";
break;
case 3:
lblStatus.Text = "Idle";
break;
case 4:
lblStatus.Text = "Igniting";
break;
case 5:
lblStatus.Text = "Preheating";
break;
case 6:
lblStatus.Text = "Manual Cook";
break;
case 7:
lblStatus.Text = "Custom Cook";
break;
case 8:
lblStatus.Text = "Cooldown";
break;
case 9:
lblStatus.Text = "Shutdown";
break;
case 10:
lblStatus.Text = "Error";
break;
case 99:
lblStatus.Text = "Offline";
break;
}
/* Device Status */
/* 2 == Sleep */
/* 3 == Idle */
/* 4 == Ignite Status */
/* 5 == Preheat */
/* 6 == Manual Cook */
/* 7 == Custom Cook */
/* 8 == Cooldown */
/* 9 == Shutdown */
/* 10 == Error */
/* 99 == Offline */
}
private void InitChart()
{
var dayConfig = Mappers.Xy<DataFrame>()
.X(model => (double)model.Time.Ticks / TimeSpan.FromHours(1).Ticks)
.Y(model => model.CurrentTemp);
AmbientTempSeries = new LineSeries
{
Values = new ChartValues<DataFrame>
{
},
PointGeometrySize = 0,
Title = "Ambient"
};
ProbeTempSeries = new LineSeries
{
Values = new ChartValues<DataFrame>
{
},
PointGeometrySize = 0,
Title = "Probe"
};
cartesianChart1.Series = new SeriesCollection(dayConfig)
{
AmbientTempSeries,
ProbeTempSeries
};
cartesianChart1.AxisX.Add(new Axis
{
LabelFormatter = value => new System.DateTime((long)(value * TimeSpan.FromHours(1).Ticks)).ToString("t")
});
cartesianChart1.AxisY.Add(new Axis { LabelFormatter = value => value + " F" });
cartesianChart1.LegendLocation = LegendLocation.Right;
}
private void BtnConnect_Click(object sender, EventArgs e)
{
ConnectDevice();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
client?.Disconnect();
}
private void Button1_Click(object sender, EventArgs e)
{
if (CurrentDevice.Smoke == 0)
{
client.Publish($"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/run_cmd", Encoding.UTF8.GetBytes("16,165"));
}else
{
client.Publish($"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/run_cmd", Encoding.UTF8.GetBytes("21"));
}
}
private void Button1_Click_1(object sender, EventArgs e)
{
client.Publish($"xi/blue/v1/{Account.AccountId}/d/{CurrentDevice.ID}/run_cmd", Encoding.UTF8.GetBytes("1"));
}
}
}