mirror of https://github.com/dapr/quickstarts.git
Change to signal controller and sound the alarm
This commit is contained in:
parent
5cd415dbab
commit
cdad270450
|
@ -54,13 +54,20 @@ class Program
|
|||
Console.WriteLine($"Smart device state: {savedData.ToString()}");
|
||||
|
||||
// Show aggregates using controller together with smart devices
|
||||
actorId = new ActorId("singleton");
|
||||
actorId = new ActorId("controller");
|
||||
actorType = "ControllerActor";
|
||||
var proxyController = ActorProxy.Create<IController>(actorId, actorType);
|
||||
|
||||
Console.WriteLine($"Calling GetAverageTemperature on {actorType}:{actorId}...");
|
||||
var avgTemp = await proxyController.GetAverageTemperature();
|
||||
Console.WriteLine($"Registering the IDs of both SmartDetectors...");
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,4 +5,11 @@ public interface IController : IActor
|
|||
{
|
||||
Task<decimal> GetNetBatteryPercentage();
|
||||
Task<decimal> GetAverageTemperature();
|
||||
Task RegisterSmokeDetectorsAsync(ControllerData data);
|
||||
Task TriggerAlarmForAllDetectors();
|
||||
}
|
||||
|
||||
public class ControllerData
|
||||
{
|
||||
public string[] DeviceIds { get; set; } = default!;
|
||||
}
|
|
@ -5,6 +5,8 @@ public interface ISmartDevice : IActor
|
|||
{
|
||||
Task<string> SetDataAsync(SmartDeviceData device);
|
||||
Task<SmartDeviceData> GetDataAsync();
|
||||
Task DetectSmokeAsync();
|
||||
Task SoundAlarm();
|
||||
}
|
||||
|
||||
public class SmartDeviceData
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using Dapr.Actors;
|
||||
using Dapr.Actors.Runtime;
|
||||
using SmartDevice.Interfaces;
|
||||
|
||||
|
@ -61,8 +62,24 @@ internal class ControllerActor : Actor, IController
|
|||
/// </summary>
|
||||
protected override Task OnDeactivateAsync()
|
||||
{
|
||||
// Provides Opporunity to perform optional cleanup.
|
||||
// Provides opportunity to perform optional cleanup.
|
||||
Console.WriteLine($"Deactivating actor id: {this.Id}");
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using Dapr.Actors;
|
||||
using Dapr.Actors.Runtime;
|
||||
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 can also be saved explicitly by calling this.StateManager.SaveStateAsync();
|
||||
// State to be saved must be DataContract serializable.
|
||||
await this.StateManager.SetStateAsync<SmartDeviceData>(
|
||||
await StateManager.SetStateAsync<SmartDeviceData>(
|
||||
"my_data", // state name
|
||||
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
|
||||
/// </summary>
|
||||
/// <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.
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue