void SensorResource::put(OCRepresentation rep)
{
	if (rep.hasAttribute("name")) {
		m_sensorName = rep.getValue<std::string>("name");
	}

	if (rep.hasAttribute("address")) {
		m_sensorAddr = rep.getValue<std::string>("address");
	}

	std::cout << "Registered sensor name : " << m_sensorName << " address : " << m_sensorAddr<< std::endl;
	if ((!m_sensorName.empty()) && (!m_sensorAddr.empty())) {
		if (m_sensorMap.find(m_sensorName) == m_sensorMap.end()) {
			m_sensorMap.insert(SensorMap::value_type(m_sensorName, m_sensorAddr));
		}
	}
	else {
		return;
	}

	if (m_sensorName == "gas") {
		m_gas.s_name = m_sensorName;
	}
	else if (m_sensorName == "fan") {
		m_fan.s_name = m_sensorName;
	}
	else if (m_sensorName == "led") {
		m_led.s_name = m_sensorName;
	}
	else if (m_sensorName == "pri") {
		m_pri.s_name = m_sensorName;
	}

	StartMonitor(m_sensorAddr);
}
void SensorResource::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
	const int& eCode, const int& sequenceNumber)
{
	int density = 0;
	int ledColor = 0;
	bool motion = false;
	try
	{
		if(eCode == OC_STACK_OK) {
			if (rep.hasAttribute("density")) {
				m_gas.s_active = true;
				rep.getValue("density", density);
				std::cout << "\tdensity: " << density << std::endl;

				if (m_rr->m_kitchenMonitor) {
					std::cout << "Kitchen Monitor is enabled" << std::endl;
					executeGasRule(density);
				}
			}

			if (rep.hasAttribute("fanstate")) {
				m_fan.s_active = true;
				std::string state = rep.getValue<std::string>("fanstate");
				m_fanState = (state == "on" ? true:false);
				std::cout << "\tfanstate: " << m_fanState << std::endl;
			}

			if (rep.hasAttribute("ledColor")) {
				m_led.s_active = true;
				rep.getValue("ledColor", ledColor);
				std::cout << "\tledColor: " << ledColor << std::endl;
			}
			if (rep.hasAttribute("motion")) {
				m_pri.s_active = true;
				rep.getValue("motion", motion);
				std::cout << "\tmotion: " << motion << std::endl;
				PutCallback p (std::bind(&SensorResource::onPut, this, PH::_1, PH::_2, PH::_3));
				if ((motion == true) && (m_led.s_active == true)) {
					OCRepresentation ledrep;
					ledrep.setValue("ledColor", BLUE);
					m_led.s_resource->put(ledrep, QueryParamsMap(), p);
				}
			}
		}
		else {
			std::cout << "onObserve Response error: " << eCode << std::endl;
		}
	}
	catch(std::exception& e) {
		std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
	}
}
Exemple #3
0
void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep,
        const int eCode)
{
    printf("\nonPost\n");

    if (rep.hasAttribute("ActionSet"))
    {
        std::string plainText;

        if (rep.getValue("ActionSet", plainText))
        {
            ActionSet *actionset = groupMgr->getActionSetfromString(plainText);
            if (actionset != NULL)
            {
                cout << endl << "\tACTIONSET NAME :: "
                        << actionset->actionsetName << endl;
                for (auto actIter = actionset->listOfAction.begin();
                        actIter != actionset->listOfAction.end(); ++actIter)
                {
                    cout << "\t\tTARGET :: " << (*actIter)->target << endl;

                    for (auto capaIter = (*actIter)->listOfCapability.begin();
                            capaIter != (*actIter)->listOfCapability.end();
                            ++capaIter)
                    {
                        cout << "\t\t\t" << (*capaIter)->capability << " :: "
                                << (*capaIter)->status << endl;
                    }
                }
            }
            delete actionset;
        }

        // printf( "\tPlain Text :: %s\n", plainText.c_str() );
    }
    else if (rep.hasAttribute("DoAction"))
    {
        std::string plainText;
        if (rep.getValue("DoAction", plainText))
        {
            cout << "\t" << plainText << endl;
        }
    }
    else
    {

    }
}
Exemple #4
0
void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
{
    printf("\nonPost\n");

    std::vector< OCRepresentation > children = rep.getChildren();

    cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
    for (auto iter = children.begin(); iter != children.end(); ++iter)
    {
        std::string power;
        (*iter).getValue("power", power);

        cout << "\tURI :: " << (*iter).getUri() << endl;
        cout << "\t\tpower :: " << power << endl;
    }

    if (rep.hasAttribute("ActionSet"))
    {
        std::string plainText;

        rep.getValue("ActionSet", plainText);

        printf("\tPlain Text :: %s\n", plainText.c_str());
    }
    else
    {
        printf("Not found ActionSet\n");
    }
}
Exemple #5
0
void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
{
    pthread_mutex_lock(&mutex_lock);
    isWaiting = 0;
    pthread_mutex_unlock(&mutex_lock);

    if (eCode != SUCCESS_RESPONSE)
    {
        return ;
    }

    std::cout << "\tResource URI: " << rep.getUri() << std::endl;

    if (rep.hasAttribute("loc"))
        std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
    if (rep.hasAttribute("st"))
        std::cout << "\t\tSystemTime:" << rep.getValue< std::string >("st") << std::endl;
    if (rep.hasAttribute("c"))
        std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
    if (rep.hasAttribute("r"))
        std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
}
JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_hasAttribute
(JNIEnv *env, jobject thiz, jstring jstr)
{
    LOGD("OcRepresentation_hasAttribute");
    if (!jstr)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
        return false;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep) return false;

    std::string str = env->GetStringUTFChars(jstr, nullptr);
    return rep->hasAttribute(str);
}
void onPost(const HeaderOptions& /*headerOptions*/,
        const OCRepresentation& rep, const int eCode)
{
    try
    {
        if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
        {
            std::cout << "POST request was successful" << std::endl;

            if(rep.hasAttribute("createduri"))
            {
                std::cout << "\tUri of the created resource: "
                    << rep.getValue<std::string>("createduri") << std::endl;
            }
            else
            {
                rep.getValue("state", mylight.m_state);
                rep.getValue("power", mylight.m_power);
                rep.getValue("name", mylight.m_name);

                //std::cout << "\tstate: " << mylight.m_state << std::endl;
                //std::cout << "\tpower: " << mylight.m_power << std::endl;
                //std::cout << "\tname: " << mylight.m_name << std::endl;
            }

            OCRepresentation rep2;

            std::cout << "Posting light representation..."<<std::endl;

            mylight.m_state = true;
            mylight.m_power = 55;

            rep2.setValue("state", mylight.m_state);
            rep2.setValue("power", mylight.m_power);

            curResource->post(rep2, QueryParamsMap(), &onPost2);
        }
        else
        {
            std::cout << "onPost Response error: " << eCode << std::endl;
            std::exit(-1);
        }
    }
    catch(std::exception& e)
    {
        std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
    }
}
void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
{
    (void)headerOptions;
    try
    {
        if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
        {
            std::cout << "POST request was successful" << std::endl;

            if (rep.hasAttribute("createduri"))
            {
                std::cout << "\tUri of the created resource: "
                          << rep.getValue<std::string>("createduri") << std::endl;
            }
            else
            {
                rep.getValue("on-off", mylight.m_on_off);
                rep.getValue("color", mylight.m_color);
                rep.getValue("dim", mylight.m_dim);

                std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
                std::cout << "\tcolor: " << mylight.m_color << std::endl;
                std::cout << "\tdim: " << mylight.m_dim << std::endl;
            }

            OCRepresentation rep2;

            std::cout << "Posting light representation..." << std::endl;

            mylight.m_on_off = true;

            rep2.setValue("on-off", mylight.m_on_off);

            curResource->post(rep2, QueryParamsMap(), &onPost2);
        }
        else
        {
            std::cout << "onPost Response error: " << eCode << std::endl;
        }
    }
    catch (std::exception &e)
    {
        std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
    }
}
void onPost2(const HeaderOptions& /*headerOptions*/,
        const OCRepresentation& rep, const int eCode)
{
    try
    {
        if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
        {
            std::cout << "POST request was successful" << std::endl;

            if(rep.hasAttribute("createduri"))
            {
                std::cout << "\tUri of the created resource: "
                    << rep.getValue<std::string>("createduri") << std::endl;
            }
            else
            {
                rep.getValue("state", mylight.m_state);
                rep.getValue("power", mylight.m_power);
                rep.getValue("name", mylight.m_name);

                //std::cout << "\tstate: " << mylight.m_state << std::endl;
                //std::cout << "\tpower: " << mylight.m_power << std::endl;
                //std::cout << "\tname: " << mylight.m_name << std::endl;
            }

            if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
                std::cout << std::endl << "Observe is used." << std::endl << std::endl;
            else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
                std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;

            curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);

        }
        else
        {
            std::cout << "onPost2 Response error: " << eCode << std::endl;
            std::exit(-1);
        }
    }
    catch(std::exception& e)
    {
        std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
    }

}
Exemple #10
0
void SensorResource::onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
{
	try {
		if(eCode == OC_STACK_OK) {
			if (rep.hasAttribute("fanstate")) {
				std::string state = rep.getValue<std::string>("fanstate");
				m_fanState = (state == "on" ? true:false);
				std::cout << "\tfanstate: " << m_fanState << std::endl;
			}
		}
		else {
			std::cout << "onPut Response error: " << eCode << std::endl;
		}
	}
	catch(std::exception& e) {
		std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
	}
}
void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
{
    (void)headerOptions;
    try
    {
        if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
        {
            std::cout << "POST request was successful" << std::endl;

            if (rep.hasAttribute("createduri"))
            {
                std::cout << "\tUri of the created resource: "
                          << rep.getValue<std::string>("createduri") << std::endl;
            }
            else
            {
                rep.getValue("on-off", mylight.m_on_off);
                rep.getValue("color", mylight.m_color);
                rep.getValue("dim", mylight.m_dim);

                std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
                std::cout << "\tcolor: " << mylight.m_color << std::endl;
                std::cout << "\tdim: " << mylight.m_dim << std::endl;
            }

            if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
                std::cout << std::endl << "Observe is used." << std::endl << std::endl;
            else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
                std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;

            curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);

        }
        else
        {
            std::cout << "onPost2 Response error: " << eCode << std::endl;
        }
    }
    catch (std::exception &e)
    {
        std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
    }

}
void onPost(const HeaderOptions& /*headerOptions*/,
        const OCRepresentation& rep, const int eCode)
{
    if(eCode == SUCCESS_RESPONSE)
    {
        std::cout << "POST request was successful" << std::endl;

        if(rep.hasAttribute("createduri"))
        {
            std::cout << "\tUri of the created resource: "
                      << rep.getValue<std::string>("createduri") << std::endl;
        }
        else
        {
            rep.getValue("state", mylight.m_state);
            rep.getValue("power", mylight.m_power);
            rep.getValue("name", mylight.m_name);

            std::cout << "\tstate: " << mylight.m_state << std::endl;
            std::cout << "\tpower: " << mylight.m_power << std::endl;
            std::cout << "\tname: " << mylight.m_name << std::endl;
        }

        OCRepresentation rep2;

        std::cout << "Posting light representation..."<<std::endl;

        mylight.m_state = true;
        mylight.m_power = 55;

        rep2.setValue("state", mylight.m_state);
        rep2.setValue("power", mylight.m_power);
        sleep(1);
        curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
    }
    else
    {
        std::cout << "onPost Response error: " << eCode << std::endl;
        std::exit(-1);
    }
}
void onPost2(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
{
    if(eCode == SUCCESS_RESPONSE)
    {
        std::cout << "POST request was successful" << std::endl;

        if(rep.hasAttribute("createduri"))
        {
            std::cout << "\tUri of the created resource: "
                      << rep.getValue<std::string>("createduri") << std::endl;
        }
        else
        {
            rep.getValue("state", mylight.m_state);
            rep.getValue("power", mylight.m_power);
            rep.getValue("name", mylight.m_name);

            std::cout << "\tstate: " << mylight.m_state << std::endl;
            std::cout << "\tpower: " << mylight.m_power << std::endl;
            std::cout << "\tname: " << mylight.m_name << std::endl;
        }

        if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
            std::cout << std::endl << "Observe is used." << std::endl << std::endl;
        else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
            std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
        sleep(1);
        curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
                OC::QualityOfService::HighQos);

    }
    else
    {
        std::cout << "onPost2 Response error: " << eCode << std::endl;
        std::exit(-1);
    }
}
Exemple #14
0
void printRepresentation(const OCRepresentation& rep)
{

        // Check if attribute "name" exists, and then getValue
        if(rep.hasAttribute("name"))
        {
            myGarage.m_name = rep.getValue<std::string>("name");
        }
        std::cout << "\tname: " << myGarage.m_name << std::endl;

        // You can directly try to get the value. this function
        // return false if there is no attribute "state"
        if(!rep.getValue("state", myGarage.m_state))
        {
            std::cout << "Attribute state doesn't exist in the representation\n";
        }
        std::cout << "\tstate: " << myGarage.m_state << std::endl;

        OCRepresentation rep2 = rep;

        std::cout << "Number of attributes in rep2: "
                  << rep2.numberOfAttributes() << std::endl;

        if(rep2.erase("name"))
        {
            std::cout << "attribute: name, was removed successfully from rep2.\n";
        }

        std::cout << "Number of attributes in rep2: "
                  << rep2.numberOfAttributes() << std::endl;


        if(rep.isNULL("nullAttribute"))
        {
            std::cout << "\tnullAttribute is null." << std::endl;
        }
        else
        {
            std::cout << "\tnullAttribute is not null." << std::endl;
        }

        rep.getValue("light", myGarage.m_lightRep);

        myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
        myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);

        std::cout << "\tlightRep: states: ";

        int first = 1;
        for(auto state: myGarage.m_lightStates)
        {
            if(first)
            {
                std::cout << state;
                first = 0;
            }
            else
            {
                std::cout << "," << state;
            }
        }

        std::cout << std::endl;
        std::cout << "\tlightRep: powers: ";
        first = 1;
        for(auto power: myGarage.m_lightPowers)
        {
            if(first)
            {
                std::cout << power;
                first = 0;
            }
            else
            {
                std::cout << "," << power;
            }
        }
        std::cout << std::endl;

        // Get vector of representations
        rep.getValue("reps", myGarage.m_reps);
        // Client know that server is sending two representations
        // and has key1 and key2 repsectively
        std::cout << "\treps[0].key1: " << myGarage.m_reps[0].getValue<int>("key1") << std::endl;
        std::cout << "\treps[0].key2: " << myGarage.m_reps[1].getValue<int>("key2") << std::endl;

        std::cout << "\tjson: " << rep.getValue<std::string>("json") << std::endl;
}
Exemple #15
0
OCEntityHandlerResult IoTServer::LEDEntityHandler(shared_ptr<OCResourceRequest> Request)
{
    OCEntityHandlerResult result = OC_EH_ERROR;
    if (Request)
    {
        string requestType = Request->getRequestType();
        int requestFlag = Request->getRequestHandlerFlag();
        if (requestFlag & RequestHandlerFlag::RequestFlag)
        {
            auto Response = std::make_shared<OC::OCResourceResponse>();
            Response->setRequestHandle(Request->getRequestHandle());
            Response->setResourceHandle(Request->getResourceHandle());
            if (requestType == "PUT")
            {
                cout << "PUT request for platform LED" << endl;
                OCRepresentation requestRep = Request->getResourceRepresentation();
                if (requestRep.hasAttribute(LED_RESOURCE_KEY))
                {
                    try
                    {
                        requestRep.getValue<int>(LED_RESOURCE_KEY);
                    }
                    catch (...)
                    {
                        Response->setResponseResult(OC_EH_ERROR);
                        OCPlatform::sendResponse(Response);
                        cerr << "Client sent invalid resource value type" << endl;
                        return result;
                    }
                }
                else
                {
                    Response->setResponseResult(OC_EH_ERROR);
                    OCPlatform::sendResponse(Response);
                    cerr << "Client sent invalid resource key" << endl;
                    return result;
                }
                m_ledRepresentation = requestRep;
                putLEDRepresentation();
                if (Response)
                {
                    Response->setErrorCode(200);
                    Response->setResourceRepresentation(getLEDRepresentation());
                    Response->setResponseResult(OC_EH_OK);
                    if (OCPlatform::sendResponse(Response) == OC_STACK_OK)
                    {
                        result = OC_EH_OK;
                    }
                }
            }
            else if (requestType == "GET")
            {
                cout << "GET request for platform LED" << endl;
                if (Response)
                {
                    Response->setErrorCode(200);
                    Response->setResourceRepresentation(getLEDRepresentation());
                    Response->setResponseResult(OC_EH_OK);
                    if (OCPlatform::sendResponse(Response) == OC_STACK_OK)
                    {
                        result = OC_EH_OK;
                    }
                }
            }
            else
            {
                Response->setResponseResult(OC_EH_ERROR);
                OCPlatform::sendResponse(Response);
                cerr << "Unsupported request type" << endl;
            }
        }
    }
    return result;
}
Exemple #16
0
void printRepresentation(const OCRepresentation& rep)
{

        // Check if attribute "name" exists, and then getValue
        if(rep.hasAttribute("name"))
        {
            myGarage.m_name = rep["name"];
        }
        std::cout << "\tname: " << myGarage.m_name << std::endl;

        // You can directly try to get the value. this function
        // return false if there is no attribute "state"
        if(!rep.getValue("state", myGarage.m_state))
        {
            std::cout << "Attribute state doesn't exist in the representation\n";
        }
        std::cout << "\tstate: " << myGarage.m_state << std::endl;

        OCRepresentation rep2 = rep;

        std::cout << "Number of attributes in rep2: "
                  << rep2.numberOfAttributes() << std::endl;

        if(rep2.erase("name"))
        {
            std::cout << "attribute: name, was removed successfully from rep2.\n";
        }

        std::cout << "Number of attributes in rep2: "
                  << rep2.numberOfAttributes() << std::endl;


        if(rep.isNULL("nullAttribute"))
        {
            std::cout << "\tnullAttribute is null." << std::endl;
        }
        else
        {
            std::cout << "\tnullAttribute is not null." << std::endl;
        }

        myGarage.m_lightRep = rep["light"];

        myGarage.m_lightStates = myGarage.m_lightRep["states"];
        myGarage.m_lightPowers = myGarage.m_lightRep["powers"];

        std::cout << "\tlightRep: states: ";

        int first = 1;
        for(auto state: myGarage.m_lightStates)
        {
            if(first)
            {
                std::cout << state;
                first = 0;
            }
            else
            {
                std::cout << "," << state;
            }
        }

        std::cout << std::endl;
        std::cout << "\tlightRep: powers: ";
        first = 1;
        for(auto power: myGarage.m_lightPowers)
        {
            if(first)
            {
                std::cout << power;
                first = 0;
            }
            else
            {
                std::cout << "," << power;
            }
        }
        std::cout << std::endl;

        // Get vector of representations
        myGarage.m_reps = rep["reps"];

        int ct = 0;
        for(auto& rep : myGarage.m_reps)
        {
            for(auto& attribute : rep)
            {
                std::cout<< "\treps["<<ct<<"]."<<attribute.attrname()<<":"
                    << attribute.type()<<" with value " <<attribute.getValueToString() <<std::endl;
            }
            ++ct;
        }

        std::cout << "\tjson: " << rep["json"] << std::endl;
        myGarage.m_hingeStates = rep["hinges"];

        std::cout<< "\tHinge parameter is type: " << rep["hinges"].type() << " with depth "<<
            rep["hinges"].depth() << " and a base type of "<< rep["hinges"].base_type()<<std::endl;


}