Revision 75cef49f
Added query_add_event to mainbox code
mainbox/Makefile | ||
---|---|---|
1 |
SRC=main.c tool.c query.c |
|
2 |
HDR=tool.h query.h ../tooltron_mb.h |
|
1 |
SRC=main.c tool.c query.c event.c
|
|
2 |
HDR=tool.h query.h event.h ../tooltron_mb.h
|
|
3 | 3 |
|
4 | 4 |
FLAGS=-O2 -g -Wall -I.. `pkg-config --cflags --libs libmodbus libcurl` |
5 | 5 |
|
mainbox/event.c | ||
---|---|---|
1 |
#include "event.h" |
|
2 |
#include "query.h" |
|
3 |
#include <stdlib.h> |
|
4 |
|
|
5 |
struct event_t *head, *tail; |
|
6 |
|
|
7 |
struct event_t *event_alloc() { |
|
8 |
return malloc(sizeof(struct event_t)); |
|
9 |
} |
|
10 |
|
|
11 |
void event_free(struct event_t *event) { |
|
12 |
free(event); |
|
13 |
} |
|
14 |
|
|
15 |
void event_q_push(struct event_t *event) { |
|
16 |
if (tail) |
|
17 |
tail->next = event; |
|
18 |
else |
|
19 |
head = event; |
|
20 |
tail = event; |
|
21 |
} |
|
22 |
|
|
23 |
void event_q_process() { |
|
24 |
struct event_t *old; |
|
25 |
if (head && query_add_event(head) == 0) { |
|
26 |
old = head; |
|
27 |
head = head->next; |
|
28 |
event_free(old); |
|
29 |
} |
|
30 |
} |
mainbox/event.h | ||
---|---|---|
1 |
#ifndef EVENT_H |
|
2 |
#define EVENT_H |
|
3 |
|
|
4 |
#include <time.h> |
|
5 |
|
|
6 |
struct event_t { |
|
7 |
unsigned int user; |
|
8 |
int tool_id; |
|
9 |
time_t tstart; |
|
10 |
time_t tend; |
|
11 |
struct event_t *next; |
|
12 |
}; |
|
13 |
|
|
14 |
struct event_t *event_alloc(); |
|
15 |
void event_free(struct event_t *event); |
|
16 |
void event_q_push(struct event_t *event); |
|
17 |
void event_q_process(); |
|
18 |
|
|
19 |
#endif |
mainbox/main.c | ||
---|---|---|
5 | 5 |
#include <strings.h> |
6 | 6 |
#include <stdio.h> |
7 | 7 |
|
8 |
#define SLEEP_MS 250 |
|
8 |
#define SLEEP_MS 10//250
|
|
9 | 9 |
|
10 | 10 |
static struct tool_t tools[] = { |
11 |
TOOL_DECL("testa", 4), |
|
12 |
TOOL_DECL("testb", 5) |
|
11 |
TOOL_DECL("test1", 1), |
|
12 |
TOOL_DECL("test2", 2), |
|
13 |
TOOL_DECL("test3", 3), |
|
14 |
TOOL_DECL("test4", 4), |
|
15 |
TOOL_DECL("test5", 5), |
|
16 |
TOOL_DECL("test6", 6), |
|
17 |
TOOL_DECL("test7", 7), |
|
18 |
TOOL_DECL("test8", 8), |
|
19 |
TOOL_DECL("test9", 9) |
|
13 | 20 |
}; |
14 | 21 |
|
15 | 22 |
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t)) |
... | ... | |
74 | 81 |
i = 0; |
75 | 82 |
while (run) { |
76 | 83 |
tool_poll(&tools[i]); |
84 |
event_q_process(); |
|
77 | 85 |
usleep(SLEEP_MS * (useconds_t)1000); |
78 | 86 |
i = (i+1) % N_TOOLS; |
79 | 87 |
} |
mainbox/query.c | ||
---|---|---|
1 | 1 |
#include "query.h" |
2 |
#include "event.h" |
|
2 | 3 |
#include <stdio.h> |
3 | 4 |
#include <curl/curl.h> |
4 | 5 |
|
... | ... | |
20 | 21 |
curl_global_cleanup(); |
21 | 22 |
} |
22 | 23 |
|
23 |
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
|
|
24 |
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
|
|
24 | 25 |
int *resultp = userp; |
25 | 26 |
char *str = buffer; |
26 | 27 |
|
... | ... | |
32 | 33 |
return nmemb; |
33 | 34 |
} |
34 | 35 |
|
35 |
int query_user_permission(int tool_id, int user_id) { |
|
36 |
/* |
|
37 |
* query_user_permission |
|
38 |
* |
|
39 |
* Makes an HTTP request to the CRM server to see if user_id has access to |
|
40 |
* tool_id. Returns 1 if the server replies with '1' or 0 otherwise. |
|
41 |
*/ |
|
42 |
int query_user_permission(int tool_id, unsigned int user_id) { |
|
36 | 43 |
CURL* handle; |
37 | 44 |
CURLcode error_code; |
38 | 45 |
char url[1024]; |
... | ... | |
47 | 54 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, url); |
48 | 55 |
if (error_code) goto error; |
49 | 56 |
|
50 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
|
|
57 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
|
|
51 | 58 |
if (error_code) goto error; |
52 | 59 |
|
53 | 60 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result); |
... | ... | |
73 | 80 |
curl_easy_cleanup(handle); |
74 | 81 |
return 0; |
75 | 82 |
} |
83 |
|
|
84 |
/* |
|
85 |
* query_add_event |
|
86 |
* |
|
87 |
* Makes an HTTPS POST request to add an event to the CRM server, including |
|
88 |
* user, tool, start time, and stop time. Reads the password from password.txt. |
|
89 |
* Returns 0 if successful, or 1 if there was an error and the caller should |
|
90 |
* try the same event again later. |
|
91 |
* |
|
92 |
* Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD |
|
93 |
* HH:MM:SS" with 24-hour time |
|
94 |
*/ |
|
95 |
int query_add_event(struct event_t *event) { |
|
96 |
CURL* handle; |
|
97 |
CURLcode error_code; |
|
98 |
struct curl_httppost *formpost = NULL, *lastptr = NULL; |
|
99 |
char buf[1024]; |
|
100 |
struct tm *timeinfo; |
|
101 |
long response = 0; |
|
102 |
|
|
103 |
handle = curl_easy_init(); |
|
104 |
if (handle == NULL) |
|
105 |
return 1; |
|
106 |
|
|
107 |
curl_formadd(&formpost, &lastptr, |
|
108 |
CURLFORM_COPYNAME, "type", |
|
109 |
CURLFORM_COPYCONTENTS, "usage", |
|
110 |
CURLFORM_END); |
|
111 |
|
|
112 |
timeinfo = localtime(&event->tstart); |
|
113 |
strftime(buf, sizeof(buf), "%F %T", timeinfo); |
|
114 |
curl_formadd(&formpost, &lastptr, |
|
115 |
CURLFORM_COPYNAME, "tstart", |
|
116 |
CURLFORM_COPYCONTENTS, buf, |
|
117 |
CURLFORM_END); |
|
118 |
|
|
119 |
timeinfo = localtime(&event->tend); |
|
120 |
strftime(buf, sizeof(buf), "%F %T", timeinfo); |
|
121 |
curl_formadd(&formpost, &lastptr, |
|
122 |
CURLFORM_COPYNAME, "tend", |
|
123 |
CURLFORM_COPYCONTENTS, buf, |
|
124 |
CURLFORM_END); |
|
125 |
|
|
126 |
sprintf(buf, "%08x", event->user); |
|
127 |
curl_formadd(&formpost, &lastptr, |
|
128 |
CURLFORM_COPYNAME, "user", |
|
129 |
CURLFORM_COPYCONTENTS, buf, |
|
130 |
CURLFORM_END); |
|
131 |
|
|
132 |
sprintf(buf, "%d", event->tool_id); |
|
133 |
curl_formadd(&formpost, &lastptr, |
|
134 |
CURLFORM_COPYNAME, "machine", |
|
135 |
CURLFORM_COPYCONTENTS, buf, |
|
136 |
CURLFORM_END); |
|
137 |
|
|
138 |
sprintf(buf, "https://%s/add_event/", server); |
|
139 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, buf); |
|
140 |
if (error_code) goto error; |
|
141 |
|
|
142 |
error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost); |
|
143 |
if (error_code) goto error; |
|
144 |
|
|
145 |
error_code = curl_easy_perform(handle); |
|
146 |
if (error_code) goto error; |
|
147 |
|
|
148 |
error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); |
|
149 |
if (error_code) goto error; |
|
150 |
if (response >= 400) |
|
151 |
fprintf(stderr, "Error %ld from %s\n", response, buf); |
|
152 |
else if (response > 200) |
|
153 |
fprintf(stderr, "Warning: response %ld from %s\n", response, buf); |
|
154 |
|
|
155 |
curl_easy_cleanup(handle); |
|
156 |
return response >= 300; |
|
157 |
|
|
158 |
error: |
|
159 |
fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code)); |
|
160 |
curl_easy_cleanup(handle); |
|
161 |
return 1; |
|
162 |
} |
mainbox/query.h | ||
---|---|---|
1 | 1 |
#ifndef QUERY_H |
2 | 2 |
#define QUERY_H |
3 | 3 |
|
4 |
#include "event.h" |
|
5 |
|
|
4 | 6 |
int query_init(); |
5 | 7 |
void query_cleanup(); |
6 |
int query_user_permission(int tool_id, int user_id); |
|
8 |
int query_user_permission(int tool_id, unsigned int user_id); |
|
9 |
int query_add_event(struct event_t *event); |
|
7 | 10 |
|
8 | 11 |
#endif |
mainbox/tool.c | ||
---|---|---|
1 | 1 |
#include "tool.h" |
2 | 2 |
#include "query.h" |
3 |
#include "event.h" |
|
3 | 4 |
#include "tooltron_mb.h" |
4 | 5 |
#include <modbus.h> |
5 | 6 |
#include <stdio.h> |
... | ... | |
60 | 61 |
tool->address); |
61 | 62 |
tool_write_coil(MB_COIL_EN, 1); |
62 | 63 |
tool->state = TS_ON; |
64 |
tool->event = event_alloc(); |
|
65 |
tool->event->user = tool->user; |
|
66 |
tool->event->tool_id = tool->address; |
|
67 |
tool->event->tstart = time(NULL); |
|
63 | 68 |
} |
64 | 69 |
|
65 | 70 |
static void tool_deny_access(struct tool_t *tool) { |
... | ... | |
69 | 74 |
tool->state = TS_OFF; |
70 | 75 |
} |
71 | 76 |
|
77 |
static void tool_off(struct tool_t *tool) { |
|
78 |
tool->state = TS_OFF; |
|
79 |
tool->event->tend = time(NULL); |
|
80 |
event_q_push(tool->event); |
|
81 |
tool->event = NULL; |
|
82 |
} |
|
83 |
|
|
72 | 84 |
void tool_request_disable(struct tool_t *tool) { |
73 | 85 |
printf("Requesting disable on %s (%d)\n", tool->name, tool->address); |
74 | 86 |
tool_write_coil(MB_COIL_REQ_DIS, 1); |
... | ... | |
130 | 142 |
case TS_ON: |
131 | 143 |
if (!status[MB_COIL_EN]) { |
132 | 144 |
printf("Tool %s (%d) is off\n", tool->name, tool->address); |
133 |
tool->state = TS_OFF;
|
|
145 |
tool_off(tool);
|
|
134 | 146 |
} |
135 | 147 |
break; |
136 | 148 |
|
... | ... | |
138 | 150 |
if (!status[MB_COIL_EN]) { |
139 | 151 |
printf("Tool %s (%d) is off after requested disable\n", tool->name, |
140 | 152 |
tool->address); |
141 |
tool->state = TS_OFF;
|
|
153 |
tool_off(tool);
|
|
142 | 154 |
} |
143 | 155 |
break; |
144 | 156 |
|
mainbox/tool.h | ||
---|---|---|
14 | 14 |
int connected; |
15 | 15 |
enum toolstate_t state; |
16 | 16 |
unsigned int user; |
17 |
struct event_t *event; |
|
17 | 18 |
}; |
18 | 19 |
|
19 |
#define TOOL_DECL(name, addr) {name, addr, 1, TS_INIT, 0} |
|
20 |
#define TOOL_DECL(name, addr) {name, addr, 1, TS_INIT, 0, NULL}
|
|
20 | 21 |
|
21 | 22 |
int tool_init_mb(const char *device); |
22 | 23 |
void tool_close_mb(); |
Also available in: Unified diff