From 7bfb48ef84384ff0460f273ea5841fba628d2a46 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Tue, 26 Mar 2013 15:52:57 +0100 Subject: code restructure --- src/scanner-session/Makefile | 9 ++ src/scanner-session/main.vala | 38 +++++ src/scanner-session/scannersession-interface.vala | 25 ++++ src/scanner-session/scannersession.vala | 164 ++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 src/scanner-session/Makefile create mode 100644 src/scanner-session/main.vala create mode 100644 src/scanner-session/scannersession-interface.vala create mode 100644 src/scanner-session/scannersession.vala (limited to 'src/scanner-session') diff --git a/src/scanner-session/Makefile b/src/scanner-session/Makefile new file mode 100644 index 0000000..ea91d3a --- /dev/null +++ b/src/scanner-session/Makefile @@ -0,0 +1,9 @@ +all: scanner-session + +scanner-session: main.vala scannersession.vala scannersession-interface.vala ../database/db-interface.vala ../serial-device/serial-device-interface.vala ../audio/audio-interface.vala ../price.vapi + valac -o $@ --pkg gio-2.0 $^ + +clean: + rm -rf scanner-session + +.PHONY: all clean diff --git a/src/scanner-session/main.vala b/src/scanner-session/main.vala new file mode 100644 index 0000000..11562e1 --- /dev/null +++ b/src/scanner-session/main.vala @@ -0,0 +1,38 @@ +/* Copyright 2013, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +ScannerSessionImplementation session; + +public static int main(string[] args) { + Bus.own_name( + BusType.SESSION, + "io.mainframe.shopsystem.ScannerSession", + BusNameOwnerFlags.NONE, + on_bus_aquired, + () => {}, + () => stderr.printf("Could not aquire name\n")); + + new MainLoop().run(); + + return 0; +} + +void on_bus_aquired(DBusConnection con) { + try { + con.register_object("/io/mainframe/shopsystem/scanner_session", session); + } catch(IOError e) { + stderr.printf("Could not register service\n"); + } +} diff --git a/src/scanner-session/scannersession-interface.vala b/src/scanner-session/scannersession-interface.vala new file mode 100644 index 0000000..0f81dd4 --- /dev/null +++ b/src/scanner-session/scannersession-interface.vala @@ -0,0 +1,25 @@ +/* Copyright 2013, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +[DBus (name = "io.mainframe.shopsystem.ScannerSession")] +public interface ScannerSession : Object { + public abstract signal void msg(MessageType type, string message); +} + +public enum MessageType { + INFO, + WARNING, + ERROR +} diff --git a/src/scanner-session/scannersession.vala b/src/scanner-session/scannersession.vala new file mode 100644 index 0000000..6926ba9 --- /dev/null +++ b/src/scanner-session/scannersession.vala @@ -0,0 +1,164 @@ +/* Copyright 2012-2013, Sebastian Reichel + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +[DBus (name = "io.mainframe.shopsystem.ScannerSession")] +public class ScannerSessionImplementation { + private int user = 0; + private string name = "Guest"; + private bool logged_in = false; + private bool disabled = false; + private string theme = "beep"; + + private Database db; + private AudioPlayer audio; + + public signal void msg(MessageType type, string message); + + public ScannerSessionImplementation() { + try { + db = Bus.get_proxy_sync(BusType.SESSION, "io.mainframe.shopsystem.Database", "/io/mainframe/shopsystem/database"); + audio = Bus.get_proxy_sync(BusType.SESSION, "io.mainframe.shopsystem.AudioPlayer", "/io/mainframe/shopsystem/audio"); + } catch(IOError e) { + error("IOError: %s\n", e.message); + } + } + + private void send_message(MessageType type, string format, ...) { + var arguments = va_list(); + var message = format.vprintf(arguments); + + msg(type, message); + } + + private void logout() { + logged_in = false; + } + + private bool login(int user) { + this.user = user; + try { + this.name = db.get_username(user); + this.disabled = db.get_user_auth(user).disabled; + } catch(DatabaseError e) { + return false; + } + this.logged_in = true; + this.theme = audio.get_random_user_theme(); + + return true; + } + + private bool interpret(string scannerdata) { + if(scannerdata.has_prefix("USER ")) { + string str_id = scannerdata.substring(5); + int32 id = int.parse(str_id); + + /* check if scannerdata has valid format */ + if(scannerdata != "USER %d".printf(id)) { + audio.play_system("error.ogg"); + send_message(MessageType.ERROR, "Invalid User ID: %s", scannerdata); + return false; + } + + if(logged_in) { + send_message(MessageType.WARNING, "Last user forgot to logout"); + logout(); + } + + if(login(id)) { + audio.play_user(theme, "login"); + send_message(MessageType.INFO, "Login: %s (%d)", name, user); + return true; + } else { + audio.play_system("error.ogg"); + send_message(MessageType.ERROR, "Login failed (User ID = %d)", id); + return false; + } + } else if(scannerdata == "GUEST") { + if(logged_in) { + send_message(MessageType.WARNING, "Last user forgot to logout"); + logout(); + } + + if(login(0)) { + audio.play_user(theme, "login"); + send_message(MessageType.INFO, "Login: %s (%d)", name, user); + return true; + } else { + audio.play_system("error.ogg"); + send_message(MessageType.ERROR, "Login failed (User ID = 0)"); + return false; + } + } else if(scannerdata == "UNDO") { + if(!logged_in) { + audio.play_system("error.ogg"); + send_message(MessageType.ERROR, "Can't undo if not logged in!"); + return false; + } else { + if(db.undo(user)) { + audio.play_user(theme, "purchase"); + return true; + } else { + audio.play_user(theme, "error"); + send_message(MessageType.ERROR, "Couldn't undo last purchase!"); + return false; + } + } + } else if(scannerdata == "LOGOUT") { + if(logged_in) { + audio.play_user(theme, "logout"); + send_message(MessageType.INFO, "Logout!"); + logout(); + return true; + } + + return false; + } else { + uint64 id = 0; + scannerdata.scanf("%llu", out id); + + /* check if scannerdata has valid format */ + if(scannerdata != "%llu".printf(id) && scannerdata != "%08llu".printf(id) && scannerdata != "%013llu".printf(id)) { + audio.play_user(theme, "error"); + send_message(MessageType.ERROR, "invalid product: %s", scannerdata); + return false; + } + + var name = db.get_product_name(id); + + if(!logged_in) { + var mprice = db.get_product_price(1, id); + var gprice = db.get_product_price(0, id); + + audio.play_system("error.ogg"); + send_message(MessageType.INFO, @"article info: $name (Member: $mprice €, Guest: $gprice €)"); + send_message(MessageType.ERROR, "Login required for purchase!"); + return false; + } + + if(db.buy(user, id)) { + var price = db.get_product_price(user, id); + audio.play_user(theme, "purchase"); + send_message(MessageType.INFO, @"article bought: $name ($price €)"); + return true; + } else { + audio.play_user(theme, "error"); + send_message(MessageType.ERROR, "purchase failed!"); + return false; + } + } + } +} + -- cgit v1.2.3