summaryrefslogtreecommitdiff
path: root/mfi-mqtt/reporting.c
blob: 7b4131ad46cb4e5c92bec6b7dd8e29ff5fcb86b0 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <json-c/json.h>

#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

#include "reporting.h"

power_statistics * init_power_statistics(int relay_num) {
    power_statistics *stats = malloc(sizeof(power_statistics));
    stats->relay_num = relay_num;
    stats->engaged = calloc(strlen("false") + 1, sizeof(char));
    return stats;
}

void free_power_statistics(power_statistics *stats) {
    free(stats->engaged);
    free(stats);
}

bool power_statistics_is_engaged(power_statistics *stats) {
    return strcmp(stats->engaged, "on") == 0;
}

int power_statistics_load_from_file(power_statistics *stats) {
    FILE *fp;
    char filename[12];

    snprintf(filename, 12, "/dev/power%d", stats->relay_num);

    fp = fopen(filename, "r");
    if (!fp) {
        return 1;
    }

    if (fscanf(fp, "%s %f\n %f\n %f\n %f\n %f\n",
                stats->engaged,
                &stats->active_power,
                &stats->energy_sum,
                &stats->current_rms,
                &stats->voltage_rms,
                &stats->power_factor) != 6) {
        fclose(fp);
        return 1;
    }

    fclose(fp);
    return 0;
}

int get_output_count() {
    return access("/dev/power8", F_OK) != -1 ? 8 : 3;
}

int get_stats_all_outputs(power_statistics ***out_stats) {
    int i;
    int output_count;
    power_statistics *stat;
    power_statistics **stats;

    output_count = get_output_count();
    stats = malloc(sizeof(power_statistics *) * (output_count + 1));

    for (i = output_count; i > 0; i--) {
        stat = init_power_statistics(i);

        if (power_statistics_load_from_file(stat)) {
            continue;
        }

        stats[i] = stat;
    }

    *out_stats = stats;

    return output_count;
}

json_object * format_power_satistics_output_json(power_statistics *stats) {
    json_object *top_object;

    top_object = json_object_new_object();
    json_object_object_add(top_object, "output", json_object_new_int(stats->relay_num));
    json_object_object_add(top_object, "engaged", json_object_new_boolean(power_statistics_is_engaged(stats)));
    json_object_object_add(top_object, "active_power", json_object_new_double(stats->active_power));
    json_object_object_add(top_object, "energy_sum", json_object_new_double(stats->energy_sum));
    json_object_object_add(top_object, "current_rms", json_object_new_double(stats->current_rms));
    json_object_object_add(top_object, "voltage_rms", json_object_new_double(stats->voltage_rms));
    json_object_object_add(top_object, "power_factor", json_object_new_double(stats->power_factor));

    return top_object;
}

char * format_report_all_outputs(power_statistics **stats, int report_count) {
    int i;
    char *output;
	char hostname[256];
    char ip_address[15];
    const char *tmp;
    json_object *top_object, *report_array;

    memset(hostname, 0, sizeof(hostname));
    gethostname(hostname, 255);

    memset(ip_address, 0, sizeof(ip_address));
    get_primary_ip_address(ip_address);

    report_array = json_object_new_array();
    top_object = json_object_new_object();
    json_object_object_add(top_object, "hostname", json_object_new_string(hostname));
    json_object_object_add(top_object, "ip_address", json_object_new_string(ip_address));
    json_object_object_add(top_object, "reports", report_array);

    for (i = report_count; i > 0; i--) {
        json_object_array_add(report_array, format_power_satistics_output_json(stats[i]));
        free_power_statistics(stats[i]);
    }

    tmp = json_object_to_json_string_ext(top_object, JSON_C_TO_STRING_PLAIN);
    output = strdup(tmp);
    json_object_put(top_object);

    return output;
}

void get_primary_ip_address(char ip_address[15])
{
    int fd;
    struct ifreq ifr;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    memcpy(ifr.ifr_name, "ath0", IFNAMSIZ-1);
    ioctl(fd, SIOCGIFADDR, &ifr);
    close(fd);

    strncpy(ip_address, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), 15);
}