Skip to content

Commit

Permalink
Refactor door action function to trigger announcement if notification…
Browse files Browse the repository at this point in the history
… settings are provided

Leveraging the Voice Monkey API the App can now leverage announcements.
  • Loading branch information
abeckDev committed Sep 21, 2024
1 parent dba5cb5 commit c8717f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions IoTCentralTriggerFunction/DoorActionFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ public static async Task<IActionResult> Run(
var credential = new DefaultAzureCredential();
var token = credential.GetToken(new Azure.Core.TokenRequestContext(new[] { "https://apps.azureiotcentral.com/.default" }));


//Check for notification settings
string announcementTriggerUrl = Environment.GetEnvironmentVariable("Announcement_TriggerUrl");
string announcementToken = Environment.GetEnvironmentVariable("Announcement_Token");
string announcementFlowId = Environment.GetEnvironmentVariable("Announcement_FlowId");


if (!string.IsNullOrEmpty(announcementTriggerUrl)
&& !string.IsNullOrEmpty(announcementToken)
&& !string.IsNullOrEmpty(announcementFlowId))
{
log.LogInformation("Announcement API settings found. Triggering announcement");

//Trigger announcement of Door Action via Voice Monkey
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync($"{announcementTriggerUrl}?token={announcementToken}&flow={announcementFlowId}");
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
log.LogInformation("Announcement request successful. Response: " + responseBody);
}
else
{
log.LogWarning("Annoucement request failed. Status code: " + response.StatusCode);
}
}
}

//Send IoT Central API Call to open/close door
using (var client = new HttpClient())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<UserSecretsId>dfb1bf76-1255-4926-8e22-d06636576ffa</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.2" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
Expand Down

0 comments on commit c8717f7

Please sign in to comment.