diff options
Diffstat (limited to 'main.vala')
-rw-r--r-- | main.vala | 99 |
1 files changed, 31 insertions, 68 deletions
@@ -1,5 +1,5 @@ -public Serial s; -public Database w; +public Device dev; +public Database db; public static int main(string[] args) { if(args.length < 2) { @@ -7,85 +7,48 @@ public static int main(string[] args) { return 1; } - s = new Serial(args[1], 9600, 8, 1); - w = new Database("shop.db"); - - char[] detected = {}; + dev = new Device(args[1], 9600, 8, 1); + db = new Database("shop.db"); while(true) { - uint8 buf[64]; - uint8 buf2[250] = { - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - - int size = (int) Posix.read(s.fd, buf, 64); - - if(size <= 0) { - stderr.printf("serial device lost.\n"); - return 1; - } - - 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'; - - /* use led as feedback */ - Posix.write(s.fd, buf2, 250); - Posix.write(s.fd, buf2, 250); - Posix.write(s.fd, buf2, 250); - Posix.write(s.fd, buf2, 250); - Posix.tcflush(s.fd, Posix.TCOFLUSH); - - interpret((string) detected); - } - detected = {}; - } + string message = dev.receive(); + if(interpret((string) message)) + dev.blink(10); } } -public static void interpret(string data) { +public static bool 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()) { - stdout.printf("logout\n"); - w.logout(); + /* check if data has valid format */ + if(data != "USER %llu".printf(id)) { + stdout.printf("ungültige Benutzernummer!\n"); + return false; } - else { - stdout.printf("login: %llu\n".printf(id)); - w.login(id); + + if(db.is_logged_in()) { + stdout.printf("Logout\n"); + return db.logout(); + } else { + stdout.printf("Login: %llu\n".printf(id)); + return db.login(id); } } else { uint64 id = uint64.parse(data); - w.buy(id); - stdout.printf("gekaufter Artikel: %s\n", w.get_product_name(id)); + /* check if data has valid format */ + if(data != "%llu".printf(id)) { + stdout.printf("ungültiges Produkt!\n"); + return false; + } + + if(db.buy(id)) { + stdout.printf("gekaufter Artikel: %s\n", db.get_product_name(id)); + return true; + } + + return false; } } |