l e m m s t e r . d e
  • Home
  • impressum
  • talks
  • Categories
  • Tags
  • Archives

scanbuttond on fritz 7390

A recent commit integrates my pull request to incorporate scanbuttond into freetz, a custom firmware for Fritz Box 7390. scanbuttond's job description is, to trigger a script whenever a button is pressed on a scanner. This is all I need in order to turn my 7390 and an old (three button) USB scanner into an always on snail mail to email converter (think paperless office). Once a freetz image with scanbuttond & sane for your scanner model has been backed, “sane-find-scanner” is uncommented in the upper box on the freetz webfrontend. The script below is pasted into the lower “Script for button” box (adapt the email addresses EMAIL_* accordingly). If you find that you cannot paste it directly, insert the script into /mod/etc/conf/scanbuttond.cfg via ssh/telnet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh

TMPFILE="scan.pnm"
LOCKFILE="/var/media/ftp/tmp/copy.lock"

RECEIPIENT_FILE="/var/media/ftp/tmp/receipient.txt"
MODE_FILE="/var/media/ftp/tmp/mode.txt"
PAGE_NUM_FILE="/var/media/ftp/tmp/page_num.txt"

# to in notification mails (mapped to button A and B)
EMAIL_A=email1@example.com
EMAIL_B=email2@example.com

# from header in notification mails
EMAIL_FROM=scanner@example.com

case $1 in
    1)
                # Set color mode to "gray" and exit
                if [ ! -e $MODE_FILE ]; then
                 logger "button 1 has been pressed on $2, setting color mode"
                 echo "Color" > $MODE_FILE && exit 0
                fi

                # Set receipient to EMAIL_A and exit
                if [ ! -e $RECEIPIENT_FILE ]; then
                 logger "button 1 has been pressed on $2, setting receiver"
                 echo $EMAIL_A > $RECEIPIENT_FILE && exit 0
                fi
                ;;
        2)
                # Set color mode to "Color"
                if [ ! -e $MODE_FILE ]; then
                 logger "button 2 has been pressed on $2, setting color mode"
                 echo "Gray" > $MODE_FILE && exit 0
                fi

                # Set receipient to EMAIL_B and exit
                if [ ! -e $RECEIPIENT_FILE ]; then
                 logger "button 2 has been pressed on $2, setting receiver"
                 echo $EMAIL_B > $RECEIPIENT_FILE && exit 0
                fi

                logger "button 1 has been pressed on $2"
                if ! mkdir -p $LOCKFILE; then
                 logger "Error: scanning already in progress for $2"
                 exit
                fi

                mkdir -p /var/media/ftp/tmp/scans

                # Find out last page number and increment or initialize to 0
                test ! -e $PAGE_NUM_FILE && echo 0 > $PAGE_NUM_FILE
                PAGE_NUM=`cat $PAGE_NUM_FILE`
                let "PAGE_NUM += 1"
                echo $PAGE_NUM > $PAGE_NUM_FILE

                logger "Starting to scan document"
                SCAN_OPTIONS="--resolution 600 --contrast 10 --brightness 0 --format=pnm"
                scanimage --verbose --device-name $2 \
                          --mode Gray -x 210 -y 297 $SCAN_OPTIONS > /var/media/ftp/tmp/scans/$PAGE_NUM$TMPFILE

                echo "Releasing lock file"
                rm -rf $LOCKFILE
                ;;
        3)
                # convert each page to pdf
                for f in `find /var/media/ftp/tmp/scans/*.pnm`; do
                 logger "Converting pnm documents to jpg"
                 pnmtojpeg --quality 25 $f > ${f/pnm/jpg}
                 FILES=$FILES,${f/pnm/jpg}
                done

        # Archive pnm files
                logger "Archiving pnm documents"
        NOW=`date +%s`
        mkdir -p /var/media/ftp/scans/$NOW
        mv /var/media/ftp/tmp/scans/*.pnm /var/media/ftp/scans/$NOW

                # send final concatenated pdf page to receipient
                RECEIPIENT=`cat $RECEIPIENT_FILE`
                logger "Sending document to $RECEIPIENT"
        echo sftp://fritz.box/var/media/ftp/scans/$NOW > /var/media/ftp/tmp/body.txt
        echo "for i in \`ls\`; do cd \$i && pnm2pdf.sh && cd ..; done" >> /var/media/ftp/tmp/body.txt
        mailer -s Scan -f $EMAIL_FROM -t $RECEIPIENT -i /var/media/ftp/tmp/body.txt -d $FILES

                logger "Cleaning up intermediate files"
        test -e /var/media/ftp/tmp/body.txt && rm /var/media/ftp/tmp/body.txt
                test -e $MODE_FILE && rm $MODE_FILE
                test -e $RECEIPIENT_FILE && rm $RECEIPIENT_FILE
                test -e $PAGE_NUM_FILE && rm $PAGE_NUM_FILE
                test -d /var/media/ftp/tmp/scans/ && rm -rf /var/media/ftp/tmp/scans/
                ;;
esac

The following finite state machine/automata shows how the buttons are programmed:

scanbuttond

If you occasionally find yourself forgetting to press the end button (#3) after the last page has been scanned, using cron to periodically check for stale tmp/ folders does the job for you. Assuming crond is part of your freetz image, just add a the following line on the crond webfrontend.

*/45 * * * * if [ `find /var/media/ftp/tmp/scans/ \
  -iname *.pnm -type f -mmin +15  2>/dev/null | wc -l` -gt 0 ]; \
  then /var/mod/etc/scanbuttond/buttonpressed.sh 3; fi

Published

Jun 3, 2013

Category

hacks

Contact

  • Powered by Pelican. Theme: Elegant