summaryrefslogtreecommitdiffstats
path: root/main.vala
blob: 80fa44f4475a0c1f22f6953a8c6019e9b720eb35 (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
public Serial s;
public Web w;

public static int main(string[] args) {
	if(args.length < 2) {
		stderr.printf("%s <device>\n", args[0]);
		return 1;
	}

	s = new Serial(args[1], 9600, 8, 1);
	w = new Web();

	char[] detected = {};

	while(true) {
		uint8 buf[64];
		int size = (int) Posix.read(s.fd, buf, 64);

		for(int i = 0; i < size; i++)
			if(buf[i] != '\r' && buf[i] != '\n') {
				detected += (char) buf[i];
			} else {
				if(detected.length > 0) {
					detected += '\0';
					interpret((string) detected);
				}
				detected = {};
			}
	}
}

public static void interpret(string data) {
	if(data.has_prefix("USER ")) {
		string str_id = data.substring(5);
		uint64 id = uint64.parse(str_id);

		if(w.is_logged_in())
			w.logout();
		else
			w.login(id);

	} else {
		uint64 id = uint64.parse(data);
		w.buy(id);
	}
}