diff options
author | Sebastian Reichel <sre@ring0.de> | 2012-05-18 00:17:36 +0200 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2012-05-18 00:17:36 +0200 |
commit | 4e3058c7d626bfb91d694d5d846b0890cb6f6df6 (patch) | |
tree | cd40f75f17f67584be915293b6f8d61b3c17f4d6 | |
parent | 81ae2de44af8ace409faf040b7e70ed85a4150c5 (diff) | |
download | serial-barcode-scanner-4e3058c7d626bfb91d694d5d846b0890cb6f6df6.tar.bz2 |
undo function and stock mode
-rw-r--r-- | create_db.sql | 65 | ||||
-rw-r--r-- | db.vala | 92 | ||||
-rw-r--r-- | device.vala | 7 | ||||
-rw-r--r-- | main.vala | 63 |
4 files changed, 191 insertions, 36 deletions
diff --git a/create_db.sql b/create_db.sql index 2d4716b..3c18327 100644 --- a/create_db.sql +++ b/create_db.sql @@ -1,34 +1,35 @@ -CREATE TABLE products (id INTEGER, name TEXT); -CREATE TABLE purchases (user INTEGER, product INTEGER, timestamp INTEGER); +CREATE TABLE products (id INTEGER PRIMARY KEY NOT NULL, name TEXT, amount INTEGER NOT NULL DEFAULT 0); +CREATE TABLE purchases (user INTEGER NOT NULL DEFAULT 0, product INTEGER NOT NULL DEFAULT 0, timestamp INTEGER NOT NULL DEFAULT 0); +CREATE TABLE restock (user INTEGER NOT NULL DEFAULT 0, product INTEGER NOT NULL DEFAULT 0, amount INTEGER NOT NULL DEFAULT 0, timestamp INTEGER NOT NULL DEFAULT 0); BEGIN TRANSACTION; -INSERT INTO products VALUES(4029764001807,'Club Mate'); -INSERT INTO products VALUES(5449000017888,'Coka Cola'); -INSERT INTO products VALUES(5449000017895,'Coka Cola Light'); -INSERT INTO products VALUES(5449000134264,'Coka Cola Zero'); -INSERT INTO products VALUES(5449000017918,'Fanta'); -INSERT INTO products VALUES(5449000017932,'Sprite'); -INSERT INTO products VALUES(4104450004383,'Limette'); -INSERT INTO products VALUES(4104450005878,'Vilsa Classic'); -INSERT INTO products VALUES(5000159407236,'Mars'); -INSERT INTO products VALUES(5000159407397,'Snickers'); -INSERT INTO products VALUES(5000159418539,'Balisto Jogurt Beeren Mix'); -INSERT INTO products VALUES(7613032625474,'Lion'); -INSERT INTO products VALUES(7613032850340,'KitKat Chunky'); -INSERT INTO products VALUES(40084015,'Duplo'); -INSERT INTO products VALUES(40358802,'Knoppers'); -INSERT INTO products VALUES(40084107,'Ü-Ei'); -INSERT INTO products VALUES(40114606,'KitKat'); -INSERT INTO products VALUES(40111315,'Twix'); -INSERT INTO products VALUES(4003586000491,'Chips Frisch Oriento'); -INSERT INTO products VALUES(8690504018568,'Üker Sesamsticks'); -INSERT INTO products VALUES(4001686216125,'Haribo Salino'); -INSERT INTO products VALUES(4001686312025,'Haribo Schnuller'); -INSERT INTO products VALUES(4001686150689,'Haribo Konfekt'); -INSERT INTO products VALUES(4001686128244,'Haribo Staffeten'); -INSERT INTO products VALUES(4001686390085,'Haribo Phantasia'); -INSERT INTO products VALUES(4001686310229,'Haribo Weinland'); -INSERT INTO products VALUES(4001686386613,'Haribo Saftgoldbären'); -INSERT INTO products VALUES(4001686301265,'Haribo Goldbären'); -INSERT INTO products VALUES(4001686313046,'Haribo Saure Bohnen'); -INSERT INTO products VALUES(4001686720028,'Haribo Colorado Mini'); +INSERT INTO products (id, name) VALUES(4029764001807,'Club Mate'); +INSERT INTO products (id, name) VALUES(5449000017888,'Coka Cola'); +INSERT INTO products (id, name) VALUES(5449000017895,'Coka Cola Light'); +INSERT INTO products (id, name) VALUES(5449000134264,'Coka Cola Zero'); +INSERT INTO products (id, name) VALUES(5449000017918,'Fanta'); +INSERT INTO products (id, name) VALUES(5449000017932,'Sprite'); +INSERT INTO products (id, name) VALUES(4104450004383,'Limette'); +INSERT INTO products (id, name) VALUES(4104450005878,'Vilsa Classic'); +INSERT INTO products (id, name) VALUES(5000159407236,'Mars'); +INSERT INTO products (id, name) VALUES(5000159407397,'Snickers'); +INSERT INTO products (id, name) VALUES(5000159418539,'Balisto Jogurt Beeren Mix'); +INSERT INTO products (id, name) VALUES(7613032625474,'Lion'); +INSERT INTO products (id, name) VALUES(7613032850340,'KitKat Chunky'); +INSERT INTO products (id, name) VALUES(40084015,'Duplo'); +INSERT INTO products (id, name) VALUES(40358802,'Knoppers'); +INSERT INTO products (id, name) VALUES(40084107,'Ü-Ei'); +INSERT INTO products (id, name) VALUES(40114606,'KitKat'); +INSERT INTO products (id, name) VALUES(40111315,'Twix'); +INSERT INTO products (id, name) VALUES(4003586000491,'Chips Frisch Oriento'); +INSERT INTO products (id, name) VALUES(8690504018568,'Üker Sesamsticks'); +INSERT INTO products (id, name) VALUES(4001686216125,'Haribo Salino'); +INSERT INTO products (id, name) VALUES(4001686312025,'Haribo Schnuller'); +INSERT INTO products (id, name) VALUES(4001686150689,'Haribo Konfekt'); +INSERT INTO products (id, name) VALUES(4001686128244,'Haribo Staffeten'); +INSERT INTO products (id, name) VALUES(4001686390085,'Haribo Phantasia'); +INSERT INTO products (id, name) VALUES(4001686310229,'Haribo Weinland'); +INSERT INTO products (id, name) VALUES(4001686386613,'Haribo Saftgoldbären'); +INSERT INTO products (id, name) VALUES(4001686301265,'Haribo Goldbären'); +INSERT INTO products (id, name) VALUES(4001686313046,'Haribo Saure Bohnen'); +INSERT INTO products (id, name) VALUES(4001686720028,'Haribo Colorado Mini'); COMMIT; @@ -2,9 +2,19 @@ public class Database { private Sqlite.Database db; private Sqlite.Statement insert_stmt; private Sqlite.Statement product_stmt; + private Sqlite.Statement undo_stmt; + private Sqlite.Statement stock_stmt1; + private Sqlite.Statement stock_stmt2; uint64 user = 0; + uint64 product = 0; + bool logged_in = false; + bool stock_mode = false; private static string insert_query = "INSERT INTO purchases ('user', 'product', 'timestamp') VALUES (?, ?, ?)"; private static string product_query = "SELECT name FROM products WHERE id = ?"; + private static string undo_query = "DELETE FROM purchases WHERE user = ? ORDER BY 'timestamp' DESC LIMIT 1"; + + private static string stock_query1 = "INSERT INTO restock ('user', 'product', 'amount', 'timestamp') VALUES (?, ?, ?, ?)"; + private static string stock_query2 = "UPDATE products SET amount = amount + ? WHERE id = ?"; public Database(string file) { int rc; @@ -23,15 +33,33 @@ public class Database { if(rc != Sqlite.OK) { error("could not prepare article statement!"); } + + rc = this.db.prepare_v2(undo_query, -1, out undo_stmt); + if(rc != Sqlite.OK) { + error("could not prepare undo statement!"); + } + + rc = this.db.prepare_v2(stock_query1, -1, out stock_stmt1); + if(rc != Sqlite.OK) { + error("could not prepare first stock statement!"); + } + + rc = this.db.prepare_v2(stock_query2, -1, out stock_stmt2); + if(rc != Sqlite.OK) { + error("could not prepare second stock statement!"); + } + } public bool login(uint64 id) { this.user = id; + this.logged_in = true; return true; } public bool logout() { this.user = 0; + this.logged_in = false; return true; } @@ -71,7 +99,69 @@ public class Database { } } + public bool undo() { + if(is_logged_in()) { + this.undo_stmt.reset(); + this.undo_stmt.bind_text(1, "%llu".printf(user)); + + int rc = this.undo_stmt.step(); + if(rc != Sqlite.DONE) + error("[interner Fehler: %d]".printf(rc)); + else + return true; + } + + return false; + } + + public bool choose_stock_product(uint64 id) { + if(is_in_stock_mode()) { + product = id; + return true; + } + return false; + } + + public bool add_stock_product(uint64 amount) { + if(is_in_stock_mode() && product != 0) { + int rc = 0; + int64 timestamp = (new DateTime.now_utc()).to_unix(); + + this.stock_stmt1.reset(); + this.stock_stmt1.bind_text(1, "%llu".printf(user)); + this.stock_stmt1.bind_text(2, "%llu".printf(product)); + this.stock_stmt1.bind_text(3, "%llu".printf(amount)); + this.stock_stmt1.bind_text(4, "%llu".printf(timestamp)); + + rc = this.stock_stmt1.step(); + if(rc != Sqlite.DONE) + error("[interner Fehler: %d]".printf(rc)); + + this.stock_stmt2.reset(); + this.stock_stmt2.bind_text(1, "%llu".printf(amount)); + this.stock_stmt2.bind_text(2, "%llu".printf(product)); + + rc = this.stock_stmt2.step(); + if(rc != Sqlite.DONE) + error("[interner Fehler: %d]".printf(rc)); + + return true; + } + + return false; + } + + public bool go_into_stock_mode() { + if(is_logged_in()) + stock_mode = true; + return stock_mode; + } + public bool is_logged_in() { - return (user != 0); + return this.logged_in; + } + + public bool is_in_stock_mode() { + return this.stock_mode; } } diff --git a/device.vala b/device.vala index 0fd6719..203e745 100644 --- a/device.vala +++ b/device.vala @@ -145,6 +145,13 @@ public class Device { detected[detected.length-2] = '\0'; } + if(((string) detected).has_prefix("AMOUNT ")) { + if(!check_code39_checksum((string) detected)) + return "SCANNER RETURNED INCORRECT DATA"; + else /* remove checksum */ + detected[detected.length-2] = '\0'; + } + return ((string) detected); } } @@ -29,11 +29,68 @@ public static bool interpret(string data) { } if(db.is_logged_in()) { - stdout.printf("Logout\n"); + stdout.printf("Last User forgot to logout!\n"); + db.logout(); + } + + stdout.printf("Login: %llu\n".printf(id)); + return db.login(id); + } else if(data == "GUEST") { + if(db.is_logged_in()) { + stdout.printf("Last User forgot to logout!\n"); + db.logout(); + } + + stdout.printf("Login: Guest\n"); + return db.login(0); + } else if(data == "UNDO") { + if(!db.is_logged_in()) { + stdout.printf("Can't undo if not logged in!\n"); + return false; + } else { + stdout.printf("Undo last purchase!\n"); + return db.undo(); + } + } else if(data == "LOGOUT") { + if(db.is_logged_in()) { + stdout.printf("Logout!\n"); return db.logout(); + } + + return false; + } else if(data == "STOCK") { + if(!db.is_logged_in()) { + stdout.printf("You must be logged in to go into the stock mode\n"); + return false; + } else { + stdout.printf("Going into stock mode!\n"); + return db.go_into_stock_mode(); + } + } else if(db.is_in_stock_mode()) { + if(!data.has_prefix("AMOUNT")) { + uint64 id = uint64.parse(data); + + /* check if data has valid format */ + if(data != "%llu".printf(id)) { + stdout.printf("ungültiges Produkt: %s\n", data); + return false; + } + + stdout.printf("wähle Produkt: %s\n", db.get_product_name(id)); + + return db.choose_stock_product(id); } else { - stdout.printf("Login: %llu\n".printf(id)); - return db.login(id); + uint64 amount = uint64.parse(data.substring(7)); + + /* check if data has valid format */ + if(data != "AMOUNT %llu".printf(amount)) { + stdout.printf("ungültiges Produkt: %s\n", data); + return false; + } + + stdout.printf("zum Bestand hinzufügen: %llu\n", amount); + + return db.add_stock_product(amount); } } else { uint64 id = uint64.parse(data); |