Presentación de la charla de Toni Recio en la #DotNetSpain2016 y la re-DotNet #4Sessions del 16 de abril de 2016, en el Mobile World Centre de Barcelona
7. #dotNetSpain2016
Estrategia Microsoft en materia IoT
La madre de Galileo
Arduino, to .NET o not .NET
Raspberry & UWP
Inspiración
El nacer de una idea
Implementación
La pelea por la idea
Agenda
65. MainPage.xaml.cs
using Microsoft.Band;
using Microsoft.Band.Tiles;
using Microsoft.Band.Tiles.Pages;
// Clase para llamar al servicio rest que hay deployado en la raspberry pi2
public RestClient mClient;
// URL DEL SERVIDOR + PORT
string URI;
// Tiles disponibles de poner en el band
int RemainingSpace;
// Objeto bandClient que nos proporciona el nuget de Microsoft.Band
IBandClient _bandClient;
// Connexion a signalR de azure
HubConnection hubConnection;
// Tipo de hub al que nos vamos a subscribir, en nuestro caso solo tenemos uno que se
llama LightsHub
IHubProxy LightsHub;
// Informacion de la band, proporcionado por el nuger Microsoft.Band
IBandInfo _bandInfo;
// Id del Tile que instalaremos en la band
68. MainPage.xaml.cs
public async void InitializeBand()
{
// Recogemos el nombre de la band (El que es visible por bluetooth
_bandInfo = (await
BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();
try
{
// Conectamos con la band pasandole al nombre del bluetooth de la band
_bandClient = await BandClientManager.Instance.ConnectAsync(_bandInfo);
// Comprovamos que hay capacidad para un nuevo Tile
this.RemainingSpace = await
_bandClient.TileManager.GetRemainingTileCapacityAsync();
if (RemainingSpace > 0)
{
…
}
catch (Exception e)
{
}
}
69. MainPage.xaml.cs
// Generamos el GUID del tile que vamos a instalar en la band
mTileAppId = Guid.NewGuid();
try {
// Creamos el nuevo tile con la siguiente configuración
var tile = new BandTile(mTileAppId) {
Name = "Techdencias",
TileIcon = (await GetBitmapFromFile(
"ms-appx:///Assets/logo-band46x46.png")).ToBandIcon(),
SmallIcon = (await GetBitmapFromFile(
"ms-appx:///Assets/logo-band24x24.png")).ToBandIcon()};
// Creamos un pageLayout para la tile que instalamos en la band, cuenta con
// los siguientes botones, Encender,Apagar,SiguienteColor,PrevioColor
TextButton btnSwitchOn = new TextButton { ElementId =
BandUiDefinitions.ButtonSwitchOn, Rect = new PageRect(0, 0, 100, 40),
VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Top,
HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Left };
...
70. MainPage.xaml.cs
...
TextButton btnSwitchOff = new TextButton { ElementId =
BandUiDefinitions.ButtonSwitchOff, Rect = new PageRect(0, 60, 100, 40),
VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Top,
HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Right
};
TextButton btnPrevScene = new TextButton { ElementId =
BandUiDefinitions.ButtonPrevScene, Rect = new PageRect(100, 0, 100, 40),
VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Bottom,
HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Left };
TextButton btnNextScene = new TextButton { ElementId =
BandUiDefinitions.ButtonNextScene, Rect = new PageRect(100, 60, 100, 40),
VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Bottom,
HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Right };
FilledPanel panel = new FilledPanel(btnSwitchOn, btnSwitchOff, btnPrevScene,
btnNextScene) { Rect = new PageRect(0, 0, 220, 150) };
// Añadimos el pageLayout a el tile
tile.PageLayouts.Add(new PageLayout(panel));
...
71. MainPage.xaml.cs
...
// recuperamos las tiles que hemos generado anteriormente
IEnumerable<BandTile> mTiles = await
_bandClient.TileManager.GetTilesAsync();
// Borramos el que creamos en la ejecucción anterior para que no se repita
if (mTiles.Count() > 0) { await
_bandClient.TileManager.RemoveTileAsync(mTiles.FirstOrDefault()); }
// Añadimos el Tile
await _bandClient.TileManager.AddTileAsync(tile);
// Setamos la pagina que creamos antes
await _bandClient.TileManager.SetPagesAsync(mTileAppId,
new PageData(BandUiDefinitions.Page1Id, 0,
new TextButtonData(BandUiDefinitions.ButtonSwitchOn, "On"),
new TextButtonData(BandUiDefinitions.ButtonSwitchOff, "Off"),
new TextButtonData(BandUiDefinitions.ButtonPrevScene, "Prev"),
new TextButtonData(BandUiDefinitions.ButtonNextScene, “Next")));
...
72. MainPage.xaml.cs
...
// nos subcribimos al evento tileOpened, con el que encendemos
//la bombilla de la raspberry
_bandClient.TileManager.TileOpened += EventHandler_TileOpened;
// nos subcribimos al evento tileOpened, con el que apagamos la
//bombilla de la raspberry
_bandClient.TileManager.TileClosed += EventHandler_TileClosed;
// nos subscribimos al evento tileButtonPressed, el resultado dependera
//del ID del boton pulsado, toda la configuración la encontraremos en
//la clase BandUiDefinitions
_bandClient.TileManager.TileButtonPressed += EventHandler_ButtonPressed;
// Empezamos a leer de la band
await _bandClient.TileManager.StartReadingsAsync();
73. MainPage.xaml.cs
private async void EventHandler_TileClosed(object sender,
BandTileEventArgs<IBandTileClosedEvent> e)
{
if (e.TileEvent.TileId.Equals(mTileAppId))
{
var res = await mClient.Request("SwitchLamp");
}
}
private async void EventHandler_TileOpened(object sender,
BandTileEventArgs<IBandTileOpenedEvent> e)
{
if (e.TileEvent.TileId.Equals(mTileAppId))
{
var res = await mClient.Request("SwitchLamp");
}
}