Change to signal controller and sound the alarm

This commit is contained in:
Marc Duiker 2023-03-06 17:52:16 +01:00
parent 5cd415dbab
commit cdad270450
5 changed files with 57 additions and 8 deletions

View File

@ -54,13 +54,20 @@ class Program
Console.WriteLine($"Smart device state: {savedData.ToString()}"); Console.WriteLine($"Smart device state: {savedData.ToString()}");
// Show aggregates using controller together with smart devices // Show aggregates using controller together with smart devices
actorId = new ActorId("singleton"); actorId = new ActorId("controller");
actorType = "ControllerActor"; actorType = "ControllerActor";
var proxyController = ActorProxy.Create<IController>(actorId, actorType); var proxyController = ActorProxy.Create<IController>(actorId, actorType);
Console.WriteLine($"Calling GetAverageTemperature on {actorType}:{actorId}..."); Console.WriteLine($"Registering the IDs of both SmartDetectors...");
var avgTemp = await proxyController.GetAverageTemperature(); var controllerData = new ControllerData(){
DeviceIds = new string[]{"1", "2"}
};
await proxyController.RegisterSmokeDetectorsAsync(controllerData);
Console.WriteLine($"Got response: {avgTemp}"); // Smoke detected on device 1
actorType = "SmokeDetectorActor";
actorId = new ActorId("1");
proxySmartDevice = ActorProxy.Create<ISmartDevice>(actorId, actorType);
await proxySmartDevice.DetectSmokeAsync();
} }
} }

View File

@ -5,4 +5,11 @@ public interface IController : IActor
{ {
Task<decimal> GetNetBatteryPercentage(); Task<decimal> GetNetBatteryPercentage();
Task<decimal> GetAverageTemperature(); Task<decimal> GetAverageTemperature();
Task RegisterSmokeDetectorsAsync(ControllerData data);
Task TriggerAlarmForAllDetectors();
}
public class ControllerData
{
public string[] DeviceIds { get; set; } = default!;
} }

View File

@ -5,6 +5,8 @@ public interface ISmartDevice : IActor
{ {
Task<string> SetDataAsync(SmartDeviceData device); Task<string> SetDataAsync(SmartDeviceData device);
Task<SmartDeviceData> GetDataAsync(); Task<SmartDeviceData> GetDataAsync();
Task DetectSmokeAsync();
Task SoundAlarm();
} }
public class SmartDeviceData public class SmartDeviceData

View File

@ -1,3 +1,4 @@
using Dapr.Actors;
using Dapr.Actors.Runtime; using Dapr.Actors.Runtime;
using SmartDevice.Interfaces; using SmartDevice.Interfaces;
@ -61,8 +62,24 @@ internal class ControllerActor : Actor, IController
/// </summary> /// </summary>
protected override Task OnDeactivateAsync() protected override Task OnDeactivateAsync()
{ {
// Provides Opporunity to perform optional cleanup. // Provides opportunity to perform optional cleanup.
Console.WriteLine($"Deactivating actor id: {this.Id}"); Console.WriteLine($"Deactivating actor id: {this.Id}");
return Task.CompletedTask; return Task.CompletedTask;
} }
public async Task RegisterSmokeDetectorsAsync(ControllerData data)
{
await this.StateManager.SetStateAsync<ControllerData>("controllerData", data);
}
public async Task TriggerAlarmForAllDetectors()
{
var data = await StateManager.GetStateAsync<ControllerData>("controllerData");
foreach (var deviceId in data.DeviceIds)
{
var actorId = new ActorId(deviceId);
var proxySmartDevice = ProxyFactory.CreateActorProxy<ISmartDevice>(actorId, "SmartDetectorActor");
await proxySmartDevice.SoundAlarm();
}
}
} }

View File

@ -1,3 +1,4 @@
using Dapr.Actors;
using Dapr.Actors.Runtime; using Dapr.Actors.Runtime;
using SmartDevice.Interfaces; using SmartDevice.Interfaces;
@ -47,7 +48,7 @@ internal class SmokeDetectorActor : Actor, ISmartDevice
// Data is saved to configured state store implicitly after each method execution by Actor's runtime. // Data is saved to configured state store implicitly after each method execution by Actor's runtime.
// Data can also be saved explicitly by calling this.StateManager.SaveStateAsync(); // Data can also be saved explicitly by calling this.StateManager.SaveStateAsync();
// State to be saved must be DataContract serializable. // State to be saved must be DataContract serializable.
await this.StateManager.SetStateAsync<SmartDeviceData>( await StateManager.SetStateAsync<SmartDeviceData>(
"my_data", // state name "my_data", // state name
data); // data saved for the named state "my_data" data); // data saved for the named state "my_data"
@ -58,9 +59,24 @@ internal class SmokeDetectorActor : Actor, ISmartDevice
/// Get MyData from actor's private state store /// Get MyData from actor's private state store
/// </summary> /// </summary>
/// <return>the user-defined MyData which is stored into state store as "my_data" state</return> /// <return>the user-defined MyData which is stored into state store as "my_data" state</return>
public Task<SmartDeviceData> GetDataAsync() public async Task<SmartDeviceData> GetDataAsync()
{ {
// Gets state from the state store. // Gets state from the state store.
return this.StateManager.GetStateAsync<SmartDeviceData>("my_data"); return await StateManager.GetStateAsync<SmartDeviceData>("my_data");
}
public async Task DetectSmokeAsync()
{
// TODO: Set Status to "Alarm"
var controllerActorId = new ActorId("controller");
var controllerActorType = "ControllerActor";
var controllerProxy = ProxyFactory.CreateActorProxy<IController>(controllerActorId, controllerActorType);
await controllerProxy.TriggerAlarmForAllDetectors();
}
public Task SoundAlarm()
{
Console.WriteLine($"ActorId: {this.Id} is sounding the alarm");
return Task.CompletedTask;
} }
} }