/*********************************************************************************
|
* Copyright: (C) 2019 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: main.c
|
* Description: This is the lux/relay/infrared auto light program
|
*
|
* Version: 1.0.0(29/01/19)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "29/01/19 15:34:41"
|
*
|
********************************************************************************/
|
#include <stdio.h>
|
#include <time.h>
|
#include <unistd.h>
|
#include <getopt.h>
|
#include <libgen.h>
|
#include <string.h>
|
#include <math.h>
|
|
#include <cjson/cJSON.h>
|
#include <mosquitto.h>
|
|
#include "util_proc.h"
|
#include "logger.h"
|
#include "hal.h"
|
|
#include "conf.h"
|
#include "mqtt.h"
|
|
#define PROG_VERSION "v1.0.0"
|
#define DAEMON_PIDFILE "/tmp/.iotd.pid"
|
|
void control_thread_loop(void *args);
|
|
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)
|
{
|
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_error("Initialise hardware failure\n");
|
return -3;
|
}
|
log_info("Initialise hardware okay.\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_error("Start MQTT subsciber worker thread failure\n");
|
goto OUT;
|
}
|
else
|
{
|
log_info("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_error("Start MQTT publisher worker thread failure\n");
|
goto OUT;
|
}
|
else
|
{
|
log_info("Start MQTT publisher worker thread ok\n");
|
}
|
}
|
|
/*+--------------------------------------------+
|
*| Control thread start dead loop |
|
*+--------------------------------------------+*/
|
control_thread_loop(&ctx);
|
|
OUT:
|
mosquitto_lib_cleanup();
|
hal_term(hal_ctx);
|
log_close();
|
|
return 0;
|
} /* ----- 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_debug("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_debug("TSL2561 get Lux[%.3f] Treshold[%.3f].\n", lux, hal_ctx->lux_threshold);
|
|
if( lux > hal_ctx->lux_threshold )
|
{
|
log_info("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_info("Someone incoming detected by indoor infrared\n");
|
thread_start(NULL, light_on_worker, "indoor" );
|
}
|
|
if( rv & FLAG_INFRARED_HALLWAY )
|
{
|
log_info("Someone incoming detected by hallway infrared\n");
|
thread_start(NULL, light_on_worker, "hallway" );
|
}
|
}
|
|
msleep(100);
|
}
|
}
|