/********************************************************************************* * 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" 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; float lux = 0.0; 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; //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"); } 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"); } } 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); } } } OUT: mosquitto_lib_cleanup(); hal_term(hal_ctx); logger_term(); return 0; } /* ----- End of main() ----- */