/********************************************************************************* * Copyright: (C) 2019 LingYun IoT System Studio * All rights reserved. * * Filename: main.c * Description: This file * * Version: 1.0.0(29/01/19) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "29/01/19 15:34:41" * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include "lylib/util_time.h" #include "lylib/logger.h" #include "lylib/util_proc.h" #include "hal/hal.h" #include "conf/conf.h" #include "mqtt/mqtt.h" #define PROG_VERSION "v1.0.0" #define DAEMON_PIDFILE "/tmp/.iotd.pid" void control_thread_loop(void *ctx); static void program_usage(char *progname) { printf("Usage: %s [OPTION]...\n", progname); printf(" %s is LingYun studio MQTT daemon program running on RaspberryPi\n", progname); printf("\nMandatory arguments to long options are mandatory for short options too:\n"); printf(" -d[debug ] Running in debug mode\n"); printf(" -c[conf ] Specify configure file\n"); printf(" -h[help ] Display this help information\n"); printf(" -v[version ] Display the program version\n"); printf("\n%s version %s\n", progname, PROG_VERSION); return; } int main (int argc, char **argv) { float temp; float rh; int daemon = 1; pthread_t tid; iotd_ctx_t ctx; hal_ctx_t *hal_ctx = &ctx.hal_ctx; char *conf_file="/etc/iotd.conf"; int debug = 0; int opt; char *progname=NULL; struct option long_options[] = { {"conf", required_argument, NULL, 'c'}, {"debug", no_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; progname = (char *)basename(argv[0]); /* Parser the command line parameters */ while ((opt = getopt_long(argc, argv, "c:dvh", long_options, NULL)) != -1) { switch (opt) { case 'c': /* Set configure file */ conf_file = optarg; break; case 'd': /* Set debug running */ daemon = 0; debug = 1; break; case 'v': /* Get software version */ printf("%s version %s\n", progname, PROG_VERSION); return 0; case 'h': /* Get help information */ program_usage(progname); return 0; default: break; } } if( !conf_file ) debug = 1; if( parser_conf(conf_file, &ctx, debug)<0 ) { fprintf(stderr, "Parser mqtted configure file failure\n"); return -2; } if( hal_init(hal_ctx) < 0 ) { log_err("Initialise hardware failure\n"); return -3; } log_nrml("Initialise hardware okay.\n"); return 0; /* todo: debug and removed */ install_default_signal(); if( check_set_program_running(daemon, DAEMON_PIDFILE) < 0 ) goto OUT; mosquitto_lib_init(); /*+--------------------------------------------+ *| Start MQTT subsciber thread if enable | *+--------------------------------------------+*/ if( ctx.mqtt_ctx.sub_enable ) { if( thread_start(&tid, mqtt_sub_worker, &ctx ) < 0 ) { log_fatal("Start MQTT subsciber worker thread failure\n"); goto OUT; } else { log_nrml("Start MQTT subsciber worker thread ok\n"); } } /*+--------------------------------------------+ *| Start MQTT publisher thread if enable | *+--------------------------------------------+*/ if( ctx.mqtt_ctx.pub_enable ) { if( thread_start(&tid, mqtt_pub_worker, &ctx ) < 0 ) { log_fatal("Start MQTT publisher worker thread failure\n"); goto OUT; } else { log_nrml("Start MQTT publisher worker thread ok\n"); } } /*+--------------------------------------------+ *| Control thread start dead loop | *+--------------------------------------------+*/ control_thread_loop(&ctx); OUT: mosquitto_lib_cleanup(); hal_term(hal_ctx); logger_term(); return 0; } /* ----- End of main() ----- */ void control_thread_loop(void *args) { iotd_ctx_t *ctx = (iotd_ctx_t *)ctx; hal_ctx_t *hal_ctx; float lux = 0.0; hal_ctx = &ctx->hal_ctx; log_nrml("Start control thread working...\n"); while( ! g_signal.stop ) { if( hal_ctx->lux_enable ) { lux = tsl2561_get_lux(); if( lux > hal_ctx->lux_threshold ) { log_nrml("Lux[%.3f] > Treshold[%.3f], don't need light on.\n", lux, hal_ctx->lux_threshold); sleep(30); continue; } } #if 0 log_nrml("start infrared monitor detect...\n"); if( infrared_detect() ) { log_nrml("Someone incoming detected by infrared\n"); log_nrml("Lux[%.3f] < Treshold[%.3f], auto light on now..\n", lux, hal_ctx->lux_threshold); } #endif } }