Example #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;
}
Example #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);
}