aboutsummaryrefslogtreecommitdiff
path: root/sklad
diff options
context:
space:
mode:
Diffstat (limited to 'sklad')
-rw-r--r--sklad/db.go21
-rw-r--r--sklad/label.tmpl13
-rw-r--r--sklad/main.go56
3 files changed, 85 insertions, 5 deletions
diff --git a/sklad/db.go b/sklad/db.go
index aae695f..def18a5 100644
--- a/sklad/db.go
+++ b/sklad/db.go
@@ -7,6 +7,8 @@ import (
"os"
"strings"
"time"
+
+ "janouch.name/sklad/bdf"
)
type Series struct {
@@ -46,6 +48,9 @@ type Database struct {
Prefix string // prefix for all container IDs
Series []*Series // all known series
Containers []*Container // all known containers
+
+ BDFPath string // path to bitmap font file
+ BDFScale int // integer scaling for the bitmap font
}
var (
@@ -57,6 +62,8 @@ var (
indexSeries = map[string]*Series{}
indexContainer = map[ContainerId]*Container{}
indexChildren = map[ContainerId][]*Container{}
+
+ labelFont *bdf.Font
)
// TODO: Some functions to add, remove and change things in the database.
@@ -192,6 +199,20 @@ func loadDatabase() error {
}
}
+ // Prepare label printing.
+ if db.BDFScale <= 0 {
+ db.BDFScale = 1
+ }
+
+ if f, err := os.Open(db.BDFPath); err != nil {
+ return fmt.Errorf("cannot load label font: %s", err)
+ } else {
+ defer f.Close()
+ if labelFont, err = bdf.NewFromBDF(f); err != nil {
+ return fmt.Errorf("cannot load label font: %s", err)
+ }
+ }
+
// Open database log file for appending.
if dbLog, err = os.OpenFile(dbPath+".log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
diff --git a/sklad/label.tmpl b/sklad/label.tmpl
new file mode 100644
index 0000000..da17c58
--- /dev/null
+++ b/sklad/label.tmpl
@@ -0,0 +1,13 @@
+{{ define "Title" }}Tisk štítku{{ end }}
+{{ define "Content" }}
+<h2>Tisk štítku pro {{ .Id }}</h2>
+
+{{ if .UnknownId }}
+<p>Neznámý obal.
+{{ else if .Error }}
+<p>Tisk selhal: {{ .Error }}
+{{ else }}
+<p>Tisk proběhl úspěšně.
+{{ end }}
+
+{{ end }}
diff --git a/sklad/main.go b/sklad/main.go
index 9df1fd5..9a7be68 100644
--- a/sklad/main.go
+++ b/sklad/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
"html/template"
"io"
"log"
@@ -9,6 +10,10 @@ import (
"os"
"path/filepath"
"time"
+
+ "janouch.name/sklad/imgutil"
+ "janouch.name/sklad/label"
+ "janouch.name/sklad/ql"
)
var templates = map[string]*template.Template{}
@@ -170,18 +175,59 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
executeTemplate("search.tmpl", w, &params)
}
+func printLabel(id string) error {
+ printer, err := ql.Open()
+ if err != nil {
+ return err
+ }
+ if printer == nil {
+ return errors.New("no suitable printer found")
+ }
+ defer printer.Close()
+
+ printer.StatusNotify = func(status *ql.Status) {
+ log.Printf("\x1b[1mreceived status\x1b[m\n%+v\n%s",
+ status[:], status)
+ }
+
+ if err := printer.Initialize(); err != nil {
+ return err
+ }
+ if err := printer.UpdateStatus(); err != nil {
+ return err
+ }
+
+ mediaInfo := ql.GetMediaInfo(
+ printer.LastStatus.MediaWidthMM(),
+ printer.LastStatus.MediaLengthMM(),
+ )
+ if mediaInfo == nil {
+ return errors.New("unknown media")
+ }
+
+ return printer.Print(&imgutil.LeftRotate{Image: label.GenLabelForHeight(
+ labelFont, id, mediaInfo.PrintAreaPins, db.BDFScale)})
+}
+
func handleLabel(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
- id := r.FormValue("id")
- _ = id
-
- // TODO: See if such a container exists, print a label on the printer.
+ params := struct {
+ Id string
+ UnknownId bool
+ Error error
+ }{
+ Id: r.FormValue("id"),
+ }
- params := struct{}{}
+ if c := indexContainer[ContainerId(params.Id)]; c == nil {
+ params.UnknownId = true
+ } else {
+ params.Error = printLabel(params.Id)
+ }
executeTemplate("label.tmpl", w, &params)
}