Ejemplo n.º 1
0
int main(void)
{
    static cfg_bool_t verbose = cfg_false;
    static char *server = NULL;
    static double delay = 1.356e-32;
    static char *username = NULL;

    /* Although the macro used to specify an integer option is called
     * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system
     * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64
     * architectures), you will get weird effects if you use an int here.
     *
     * If you use the regular (non-"simple") options, ie CFG_INT() and use
     * cfg_getint(), this is not a problem as the data types are implicitly
     * cast.
     */
    static long int debug = 1;

    cfg_opt_t opts[] = {
        CFG_SIMPLE_BOOL("verbose", &verbose),
        CFG_SIMPLE_STR("server", &server),
        CFG_SIMPLE_STR("user", &username),
        CFG_SIMPLE_INT("debug", &debug),
        CFG_SIMPLE_FLOAT("delay", &delay),
        CFG_END()
    };
    cfg_t *cfg;

    /* set default value for the server option */
    server = strdup("gazonk");

    cfg = cfg_init(opts, 0);
    cfg_parse(cfg, "simple.conf");

    printf("verbose: %s\n", verbose ? "true" : "false");
    printf("server: %s\n", server);
    printf("username: %s\n", username);
    printf("debug: %ld\n", debug);
    printf("delay: %G\n", delay);

    printf("setting username to 'foo'\n");
    /* using cfg_setstr here is not necessary at all, the equivalent
     * code is:
     *   free(username);
     *   username = strdup("foo");
     */
    cfg_setstr(cfg, "user", "foo");
    printf("username: %s\n", username);

    /* print the parsed values to another file */
    {
        FILE *fp = fopen("simple.conf.out", "w");
        cfg_print(cfg, fp);
        fclose(fp);
    }

    cfg_free(cfg);

    /* You are responsible for freeing string values. */
    free(server);
    free(username);

    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char* argv[] )
{
    int i;
    pthread_t *pS0Thread[NUM_GPIOS];
    pthread_t *pW1Thread;
    cfg_t *cfg;
    char logBuffer[LOG_BUFFER_SIZE+1];

    cfg_opt_t opts[] = {
        CFG_SIMPLE_FLOAT("thermal_coefficient", &confOptions.termCoefficent),
        CFG_SIMPLE_INT("flow_per_hour", &confOptions.literAnHour),
        CFG_SIMPLE_INT("log_level", &confOptions.logLevel),
        CFG_SIMPLE_INT("temp_reader_delay", &confOptions.tempReaderDelay),
        CFG_SIMPLE_INT("temp_reader_interval", &confOptions.tempReaderIntval),
        CFG_SIMPLE_BOOL("push_bullet_notification", &confOptions.bulletNotificationEnabled),
        CFG_SIMPLE_BOOL("email_notification", &confOptions.emailNotificationEnabled),
        CFG_SIMPLE_STR("emoncms_api_key", &confOptions.emonApiKey),
        CFG_SIMPLE_BOOL("client_watchdog", &confOptions.watchdogEnabled),
        CFG_SIMPLE_INT("gpio_4_s0_in_use", &confOptions.numGPIO4S0InUse),
        CFG_SIMPLE_STR("therm_id_vorlauf", &confOptions.thermIdVorlauf),
        CFG_SIMPLE_STR("therm_id_ruecklauf", &confOptions.thermIdRuecklauf),
        CFG_SIMPLE_INT("oil_consumption_per_hour", &confOptions.oilConsumptionPerHour),
        CFG_END()
    };

    confOptions.run_mode                  = DEFAULT_RUN_MODE;
    confOptions.termCoefficent            = CONF_DEFAULT_THERMCOEFF;
    confOptions.literAnHour               = CONF_DEFAULT_LITERANHOUR;
    confOptions.logLevel                  = CONF_DEFAULT_LOGLEVEL;
    confOptions.tempReaderDelay           = CONF_DEFAULT_READERDELAY;
    confOptions.tempReaderIntval          = CONF_DEFAULT_READERINTVAL;
    confOptions.bulletNotificationEnabled = CONF_DEFAULT_BULLETNOTICICATION;
    confOptions.emailNotificationEnabled  = CONF_DEFAULT_EMAILNOTICICATION;
    confOptions.emonApiKey                = CONF_DEFAULT_APIKEY;
    confOptions.watchdogEnabled           = CONF_DEFAULT_WATCHDOGENABLED;
    confOptions.numGPIO4S0InUse           = CONF_DEFAULT_NUMS0PINS;
    confOptions.thermIdVorlauf            = strdup(CONF_DEFAULT_THERMIDVORLAUF);
    confOptions.thermIdRuecklauf          = strdup(CONF_DEFAULT_THERMIDRUECKLAUF);


    if( (cfg = cfg_init(opts, 0)) != NULL )
    {
        cfg_parse(cfg, CONF_FILE_PATH);

#ifdef DEBUG_ALL
    printf("thermal_coefficient ....: %f\n",  confOptions.termCoefficent);
    printf("flow_per_hour ..........: %ld\n", confOptions.literAnHour);
    printf("log_level ..............: %ld\n", confOptions.logLevel);
    printf("temp_reader_delay ......: %ld\n", confOptions.tempReaderDelay);
    printf("temp_reader_interval ...: %ld\n", confOptions.tempReaderIntval);
    printf("push_bullet_notification: %s\n",  confOptions.bulletNotificationEnabled?"true":"false");
    printf("email_notification .....: %s\n",  confOptions.emailNotificationEnabled?"true":"false");
    printf("emoncms_api_key ........: %s\n",  confOptions.emonApiKey?confOptions.emonApiKey:"NULL");
    printf("client_watchdog ........: %s\n",  confOptions.watchdogEnabled?"true":"false");
    printf("gpio_4_s0_in_use .......: %ld\n", confOptions.numGPIO4S0InUse);
    printf("therm_id_vorlauf .......: %s\n",  confOptions.thermIdVorlauf?confOptions.thermIdVorlauf:"NULL");
    printf("therm_id_ruecklauf .....: %s\n",  confOptions.thermIdRuecklauf?confOptions.thermIdRuecklauf:"NULL");
    printf("oil_consumption_per_hour: %ld\n", confOptions.oilConsumptionPerHour);
#endif // DEBUG_ALL

    }
    else
    {
exit(3);
    }

    mainEndFlag = 0;


    if( confOptions.emonApiKey != NULL && confOptions.thermIdVorlauf != NULL &&
        confOptions.thermIdRuecklauf != NULL && confOptions.tempReaderDelay <= READER_DELAY_MAX &&
        confOptions.tempReaderDelay >= 0 )
    {
        if (gpioInitialise() < 0)
        {
            // pigpio initialisation failed.
            fprintf(stderr, "cannot initialize pigpio!\n");
            return(1);
        }
        else
        {
            signal( SIGINT,  sig_handler );
            signal( SIGQUIT, sig_handler );
            signal( SIGKILL, sig_handler );
            signal( SIGTERM, sig_handler );
            signal( SIGALRM, alrm_handler );

            // pigpio initialisation okay ...
            for( i = 0; i < NUM_GPIOS; i++)
            {

#ifdef DEBUG_ALL
printf("Index %d\n", i);
#endif // DEBUG_ALL
                gpioSetMode( gpioInUse[i].bcm_gpio, PI_INPUT );         // set GPIO as an input
                gpioSetPullUpDown( gpioInUse[i].bcm_gpio, PI_PUD_DOWN); // activate internal pull-up
                // create thread for this gpio
                pS0Thread[i] = gpioStartThread(tickerThread, (void*) &gpioInUse[i]); 
//                gpioSetAlertFunc( gpioInUse[i].bcm_gpio, ticker[i] );      // set event handler "ticker"

            }

            if( (pW1Thread = gpioStartThread(W1ThermThread, NULL)) == NULL )
            {
                fprintf(stderr, "W1Thread failed to start ...\n");

                for( i = 0; i < NUM_GPIOS; i++)
                {
                    if( pS0Thread[i] != NULL )
                    {
                        gpioStopThread( pS0Thread[i] );
                    }
                }
                gpioTerminate();
            }
            else
            {
                while( !mainEndFlag )
                {
                    gpioSleep(PI_TIME_RELATIVE, 0, 10000); // hold 1/100 sec high level
                }

                for( i = 0; i < NUM_GPIOS; i++)
                {
                    if( pS0Thread[i] != NULL )
                    {
                        gpioStopThread( pS0Thread[i] );
                    }
                }

                gpioStopThread( pW1Thread );

                gpioTerminate();

                cfg_free(cfg);

                if(confOptions.emonApiKey)
                {
                    free(confOptions.emonApiKey);
                }

                if(confOptions.thermIdVorlauf)
                {
                    free(confOptions.thermIdVorlauf);
                }

                if(confOptions.thermIdRuecklauf)
                {
                    free(confOptions.thermIdRuecklauf);
                }
            }
        }
    }
    else
    {
        confFail();
    }

    return(0);
}
Ejemplo n.º 3
0
double  m_rate = 0.01,
        c_rate = 0.70;
int64_t max_iter = 10000,
        data_rows = 0,
        data_cols = 0;
char    *data_file = NULL,
        *centroids_file = NULL,
        *fitness_file = NULL,
        *cluster_file = NULL;

// The configuration file parsing mappings
cfg_opt_t opts[] = {
    CFG_SIMPLE_INT("n_clusters", &n_clusters),
    CFG_SIMPLE_INT("trials", &trials),
    CFG_SIMPLE_INT("size", &size),
    CFG_SIMPLE_FLOAT("m_rate", &m_rate),
    CFG_SIMPLE_FLOAT("c_rate", &c_rate),
    CFG_SIMPLE_INT("max_iter", &max_iter),
    CFG_SIMPLE_INT("data_rows", &data_rows),
    CFG_SIMPLE_INT("data_cols", &data_cols),
    CFG_SIMPLE_STR("data_file", &data_file),
    CFG_SIMPLE_STR("centroids_file", &centroids_file),
    CFG_SIMPLE_STR("fitness_file", &fitness_file),
    CFG_SIMPLE_STR("cluster_file", &cluster_file),
    CFG_END()
};
cfg_t *cfg;


/**
 * The E-means algorithm, uses a genetic algorithm to optimize the parameters