Esempio n. 1
0
int main()
{
    // Create PlatformConfig object
    PlatformConfig cfg
    {   OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0",
        // By setting to "0.0.0.0", it binds to all available interfaces
        0,// Uses randomly available port
        OC::QualityOfService::LowQos };

    OCPlatform::Configure(cfg);
    try
    {
        // Create the instance of the resource class
        // (in this case instance of class 'LightResource').
        FanResource myFan;

        // Invoke createResource function of class light.
        myFan.createResource();

        while(OCProcess() == OC_STACK_OK)
        {
            sleep(0.5);
        }
    }
    catch (OCException e)
    {
        //log(e.what());
    }

    // No explicit call to stop the platform.
    // When OCPlatform::destructor is invoked, internally we do platform cleanup

    return 0;
}
Esempio n. 2
0
//int start_fanserver(void*)                // 1
void *start_fanserver(void *d)      // 2
{
    // Create PlatformConfig object
    PlatformConfig cfg
    {
        OC::ServiceType::InProc,
        OC::ModeType::Both,
        "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
        0,         // Uses randomly available port
        OC::QualityOfService::LowQos
    };

    OCPlatform::Configure(cfg);

    printf("start_fanserver [mosquitto] Null\n");
    try
    {
        FanResource myFanResource;
        mosquitto_lib_init();
        myMosquitto = mosquitto_new("MQTT plug-in", true, NULL);
        if (!myMosquitto)
        {
            printf("[mosquitto] Null\n");
            printf("You need to install mqtt broker\n");
        }
        else
        {
            printf("Mosquitto is working\n");
        }

        mosquitto_connect(myMosquitto, "127.0.0.1", 1883, 60);
        printf("Mosquitto Connection is done\n");
        myFanResource.createResource();
        // Get time of day
        timer = time(NULL);
        // Converts date/time to a structure
        tblock = localtime(&timer);
        // Output ASCII data/time
        printf("FanReousrce reigishter time is: %s", asctime(tblock));
        // Perform app tasks
        while (true)
        {
            // some tasks
        }
    }
    catch (OCException e)
    {
        //log(e.what());
    }
    // No explicit call to stop the platform.
    // When OCPlatform destructor is invoked, internally we do platform cleanup
    mosquitto_destroy(myMosquitto);

    mosquitto_lib_cleanup();
    printf("start_fanserver finish\n");
    pthread_exit((void *)0);
}
Esempio n. 3
0
// ChangeFanRepresentaion is an observation function,
// which notifies any changes to the resource to stack
// via notifyObservers
void *ChangeFanRepresentation (void *param)
{
    FanResource *fanPtr = (FanResource *) param;

    // This function continuously monitors for the changes
    while (1)
    {
        sleep (5);

        if (gObservation)
        {
            // If under observation if there are any changes to the fan resource
            // we call notifyObservors
            //
            // For demostration we are changing the power value and notifying.
            fanPtr->m_power += 10;

            cout << "\nPower updated to : " << fanPtr->m_power << endl;
            cout << "Notifying observers with resource handle: " << fanPtr->getHandle() << endl;

            OCStackResult result = OC_STACK_OK;

            if (isListOfObservers)
            {
                std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());

                resourceResponse->setErrorCode(200);
                resourceResponse->setResourceRepresentation(fanPtr->get(), DEFAULT_INTERFACE);

                result = OCPlatform::notifyListOfObservers(  fanPtr->getHandle(),
                         fanPtr->m_interestedObservers,
                         resourceResponse);
            }
            else
            {
                result = OCPlatform::notifyAllObservers(fanPtr->getHandle());
            }

            if (OC_STACK_NO_OBSERVERS == result)
            {
                cout << "No More observers, stopping notifications" << endl;
                gObservation = 0;
            }
        }
    }

    return NULL;
}
Esempio n. 4
0
void * handleSlowResponse(void *param, std::shared_ptr< OCResourceRequest > pRequest)
{
    // This function handles slow response case
    FanResource* fanPtr = (FanResource*) param;
    // Induce a case for slow response by using sleep
    std::cout << "SLOW response" << std::endl;
    sleep(10);

    auto pResponse = std::make_shared< OC::OCResourceResponse >();
    pResponse->setRequestHandle(pRequest->getRequestHandle());
    pResponse->setResourceHandle(pRequest->getResourceHandle());
    pResponse->setResourceRepresentation(fanPtr->get());
    pResponse->setErrorCode(200);
    pResponse->setResponseResult(OC_EH_OK);

    // Set the slow response flag back to false
    isSlowResponse = false;
    OCPlatform::sendResponse(pResponse);
    return NULL;
}