diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | freebsd/kqueue.c | 142 | ||||
-rwxr-xr-x | printloop.sh | 15 | ||||
-rwxr-xr-x | start.sh | 1 | ||||
-rwxr-xr-x | trigger.sh | 19 | ||||
-rw-r--r-- | udev.rules | 2 |
7 files changed, 186 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e38f775 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.*swp +queue +pack.tar.xz +oldversions @@ -13,4 +13,6 @@ $(SUBDIRS): $(SUBDIRS_CLEAN): make -C $(subst _clean,,$@) clean pack: - tar zcfv pack.tar.gz $(shell cat packlist) + tar Jcfv pack.tar.xz $(shell cat packlist) +deploy: + tar Jcf - $(shell cat packlist) | ssh space.labitat.dk tar Jxf - diff --git a/freebsd/kqueue.c b/freebsd/kqueue.c new file mode 100644 index 0000000..e8825db --- /dev/null +++ b/freebsd/kqueue.c @@ -0,0 +1,142 @@ +#include <sys/types.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/event.h> +#include <sys/time.h> +#include <sys/stat.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +int printer_ready; +char * printer; +char * queue_dir; +char * trigger; +#define BUFLEN 50 +char queue_dir_new[BUFLEN]; +char trigger_cmd[BUFLEN]; + +int open_and_subscribe(struct kevent * change, const char * path) +{ + int f; + f = open(path, O_RDONLY); + if (f == -1) { + perror("open"); + return -1; + } + + EV_SET(change, f, EVFILT_VNODE, + EV_ADD | EV_ENABLE | EV_ONESHOT, + NOTE_DELETE | NOTE_EXTEND | + NOTE_WRITE | NOTE_ATTRIB, + 0, 0); +} + +int check_printer_status(){ + struct stat buf; + //printf("%s\n", printer); + int r = stat(printer, &buf); + if (r<0) { + if (printer_ready != 0) { + printf("Printer offline\n"); + } + printer_ready = 0; + if (errno == ENOENT) { + printf("Printer not detected\n"); + } else { + perror("stat"); + return -1; + } + } else { + if (printer_ready != 1) { + printf("Printer online\n"); + print_loop(printer, queue_dir); + } + printer_ready = 1; + } + return 0; +} + +int print_loop() +{ + FILE * p; + int status; + char buf[BUFLEN]; + printf("Print loop\n"); + p = popen(trigger_cmd, "r"); + if (p == NULL) + perror("popen"); + while (fgets(buf, BUFLEN, p) != NULL) + printf("%s", buf); + status = pclose(p); + if (status != 0) { + printf("Trigger exited with status %d\n", status); + } + return status; +} + +int main(int argc, const char * argv[]) +{ + int fd, fq, kq, nev; + struct kevent change; + struct kevent event; + + int len; + + if (argc != 4) { + fprintf(stderr, "usage: %s printer queue_dir trigger\n", argv[0]); + return -1; + } + printer = strdup(argv[1]); + queue_dir = strdup(argv[2]); + trigger = strdup(argv[3]); + + len = snprintf(trigger_cmd, BUFLEN, "%s %s %s", trigger, printer, queue_dir); + if (len >= BUFLEN - 1) { + printf("Buffer too small\n"); + return -1; + } + + len = snprintf(queue_dir_new, BUFLEN, "%s/new", queue_dir); + if (len >= BUFLEN - 1) { + printf("Buffer too small\n"); + return -1; + } + + printer_ready = 0; + + kq = kqueue(); + if (kq == -1) + perror("kqueue"); + + fq = open_and_subscribe(&change, queue_dir_new); + + struct timespec timeout; + timeout.tv_sec = 5; + timeout.tv_nsec = 0; + + check_printer_status(); + for (;;) { + nev = kevent(kq, &change, 1, &event, 1, &timeout); + if (nev == -1) + perror("kevent"); + if (nev == 0) { // timeout + check_printer_status(); + } else if (nev > 0) { + if (event.fflags & NOTE_DELETE) { + continue; + } + printf("Got event for %d\n", event.ident); + if (printer_ready) { + print_loop(printer, queue_dir, trigger); + } + } + } + + close(kq); + close(fq); + close(fd); + return EXIT_SUCCESS; +} diff --git a/printloop.sh b/printloop.sh new file mode 100755 index 0000000..74ad25e --- /dev/null +++ b/printloop.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +printer="/dev/labelprinter" +queuedir=queue + +print_queue(){ + for job in $(ls -1 "$queuedir/new"); do + ./trigger.sh "$printer" "$queuedir" + done +} + +print_queue +while inotifywait --quiet "$queuedir/new" /dev; do + print_queue +done diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..4cae230 --- /dev/null +++ b/start.sh @@ -0,0 +1 @@ +./printloop.sh | tee /dev/stderr | (cd web && ./labitrackd.lua 0.0.0.0:8081 ../queue/ diff --git a/trigger.sh b/trigger.sh new file mode 100755 index 0000000..9740048 --- /dev/null +++ b/trigger.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +printer="$1" +queue_dir="$2" +printed_dir=$queue_dir/printed/ + +mkdir -p $printed_dir + +for file in $(ls $queue_dir/new/* 2>/dev/null); do + echo printing $file + ./ql570/ql570 $printer $file + status=$? + if [ $status -ne 0 ] ; then + exit $status + fi + mv $file $printed_dir +done + +exit 0 diff --git a/udev.rules b/udev.rules new file mode 100644 index 0000000..d4c8353 --- /dev/null +++ b/udev.rules @@ -0,0 +1,2 @@ +# rule for labelprinter +SUBSYSTEM=="usb", ATTRS{idVendor}=="04f9", ATTRS{idProduct}=="2028", SYMLINK+="labelprinter", MODE="0666" |