Skip to content

Commit aee1198

Browse files
authored
Merge pull request #10 from PepperDash/9-bug-single-http-client-per-device
fix: single http client per device
2 parents 0a81043 + 998eceb commit aee1198

2 files changed

Lines changed: 42 additions & 56 deletions

File tree

src/LightingGatewayDevice.cs

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,21 @@
1111
using PepperDash.Essentials.Core.Bridges;
1212
using PepperDash.Essentials.Core.Config;
1313
using PepperDash.Essentials.Core.Lighting;
14-
using PepperDash.Essentials.Core.Queues;
1514
using RequestType = Crestron.SimplSharp.Net.Http.RequestType;
1615
using Timeout = System.Threading.Timeout;
1716

1817
namespace PoeTexasCorTap
1918
{
2019
public class LightingGatewayDevice : LightingBase, ICommunicationMonitor, IOnline
2120
{
21+
public readonly HttpClient Client = new HttpClient { KeepAlive = true };
2222
public readonly string Url;
2323
public readonly string FixtureName;
2424

2525
private int _currentLevel;
2626
private readonly LightingGatewayQueue _dispatchQueue;
2727
private readonly CTimer _levelPoll;
2828

29-
private static readonly Dictionary<string, CTimer> PollTimers =
30-
new Dictionary<string, CTimer>(StringComparer.OrdinalIgnoreCase);
31-
3229
public readonly IntFeedback CurrentLevelFeedback;
3330

3431
public LightingGatewayDevice(DeviceConfig config) : base(config.Key, config.Name)
@@ -65,10 +62,8 @@ public void Poll()
6562
try
6663
{
6764
var request = GetRequestForInfo(Url);
68-
using (var client = new HttpClient())
69-
using (var response = client.Dispatch(request))
70-
{
71-
Debug.Console(
65+
var response = Client.Dispatch(request);
66+
Debug.Console(
7267
1,
7368
this,
7469
"Dispatched a level poll");
@@ -79,19 +74,18 @@ public void Poll()
7974
"Response: {0}",
8075
response.ContentString ?? String.Empty);
8176

82-
var data = JsonConvert.DeserializeObject<List<LightingDeviceState>>(response.ContentString);
83-
data.Where(d => d.Name.Equals(FixtureName, StringComparison.OrdinalIgnoreCase))
84-
.ToList()
85-
.ForEach(
86-
d =>
87-
{
88-
var result = d.Level ?? default (int);
89-
_currentLevel = (int) result;
90-
91-
Debug.Console(1, this, "Setting current level:{0}", _currentLevel);
92-
CurrentLevelFeedback.FireUpdate();
93-
});
94-
}
77+
var data = JsonConvert.DeserializeObject<List<LightingDeviceState>>(response.ContentString);
78+
data.Where(d => d.Name.Equals(FixtureName, StringComparison.OrdinalIgnoreCase))
79+
.ToList()
80+
.ForEach(
81+
d =>
82+
{
83+
var result = d.Level ?? default (int);
84+
_currentLevel = (int) result;
85+
86+
Debug.Console(1, this, "Setting current level:{0}", _currentLevel);
87+
CurrentLevelFeedback.FireUpdate();
88+
});
9589
}
9690
catch (HttpsException ex)
9791
{
@@ -194,19 +188,16 @@ public override void SelectScene(LightingScene scene)
194188
scene.Name);
195189

196190
var request = scene.GetRequestForScene(Url);
197-
using (var client = new HttpClient())
198-
using (var response = client.Dispatch(request))
199-
{
200-
Debug.Console(
201-
2,
202-
this,
203-
"SelectScene: Dispatched a scene request: {0} | Response: {1}",
204-
request.Url.PathAndParams,
205-
response.Code);
191+
var response = Client.Dispatch(request);
192+
Debug.Console(
193+
2,
194+
this,
195+
"SelectScene: Dispatched a scene request: {0} | Response: {1}",
196+
request.Url.PathAndParams,
197+
response.Code);
206198

207-
if (response.Code != 200)
208-
throw new HttpException(response);
209-
}
199+
if (response.Code != 200)
200+
throw new HttpException(response);
210201
}
211202
catch (Exception ex)
212203
{
@@ -218,15 +209,14 @@ public override void SelectScene(LightingScene scene)
218209
ex.Message,
219210
ex.StackTrace);
220211
}
221-
}
222-
);
212+
});
223213

224214
_levelPoll.Reset(0, 5000);
225215
}
226216

227217
public void SetLoadLevel(ushort level)
228218
{
229-
_dispatchQueue.Enqueue(() => DispatchLevelRequest(Url, FixtureName, level));
219+
_dispatchQueue.Enqueue(() => DispatchLevelRequest(Url, FixtureName, level, Client));
230220
_levelPoll.Reset(100, 5000);
231221
}
232222

@@ -260,19 +250,16 @@ public static HttpClientRequest GetRequestForLevelSet(string url, string name, i
260250
return request;
261251
}
262252

263-
public static void DispatchLevelRequest(string url, string fixtureName, int requestedLevel)
253+
public static void DispatchLevelRequest(string url, string fixtureName, int requestedLevel, HttpClient client)
264254
{
265255
var request = GetRequestForLevelSet(url, fixtureName, requestedLevel);
266-
using (var client = new HttpClient())
267-
using (var response = client.Dispatch(request))
268-
{
269-
var responseString = response.ContentString;
270-
Debug.Console(
271-
2,
272-
"Dispatched a level request:{0}: Response: {1}",
273-
requestedLevel,
274-
responseString ?? String.Empty);
275-
}
256+
var response = client.Dispatch(request);
257+
var responseString = response.ContentString;
258+
Debug.Console(
259+
2,
260+
"Dispatched a level request:{0}: Response: {1}",
261+
requestedLevel,
262+
responseString ?? String.Empty);
276263
}
277264

278265
public StatusMonitorBase CommunicationMonitor { get; private set; }

src/LightingGatewayStatusMonitor.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class LightingGatewayStatusMonitor : StatusMonitorBase
1515
private readonly CTimer _pollTimer;
1616
private static GenericQueue _queue;
1717

18+
public readonly HttpClient Client = new HttpClient {KeepAlive = true};
19+
1820
class ActionQueueMessage : IQueueMessage
1921
{
2022
public Action DispatchAction { get; set; }
@@ -66,13 +68,12 @@ public void Poll()
6668
try
6769
{
6870
var request = GetPollRequest(_url);
69-
using (var client = new HttpClient())
70-
using (var response = client.Dispatch(request))
71-
{
72-
if (response.Code != 200) return;
73-
Status = MonitorStatus.IsOk;
74-
ResetErrorTimers();
75-
}
71+
var response = Client.Dispatch(request);
72+
if (response.Code != 200)
73+
return;
74+
75+
Status = MonitorStatus.IsOk;
76+
ResetErrorTimers();
7677
}
7778
catch (HttpException ex)
7879
{
@@ -89,8 +90,6 @@ public static HttpClientRequest GetPollRequest(string url)
8990
var request = new HttpClientRequest { RequestType = RequestType.Get };
9091

9192
request.Header.SetHeaderValue("accept", "application/json");
92-
request.FinalizeHeader();
93-
9493
request.Url.Parse("http://" + url + "/v2/config/version");
9594
return request;
9695
}

0 commit comments

Comments
 (0)