Guo Wenxue
2022-04-09 c3895785a268c067039ce30c0c77a8c2782c63fe
iotd/main.c
@@ -31,6 +31,7 @@
#define PROG_VERSION               "v1.0.0"
#define DAEMON_PIDFILE             "/tmp/.iotd.pid"
void control_thread_loop(void *args);
static void program_usage(char *progname)
{
@@ -59,7 +60,6 @@
    int                debug = 0;
    int                opt;
    char              *progname=NULL;
    float              lux = 0.0;
    struct option long_options[] = {
        {"conf", required_argument, NULL, 'c'},
@@ -103,33 +103,27 @@
    if( !conf_file )
        debug = 1;
    //printf("conf_file: %s debug:%d\n", conf_file, debug);
    if( parser_conf(conf_file, &ctx, debug)<0 )
    {
        fprintf(stderr, "Parser mqtted configure file failure\n");
        return -2;
    }
    return 0;
    if( hal_init(hal_ctx) < 0 )
    {
        log_err("Initialise hardware failure\n");
        return -3;
    }
    else
    {
        log_nrml("HAL initialise ok\n");
    }
    log_nrml("Initialise hardware okay.\n");
    install_default_signal();
    if( check_set_program_running(daemon, DAEMON_PIDFILE) < 0 ) 
        goto OUT;
    mosquitto_lib_init();
    mosquitto_lib_init();
    /*+--------------------------------------------+
     *|  Start  MQTT subsciber thread if enable    |
@@ -147,7 +141,6 @@
        }
    }
    /*+--------------------------------------------+
     *|  Start  MQTT publisher thread if enable    |
     *+--------------------------------------------+*/
@@ -164,31 +157,10 @@
        }
    }
    log_nrml("Start infrared monitor thread working...\n");
    while( ! g_signal.stop )
    {
        lux = tsl2561_get_lux();
        if( lux > hal_ctx->lux_threshold )
        {
            log_nrml("Lux[%.3f] > Treshold[%.3f], don't need auto light.\n", lux, hal_ctx->lux_threshold);
            sleep(30);
            continue;
        }
        log_nrml("start infrared monitor detect...\n");
        if( infrared_detect() )
        {
            log_nrml("Someone incoming detected by infrared\n");
            if( lux < hal_ctx->lux_threshold )
            {
                log_nrml("Lux[%.3f] < Treshold[%.3f], auto light on now..\n", lux, hal_ctx->lux_threshold);
                //turn_light(LIGHT_HALLWAY, ON);
                alarm(hal_ctx->gpio.light_intval);
            }
        }
    }
    /*+--------------------------------------------+
     *|      Control thread start dead loop        |
     *+--------------------------------------------+*/
    control_thread_loop(&ctx);
OUT:
    mosquitto_lib_cleanup();
@@ -199,3 +171,55 @@
} /* ----- End of main() ----- */
void control_thread_loop(void *args)
{
    iotd_ctx_t            *ctx = (iotd_ctx_t *)args;
    hal_ctx_t             *hal_ctx;
    float                  lux = 0.0;
    int                    rv;
    hal_ctx = &ctx->hal_ctx;
    log_dbg("infrared configured [%d], lux configured [%d]\n", hal_ctx->gpio.infrared_enable, hal_ctx->lux_enable);
    while( ! g_signal.stop )
    {
        if( hal_ctx->gpio.infrared_enable )
        {
            if( hal_ctx->lux_enable )
            {
                lux = tsl2561_get_lux();
                log_dbg("TSL2561 get Lux[%.3f] Treshold[%.3f].\n", lux, hal_ctx->lux_threshold);
                if( lux > hal_ctx->lux_threshold )
                {
                    log_nrml("Lux[%.3f] > Treshold[%.3f], don't need light on and sleep now.\n", lux, hal_ctx->lux_threshold);
                    if( hal_ctx->gpio.light_intval > 0)
                        sleep( hal_ctx->gpio.light_intval );
                    else
                        sleep(30);
                    continue;
                }
            }
            rv = infrared_detect();
            if( rv & FLAG_INFRARED_INDOOR )
            {
                log_nrml("Someone incoming detected by indoor infrared\n");
                thread_start(NULL, light_on_worker, "indoor" );
            }
            if( rv & FLAG_INFRARED_HALLWAY )
            {
                log_nrml("Someone incoming detected by hallway infrared\n");
                thread_start(NULL, light_on_worker, "hallway" );
            }
        }
        msleep(100);
    }
}