summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-05-16 21:32:42 +0200
committerSebastian Reichel <sre@ring0.de>2012-05-16 21:32:42 +0200
commit0a2c6b377209b8bf29fa53fa1a2c6ebf13a7c570 (patch)
tree496f2a0f36b863a003af13af14615a943bdcf04c
parent496fc7e418d8798565efd65a26280c05368c08dd (diff)
downloadserial-barcode-scanner-0a2c6b377209b8bf29fa53fa1a2c6ebf13a7c570.tar.bz2
code refactor + changed status led meaning
-rw-r--r--Makefile2
-rw-r--r--db.vala26
-rw-r--r--device.vala (renamed from serial.vala)53
-rw-r--r--main.vala99
4 files changed, 98 insertions, 82 deletions
diff --git a/Makefile b/Makefile
index 1d993f3..0c33e42 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
PREFIX=/usr/local
-barcode-scanner: main.vala serial.vala web.vala db.vala
+barcode-scanner: main.vala device.vala web.vala db.vala
valac-0.16 --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 $^
install: barcode-scanner
diff --git a/db.vala b/db.vala
index 6467e6c..5679eb7 100644
--- a/db.vala
+++ b/db.vala
@@ -25,23 +25,31 @@ public class Database {
}
}
- public void login(uint64 id) {
+ public bool login(uint64 id) {
this.user = id;
+ return true;
}
- public void logout() {
+ public bool logout() {
this.user = 0;
+ return true;
}
- public void buy(uint64 article) {
- this.insert_stmt.reset();
- this.insert_stmt.bind_text(1, "%llu".printf(user));
- this.insert_stmt.bind_text(2, "%llu".printf(article));
+ public bool buy(uint64 article) {
+ if(is_logged_in()) {
+ this.insert_stmt.reset();
+ this.insert_stmt.bind_text(1, "%llu".printf(user));
+ this.insert_stmt.bind_text(2, "%llu".printf(article));
- int rc = this.insert_stmt.step();
+ int rc = this.insert_stmt.step();
- if(rc != Sqlite.DONE)
- error("[interner Fehler: %d]".printf(rc));
+ if(rc != Sqlite.DONE)
+ error("[interner Fehler: %d]".printf(rc));
+ else
+ return true;
+ } else {
+ return false;
+ }
}
public string get_product_name(uint64 article) {
diff --git a/serial.vala b/device.vala
index acdcf41..ee0967b 100644
--- a/serial.vala
+++ b/device.vala
@@ -1,11 +1,10 @@
-public class Serial {
+public class Device {
private Posix.termios newtio;
private Posix.termios restoretio;
public int fd=-1;
+ public int byterate;
- public signal void new_data(uchar[] data);
-
- public Serial(string device, int rate, int bits, int stopbits) {
+ public Device(string device, int rate, int bits, int stopbits) {
Posix.speed_t baudrate = Posix.B9600;
fd = Posix.open(device, Posix.O_RDWR /*| Posix.O_NONBLOCK*/);
@@ -56,6 +55,7 @@ public class Serial {
break;
default:
/* not supported */
+ rate = 9600;
break;
}
@@ -107,5 +107,50 @@ public class Serial {
Posix.ioctl(fd, Linux.Termios.TIOCMSET, out mcs);
Posix.tcsetattr(fd, Posix.TCSANOW, newtio);
+
+ this.byterate = rate/bits;
+ }
+
+ private ssize_t read(void *buf, size_t count) {
+ return Posix.read(fd, buf, count);
}
+
+ private ssize_t write(void *buf, size_t count) {
+ ssize_t size = Posix.write(fd, buf, count);
+ Posix.tcflush(fd, Posix.TCOFLUSH);
+ return size;
+ }
+
+ public string receive() {
+ char[] detected = {};
+ char buf[64];
+
+ int size = (int) this.read(buf, 64);
+
+ if(size <= 0)
+ error("serial device lost.\n");
+
+ 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';
+ return (string) detected;
+ }
+ }
+ }
+
+ return "";
+ }
+
+ /**
+ * @param duration duration of the blink in 0.1 seconds
+ */
+ public void blink(uint duration) {
+ uint size = byterate/10 * duration;
+ var msg = new uint8[size];
+ Posix.memset(msg, 0xFF, msg.length);
+ this.write(msg, msg.length);
+}
}
diff --git a/main.vala b/main.vala
index 9ffd108..5886b11 100644
--- a/main.vala
+++ b/main.vala
@@ -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;
}
}