Implementando el dispositivo BinaryLight

Este es un ejemplo de dispositivo/servicio, implementando el dispositivo BinaryLight y el servicio SwitchPower para emular un interruptor de luz.

La interfaz de usuario se ha simplificado con el fin de demostrar los conceptos y métodos básicos.

Ejemplo #1 Implementing light server

<?php

/* Establece Objetivo */
function set_target_cb($service$action$arg)
{
    
/* Obtiene nuevo valor del objetivo */
    
$target gupnp_service_action_get($action'NewTargetValue'GUPNP_TYPE_BOOLEAN);

    
/* Si el nuevo objetivo no coincide con el status actual, cambiar status y 
       emitir una notificación de que el status ha cambiado. */
    
if ($target != $GLOBALS['status']) {
        
$GLOBALS['status'] = $target;
        
gupnp_service_notify($service'Status'GUPNP_TYPE_BOOLEAN$GLOBALS['status']);
        
printf("La luz está ahora %s.\n"$GLOBALS['status'] ? "on" "off");
    }

    
/* Devuelve éxito al cliente */
    
gupnp_service_action_return($action);
}

/* Obtiene Objetivo*/
function get_target_cb($service$action$arg)
{
    
gupnp_service_action_set($action'RetTargetValue'GUPNP_TYPE_BOOLEAN$GLOBALS['status']);
    
gupnp_service_action_return($action);
}

/* Obtiene Status*/
function get_status_cb($service$action$arg)
{
    
gupnp_service_action_set($action'ResultStatus'GUPNP_TYPE_BOOLEAN$GLOBALS['status']);
    
gupnp_service_action_return($action);
}

/* La luz está apagada de forma predeterminada */
$GLOBALS['status'] = false;
printf("La luz está ahora %s.\n"$GLOBALS['status'] ? "on" "off");

/* Crea el contexto UPnP */
$context gupnp_context_new();
if (!
$context) {
    
printf("Error creando el contexto GUPnP\n");
    exit(-
1);
}

/* Hospeda el directorio que contiene los archivos de descripción de los dispositivos y servicios */
gupnp_context_host_path($context"./web""");

/* crea el dispositivo raíz */
$location "/BinaryLight.xml";
$dev gupnp_root_device_new($context$location);
gupnp_root_device_set_available($devtrue);

/* Obtiene el servicio switch del dispositivo raíz */
$service_type "urn:schemas-upnp-org:service:SwitchPower:1";
$service gupnp_device_info_get_service($dev$service_type);
if (!
$service) {
    die(
"Cannot get SwitchPower1 service\n");
}

/* Set callback for action GetStatus */
gupnp_device_action_callback_set($serviceGUPNP_SIGNAL_ACTION_INVOKED"GetStatus"
    
"get_status_cb""action data, GetStatus");

/* Establece el callback para la acción GetTarget */
gupnp_device_action_callback_set($serviceGUPNP_SIGNAL_ACTION_INVOKED"GetTarget"
    
"get_target_cb""action data, GetTarget");

/* Establece el callback para la acción SetTarget */
gupnp_device_action_callback_set($serviceGUPNP_SIGNAL_ACTION_INVOKED"SetTarget"
    
"set_target_cb""action data, SetTarget");

/* Ejecuta el bucle principal */
gupnp_root_device_start($dev);

?>

Ejemplo #2 Implementando el cliente "luz"

<?php

function service_proxy_available_cb($proxy$arg)
{
    
$mode $arg['mode'];

    
printf("Set subscribed\n");
    
gupnp_service_proxy_set_subscribed($proxytrue);

    
/* Agrega una notificación si el status cambia */
    
if (!gupnp_service_proxy_add_notify($proxy"Status"
            
GUPNP_TYPE_BOOLEAN"status_changed_cb"NULL)) {
        
printf("Falló agregando notificación\n");
    }
    
    if (
$mode == 'TOGGLE') {
        
/* estamos alternando, así que primero obtenemos el status actual */
        
$target gupnp_service_proxy_action_get($proxy'GetStatus''ResultStatus'GUPNP_TYPE_BOOLEAN);

        
/* y entonces alternamos */
        
$target = ! $target;
    } else {
        
/* Mode es booleano, así que el objetivo es mode gracias a nuestra buena selección de valores numéricos. */
        
$target = ($mode == 'ON') ? true false;
    }

    
/* Establece el objetivo */
    
if (!gupnp_service_proxy_action_set($proxy'SetTarget''NewTargetValue'$targetGUPNP_TYPE_BOOLEAN)) {
        
printf("No se puede establecer interruptor\n");
    } else {
        
printf("Establece interruptor a %s.\n"$target "on" "off");
    }
    
    
/* Detiene navegador */
    
gupnp_control_point_browse_stop($arg['cp']);
}

function 
status_changed_cb($variable$value$arg)
{
    
printf("Status ha cambiado\n");
    
printf("\tvariable: %s\n"$variable);
    
printf("\tvalor: %s\n", (int)$value);
    
printf("\n");
}

/* Verifica y descompone los argumentos de la línea de comandos */
if (count($argv) != 2) {
    
printf("Use: light-client.php [on|off|toggle]\n");
    exit(-
1);
}

if (
$argv[1] == "on") {
    
$mode 'ON';
} elseif (
$argv[1] == "off") {
    
$mode 'OFF';
} elseif (
$argv[1] == "toggle") {
    
$mode 'TOGGLE';
} else {
    
usage ();
    exit(-
1);
}

/* Crea el contexto UPnP */
$context gupnp_context_new();
if (!
$context) {
    
printf("Error creando el contexto GUPnP\n");
    exit(-
1);
}

/* Crea el punto de control, buscando en los servicios SwitchPower */
$cp gupnp_control_point_new ($context,
        
"urn:schemas-upnp-org:service:SwitchPower:1");

/* Conecta al callback del servicio encontrado*/
$cb "service_proxy_available_cb";
$arg = array('mode' => $mode'cp' => $cp);
gupnp_control_point_callback_set($cpGUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE$cb$arg);

/* Start for browsing */
gupnp_control_point_browse_start($cp);

?>