void SensorResource::executeGasRule(int density) { if (m_fan.s_active && m_fan.s_resource) { OCRepresentation fanrep; PutCallback p (std::bind(&SensorResource::onPut, this, PH::_1, PH::_2, PH::_3)); if ((density > m_rr->m_density) && (m_fanState == false)) { std::cout << "Turning on the fan..."<<std::endl; fanrep.setValue("fanstate", std::string("on")); m_fan.s_resource->put(fanrep, QueryParamsMap(), p); } else if ((density <= m_rr->m_density) && (m_fanState == true)) { std::cout << "Turning off the fan..."<<std::endl; fanrep.setValue("fanstate", std::string("off")); m_fan.s_resource->put(fanrep, QueryParamsMap(), p); } } }
void RemoteEnrolleeResource::getProvStatusResponse(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : %s, eCode = %d", rep.getUri().c_str(), eCode); if (eCode != 0) { OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); return; } int ps = -1; rep.getValue(OC_RSRVD_ES_PS, ps); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ps - %d", ps); if (ps == ES_PS_NEED_PROVISIONING) //Indicates the need for provisioning { OCRepresentation provisioningRepresentation; provisioningRepresentation.setValue(OC_RSRVD_ES_TNN, std::string(m_ProvConfig.provData.WIFI.ssid)); provisioningRepresentation.setValue(OC_RSRVD_ES_CD, std::string(m_ProvConfig.provData.WIFI.pwd)); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s", m_ProvConfig.provData.WIFI.ssid); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s", m_ProvConfig.provData.WIFI.pwd); m_ocResource->post(provisioningRepresentation, QueryParamsMap(), std::function< void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) >( std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))); } else if (ps == ES_PS_PROVISIONING_COMPLETED) //Indicates that provisioning is completed { OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : Provisioning is successful"); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONED_ALREADY); m_provStatusCb(provStatus); } }
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; } }
void SensorResource::executeJumpingRule(int heartRate) { std::cout << "Crazy Jumping enable flag: " << m_rr->m_crazyJumping << "current heartRate: " << heartRate << std::endl; if (m_rr->m_crazyJumping && m_led.s_active && m_led.s_resource) { OCRepresentation ledrep; PutCallback p (std::bind(&SensorResource::onPut, this, PH::_1, PH::_2, PH::_3)); if (heartRate >= m_rr->m_heartRate){ ledrep.setValue("ledColor", RED); } else { ledrep.setValue("ledColor", GREEN); } m_led.s_resource->put(ledrep, QueryParamsMap(), p); } }
void CloudResource::provisionProperties(const CloudProp& cloudProp) { OIC_LOG(DEBUG, ES_CLOUD_RES_TAG, "provisionProperties IN"); OCRepresentation provisioningRepresentation = cloudProp.toOCRepresentation(); ESCloudResourceCb cb = std::bind(&CloudResource::onCloudProvResponseSafetyCb, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, static_cast<ESCloudResourceCb>( std::bind(&CloudResource::onCloudProvResponse, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), shared_from_this()); m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE, provisioningRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos); OIC_LOG(DEBUG, ES_CLOUD_RES_TAG, "provisionProperties OUT"); }
void RemoteEnrolleeResource::triggerNetworkConnection() { if (m_ocResource == nullptr) { throw ESBadRequestException("Resource is not initialized"); } OCRepresentation provisioningRepresentation; provisioningRepresentation.setValue(OC_RSRVD_ES_TR, 1); m_ocResource->post(provisioningRepresentation, QueryParamsMap(), std::function< void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) >( std::bind(&RemoteEnrolleeResource::triggerNetworkConnectionCb, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))); }
// Callback to found resources void SensorResource::foundResource(std::shared_ptr<OCResource> resource) { std::string resourceURI; std::string hostAddress; std::lock_guard<std::mutex> lock(m_resourceLock); try { if(resource) { std::cout<<"DISCOVERED Resource:"<<std::endl; resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } GetCallback g (std::bind(&SensorResource::onGet, this, PH::_1, PH::_2, PH::_3)); ObserveCallback o (std::bind(&SensorResource::onObserve, this, PH::_1, PH::_2, PH::_3, PH::_4)); if (resourceURI == "/sensor/gas") { std::cout << "\tFound Gas Sensor." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); m_gas.s_resource = resource; m_gas.s_active = true; } else if (resourceURI == "/a/fan") { std::cout << "\tFound Fan Device." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); QueryParamsMap params; resource->get(params, g); m_fan.s_resource = resource; m_fan.s_active = true; } else if (resourceURI == "/intel/chainable_led_edison") { std::cout << "\tFound Edison Led Device." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); QueryParamsMap params; resource->get(params, g); m_led.s_resource = resource; m_led.s_active = true; } else if (resourceURI == "/sensor/pri") { std::cout << "\tFound Pri Device." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); m_pri.s_resource = resource; m_pri.s_active = true; } else { std::cout << "Resource unknown." << std::endl; } } else { std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { //log(e.what()); } }