commit | author | age
|
e30a4c
|
1 |
/********************************************************************************* |
GW |
2 |
* Copyright: (C) 2019 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: main.c |
|
6 |
* Description: This is the lux/relay/infrared auto light program |
|
7 |
* |
|
8 |
* Version: 1.0.0(29/01/19) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "29/01/19 15:34:41" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
#include <stdio.h> |
|
14 |
#include <time.h> |
|
15 |
#include <unistd.h> |
|
16 |
#include <getopt.h> |
|
17 |
#include <libgen.h> |
|
18 |
#include <string.h> |
|
19 |
#include <math.h> |
|
20 |
|
|
21 |
#include <cjson/cJSON.h> |
|
22 |
#include <mosquitto.h> |
|
23 |
|
|
24 |
#include "util_proc.h" |
|
25 |
#include "logger.h" |
|
26 |
#include "hal.h" |
|
27 |
|
|
28 |
#include "conf.h" |
|
29 |
#include "mqtt.h" |
|
30 |
|
|
31 |
#define PROG_VERSION "v1.0.0" |
|
32 |
#define DAEMON_PIDFILE "/tmp/.iotd.pid" |
|
33 |
|
|
34 |
void control_thread_loop(void *args); |
|
35 |
|
|
36 |
static void program_usage(char *progname) |
|
37 |
{ |
|
38 |
printf("Usage: %s [OPTION]...\n", progname); |
|
39 |
printf(" %s is LingYun studio MQTT daemon program running on RaspberryPi\n", progname); |
|
40 |
|
|
41 |
printf("\nMandatory arguments to long options are mandatory for short options too:\n"); |
|
42 |
printf(" -d[debug ] Running in debug mode\n"); |
|
43 |
printf(" -c[conf ] Specify configure file\n"); |
|
44 |
printf(" -h[help ] Display this help information\n"); |
|
45 |
printf(" -v[version ] Display the program version\n"); |
|
46 |
|
|
47 |
printf("\n%s version %s\n", progname, PROG_VERSION); |
|
48 |
return; |
|
49 |
} |
|
50 |
|
|
51 |
int main (int argc, char **argv) |
|
52 |
{ |
|
53 |
int daemon = 1; |
|
54 |
pthread_t tid; |
|
55 |
iotd_ctx_t ctx; |
|
56 |
hal_ctx_t *hal_ctx = &ctx.hal_ctx; |
|
57 |
char *conf_file="/etc/iotd.conf"; |
|
58 |
int debug = 0; |
|
59 |
int opt; |
|
60 |
char *progname=NULL; |
|
61 |
|
|
62 |
struct option long_options[] = { |
|
63 |
{"conf", required_argument, NULL, 'c'}, |
|
64 |
{"debug", no_argument, NULL, 'd'}, |
|
65 |
{"version", no_argument, NULL, 'v'}, |
|
66 |
{"help", no_argument, NULL, 'h'}, |
|
67 |
{NULL, 0, NULL, 0} |
|
68 |
}; |
|
69 |
|
|
70 |
progname = (char *)basename(argv[0]); |
|
71 |
|
|
72 |
/* Parser the command line parameters */ |
|
73 |
while ((opt = getopt_long(argc, argv, "c:dvh", long_options, NULL)) != -1) |
|
74 |
{ |
|
75 |
switch (opt) |
|
76 |
{ |
|
77 |
case 'c': /* Set configure file */ |
|
78 |
conf_file = optarg; |
|
79 |
break; |
|
80 |
|
|
81 |
case 'd': /* Set debug running */ |
|
82 |
daemon = 0; |
|
83 |
debug = 1; |
|
84 |
break; |
|
85 |
|
|
86 |
case 'v': /* Get software version */ |
|
87 |
printf("%s version %s\n", progname, PROG_VERSION); |
|
88 |
return 0; |
|
89 |
|
|
90 |
case 'h': /* Get help information */ |
|
91 |
program_usage(progname); |
|
92 |
return 0; |
|
93 |
|
|
94 |
default: |
|
95 |
break; |
|
96 |
} |
|
97 |
|
|
98 |
} |
|
99 |
|
|
100 |
|
|
101 |
if( !conf_file ) |
|
102 |
debug = 1; |
|
103 |
|
|
104 |
if( parser_conf(conf_file, &ctx, debug)<0 ) |
|
105 |
{ |
|
106 |
fprintf(stderr, "Parser mqtted configure file failure\n"); |
|
107 |
return -2; |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
if( hal_init(hal_ctx) < 0 ) |
|
112 |
{ |
|
113 |
log_error("Initialise hardware failure\n"); |
|
114 |
return -3; |
|
115 |
} |
|
116 |
log_info("Initialise hardware okay.\n"); |
|
117 |
|
|
118 |
|
|
119 |
install_default_signal(); |
|
120 |
if( check_set_program_running(daemon, DAEMON_PIDFILE) < 0 ) |
|
121 |
goto OUT; |
|
122 |
|
|
123 |
|
|
124 |
mosquitto_lib_init(); |
|
125 |
|
|
126 |
/*+--------------------------------------------+ |
|
127 |
*| Start MQTT subsciber thread if enable | |
|
128 |
*+--------------------------------------------+*/ |
|
129 |
if( ctx.mqtt_ctx.sub_enable ) |
|
130 |
{ |
|
131 |
if( thread_start(&tid, mqtt_sub_worker, &ctx ) < 0 ) |
|
132 |
{ |
|
133 |
log_error("Start MQTT subsciber worker thread failure\n"); |
|
134 |
goto OUT; |
|
135 |
} |
|
136 |
else |
|
137 |
{ |
|
138 |
log_info("Start MQTT subsciber worker thread ok\n"); |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
/*+--------------------------------------------+ |
|
143 |
*| Start MQTT publisher thread if enable | |
|
144 |
*+--------------------------------------------+*/ |
|
145 |
if( ctx.mqtt_ctx.pub_enable ) |
|
146 |
{ |
|
147 |
if( thread_start(&tid, mqtt_pub_worker, &ctx ) < 0 ) |
|
148 |
{ |
|
149 |
log_error("Start MQTT publisher worker thread failure\n"); |
|
150 |
goto OUT; |
|
151 |
} |
|
152 |
else |
|
153 |
{ |
|
154 |
log_info("Start MQTT publisher worker thread ok\n"); |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
/*+--------------------------------------------+ |
|
159 |
*| Control thread start dead loop | |
|
160 |
*+--------------------------------------------+*/ |
|
161 |
control_thread_loop(&ctx); |
|
162 |
|
|
163 |
OUT: |
|
164 |
mosquitto_lib_cleanup(); |
|
165 |
hal_term(hal_ctx); |
|
166 |
log_close(); |
|
167 |
|
|
168 |
return 0; |
|
169 |
} /* ----- End of main() ----- */ |
|
170 |
|
|
171 |
|
|
172 |
void control_thread_loop(void *args) |
|
173 |
{ |
|
174 |
iotd_ctx_t *ctx = (iotd_ctx_t *)args; |
|
175 |
hal_ctx_t *hal_ctx; |
|
176 |
float lux = 0.0; |
|
177 |
int rv; |
|
178 |
|
|
179 |
hal_ctx = &ctx->hal_ctx; |
|
180 |
|
|
181 |
log_debug("infrared configured [%d], lux configured [%d]\n", hal_ctx->gpio.infrared_enable, hal_ctx->lux_enable); |
|
182 |
|
|
183 |
while( ! g_signal.stop ) |
|
184 |
{ |
|
185 |
if( hal_ctx->gpio.infrared_enable ) |
|
186 |
{ |
|
187 |
if( hal_ctx->lux_enable ) |
|
188 |
{ |
|
189 |
lux = tsl2561_get_lux(); |
|
190 |
|
|
191 |
log_debug("TSL2561 get Lux[%.3f] Treshold[%.3f].\n", lux, hal_ctx->lux_threshold); |
|
192 |
|
|
193 |
if( lux > hal_ctx->lux_threshold ) |
|
194 |
{ |
|
195 |
log_info("Lux[%.3f] > Treshold[%.3f], don't need light on and sleep now.\n", lux, hal_ctx->lux_threshold); |
|
196 |
if( hal_ctx->gpio.light_intval > 0) |
|
197 |
sleep( hal_ctx->gpio.light_intval ); |
|
198 |
else |
|
199 |
sleep(30); |
|
200 |
|
|
201 |
continue; |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
205 |
rv = infrared_detect(); |
|
206 |
if( rv & FLAG_INFRARED_INDOOR ) |
|
207 |
{ |
|
208 |
log_info("Someone incoming detected by indoor infrared\n"); |
|
209 |
thread_start(NULL, light_on_worker, "indoor" ); |
|
210 |
} |
|
211 |
|
|
212 |
if( rv & FLAG_INFRARED_HALLWAY ) |
|
213 |
{ |
|
214 |
log_info("Someone incoming detected by hallway infrared\n"); |
|
215 |
thread_start(NULL, light_on_worker, "hallway" ); |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
msleep(100); |
|
220 |
} |
|
221 |
} |
|
222 |
|
|
223 |
|