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