summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-10-06 21:09:54 +0200
committerSebastian Reichel <sre@ring0.de>2012-10-06 21:09:54 +0200
commitd70dcb4f20d1c52add759ed4ec5ccbf7131de069 (patch)
tree64db0591701c57fa49e39cdb7a71cb22e2b46551 /src
parent4bc20854220c7fbfb655a6ed476d12cdafce2f2d (diff)
downloadserial-barcode-scanner-d70dcb4f20d1c52add759ed4ec5ccbf7131de069.tar.bz2
Add full audio support (Closes: GH-13)
Diffstat (limited to 'src')
-rw-r--r--src/audio.vala41
-rw-r--r--src/main.vala5
-rw-r--r--src/scannersession.vala36
3 files changed, 66 insertions, 16 deletions
diff --git a/src/audio.vala b/src/audio.vala
index e30f3b1..eeff7b1 100644
--- a/src/audio.vala
+++ b/src/audio.vala
@@ -15,6 +15,7 @@
public class AudioPlayer {
private dynamic Gst.Element p;
+ string path;
public signal void end_of_stream();
@@ -29,13 +30,49 @@ public class AudioPlayer {
}
public AudioPlayer() {
+ path = Environment.get_current_dir()+"/sounds/";
p = Gst.ElementFactory.make("playbin", "play");
p.get_bus().add_watch(bus_callback);
}
- public void play(string file) {
+ public void play_system(string file) {
p.set_state(Gst.State.NULL);
- p.uri = "file://"+Environment.get_current_dir()+"/sounds/"+file;
+ p.uri = "file://" + path + "system/"+file;
+ p.set_state(Gst.State.PLAYING);
+ }
+
+ private string[] get_files(string dir) {
+ try {
+ var directory = File.new_for_path(dir);
+ var enumerator = directory.enumerate_children(FileAttribute.STANDARD_NAME, 0);
+ string[] result = {};
+
+ FileInfo file_info;
+ while ((file_info = enumerator.next_file ()) != null) {
+ result += file_info.get_name();
+ }
+
+ return result;
+ } catch (Error e) {
+ write_to_log("Error: %s\n", e.message);
+ return {};
+ }
+ }
+
+ private string get_random_file(string dir) {
+ var files = get_files(dir);
+ var index = Random.int_range(0, files.length);
+ return files[index];
+ }
+
+ public string get_random_user_theme() {
+ return get_random_file(path + "user/");
+ }
+
+ public void play_user(string theme, string type) {
+ p.set_state(Gst.State.NULL);
+ var file = get_random_file(path + "user/" + theme+ "/" + type);
+ p.uri = "file://" + path + "user/" + theme+ "/" + type + "/" + file;
p.set_state(Gst.State.PLAYING);
}
}
diff --git a/src/main.vala b/src/main.vala
index aadafe7..7f9d3c1 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -62,6 +62,7 @@ public static int main(string[] args) {
db = new Database("shop.db");
audio = new AudioPlayer();
loop = new MainLoop();
+ localsession = new ScannerSession();
dev.received_barcode.connect((data) => {
if(localsession.interpret(data))
@@ -69,7 +70,7 @@ public static int main(string[] args) {
});
write_to_log("KtT Shop System has been started");
- audio.play("system/startup.ogg");
+ audio.play_system("startup.ogg");
/* attach webserver to mainloop */
new WebServer();
@@ -78,7 +79,7 @@ public static int main(string[] args) {
loop.run();
write_to_log("Stopping Shop System");
- audio.play("system/shutdown.ogg");
+ audio.play_system("shutdown.ogg");
/* we need to run the mainloop to play audio */
audio.end_of_stream.connect(() => { loop.quit(); });
diff --git a/src/scannersession.vala b/src/scannersession.vala
index e286387..7f19b77 100644
--- a/src/scannersession.vala
+++ b/src/scannersession.vala
@@ -34,6 +34,11 @@ public class ScannerSession {
private set;
default = false;
}
+ public string theme {
+ get;
+ private set;
+ default = "beep";
+ }
public void logout() {
logged_in = false;
@@ -48,6 +53,7 @@ public class ScannerSession {
return false;
}
this.logged_in = true;
+ this.theme = audio.get_random_user_theme();
return true;
}
@@ -62,7 +68,7 @@ public class ScannerSession {
/* check if scannerdata has valid format */
if(scannerdata != "USER %d".printf(id)) {
- audio.play("system/error.ogg");
+ audio.play_system("error.ogg");
write_to_log("Error: Invalid User ID: %s", scannerdata);
return false;
}
@@ -73,11 +79,11 @@ public class ScannerSession {
}
if(login(id)) {
- /* TODO: play audio */
+ audio.play_user(theme, "login");
write_to_log("Login: %s (%d)", name, user);
return true;
} else {
- audio.play("system/error.ogg");
+ audio.play_system("error.ogg");
write_to_log("Error: Login failed (User ID = %d)", id);
return false;
}
@@ -88,33 +94,33 @@ public class ScannerSession {
}
if(login(0)) {
- /* TODO: play audio */
+ audio.play_user(theme, "login");
write_to_log("Login: %s (%d)", name, user);
return true;
} else {
- audio.play("system/error.ogg");
+ audio.play_system("error.ogg");
write_to_log("Error: Login failed (User ID = 0)");
return false;
}
} else if(scannerdata == "UNDO") {
if(!logged_in) {
- audio.play("system/error.ogg");
+ audio.play_system("error.ogg");
write_to_log("Error: Can't undo if not logged in!");
return false;
} else {
if(db.undo(user)) {
- /* TODO: play audio */
+ audio.play_user(theme, "purchase");
write_to_log("Undo last purchase!");
return true;
} else {
- /* TODO: play audio */
+ audio.play_user(theme, "error");
write_to_log("Error: Couldn't undo last purchase!");
return false;
}
}
} else if(scannerdata == "LOGOUT") {
if(logged_in) {
- /* TODO: play audio */
+ audio.play_user(theme, "logout");
write_to_log("Logout!");
logout();
return true;
@@ -126,19 +132,25 @@ public class ScannerSession {
/* check if scannerdata has valid format */
if(scannerdata != "%llu".printf(id)) {
- /* TODO: play audio */
+ audio.play_user(theme, "error");
write_to_log("Error: invalid product: %s", scannerdata);
return false;
}
+ if(!logged_in) {
+ audio.play_system("error.ogg");
+ write_to_log("Error: Login Required!");
+ return false;
+ }
+
if(db.buy(user, id)) {
- /* TODO: play audio */
+ audio.play_user(theme, "purchase");
var name = db.get_product_name(id);
var price = db.get_product_price(user, id);
write_to_log(@"article bought: $name ($price €)");
return true;
} else {
- /* TODO: play audio */
+ audio.play_user(theme, "error");
write_to_log("Error: purchase failed!");
return false;
}