Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit 7a9dc7f

Browse files
ap-git-hubsaixiaohui
authored andcommitted
Custom model send telemetry updates (#342)
* Update partition size * Device state * config * space
1 parent 5bbfad0 commit 7a9dc7f

File tree

3 files changed

+73
-39
lines changed

3 files changed

+73
-39
lines changed

SimulationAgent/DeviceTelemetry/DeviceTelemetryActor.cs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,36 +262,60 @@ public void HandleEvent(ActorEvents e)
262262
public void Stop()
263263
{
264264
this.status = ActorStatus.Stopped;
265+
this.log.Debug("Telemetry stopped",
266+
() => new
267+
{
268+
this.deviceId,
269+
Status = this.status.ToString()
270+
});
265271
}
266272

267273
private void Reset()
268274
{
269275
this.status = ActorStatus.ReadyToStart;
270-
}
271-
272-
private void ScheduleTelemetry()
273-
{
274-
// Considering the throttling settings, when can the message be sent
275-
var availableSchedule = Now + this.simulationContext.RateLimiting.GetPauseForNextMessage();
276-
277-
// Looking at the device model, when should the message be sent
278-
// note: this.whenToRun contains the time when the last msg was sent
279-
var optimalSchedule = this.whenToRun + (long) this.Message.Interval.TotalMilliseconds;
280-
281-
// TODO: review this approach: when choosing optimalSchedule the app might overload the hub and cause throttling
282-
this.whenToRun = Math.Max(optimalSchedule, availableSchedule);
283-
this.status = ActorStatus.ReadyToSend;
284-
285-
this.actorLogger.TelemetryScheduled(this.whenToRun);
286-
this.log.Debug("Telemetry scheduled",
276+
this.log.Debug("Telemetry reset",
287277
() => new
288278
{
289279
this.deviceId,
290-
Status = this.status.ToString(),
291-
When = this.log.FormatDate(this.whenToRun)
280+
Status = this.status.ToString()
292281
});
293282
}
294283

284+
private void ScheduleTelemetry()
285+
{
286+
if (this.deviceStateActor.IsDeviceActive)
287+
{
288+
// Considering the throttling settings, when can the message be sent
289+
var availableSchedule = Now + this.simulationContext.RateLimiting.GetPauseForNextMessage();
290+
291+
// Looking at the device model, when should the message be sent
292+
// note: this.whenToRun contains the time when the last msg was sent
293+
var optimalSchedule = this.whenToRun + (long) this.Message.Interval.TotalMilliseconds;
294+
295+
// TODO: review this approach: when choosing optimalSchedule the app might overload the hub and cause throttling
296+
this.whenToRun = Math.Max(optimalSchedule, availableSchedule);
297+
this.status = ActorStatus.ReadyToSend;
298+
299+
this.actorLogger.TelemetryScheduled(this.whenToRun);
300+
this.log.Debug("Telemetry scheduled",
301+
() => new
302+
{
303+
this.deviceId,
304+
Status = this.status.ToString(),
305+
When = this.log.FormatDate(this.whenToRun)
306+
});
307+
}
308+
else
309+
{
310+
this.log.Debug("Device is not active, skipping telemetry send action.",
311+
() => new
312+
{
313+
this.deviceId,
314+
Status = this.status.ToString()
315+
});
316+
}
317+
}
318+
295319
private void PauseForQuotaExceeded()
296320
{
297321
this.status = ActorStatus.WaitingForQuota;

SimulationAgent/DeviceTelemetry/SendTelemetry.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,40 @@ public void Init(IDeviceTelemetryActor context, string deviceId, DeviceModel dev
3535

3636
public async Task RunAsync()
3737
{
38-
var state = this.context.DeviceState.GetAll();
39-
40-
// device could be rebooting, updating firmware, etc.
41-
this.log.Debug("Checking to see if device is online", () => new { this.deviceId });
42-
if (!state.ContainsKey("online") || (bool) state["online"])
38+
try
4339
{
44-
this.log.Debug("The device state says the device is online, sending telemetry...", () => new { this.deviceId });
40+
var state = this.context.DeviceState.GetAll();
41+
42+
// device could be rebooting, updating firmware, etc.
43+
this.log.Debug("Checking to see if device is online", () => new { this.deviceId });
44+
if (!state.ContainsKey("online") || (bool) state["online"])
45+
{
46+
this.log.Debug("The device state says the device is online, sending telemetry...", () => new { this.deviceId });
47+
}
48+
else
49+
{
50+
this.log.Debug("No telemetry will be sent because the device is offline...", () => new { this.deviceId });
51+
this.context.HandleEvent(DeviceTelemetryActor.ActorEvents.TelemetryDelivered);
52+
return;
53+
}
54+
55+
// Inject the device state into the message template
56+
this.log.Debug("Preparing the message content using the device state", () => new { this.deviceId });
57+
var msg = this.message.MessageTemplate;
58+
foreach (var value in state)
59+
{
60+
msg = msg.Replace("${" + value.Key + "}", value.Value.ToString());
61+
}
62+
63+
await this.SendTelemetryMessageAsync(msg);
4564
}
46-
else
65+
catch (Exception e)
4766
{
48-
this.log.Debug("No telemetry will be sent because the device is offline...", () => new { this.deviceId });
49-
this.context.HandleEvent(DeviceTelemetryActor.ActorEvents.TelemetryDelivered);
50-
return;
51-
}
67+
this.log.Error("Unexpected error while sending telemetry",
68+
() => new { this.deviceId, e });
5269

53-
// Inject the device state into the message template
54-
this.log.Debug("Preparing the message content using the device state", () => new { this.deviceId });
55-
var msg = this.message.MessageTemplate;
56-
foreach (var value in state)
57-
{
58-
msg = msg.Replace("${" + value.Key + "}", value.Value.ToString());
70+
this.context.HandleEvent(DeviceTelemetryActor.ActorEvents.TelemetrySendFailure);
5971
}
60-
61-
await this.SendTelemetryMessageAsync(msg);
6272
}
6373

6474
private async Task SendTelemetryMessageAsync(string msg)

WebService/appsettings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ master_lock_duration = 120000
379379
# Note that a number bigger than ~10000 can cause errors when storing partitions
380380
# in Azure Tables due to the records size limit of 1MB.
381381
# Default: 1000, Min: 1, Max: 10000
382-
max_partition_size = 5000
382+
max_partition_size = 3000
383383

384384
# Limit the amount of devices per node
385385
# Default: 20000, Min: 1, Max: 1000000

0 commit comments

Comments
 (0)