1
0
Fork 0
Browse Source

implement a audio notification feature (for status changes).

The changes here are a completion of a previous P.O.C. commit. When the program detects a change in the status of a device (I.E. 'on' to 'off' and vice-versa), Eyes and Ears will play an text-to-speech phrase to indicate the change.
stable
Craig Oates 3 years ago
parent
commit
a74774da5b
  1. 6
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml
  2. 26
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs
  3. 2
      src/EyesAndEars.UWP/EyesAndEars.UWP/Package.appxmanifest
  4. 34
      src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs

6
src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml

@ -28,7 +28,7 @@
<StackPanel Grid.Row="0" Orientation="Horizontal"
VerticalAlignment="Top">
<MediaElement Name="AudioUpdater" AutoPlay="False"/>
<MediaElement Name="AudioUpdater" AutoPlay="True"/>
<Image Grid.Column="0" Width="88" Height="88" VerticalAlignment="Center"
Source="Images\logo.png"/>
<StackPanel Padding="8,0">
@ -225,15 +225,13 @@
<StackPanel Orientation="Horizontal" Grid.Column="0">
<ToggleButton x:Name="LogToggle" FontFamily="Segoe MDL2 Assets"
<ToggleButton x:Name="LogToggle" FontFamily="Segoe MDL2 Assets"
Content="&#xE8BC;" HorizontalAlignment="Right"
VerticalAlignment="Top" FontSize="28"
Height="79" Width="79"
Unchecked="LogToggle_Unchecked"
Checked="LogToggle_Checked"
IsChecked="True"/>
<Button x:Name="speech" Content="speak" Margin="10"
Click="speech_Click"/>
<HyperlinkButton x:Name="InfoButton" Grid.Column="0"
NavigateUri="http://www.nicolaellis.com"

26
src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs

@ -12,8 +12,6 @@ namespace EyesAndEars.UWP {
MainPageVM _vm = new MainPageVM();
DispatcherTimer _dispatcherTimer = new DispatcherTimer();
// The object for controlling the speech synthesis engine (voice).
SpeechSynthesizer synth = new SpeechSynthesizer();
public MainPage() {
InitializeComponent();
@ -22,14 +20,6 @@ namespace EyesAndEars.UWP {
IntialiseRefreshTime();
}
private async void PlayText() {
// Generate the audio stream from plain text.
var stream = await synth.SynthesizeTextToStreamAsync("Hello World");
// Send the stream to the media object.
AudioUpdater.SetSource(stream, stream.ContentType);
AudioUpdater.Play();
}
void SetVersionNumber() {
var package = Package.Current;
var packageId = package.Id;
@ -61,11 +51,15 @@ namespace EyesAndEars.UWP {
var url = _vm.BaseURL;
if (!string.IsNullOrEmpty(url)) {
// Devices 3 and 6 are not in use.
_vm.Device1 = await DataServices.UpdateDevice(url, 1, Logs);
_vm.Device2 = await DataServices.UpdateDevice(url, 2, Logs);
_vm.Device1 = await DataServices.UpdateDevice
(url, 1, Logs, _vm.Device1.LatestStatus.status, AudioUpdater);
_vm.Device2 = await DataServices.UpdateDevice
(url, 2, Logs, _vm.Device2.LatestStatus.status, AudioUpdater);
// _vm.Device3 = await DataServices.UpdateDevice(url, 3);
_vm.Device4 = await DataServices.UpdateDevice(url, 4, Logs);
_vm.Device5 = await DataServices.UpdateDevice(url, 5, Logs);
_vm.Device4 = await DataServices.UpdateDevice
(url, 4, Logs, _vm.Device4.LatestStatus.status, AudioUpdater);
_vm.Device5 = await DataServices.UpdateDevice
(url, 5, Logs, _vm.Device5.LatestStatus.status, AudioUpdater);
// _vm.Device6 = await DataServices.UpdateDevice(url, 6);
_vm.CurrentTime = DateTime.UtcNow.ToShortTimeString();
}
@ -95,9 +89,5 @@ namespace EyesAndEars.UWP {
private void LogToggle_Checked(object sender, RoutedEventArgs e) {
LogViewer.Visibility = Visibility.Visible;
}
private void speech_Click(object sender, RoutedEventArgs e) {
PlayText();
}
}
}

2
src/EyesAndEars.UWP/EyesAndEars.UWP/Package.appxmanifest

@ -9,7 +9,7 @@
<Identity
Name="55acd946-60d4-4776-b6c6-03fef750e3da"
Publisher="CN=Craig Oates"
Version="2021.2.6.0" />
Version="2021.2.7.0" />
<mp:PhoneIdentity PhoneProductId="55acd946-60d4-4776-b6c6-03fef750e3da" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

34
src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs

@ -9,12 +9,14 @@ using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Windows.UI.Text;
using Windows.Media.SpeechSynthesis;
namespace EyesAndEars.UWP.Services {
public static class DataServices {
static SpeechSynthesizer _synth = new SpeechSynthesizer();
public static async Task<Device> UpdateDevice(string url, int deviceId,
RichTextBlock logs) {
RichTextBlock logs, string currentStatus, MediaElement audioUpdater) {
try {
var id = MapFactoryDeviceToGalleyDevice(deviceId); // Has note.
var readingAPI = $"{url}/api/readings/latest/{id}";
@ -26,6 +28,7 @@ namespace EyesAndEars.UWP.Services {
var c = UpdateStatusColour(deviceId, s.status, r.reading);
var dev = new Device(deviceId, r, s, c);
LogUpdate(logs, dev);
NotifyAnyStatusChanges(currentStatus, dev, audioUpdater);
return dev;
}
catch (Exception e) {
@ -34,13 +37,28 @@ namespace EyesAndEars.UWP.Services {
}
}
private static async void NotifyAnyStatusChanges(string currentStatus,
Device dev, MediaElement audioUpdater) {
if (currentStatus.Equals
(dev.LatestStatus.status, StringComparison.InvariantCulture)
!= true) {
var stream = await _synth.SynthesizeTextToStreamAsync
($"{TranslateDeviceIdToDeviceName(dev)} {dev.LatestStatus.status}");
audioUpdater.SetSource(stream, stream.ContentType);
audioUpdater.Play();
}
}
private static string TranslateDeviceIdToDeviceName(Device dev) {
if (dev.Id == 1) return "factory1";
else if (dev.Id == 2) return "factory2";
else if (dev.Id == 4) return "gallery1";
else return "gallery2";
}
private static void LogUpdate(RichTextBlock logs, Device dev) {
// Devices 3 and 6 are not in use.
string device;
if (dev.Id == 1) device = "factory1";
else if (dev.Id == 2) device = "factory2";
else if (dev.Id == 4) device = "gallery1";
else device = "gallery2";
// string device = TranslateDeviceIdToDeviceName(dev);
Paragraph paragraph = new Paragraph();
var run1 = new Run {
FontWeight = FontWeights.Bold,
@ -49,8 +67,8 @@ namespace EyesAndEars.UWP.Services {
var run2 = new Run {
Foreground = dev.StatusColour,
FontWeight = FontWeights.Bold,
Text = device
};
Text = TranslateDeviceIdToDeviceName(dev)
};
var run3 = new Run {
Foreground = dev.StatusColour,
Text = $" | {dev.LatestReading.id} | {dev.LatestReading.reading} | " +