This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
factory.c
99 lines (87 loc) · 3.32 KB
/
factory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "network/tcp.h"
#include "interfaces/network_commands.h"
#include "interfaces/peripherals.h"
#include "utils/host_list.h"
#include "network/connection.h"
#include "utils/sensor_history.h"
#include "utils/sensor_data_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
host_node * host_list;
pthread_t server_thread;
SensorHistoryWriteBuffer * sensor_history;
int fact_ID = -1;
char cmd_args[MAX_ARGS_BUFFER_SIZE];
short alarm_state = 0;
void handle_command(int commandId, char * args, char * response, int connfd, char * client_ip) {
if (commandId == CMD_ANNOUNCE_NEW_HOST) {
connect_new_factory(args, host_list);
} else if (commandId == CMD_SEND_SENSOR_DATA) {
int factId;
SensorData data;
sensor_data_from_command(args, &factId, &data);
print_sensor_data(data, factId);
} else if (commandId == CMD_TRIGGER_ALARM) {
int fact_id_alarm;
sscanf(args, "%d", &fact_id_alarm);
printf("[!] Alarm triggered because of factory ID %d\n.", fact_id_alarm);
trigger_factory_alarm(fact_id_alarm, &alarm_state);
} else if (commandId == CMD_GET_PERIPHERALS) {
sprintf(response, "%d %d %d", has_sensors(), has_led(), has_relay());
} else if (commandId == CMD_SET_LED_STATE) {
short state;
sscanf(args, "%hd", &state);
set_led_state(state);
} else if (commandId == CMD_SET_RELAY_STATE) {
short state;
sscanf(args, "%hd", &state);
set_relay_state(state);
} else if (commandId == CMD_SEND_SENSOR_HISTORY_FILE) {
printf("Client requested sensor history file. Sending...\n");
send_sensor_history_file(sensor_history, connfd);
response[0] = -1;
} else if (commandId == CMD_GET_ALARM_STATE) {
printf("Client requested alarm state.\n");
sprintf(response, "%hd", alarm_state);
}
}
void broadcast_sensor_data(SensorData sensorData) {
sprintf(cmd_args, "%d %ld %lf %lf %lf", fact_ID, sensorData.time, sensorData.temperature, sensorData.humidity, sensorData.pressure);
host_node * host = host_list;
while (host->next != NULL){
host = host->next;
send_command_to_server(CMD_SEND_SENSOR_DATA, cmd_args, NULL, host->host);
}
}
int main(int argc, char **argv) {
// setbuf(stdout, NULL);
if (argc != 2) {
printf("Factory takes exactly 1 command line argument: the dashboard's IP address\n");
exit(-1);
}
// start tcp server
accept_tcp_connections_non_blocking(handle_command, &server_thread);
// connect to the dashboard
const char * dashboardAddr = argv[1];
printf("Starting factory! Dashboard IP address: %s\n", dashboardAddr);
init_sensor_data_buffer(&sensor_history);
connect_to_dashboard(dashboardAddr, &host_list, &fact_ID, 1);
init_sensor();
short sensors_enabled = has_sensors();
sleep(MEASUREMENT_PERIOD);
while(1) {
if (sensors_enabled && !alarm_state) {
SensorData sensorData;
read_sensor_data(&sensorData);
store_sensor_data(sensor_history, sensorData);
broadcast_sensor_data(sensorData);
}
sleep(MEASUREMENT_PERIOD);
}
write_sensor_data_to_file(sensor_history, 1);
close_all_connections(host_list);
free_host_list(host_list);
free_sensor_data_buffer(&sensor_history);
return 0;
}