From 7846516d997ebd280e44ea0e46a24c587d2a91e5 Mon Sep 17 00:00:00 2001 From: Younghyun Joo Date: Mon, 23 Feb 2015 14:27:14 +0900 Subject: [PATCH] [PPM] Fix sample plugins and sample app for Linux 1. Modify sample plugins for Linux: mqtt-fan, mqtt-light 2. Modify sample app for Linux Change-Id: I6bb156b9288ce13ba98ec3b3b02a084f4fcf5919 Signed-off-by: Younghyun Joo Reviewed-on: https://gerrit.iotivity.org/gerrit/395 Tested-by: jenkins-iotivity Reviewed-by: yunhee.hwang --- .../plugins/mqtt-fan/src/fanserver.cpp | 11 +- .../plugins/mqtt-light/src/lightserver.cpp | 11 +- .../sample-app/linux/mqtt/mqttclient.cpp | 258 ++------------------- 3 files changed, 34 insertions(+), 246 deletions(-) diff --git a/service/protocol-plugin/plugins/mqtt-fan/src/fanserver.cpp b/service/protocol-plugin/plugins/mqtt-fan/src/fanserver.cpp index 12111e0..3d8f024 100644 --- a/service/protocol-plugin/plugins/mqtt-fan/src/fanserver.cpp +++ b/service/protocol-plugin/plugins/mqtt-fan/src/fanserver.cpp @@ -73,7 +73,7 @@ struct plugin_data_t bool flag; }; - +struct mosquitto *myMosquitto; class FanResource { public: @@ -176,6 +176,14 @@ class FanResource if (rep.getValue("power", m_power)) { cout << "\t\t\t\t" << "power: " << m_power << endl; + if (m_power == 1) + { + mosquitto_publish(myMosquitto, NULL, "actuators/fan", 32, "onfan", 0, true); + } + else + { + mosquitto_publish(myMosquitto, NULL, "actuators/fan", 32, "offfan", 0, true); + } } else { @@ -384,7 +392,6 @@ class FanResource }; // Create the instance of the resource class (in this case instance of class 'FanResource'). -struct mosquitto *myMosquitto; // ChangeFanRepresentaion is an observation function, // which notifies any changes to the resource to stack diff --git a/service/protocol-plugin/plugins/mqtt-light/src/lightserver.cpp b/service/protocol-plugin/plugins/mqtt-light/src/lightserver.cpp index 9f30718..d7146af 100644 --- a/service/protocol-plugin/plugins/mqtt-light/src/lightserver.cpp +++ b/service/protocol-plugin/plugins/mqtt-light/src/lightserver.cpp @@ -73,7 +73,7 @@ struct plugin_data_t bool flag; }; - +struct mosquitto *myMosquitto; class LightResource { public: @@ -176,6 +176,14 @@ class LightResource if (rep.getValue("power", m_power)) { cout << "\t\t\t\t" << "power: " << m_power << endl; + if (m_power == 1) + { + mosquitto_publish(myMosquitto, NULL, "actuators/fan", 32, "onfan", 0, true); + } + else + { + mosquitto_publish(myMosquitto, NULL, "actuators/fan", 32, "offfan", 0, true); + } } else { @@ -384,7 +392,6 @@ class LightResource }; // Create the instance of the resource class (in this case instance of class 'LightResource'). -struct mosquitto *myMosquitto; // ChangeLightRepresentaion is an observation function, // which notifies any changes to the resource to stack diff --git a/service/protocol-plugin/sample-app/linux/mqtt/mqttclient.cpp b/service/protocol-plugin/sample-app/linux/mqtt/mqttclient.cpp index 59ec85d..c9fc4e7 100644 --- a/service/protocol-plugin/sample-app/linux/mqtt/mqttclient.cpp +++ b/service/protocol-plugin/sample-app/linux/mqtt/mqttclient.cpp @@ -27,17 +27,15 @@ #include "OCPlatform.h" #include "PluginManager.h" #include "OCApi.h" -#include using namespace OC; using namespace OIC; +const int SUCCESS_RESPONSE = 0; std::shared_ptr curFanResource; -std::shared_ptr curLightResource; static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe; - -time_t timer; // Define the timer -struct tm *tblock; // Define a structure for time block +int count = 0; +void putFanRepresentation(std::shared_ptr resource); class Fan { @@ -54,21 +52,6 @@ class Fan Fan myfan; -class Light -{ - public: - - bool m_state; - int m_power; - std::string m_name; - - Light() : m_state(false), m_power(0), m_name("") - { - } -}; - -Light mylight; - int observe_count() { static int oc = 0; @@ -222,7 +205,7 @@ void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, cons std::cout << "\tpower: " << myfan.m_power << std::endl; std::cout << "\tname: " << myfan.m_name << std::endl; - postFanRepresentation(curFanResource); + putFanRepresentation(curFanResource); } else { @@ -241,8 +224,11 @@ void putFanRepresentation(std::shared_ptr resource) std::cout << "Putting fan representation..." << std::endl; myfan.m_state = true; - myfan.m_power = 15; - + if (myfan.m_power == 1) + myfan.m_power = 0; + else + myfan.m_power = 1; + sleep(5); rep.setValue("state", myfan.m_state); rep.setValue("power", myfan.m_power); @@ -276,28 +262,6 @@ void onFanGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, c } } -void onLightGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) -{ - if (eCode == OC_STACK_OK) - { - std::cout << "GET Light request was successful" << std::endl; - std::cout << "Resource URI: " << rep.getUri() << std::endl; - - 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; - } - else - { - std::cout << "onGET Response error: " << eCode << std::endl; - std::exit(-1); - } -} - // Local function to get representation of fan resource void getFanRepresentation(std::shared_ptr resource) @@ -312,18 +276,6 @@ void getFanRepresentation(std::shared_ptr resource) } } -void getLightRepresentation(std::shared_ptr resource) -{ - if (resource) - { - std::cout << "Getting Light Representation..." << std::endl; - // Invoke resource's get API with the callback parameter - - QueryParamsMap test; - resource->get(test, &onLightGet); - } -} - // Callback to found resources void foundResourceFan(std::shared_ptr resource) { @@ -366,65 +318,7 @@ void foundResourceFan(std::shared_ptr resource) { curFanResource = resource; // Call a local function which will internally invoke get API on the resource pointer - getFanRepresentation(resource); - } - } - else - { - // Resource is invalid - std::cout << "Resource is invalid" << std::endl; - } - - } - catch (std::exception &e) - { - //log(e.what()); - } -} - -// Callback to found resources -void foundResourceLight(std::shared_ptr resource) -{ - if (curLightResource) - { - std::cout << "Found another resource, ignoring" << std::endl; - } - - std::string resourceURI; - std::string hostAddress; - try - { - // Do some operations with resource object. - if (resource) - { - std::cout << "DISCOVERED Resource:" << std::endl; - // Get the resource URI - resourceURI = resource->uri(); - std::cout << "\tURI of the resource: " << resourceURI << std::endl; - - // Get the resource host address - hostAddress = resource->host(); - std::cout << "\tHost address of the resource: " << hostAddress << std::endl; - - // Get the resource types - std::cout << "\tList of resource types: " << std::endl; - for (auto &resourceTypes : resource->getResourceTypes()) - { - std::cout << "\t\t" << resourceTypes << std::endl; - } - - // Get the resource interfaces - std::cout << "\tList of resource interfaces: " << std::endl; - for (auto &resourceInterfaces : resource->getResourceInterfaces()) - { - std::cout << "\t\t" << resourceInterfaces << std::endl; - } - - if (resourceURI == "/a/light") - { - curLightResource = resource; - // Call a local function which will internally invoke get API on the resource pointer - getLightRepresentation(resource); + putFanRepresentation(curFanResource); } } else @@ -448,54 +342,6 @@ void PrintUsage() std::cout << " ObserveType : 2 - ObserveAll" << std::endl; } -void client1() -{ - std::cout << "in client1\n"; - - // OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", - // foundResource1); - std::cout << "starting findResource = core.fan" << std::endl; - // Find all resources - OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.fan", &foundResourceFan); - // Get time of day - timer = time(NULL); - // Converts date/time to a structure - tblock = localtime(&timer); - std::cout << "Finding Fan Resource... time : " << asctime(tblock) << std::endl; - while (1) - { - std::cout << "Get Fan Resource....." << std::endl; - sleep(5); - getFanRepresentation(curFanResource); - } -} - -void client2() -{ - std::cout << "in client2\n"; - - // OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", - // foundResource2); - - std::cout << "starting findResource = core.light" << std::endl; - // Find all resources - OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResourceLight); - // Get time of day - timer = time(NULL); - // Converts date/time to a structure - tblock = localtime(&timer); - std::cout << "Finding Light Resource... time : " << asctime(tblock) << std::endl; - - - - while (1) - { - std::cout << "Get Light Resource....." << std::endl; - sleep(5); - getLightRepresentation(curLightResource); - } -} - int main(int argc, char *argv[]) @@ -539,86 +385,15 @@ int main(int argc, char *argv[]) try { - - std::cout << "Created Platform..." << std::endl; - PluginManager *m_pm = new PluginManager(); - - std::cout << "==== 1st TEST CASE ===" << std::endl; - std::cout << "======================" << std::endl; std::cout << "start light Plugin by Resource Type" << std::endl; - - m_pm->startPlugins("ResourceType", "oic.light"); - + m_pm->startPlugins("ResourceType", "oic.fan"); sleep(2); - - std::cout << "\n==== 2nd TEST CASE =====" << std::endl; - std::cout << "======================" << std::endl; - std::cout << "Get Plugin List\n" << std::endl; - std::vector user_plugin; - - user_plugin = m_pm->getPlugins(); - - for (unsigned int i = 0; i < user_plugin.size(); i++) - { - std::cout << "value Name = " << user_plugin[i].getName() << std::endl; - std::cout << "value ID = " << user_plugin[i].getID() << std::endl; - id = user_plugin[i].getID(); - } - std::cout << "\n===== 3rd TEST CASE =====" << std::endl; - std::cout << "======================" << std::endl; - std::cout << "Start Fan Plugin by Name\n" << std::endl; - - name = user_plugin[0].getName(); - m_pm->startPlugins(key, name); - - sleep(5); - - std::cout << "\n====== 4th TEST CASE ======" << std::endl; - std::cout << "========================" << std::endl; - std::cout << "Get Plugin Status\n" << std::endl; - - state = m_pm->getState(id); - - std::cout << "last plugin status : " << state << std::endl; - std::cout << "sleep 15 seconds please add new plugin in the plugin folder " << std::endl; - - sleep(15); - - std::cout << "\n==== 5th TEST CASE ====" << std::endl; - std::cout << "========================" << std::endl; - std::cout << "Rescan Plugins.........\n" << std::endl; - - m_pm->rescanPlugin(); - - std::cout << "\n==== 6th TEST CASE ====" << std::endl; - std::cout << "========================" << std::endl; - std::cout << "Try to start new resource type.........\n" << std::endl; - std::cout << "start oic.test resource" << std::endl; - - sleep(5); - - int flag = m_pm->startPlugins("ResourceType", "oic.test"); - - if (!flag) - std::cout << "There are no resouce type. Start plugin failed" << std::endl; - // Start each client in a seperate thread - - sleep(10); - - std::cout << "\n====== 7th TEST CASE =======" << std::endl; - std::cout << "========================" << std::endl; - std::cout << " start client to find resource" << std::endl; - - std::thread t1(client1); - t1.detach(); - - sleep(5); - - // Start each client in a seperate thread - std::thread t2(client2); - t2.detach(); - + // makes it so that all boolean values are printed as 'true/false' in this stream + std::cout.setf(std::ios::boolalpha); + // Find all resources + OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.fan", &foundResourceFan); + std::cout << "Finding Resource... " << std::endl; while (true) { // some operations @@ -632,4 +407,3 @@ int main(int argc, char *argv[]) return 0; } - -- 2.7.4