apéndice a1. listado de código del punto de control universal

Anuncio
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
APÉNDICE A1. LISTADO DE CÓDIGO DEL
PUNTO DE CONTROL UNIVERSAL
104
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
//////////////////////////////////////////////////////////////////////////////////
// Archivo: Main.cs
//
// Autor: Guillermo Almonacid Olleros
//
//
//
// Clase principal del punto de control que inicia //
// la aplicación
//
/////////////////////////////////////////////////////////////////////////////////
using System;
using Intel.UPNP;
using UCP;
using System.Windows.Forms;
namespace UCP
{
class SampleDeviceMain
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// crear instancia de la ventana principal y ejecución
VentanaPpal app = new VentanaPpal();
Application.Run(app);
}
}
}
105
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
/////////////////////////////////////////////////////////////////////////////////////////////////
// Archivo: VentanaPpal.cs
//
// Autor: Guillermo Almonacid Olleros
//
//
//
// Clase parcial que gestiona todas las funciones
//
// del punto de control.
//
/////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Intel.UPNP;
using System.IO;
namespace UCP
{
public partial class VentanaPpal : Form
{
UPnPDevice[] ListaDisp = new UPnPDevice[100];
int numDisp = 0; // número de dispositivos detectados
int posLista = 0; // índice para ListaDisp
string logEvent = "LogEventos.txt"; // archivo para el registro de eventos
//último nodo del árbol seleccionado
System.Windows.Forms.TreeNode nodo = new System.Windows.Forms.TreeNode();
UPnPService.UPnPEventSubscribeHandler sus = suscripcion;
UPnPService.UPnPEventHandler canc_sus = canc_Suscrip;
SampleDiscovery disco;
public VentanaPpal()
{
// instancia del objeto disco y
// métodos de dispositivo encontrado/perdido
disco = new SampleDiscovery();
disco.OnAddedDevice += new SampleDiscovery.DiscoveryHandler(AddSink);
disco.OnRemovedDevice += new SampleDiscovery.DiscoveryHandler(RemoveSink);
disco.Start();
// creación/borrado del fichero de eventos
File.Create(logEvent);
// lanzamiento de la interfaz
InitializeComponent();
}
// Método que añade el dispositivo encontrado
void AddSink(SampleDiscovery sender, UPnPDevice d)
{
// añadir dispositivo a la lista
ListaDisp[posLista] = d;
posLista++;
numDisp++;
}
// Método que borra el dispositivo desconectado
void RemoveSink(SampleDiscovery sender, UPnPDevice d)
{
// borrar dispositivo de la lista
for (int m = 0; m < posLista; m++)
{
if (System.String.Equals(ListaDisp[m].UniqueDeviceName, d.UniqueDeviceName) == true)
{
ListaDisp[m] = null;
numDisp--;
}
}
}
106
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
// Función: Actualizar Red
private void button1_Click_1(object sender, EventArgs e)
{
int numSer = 0; // número de servicios en el dispositivo
int numAcc = 0; // número de acciones en el servicio
disco.ReScan(); // reescaneo de la red
// creación del árbol de dispositivos, un bucle por nivel del árbol
if (numDisp > 0)
{
this.treeView1.SuspendLayout();
treeView1.Nodes.Clear();
System.Windows.Forms.TreeNode[] arbol = new System.Windows.Forms.TreeNode[numDisp];
int j = 0;
for (int m = 0; m < posLista; m++)
{
if (ListaDisp[m] != null)
{
numSer = ListaDisp[m].Services.GetLength(0);
System.Windows.Forms.TreeNode[] serv = new System.Windows.Forms.TreeNode[numSer];
for (int r = 0; r < numSer; r++)
{
numAcc = ListaDisp[m].Services[r].Actions.Count;
System.Windows.Forms.TreeNode[] acs = new System.Windows.Forms.TreeNode[numAcc];
UPnPAction[] actions = ListaDisp[m].Services[r].GetActions();
for (int f = 0; f < numAcc; f++)
{
int numArg = actions[f].ArgumentList.Length;
System.Windows.Forms.TreeNode[] args = new System.Windows.Forms.TreeNode[numArg];
// nodos correspondientes a argumentos
for (int g = 0; g < numArg; g++)
{
args[g] = new System.Windows.Forms.TreeNode("argumento " + actions[f].ArgumentList[g].Direction + ": " +
actions[f].ArgumentList[g].Name);
}
// nodos de acciones
acs[f] = new System.Windows.Forms.TreeNode(actions[f].Name, args);
}
// nodos de servicios
serv[r] = new System.Windows.Forms.TreeNode(ListaDisp[m].Services[r].ServiceID, acs);
serv[r].BackColor = Color.White;
}
// nodos de dispositivos
arbol[j] = new System.Windows.Forms.TreeNode(ListaDisp[m].FriendlyName, serv);
arbol[j].Name = ListaDisp[m].UniqueDeviceName;
j++;
}
}
// mostrar el árbol en la interfaz
if (arbol.GetLength(0) > 0)
{
try
{
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Red", arbol);
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] { treeNode2 });
treeNode2.Expand(); // expande el nivel de dispositivos
}
catch (SystemException)
{
MessageBox.Show("No se puede crear el árbol, reiniciar la aplicación");
}
}
}
}
107
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
// Activación/desactivación de los botones en función
// del nodo seleccionado (disp, servicio o acción)
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode != null)
{
nodo = treeView1.SelectedNode;
}
if (nodo.Level == 3) // Acción señalada
{
button2.Enabled = true;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
}
else if (nodo.Level == 2) // servicio señalado
{
// botones de suscripción en función del sombreado del nodo
if (nodo.BackColor == Color.White)
{
button3.Enabled = true;
button5.Enabled = false;
}
else
{
button3.Enabled = false;
button5.Enabled = true;
}
button2.Enabled = false;
button4.Enabled = false;
button6.Enabled = true;
}
else if (nodo.Level == 1) // dispositivo señalado
{
button4.Enabled = true;
button3.Enabled = false;
button2.Enabled = false;
button5.Enabled = false;
button6.Enabled = true;
}
else // ninguna de las anteriores
{
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
}
}
// Función: "Ejecutar Acción"
private void button2_Click(object sender, EventArgs e)
{
// instancia de la ventana de acción y diálogo
VentanaAcc diag = new VentanaAcc(nodo, ListaDisp, posLista);
diag.ShowDialog();
}
// Función: Presentación
private void button4_Click(object sender, EventArgs e)
{
// selección de la pestaña y borrar contenido del navegador
tabControl1.SelectTab(2);
webBrowser1.Navigate("about:blank");
// buscar el dispositivo y mostrar su dirección de presentación si la tiene
for (int h = 0; h < posLista; h++)
{
if (ListaDisp[h] != null)
108
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
{
if (System.String.Equals(ListaDisp[h].UniqueDeviceName, nodo.Name) == true)
{
if (ListaDisp[h].HasPresentation == true)
{
webBrowser1.Navigate(ListaDisp[h].PresentationURL);
}
else
{
MessageBox.Show("El dispositivo no ofrece página de presentación");
}
}
}
}
}
// Función: Suscripción a eventos
private void button3_Click(object sender, EventArgs e)
{
int tsus = 300; // segundos que dura la suscripción a eventos
//buscar el servicio y suscribir
for (int h = 0; h < posLista; h++)
{
if (ListaDisp[h] != null)
{
if (System.String.Equals(ListaDisp[h].UniqueDeviceName, nodo.Parent.Name) == true)
{
for (int k = 0; k < ListaDisp[h].Services.GetLength(0); k++)
{
if (System.String.Equals(ListaDisp[h].Services[k].ServiceID, nodo.Text) == true)
{
// suscripción durante un tiempo tsus
ListaDisp[h].Services[k].Subscribe(tsus, sus);
ListaDisp[h].Services[k].OnUPnPEvent += new UPnPService.UPnPEventHandler(Form1_OnUPnPEvent);
// sombrear el nodo correspondiente
nodo.BackColor = Color.Wheat;
button3.Enabled = false;
button5.Enabled = true;
}
}
}
}
}
}
// Método que se ejecuta al recibir un evento
private void Form1_OnUPnPEvent(UPnPService sender, long SEQ)
{
// buscar las variables marcadas para eventos en el servicio
using (StreamWriter sw = File.AppendText(logEvent))
{
for (int f = 0; f < sender.GetStateVariables().GetLength(0); f++)
{
if (sender.GetStateVariables()[f].SendEvent == true)
{
// escritura en el fichero de eventos
sw.Write("Evento " + SEQ + " de " + sender.ServiceID + " (" + sender.ParentDevice.FriendlyName + ") -> ");
sw.Write(sender.GetStateVariables()[f].Name + ": " + sender.GetStateVariables()[f].Value);
sw.WriteLine("
" + System.DateTime.Now.TimeOfDay.ToString());
}
}
sw.Close();
}
}
// callback para cuando se realiza la suscripción a un servicio
static void suscripcion(UPnPService serv, bool k)
{
}
// callback para cuando se cancela la suscripción
static void canc_Suscrip(UPnPService serv, long SEQ)
{
}
109
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
// Selección de la pestaña de eventos
private void tabPage2_Enter(object sender, EventArgs e)
{
// limpiar la información anterior
listBox1.Items.Clear();
// añadir la información del fichero de eventos
try
{
string[] lineas = File.ReadAllLines(logEvent);
this.listBox1.Items.AddRange(lineas);
button8.Enabled = true;
}
catch (System.IO.IOException io_exc)
{
MessageBox.Show(io_exc.Message);
}
}
// Función: Cancelar suscripción
private void button5_Click(object sender, EventArgs e)
{
// buscar el servicio y cancelar suscripción
for (int h = 0; h < posLista; h++)
{
if (ListaDisp[h] != null)
{
if (System.String.Equals(ListaDisp[h].UniqueDeviceName, nodo.Parent.Name) == true)
{
for (int k = 0; k < ListaDisp[h].Services.GetLength(0); k++)
{
if (System.String.Equals(ListaDisp[h].Services[k].ServiceID, nodo.Text) == true)
{
ListaDisp[h].Services[k].UnSubscribe(canc_sus);
// quitar el sombreado al nodo
nodo.BackColor = Color.White;
button3.Enabled = true;
button5.Enabled = false;
}
}
}
}
}
}
// Función: Mostrar descripción del dispositivo/servicio
private void button6_Click(object sender, EventArgs e)
{
string xmlDoc = "xmlDoc.xml"; // nombre del fichero donde se guarda el XML
tabControl1.SelectTab(0); // seleccionar la pestaña
// buscar el nodo marcado
for (int h = 0; h < posLista; h++)
{
if (ListaDisp[h] != null)
{
// si el nodo marcado es un dispositivo
if (System.String.Equals(ListaDisp[h].UniqueDeviceName, nodo.Name) == true)
{
// mostrar el navegador con la información
webBrowser2.BringToFront();
webBrowser2.Navigate(ListaDisp[h].LocationURL);
}
if (System.String.Equals(ListaDisp[h].UniqueDeviceName, nodo.Parent.Name) == true)
{
for (int k = 0; k < ListaDisp[h].Services.GetLength(0); k++)
{
// si el nodo marcado es un servicio
if (System.String.Equals(ListaDisp[h].Services[k].ServiceID, nodo.Text) == true)
{
// mostrar el cuadro de texto y añadir información del XML
textBox1.BringToFront();
110
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
xmlDataSet.Clear();
using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlDoc, Encoding.UTF8))
{
ListaDisp[h].Services[k].GetServiceXML(writer);
}
xmlDataSet.ReadXml(xmlDoc);
System.IO.StringWriter swXML = new System.IO.StringWriter();
xmlDataSet.WriteXml(swXML);
textBox1.Text = swXML.ToString();
}
}
}
}
}
}
// Actualizar el log de eventos
private void button7_Click(object sender, EventArgs e)
{
// deseleccionar y seleccionar pestaña
tabControl1.DeselectTab(1);
tabControl1.SelectTab(1);
}
// Limpiar el log de eventos
private void button8_Click(object sender, EventArgs e)
{
// borrar el contenido del fichero de registro de eventos y
// borrar información de la pestaña
File.Create(logEvent);
button8.Enabled = false;
listBox1.Items.Clear();
}
}
}
111
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
////////////////////////////////////////////////////////////////////////////////////////////
// Archivo: VentanaPpal.Designer.cs
//
// Autor: Guillermo Almonacid Olleros
//
//
//
// Clase parcial que muestra la interfaz gráfica
//
// de la ventana principal
//
//
//
// Código generado por el diseñador de Windows Form //
///////////////////////////////////////////////////////////////////////////////////////////
namespace UCP
{
partial class VentanaPpal
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Código generado por el Diseñador de Windows Forms
private void InitializeComponent()
{
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Red");
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.treeView1 = new System.Windows.Forms.TreeView();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.button6 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button5 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.webBrowser2 = new System.Windows.Forms.WebBrowser();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.listBox1 = new System.Windows.Forms.ListBox();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.textBox1 = new System.Windows.Forms.TextBox();
this.xmlDataSet = new System.Data.DataSet();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.flowLayoutPanel1.SuspendLayout();
this.groupBox4.SuspendLayout();
this.groupBox5.SuspendLayout();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.tabControl1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.xmlDataSet)).BeginInit();
this.tabPage1.SuspendLayout();
112
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
this.tabPage2.SuspendLayout();
this.tabPage3.SuspendLayout();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.treeView1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.AutoScroll = true;
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size = new System.Drawing.Size(945, 442);
this.splitContainer1.SplitterDistance = 291;
this.splitContainer1.TabIndex = 0;
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
treeNode3.Name = "Nodo0";
treeNode3.Text = "Red";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
treeNode3});
this.treeView1.Size = new System.Drawing.Size(287, 438);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// splitContainer2
//
this.splitContainer2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.AutoScroll = true;
this.splitContainer2.Panel1.Controls.Add(this.flowLayoutPanel1);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.AutoScroll = true;
this.splitContainer2.Panel2.Controls.Add(this.tabControl1);
this.splitContainer2.Size = new System.Drawing.Size(650, 442);
this.splitContainer2.SplitterDistance = 221;
this.splitContainer2.TabIndex = 6;
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.AutoScroll = true;
this.flowLayoutPanel1.Controls.Add(this.groupBox4);
this.flowLayoutPanel1.Controls.Add(this.groupBox5);
this.flowLayoutPanel1.Controls.Add(this.groupBox1);
this.flowLayoutPanel1.Controls.Add(this.groupBox2);
this.flowLayoutPanel1.Controls.Add(this.groupBox3);
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(646, 217);
this.flowLayoutPanel1.TabIndex = 5;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.button1);
this.groupBox4.Location = new System.Drawing.Point(3, 3);
this.groupBox4.Name = "groupBox4";
113
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
this.groupBox4.Size = new System.Drawing.Size(121, 102);
this.groupBox4.TabIndex = 15;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "Descubrimiento";
//
// button1
//
this.button1.Location = new System.Drawing.Point(9, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 25);
this.button1.TabIndex = 0;
this.button1.Text = "Actualizar Red";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// groupBox5
//
this.groupBox5.Controls.Add(this.button6);
this.groupBox5.Location = new System.Drawing.Point(130, 3);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(122, 102);
this.groupBox5.TabIndex = 16;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "Descripción";
//
// button6
//
this.button6.Enabled = false;
this.button6.Location = new System.Drawing.Point(7, 21);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(109, 25);
this.button6.TabIndex = 0;
this.button6.Text = "Mostrar Descripción";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Location = new System.Drawing.Point(258, 3);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(124, 103);
this.groupBox1.TabIndex = 12;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Control";
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(6, 21);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(107, 26);
this.button2.TabIndex = 1;
this.button2.Text = "Ejecutar Acción";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button5);
this.groupBox2.Controls.Add(this.button3);
this.groupBox2.Location = new System.Drawing.Point(388, 3);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(137, 103);
this.groupBox2.TabIndex = 13;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Eventos";
//
// button5
//
this.button5.Enabled = false;
this.button5.Location = new System.Drawing.Point(6, 62);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(118, 26);
this.button5.TabIndex = 6;
this.button5.Text = "Cancelar Suscripción";
114
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button3
//
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(6, 21);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(118, 26);
this.button3.TabIndex = 3;
this.button3.Text = "Suscribir a Eventos";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.button4);
this.groupBox3.Location = new System.Drawing.Point(531, 3);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(112, 103);
this.groupBox3.TabIndex = 14;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Presentación";
//
// button4
//
this.button4.Enabled = false;
this.button4.Location = new System.Drawing.Point(6, 21);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(97, 25);
this.button4.TabIndex = 5;
this.button4.Text = "Presentación";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(646, 213);
this.tabControl1.TabIndex = 4;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.webBrowser2);
this.tabPage1.Controls.Add(this.textBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(638, 187);
this.tabPage1.TabIndex = 3;
this.tabPage1.Text = "Descripción";
this.tabPage1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(638, 187);
this.textBox1.TabIndex = 0;
//
// webBrowser2
//
this.webBrowser2.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser2.Location = new System.Drawing.Point(0, 0);
this.webBrowser2.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser2.Name = "webBrowser2";
this.webBrowser2.Size = new System.Drawing.Size(638, 187);
115
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
this.webBrowser2.TabIndex = 0;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.button8);
this.tabPage2.Controls.Add(this.button7);
this.tabPage2.Controls.Add(this.listBox1);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(638, 187);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "Eventos";
this.tabPage2.UseVisualStyleBackColor = true;
this.tabPage2.Enter += new System.EventHandler(this.tabPage2_Enter);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;
this.listBox1.Location = new System.Drawing.Point(3, 3);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(630, 147);
this.listBox1.TabIndex = 0;
this.listBox1.Click += new System.EventHandler(this.tabPage2_Enter);
//
// tabPage3
//
this.tabPage3.Controls.Add(this.webBrowser1);
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Padding = new System.Windows.Forms.Padding(3);
this.tabPage3.Size = new System.Drawing.Size(638, 187);
this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "Presentación";
this.tabPage3.UseVisualStyleBackColor = true;
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(3, 3);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(632, 181);
this.webBrowser1.TabIndex = 0;
//
// xmlDataSet
//
this.xmlDataSet.DataSetName = "xmlDataSet";
//
// button7
//
this.button7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Left)));
this.button7.Location = new System.Drawing.Point(8, 157);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(112, 23);
this.button7.TabIndex = 1;
this.button7.Text = "Actualizar";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.button8.Location = new System.Drawing.Point(527, 157);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(105, 23);
this.button8.TabIndex = 2;
this.button8.Text = "Limpiar";
this.button8.UseVisualStyleBackColor = true;
116
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// VentanaPpal
//
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(945, 442);
this.Controls.Add(this.splitContainer1);
this.Name = "VentanaPpal";
this.Text = "Punto de Control Universal";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.xmlDataSet)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel2.ResumeLayout(false);
this.splitContainer2.ResumeLayout(false);
this.flowLayoutPanel1.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.groupBox5.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.tabPage3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage2;
public System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Data.DataSet xmlDataSet;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.WebBrowser webBrowser2;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button7;
}
}
117
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
///////////////////////////////////////////////////////////////////////////////////////////
//
Archivo: SampleDiscovery.cs
//
// Autor:Intel Device Builder Build#1.0.2777.24761
//
//
//
// Clase que instancia un punto de control y define los //
// atributos y métodos necesarios para la notificación //
// de la conexión o desconexión de los dispositivos. //
//
//
// Este fichero no ha sido modificado.
//
/////////////////////////////////////////////////////////////////////////////////////////
using System;
using Intel.UPNP;
using Intel.Utilities;
using UCP;
namespace UCP
{
class SampleDiscovery
{
private UPnPSmartControlPoint scp;
private WeakEvent AddEvent = new WeakEvent();
private WeakEvent RemoveEvent = new WeakEvent();
public delegate void DiscoveryHandler(SampleDiscovery sender, UPnPDevice dev);
public event DiscoveryHandler OnAddedDevice
{
add
{
AddEvent.Register(value);
}
remove
{
AddEvent.UnRegister(value);
}
}
public event DiscoveryHandler OnRemovedDevice
{
add
{
RemoveEvent.Register(value);
}
remove
{
RemoveEvent.UnRegister(value);
}
}
public SampleDiscovery()
{
}
public void Start()
{
scp = new UPnPSmartControlPoint(new UPnPSmartControlPoint.DeviceHandler(OnAddSink));
scp.OnRemovedDevice += new UPnPSmartControlPoint.DeviceHandler(OnRemoveSink);
}
public void ReScan()
{
scp.Rescan();
}
public void ForceDisposeDevice(UPnPDevice dev)
{
if (dev.ParentDevice != null)
{
ForceDisposeDevice(dev.ParentDevice);
}
else
{
scp.ForceDisposeDevice(dev);
}
}
118
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
private void OnAddSink(UPnPSmartControlPoint sender, UPnPDevice d)
{
AddEvent.Fire(this, d);
}
private void OnRemoveSink(UPnPSmartControlPoint sender, UPnPDevice d)
{
RemoveEvent.Fire(this, d);
}
}
}
119
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
//////////////////////////////////////////////////////////////////////////////////
// Archivo: VentanaAcc.cs
//
// Autor: Guillermo Almonacid Olleros
//
//
//
// Clase parcial que gestiona todas la solicitud de //
// invocar una acción.
//
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Intel.UPNP;
namespace UCP
{
public partial class VentanaAcc : Form
{
UPnPService servicio; // servicio del cual se quiere invocar la acción
UPnPArgument[] listArg; // lista de argumentos de la acción
UPnPAction accion; // acción que se quiere invocar
int numArg = 0; //numero de argumentos de la acción
public VentanaAcc(System.Windows.Forms.TreeNode nodo, UPnPDevice[] ListaDisp, int posLista)
{
// mostrar la ventana de acción
InitializeComponent(nodo, ListaDisp, posLista);
}
// Función: Invocar acción
private void button1_Click_1(object sender, EventArgs e)
{
listArg = new UPnPArgument[numArg];
// bucle para recorrer las filas de argumentos
for (int row = 0; row < numArg; row++)
{
// tomar la pareja de controles para cada argumento
Control c1 = tableLayoutPanel1.GetControlFromPosition(0, row);
string nom = c1.Name;
Control c2 = tableLayoutPanel1.GetControlFromPosition(1, row);
string tipo = c2.Name;
// comprobar el tipo de entrada y rellenar la lista de argumentos
try
{
if (tipo == "boolean")
{
if ((c2.Text == "true") | (c2.Text == "True"))
{
listArg[row] = new UPnPArgument(nom, true);
}
else if ((c2.Text == "false") | (c2.Text == "False"))
{
listArg[row] = new UPnPArgument(nom, false);
}
else //otros, dará un error en la validación
{
listArg[row] = new UPnPArgument(nom, c2.Text);
}
}
else if (tipo == "string")
{
listArg[row] = new UPnPArgument(nom, c2.Text);
}
else if (tipo == "ui1")
{
120
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
byte val = byte.Parse(c2.Text);
listArg[row] = new UPnPArgument(nom, val);
}
else if (tipo == "uri")
{
Uri val = new Uri(c2.Text);
listArg[row] = new UPnPArgument(nom, val);
}
else // tipo == "ui4" y otras
{
int val = int.Parse(c2.Text);
listArg[row] = new UPnPArgument(nom, val);
}
}
catch (System.FormatException f_exc) // error en el formato
{
MessageBox.Show(f_exc.Message);
return;
}
catch (System.OverflowException o_exc) // error de rango
{
MessageBox.Show(o_exc.Message);
return;
}
}
// Prevalidación de los argumentos
try
{
if (accion.ValidateArgs(listArg) == true)
{
// invocar la acción
servicio.InvokeSync(accion.Name, listArg);
}
}
catch (Intel.UPNP.UPnPInvokeException exc)
{
MessageBox.Show(exc.Message);
}
// Actualización de los argumentos
for (int row = 0; row < numArg; row++)
{
// introducir en el control el valor
Control c = tableLayoutPanel1.GetControlFromPosition(1, row);
c.Text = listArg[row].DataValue.ToString();
}
}
}
}
121
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
//////////////////////////////////////////////////////////////////////////////////////////////////
// Archivo: VentanaAcc.Designer.cs
//
// Autor: Guillermo Almonacid Olleros
//
//
//
// Clase parcial que muestra la interfaz gráfica
//
// de la ventana de acción
//
//
//
// Código generado por el diseñador de Windows Form
//
//////////////////////////////////////////////////////////////////////////////////////////////////
using Intel.UPNP;
namespace UCP
{
partial class VentanaAcc
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Código generado por el Diseñador de Windows Forms
/// <summary>
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido del método con el editor de código.
/// </summary>
private void InitializeComponent(System.Windows.Forms.TreeNode nodo, UPnPDevice[] ListaDisp, int posLista)
{
this.button1 = new System.Windows.Forms.Button();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
for (int p = 0; p < posLista; p++)
{
if ((ListaDisp[p] != null) & (string.Equals(ListaDisp[p].UniqueDeviceName, nodo.Parent.Parent.Name) == true))
{
for (int q = 0; q < ListaDisp[p].Services.Length; q++)
{
if (string.Equals(nodo.Parent.Text, ListaDisp[p].Services[q].ServiceID) == true)
{
servicio = ListaDisp[p].Services[q];
for (int d = 0; d < ListaDisp[p].Services[q].GetActions().Length; d++)
{
if (string.Equals(nodo.Text, ListaDisp[p].Services[q].GetActions()[d].Name) == true)
{
accion = ListaDisp[p].Services[q].GetActions()[d];
this.tableLayoutPanel1.RowCount = ListaDisp[p].Services[q].GetActions()[d].ArgumentList.Length;
numArg = ListaDisp[p].Services[q].GetActions()[d].ArgumentList.Length;
for (int a = 0; a < ListaDisp[p].Services[q].GetActions()[d].ArgumentList.Length; a++)
{
//tipo de dato en función de la variable relacionada
ListaDisp[p].Services[q].GetActions()[d].ArgumentList[a].DataType =
ListaDisp[p].Services[q].GetActions()[d].ArgumentList[a].RelatedStateVar.ValueType;
UPnPArgument arg = ListaDisp[p].Services[q].GetActions()[d].ArgumentList[a];
if (arg.Direction == "in")
{
this.label1 = new System.Windows.Forms.Label();
122
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(3, 0);
this.label1.Name = arg.Name;
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = arg.Name + " (" + arg.DataType + ")";
if (arg.DataType == "boolean")
{
//
// comboBox1
//
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"True",
"False"});
this.comboBox1.Location = new System.Drawing.Point(107, 67);
this.comboBox1.Name = arg.DataType;
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 1;
this.tableLayoutPanel1.Controls.Add(this.comboBox1, 1, a);
}
else
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox1.Location = new System.Drawing.Point(44, 3);
this.textBox1.Name = arg.DataType;
this.textBox1.Size = new System.Drawing.Size(140, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Name = arg.DataType;
this.textBox1.Text = "Valor";
this.tableLayoutPanel1.Controls.Add(this.textBox1, 1, a);
}
this.tableLayoutPanel1.Controls.Add(this.label1, 0, a);
}
if (arg.Direction == "out")
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(3, 0);
this.label1.Name = arg.Name;
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = arg.Name;
this.label2.Location = new System.Drawing.Point(44, 3);
this.label2.Name = arg.DataType;
this.label2.Size = new System.Drawing.Size(140, 20);
this.label2.TabIndex = 1;
this.label2.Text = "0";
this.tableLayoutPanel1.Controls.Add(this.label1, 0, a);
this.tableLayoutPanel1.Controls.Add(this.label2, 1, a);
}
}
}
}
}
}
}
}
123
DOMÓTICA: PROTOCOLO UPNP Y HOGAR DIGITAL
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(305, 81);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Invocar";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AutoScroll = true;
this.tableLayoutPanel1.AutoSize = true;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 12);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(50, 50);
this.tableLayoutPanel1.TabIndex = 1;
//
// Form2
//
this.AutoScroll = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(392, 116);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = nodo.Text;
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
}
}
124
Descargar