Beispiel #1
0
/*----------------------------------------------------------------------
|   PLT_LightSampleDevice::OnAction
+---------------------------------------------------------------------*/
NPT_Result
PLT_LightSampleDevice::OnAction(PLT_ActionReference& action, NPT_SocketInfo* /* info */)
{
    /* parse the action name */
    NPT_String name = action->GetActionDesc()->GetName();
    if (name.Compare("SetTarget") == 0) {
        NPT_String value;
        action->GetArgumentValue("newTargetValue", value);

        PLT_StateVariable* variable = action->GetActionDesc()->GetService()->FindStateVariable("Status");
        if (NPT_FAILED(variable->SetValue(value))) {
            action->SetError(402, "Invalid Args");
            return NPT_FAILURE;
        }
        return NPT_SUCCESS;
    } else if (name.Compare("GetStatus") == 0) {
        PLT_StateVariable* variable = action->GetActionDesc()->GetService()->FindStateVariable("Status");
        if (variable) {
            action->SetArgumentValue("ResultStatus", variable->GetValue());
            return NPT_SUCCESS;
        }
    }
    
    action->SetError(501, "Action Failed");
    return NPT_FAILURE;
}
Beispiel #2
0
/*----------------------------------------------------------------------
|   PLT_MediaController::OnEventNotify
+---------------------------------------------------------------------*/
NPT_Result
PLT_MediaController::OnEventNotify(PLT_Service* service, NPT_List<PLT_StateVariable*>* vars)
{
    if (m_Listener) {
        // parse LastChange var into smaller vars
        PLT_StateVariable* lastChangeVar = NULL;
        if (NPT_SUCCEEDED(NPT_ContainerFind(*vars, PLT_ListStateVariableNameFinder("LastChange"), lastChangeVar))) {
            vars->Remove(lastChangeVar);
            PLT_Service* var_service = lastChangeVar->GetService();
            NPT_String text = lastChangeVar->GetValue();
            
            NPT_XmlNode* xml = NULL;
            NPT_XmlParser parser;
            if (NPT_FAILED(parser.Parse(text, xml)) || !xml || !xml->AsElementNode()) {
                delete xml;
                return NPT_FAILURE;
            }

            NPT_XmlElementNode* node = xml->AsElementNode();
            if (!node->GetTag().Compare("Event", true)) {
                // look for the instance with attribute id = 0
                NPT_XmlElementNode* instance = NULL;
                for (NPT_Cardinal i=0; i<node->GetChildren().GetItemCount(); i++) {
                    NPT_XmlElementNode* child;
                    if (NPT_FAILED(PLT_XmlHelper::GetChild(node, child, i)))
                        continue;

                    if (!child->GetTag().Compare("InstanceID", true)) {
                        // extract the "val" attribute value
                        NPT_String value;
                        if (NPT_SUCCEEDED(PLT_XmlHelper::GetAttribute(child, "val", value)) &&
                            !value.Compare("0")) {
                            instance = child;
                            break;
                        }
                    }
                }

                // did we find an instance with id = 0 ?
                if (instance != NULL) {
                    // all the children of the Instance node are state variables
                    for (NPT_Cardinal j=0; j<instance->GetChildren().GetItemCount(); j++) {
                        NPT_XmlElementNode* var_node;
                        if (NPT_FAILED(PLT_XmlHelper::GetChild(instance, var_node, j)))
                            continue;

                        // look for the state variable in this service
                        const NPT_String* value = var_node->GetAttribute("val");
                        PLT_StateVariable* var = var_service->FindStateVariable(var_node->GetTag());
                        if (value != NULL && var != NULL) {
                            // get the value and set the state variable
                            // if it succeeded, add it to the list of vars we'll event
                            if (NPT_SUCCEEDED(var->SetValue(*value, false))) {
                                vars->Add(var);
                                NPT_LOG_FINE_2("PLT_MediaController received var change for (%s): %s", (const char*)var->GetName(), (const char*)var->GetValue());
                            }
                        }
                    }
                }
            }
            delete xml;
        }

        if (vars->GetItemCount()) {
            m_Listener->OnMRStateVariablesChanged(service, vars);
        }
    }
    return NPT_SUCCESS;
}