Skip to content

Commit

Permalink
Added topic for default device volume
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanbujdea committed Mar 31, 2021
1 parent 0308904 commit 258dc40
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/AudioCommands/Service/AudioCommandsService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Timers;
using AudioCommands.Helpers;
using IOTLinkAPI.Addons;
using IOTLinkAPI.Helpers;
using IOTLinkAPI.Platform.Events.MQTT;
using IOTLinkAPI.Platform.HomeAssistant;

namespace AudioCommands.Service
{
public class AudioCommandsService : ServiceAddon
{
private Timer _monitorTimer;
private string _audioTopic = "audio/volume";

public override void Init(IAddonManager addonManager)
{
base.Init(addonManager);
Expand All @@ -16,6 +21,33 @@ public override void Init(IAddonManager addonManager)
GetManager().SubscribeTopic(this, "audio/mute", OnAudioMuteMessage);
GetManager().SubscribeTopic(this, "audio/default", OnAudioSetDefaultMessage);
GetManager().SubscribeTopic(this, "audio/default-comms", OnAudioSetDefaultCommsMessage);


GetManager().PublishDiscoveryMessage(this, _audioTopic, "Volume", new HassDiscoveryOptions
{
Id = "Volume",
Name = "Volume",
Component = HomeAssistantComponent.Sensor,
Icon = "mdi:volume-high"
});
_monitorTimer = new Timer();
_monitorTimer.Interval = 10000;
_monitorTimer.Elapsed += TimerElapsed;
_monitorTimer.Start();
}

private void TimerElapsed(object sender, ElapsedEventArgs e)
{
try
{
var volume = AudioPlatformHelper.GetAudioDeviceInfo(Guid.Empty).Volume;
LoggerHelper.Info($"Current volume is {volume}");
GetManager().PublishMessage(this, _audioTopic, volume.ToString());
}
catch (Exception exception)
{
LoggerHelper.Error("Failed to send volume " + exception);
}
}

private async void OnAudioVolumeSetMessage(object sender, MQTTMessageEventEventArgs e)
Expand Down

0 comments on commit 258dc40

Please sign in to comment.