add: pulse mode
add: editorconfig
This commit is contained in:
parent
679175f1a9
commit
0f342b4aa8
13 changed files with 151 additions and 70 deletions
12
.editorconfig
Normal file
12
.editorconfig
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# EditorConfig is awesome:
|
||||||
|
https://EditorConfig.org
|
||||||
|
|
||||||
|
# top-most EditorConfig file
|
||||||
|
root = true
|
||||||
|
|
||||||
|
# Unix-style newlines with a newline ending every file
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(controller
|
project(controller
|
||||||
VERSION 0.2.0
|
VERSION 0.2.1
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(controller src/main.c)
|
add_executable(controller src/main.c)
|
||||||
|
|
|
@ -54,8 +54,10 @@ inverted = 1
|
||||||
driver = gpio
|
driver = gpio
|
||||||
pin = 16
|
pin = 16
|
||||||
inverted = 1
|
inverted = 1
|
||||||
|
pulse-duration = 3
|
||||||
|
|
||||||
[relay-9]
|
[relay-9]
|
||||||
driver = gpio
|
driver = gpio
|
||||||
pin = 15
|
pin = 15
|
||||||
inverted = 1
|
inverted = 1
|
||||||
|
pulse-duration = 3
|
||||||
|
|
|
@ -11,6 +11,7 @@ typedef struct
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
int inverted;
|
int inverted;
|
||||||
relay_driver_t driver;
|
relay_driver_t driver;
|
||||||
|
uint8_t pulse_duration;
|
||||||
} config_relay_t;
|
} config_relay_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|
|
@ -24,6 +24,7 @@ typedef enum
|
||||||
COMMAND_MAPPING_SCHEDULE_ID = 4,
|
COMMAND_MAPPING_SCHEDULE_ID = 4,
|
||||||
COMMAND_MAPPING_PERIODS_COUNT = 5,
|
COMMAND_MAPPING_PERIODS_COUNT = 5,
|
||||||
COMMAND_MAPPING_PERIODS_BLOB = 6,
|
COMMAND_MAPPING_PERIODS_BLOB = 6,
|
||||||
|
COMMAND_MAPPING_PULSE_DURATION = 7,
|
||||||
} control_mapping_t;
|
} control_mapping_t;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
|
@ -36,6 +37,7 @@ typedef enum
|
||||||
COMMAND_CODE_GET_SCHEDULE = 103,
|
COMMAND_CODE_GET_SCHEDULE = 103,
|
||||||
COMMAND_CODE_SET_RELAY_NAME = 104,
|
COMMAND_CODE_SET_RELAY_NAME = 104,
|
||||||
COMMAND_CODE_GET_RELAY_NAME = 105,
|
COMMAND_CODE_GET_RELAY_NAME = 105,
|
||||||
|
COMMAND_CODE_PULSE = 200,
|
||||||
} command_code_t;
|
} command_code_t;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
|
|
|
@ -12,6 +12,8 @@ typedef struct
|
||||||
{
|
{
|
||||||
uint8_t number;
|
uint8_t number;
|
||||||
int is_on;
|
int is_on;
|
||||||
|
int is_on_schedule;
|
||||||
|
int pulse_timer;
|
||||||
int sent_to_broker;
|
int sent_to_broker;
|
||||||
char name[MAX_NAME_LENGTH + 1];
|
char name[MAX_NAME_LENGTH + 1];
|
||||||
schedule_t *schedules[7];
|
schedule_t *schedules[7];
|
||||||
|
@ -53,7 +55,7 @@ int
|
||||||
relay_save(relay_t *relay, MDB_env *mdb_env);
|
relay_save(relay_t *relay, MDB_env *mdb_env);
|
||||||
|
|
||||||
int
|
int
|
||||||
relay_is_active(relay_t *relay, struct tm *time_struct);
|
relay_is_on_schedule(relay_t *relay, struct tm *time_struct);
|
||||||
|
|
||||||
void
|
void
|
||||||
relay_free(relay_t *relay);
|
relay_free(relay_t *relay);
|
||||||
|
|
|
@ -4,12 +4,13 @@
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
|
|
||||||
#ifdef WIRING_PI_DEBUG
|
#ifdef WIRING_PI_DEBUG
|
||||||
#define wiringPiSetup() LOG_DEBUG("wiringPi wiringPiSetup()\n")
|
#define LOG_WIRING_PI LOG_TRACE
|
||||||
#define wiringPiSetupSys() LOG_DEBUG("wiringPi wiringPiSetupSys()\n")
|
#define wiringPiSetup() LOG_WIRING_PI("wiringPi wiringPiSetup()\n")
|
||||||
#define pinMode(x,y) LOG_DEBUG("wiringPi pinMode(%d, %d)\n", x, y)
|
#define wiringPiSetupSys() LOG_WIRING_PI("wiringPi wiringPiSetupSys()\n")
|
||||||
#define digitalWrite(x,y) LOG_DEBUG("wiringPi digitalWrite(%d, %d)\n", x, y)
|
#define pinMode(x,y) LOG_WIRING_PI("wiringPi pinMode(%d, %d)\n", x, y)
|
||||||
|
#define digitalWrite(x,y) LOG_WIRING_PI("wiringPi digitalWrite(%d, %d)\n", x, y)
|
||||||
|
|
||||||
#define piFaceSetup(x) LOG_DEBUG("wiringPi piFaceSetup(%d)\n", x)
|
#define piFaceSetup(x) LOG_WIRING_PI("wiringPi piFaceSetup(%d)\n", x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* CONTROLLER_WIRING_DEBUG_H */
|
#endif /* CONTROLLER_WIRING_DEBUG_H */
|
||||||
|
|
|
@ -18,6 +18,22 @@
|
||||||
#include <mpack.h>
|
#include <mpack.h>
|
||||||
#include <models/schedule.h>
|
#include <models/schedule.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
handler_command_pulse(mpack_node_t map, controller_t *controller)
|
||||||
|
{
|
||||||
|
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
|
||||||
|
|
||||||
|
relay_t *target_relay = controller->relays[relay_num];
|
||||||
|
(void)target_relay;
|
||||||
|
|
||||||
|
uint8_t duration = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_PULSE_DURATION));
|
||||||
|
if(duration == 0)
|
||||||
|
{
|
||||||
|
duration = global_config.relay_configs[relay_num].pulse_duration;
|
||||||
|
}
|
||||||
|
target_relay->pulse_timer = duration;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handler_command_set_name(mpack_node_t map, controller_t *controller)
|
handler_command_set_name(mpack_node_t map, controller_t *controller)
|
||||||
{
|
{
|
||||||
|
@ -72,56 +88,60 @@ void
|
||||||
handler_command(struct mg_connection *c, int ev, void *ev_data)
|
handler_command(struct mg_connection *c, int ev, void *ev_data)
|
||||||
{
|
{
|
||||||
(void)ev_data;
|
(void)ev_data;
|
||||||
if(ev == MG_EV_RECV)
|
if(ev != MG_EV_RECV)
|
||||||
{
|
{
|
||||||
uint32_t payload_length = *((uint32_t*)c->recv_mbuf.buf);
|
return;
|
||||||
LOG_DEBUG("payload_length %d\n", payload_length);
|
}
|
||||||
|
uint32_t payload_length = *((uint32_t*)c->recv_mbuf.buf);
|
||||||
|
LOG_DEBUG("payload_length %d\n", payload_length);
|
||||||
|
|
||||||
if(c->recv_mbuf.len < payload_length + sizeof(payload_length))
|
if(c->recv_mbuf.len < payload_length + sizeof(payload_length))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *payload = c->recv_mbuf.buf + sizeof(payload_length);
|
char *payload = c->recv_mbuf.buf + sizeof(payload_length);
|
||||||
|
|
||||||
mpack_tree_t tree;
|
mpack_tree_t tree;
|
||||||
mpack_tree_init_data(&tree, payload, payload_length);
|
mpack_tree_init_data(&tree, payload, payload_length);
|
||||||
mpack_tree_parse(&tree);
|
mpack_tree_parse(&tree);
|
||||||
mpack_node_t root = mpack_tree_root(&tree);
|
mpack_node_t root = mpack_tree_root(&tree);
|
||||||
|
|
||||||
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
|
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
|
||||||
|
|
||||||
LOG_INFO("received command %d\n", command_code);
|
LOG_INFO("received command %d\n", command_code);
|
||||||
|
|
||||||
switch(command_code)
|
switch(command_code)
|
||||||
{
|
{
|
||||||
case COMMAND_CODE_GET_TIME:
|
case COMMAND_CODE_GET_TIME:
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_GET_ID:
|
case COMMAND_CODE_GET_ID:
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_SET_NAME:
|
case COMMAND_CODE_SET_NAME:
|
||||||
handler_command_set_name(root, global_controller);
|
handler_command_set_name(root, global_controller);
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_GET_NAME:
|
case COMMAND_CODE_GET_NAME:
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_SET_SCHEDULE:
|
case COMMAND_CODE_SET_SCHEDULE:
|
||||||
handler_command_set_schedule(root, global_controller);
|
handler_command_set_schedule(root, global_controller);
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_GET_SCHEDULE:
|
case COMMAND_CODE_GET_SCHEDULE:
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_SET_RELAY_NAME:
|
case COMMAND_CODE_SET_RELAY_NAME:
|
||||||
handler_command_set_relay_name(root, global_controller);
|
handler_command_set_relay_name(root, global_controller);
|
||||||
break;
|
break;
|
||||||
case COMMAND_CODE_GET_RELAY_NAME:
|
case COMMAND_CODE_GET_RELAY_NAME:
|
||||||
break;
|
break;
|
||||||
default:
|
case COMMAND_CODE_PULSE:
|
||||||
LOG_ERROR("received invalid command\n");
|
handler_command_pulse(root, global_controller);
|
||||||
}
|
break;
|
||||||
|
default:
|
||||||
|
LOG_ERROR("received invalid command\n");
|
||||||
|
}
|
||||||
|
|
||||||
if(mpack_tree_destroy(&tree) != mpack_ok)
|
if(mpack_tree_destroy(&tree) != mpack_ok)
|
||||||
{
|
{
|
||||||
LOG_WARN("error when destroying mpack tree\n");
|
LOG_WARN("error when destroying mpack tree\n");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
controller_save(global_controller, global_mdb_env);
|
controller_save(global_controller, global_mdb_env);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,43 +21,65 @@ handler_loop(struct mg_connection *c_mqtt)
|
||||||
char controller_uid[UUID_STR_LEN];
|
char controller_uid[UUID_STR_LEN];
|
||||||
uuid_unparse(global_controller->id, controller_uid);
|
uuid_unparse(global_controller->id, controller_uid);
|
||||||
|
|
||||||
time_t timestamp = time(NULL);
|
struct tm time_last, time_now;
|
||||||
struct tm *time_struct = localtime(×tamp);
|
time_t timestamp;
|
||||||
LOG_DEBUG("===== IDLE LOOP START =====\n");
|
|
||||||
|
timestamp = time(NULL) - 1;
|
||||||
|
localtime_r(×tamp, &time_last);
|
||||||
|
timestamp = time(NULL);
|
||||||
|
localtime_r(×tamp, &time_now);
|
||||||
|
LOG_TRACE("===== IDLE LOOP START =====\n");
|
||||||
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
|
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
|
||||||
{
|
{
|
||||||
relay_t *relay = global_controller->relays[i];
|
relay_t *relay = global_controller->relays[i];
|
||||||
int is_active = 0;
|
int is_on = 0;
|
||||||
if(relay_is_active(relay, time_struct))
|
|
||||||
|
int is_on_schedule = relay_is_on_schedule(relay, &time_now);
|
||||||
|
|
||||||
|
if(relay->is_on_schedule != is_on_schedule && relay->is_on_schedule != -1)
|
||||||
{
|
{
|
||||||
LOG_DEBUG("relay %d is active\n", i);
|
relay->pulse_timer = global_config.relay_configs[i].pulse_duration;
|
||||||
is_active = 1;
|
}
|
||||||
|
if(is_on_schedule && !global_config.relay_configs[i].pulse_duration)
|
||||||
|
{
|
||||||
|
is_on = 1;
|
||||||
|
}
|
||||||
|
if(relay->pulse_timer > 0)
|
||||||
|
{
|
||||||
|
is_on = 1;
|
||||||
|
--relay->pulse_timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(relay->is_on != is_active)
|
if(is_on)
|
||||||
|
{
|
||||||
|
LOG_DEBUG("relay %d is active\n", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(relay->is_on != is_on)
|
||||||
{
|
{
|
||||||
relay->sent_to_broker = 0;
|
relay->sent_to_broker = 0;
|
||||||
}
|
}
|
||||||
if(!relay->sent_to_broker && c_mqtt)
|
if(!relay->sent_to_broker && c_mqtt)
|
||||||
{
|
{
|
||||||
sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i);
|
sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i);
|
||||||
sprintf(payload_buf, "%u", is_active);
|
sprintf(payload_buf, "%u", is_on);
|
||||||
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
|
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
|
||||||
relay->sent_to_broker = 1;
|
relay->sent_to_broker = 1;
|
||||||
}
|
}
|
||||||
relay->is_on = is_active;
|
relay->is_on = is_on;
|
||||||
|
relay->is_on_schedule = is_on_schedule;
|
||||||
|
|
||||||
if(global_config.relay_configs[i].inverted)
|
if(global_config.relay_configs[i].inverted)
|
||||||
{
|
{
|
||||||
is_active = !is_active;
|
is_on = !is_on;
|
||||||
}
|
}
|
||||||
switch(global_config.relay_configs[i].driver)
|
switch(global_config.relay_configs[i].driver)
|
||||||
{
|
{
|
||||||
case RELAY_DRIVER_GPIO:
|
case RELAY_DRIVER_GPIO:
|
||||||
driver_gpio_set(global_config.relay_configs[i].pin, is_active);
|
driver_gpio_set(global_config.relay_configs[i].pin, is_on);
|
||||||
break;
|
break;
|
||||||
case RELAY_DRIVER_PIFACE:
|
case RELAY_DRIVER_PIFACE:
|
||||||
driver_piface_set(global_config.relay_configs[i].pin, is_active);
|
driver_piface_set(global_config.relay_configs[i].pin, is_on);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOG_WARN("relay %d is not using a driver\n", i);
|
LOG_WARN("relay %d is not using a driver\n", i);
|
||||||
|
|
|
@ -81,7 +81,10 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
||||||
config->relay_configs = malloc(sizeof(config_relay_t) * config->relay_count);
|
config->relay_configs = malloc(sizeof(config_relay_t) * config->relay_count);
|
||||||
for(uint8_t i = 0; i < config->relay_count; ++i)
|
for(uint8_t i = 0; i < config->relay_count; ++i)
|
||||||
{
|
{
|
||||||
config->relay_configs[i].driver= RELAY_DRIVER_NONE;
|
config->relay_configs[i].driver = RELAY_DRIVER_NONE;
|
||||||
|
config->relay_configs[i].inverted = 0;
|
||||||
|
config->relay_configs[i].pin = 0;
|
||||||
|
config->relay_configs[i].pulse_duration = 0;
|
||||||
}
|
}
|
||||||
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -99,6 +102,11 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
||||||
config->relay_configs[i].inverted = atoi(disp->value);
|
config->relay_configs[i].inverted = atoi(disp->value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if(CONFINI_IS_KEY(relay_section_name, "pulse-duration"))
|
||||||
|
{
|
||||||
|
config->relay_configs[i].pulse_duration = atoi(disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if(CONFINI_IS_KEY(relay_section_name, "driver"))
|
if(CONFINI_IS_KEY(relay_section_name, "driver"))
|
||||||
{
|
{
|
||||||
if(strcasecmp(disp->value, "gpio") == 0)
|
if(strcasecmp(disp->value, "gpio") == 0)
|
||||||
|
|
17
src/main.c
17
src/main.c
|
@ -140,15 +140,20 @@ main(int argc, const char** argv)
|
||||||
|
|
||||||
/******************** START MAIN LOOP ********************/
|
/******************** START MAIN LOOP ********************/
|
||||||
|
|
||||||
for(;;)
|
time_t timer = time(NULL);
|
||||||
|
|
||||||
|
for (;;)
|
||||||
{
|
{
|
||||||
mg_mgr_poll(&mgr, 1000);
|
mg_mgr_poll(&mgr, 200);
|
||||||
if(!global_connection_mqtt)
|
if(time(NULL) - timer >= 1)
|
||||||
{
|
{
|
||||||
LOG_DEBUG("mqtt connection is not open\n");
|
if(!global_connection_mqtt)
|
||||||
connection_mqtt_connect(&mgr);
|
{
|
||||||
|
connection_mqtt_connect(&mgr);
|
||||||
|
}
|
||||||
|
handler_loop(global_connection_mqtt);
|
||||||
|
timer = time(NULL);
|
||||||
}
|
}
|
||||||
handler_loop(global_connection_mqtt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
terminate(0);
|
terminate(0);
|
||||||
|
|
|
@ -15,6 +15,8 @@ relay_create(uint8_t number)
|
||||||
new_relay->name[0] = '\0';
|
new_relay->name[0] = '\0';
|
||||||
|
|
||||||
new_relay->is_on = -1;
|
new_relay->is_on = -1;
|
||||||
|
new_relay->is_on_schedule = -1;
|
||||||
|
new_relay->pulse_timer = 0;
|
||||||
new_relay->sent_to_broker = 0;
|
new_relay->sent_to_broker = 0;
|
||||||
|
|
||||||
uuid_t off_id;
|
uuid_t off_id;
|
||||||
|
@ -37,7 +39,7 @@ relay_set_name(relay_t *relay, const char *name)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
relay_is_active(relay_t *relay, struct tm *time_struct)
|
relay_is_on_schedule(relay_t *relay, struct tm *time_struct)
|
||||||
{
|
{
|
||||||
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||||
if(schedule->length == 0)
|
if(schedule->length == 0)
|
||||||
|
|
|
@ -65,7 +65,11 @@ relay_load(MDB_env *mdb_env, uint8_t num)
|
||||||
|
|
||||||
new_relay = malloc(sizeof(relay_t));
|
new_relay = malloc(sizeof(relay_t));
|
||||||
new_relay->number = num;
|
new_relay->number = num;
|
||||||
|
|
||||||
new_relay->is_on = -1;
|
new_relay->is_on = -1;
|
||||||
|
new_relay->is_on_schedule = -1;
|
||||||
|
new_relay->pulse_timer = 0;
|
||||||
|
new_relay->sent_to_broker = 0;
|
||||||
|
|
||||||
MDB_val value;
|
MDB_val value;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue