forked from mattronix/escpos-go-labels
basic web app
This commit is contained in:
parent
4da54d788e
commit
8721646b29
2 changed files with 111 additions and 87 deletions
147
main.go
147
main.go
|
@ -1,60 +1,125 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/kenshaw/escpos"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RequestData represents the JSON request body structure
|
const ESC = byte(27)
|
||||||
type RequestData struct {
|
|
||||||
Date string `json:"date"`
|
func pageWidth(widht int) []byte {
|
||||||
Nickname string `json:"nickname"`
|
return []byte{escpos.GS, 87, byte(widht), 0}
|
||||||
|
}
|
||||||
|
func pageMargin(margin int) []byte {
|
||||||
|
return []byte{escpos.GS, 76, byte(margin), 0}
|
||||||
|
}
|
||||||
|
func pageMode() []byte {
|
||||||
|
return []byte{ESC, 'L'}
|
||||||
|
}
|
||||||
|
func pageDirection(direction uint8) []byte {
|
||||||
|
// https://www.epson-biz.com/modules/ref_escpos/index.php?content_id=55
|
||||||
|
return []byte{ESC, 'T', direction}
|
||||||
|
}
|
||||||
|
func pagePrintArea(x, y, width, height int) []byte {
|
||||||
|
return []byte{ESC, 'W',
|
||||||
|
byte(x & 255), byte(x >> 8 & 255),
|
||||||
|
byte(y & 255), byte(y >> 8 & 255),
|
||||||
|
byte(width & 255), byte(width >> 8 & 255),
|
||||||
|
byte(height & 255), byte(height >> 8 & 255)}
|
||||||
|
}
|
||||||
|
func offsetY(offset int) []byte {
|
||||||
|
// A positive number specifies movement downward, and a negative number specifies movement upward.
|
||||||
|
// The horizontal or vertical motion unit is used for the print direction set by ESC T.
|
||||||
|
// When the starting position is set to the upper left or lower right of the print area using ESC T, the vertical motion unit is used.
|
||||||
|
// When the starting position is set to the upper right or lower left of the print area using ESC T, the horizontal motion unit is used.
|
||||||
|
return []byte{escpos.GS, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
|
||||||
|
}
|
||||||
|
func offsetX(offset int) []byte {
|
||||||
|
// A positive number specifies movement to the right, and a negative number specifies movement to the left.
|
||||||
|
// When Standard mode is selected, the horizontal motion unit is used.
|
||||||
|
// when Page mode is selected, the horizontal or vertical motion unit is used for the print direction set by ESC T.
|
||||||
|
// When the starting position is set to the upper left or lower right of the print area using ESC T, the horizontal motion unit is used.
|
||||||
|
// When the starting position is set to the upper right or lower left of the print area using ESC T, the vertical motion unit is used.
|
||||||
|
return []byte{ESC, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
|
||||||
|
}
|
||||||
|
func invert(n int) []byte {
|
||||||
|
//When the LSB of n is 0, white/black reverse print mode is turned off.
|
||||||
|
//n = 0 – 255
|
||||||
|
return []byte{escpos.GS, byte('B'), byte(n)}
|
||||||
|
}
|
||||||
|
func disableSensors() []byte {
|
||||||
|
return []byte{ESC, 'c', '3', 0}
|
||||||
|
}
|
||||||
|
func printSpeed(m int) []byte {
|
||||||
|
// m = 0 – 9, 48 – 57 : slowest - fastest
|
||||||
|
// [Default] 0 = Setting value of GS ( E <Function 5> customize value (a = 6)
|
||||||
|
// m = 0
|
||||||
|
//Slower print speed seems to retain a bit more quality in dithered images. pure B/W images do not have a noticable difference
|
||||||
|
return []byte{escpos.GS, '(', 'K', 2, 0, 50, byte(m & 255), byte(m >> 8 & 255)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Set the output to unbuffered
|
f, err := os.OpenFile("/dev/usb/lp0", os.O_WRONLY|os.O_CREATE, 0)
|
||||||
log.SetOutput(os.Stdout)
|
|
||||||
|
|
||||||
http.HandleFunc("/print/dnh-label", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Only handle POST requests
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse JSON request body
|
|
||||||
var requestData RequestData
|
|
||||||
decoder := json.NewDecoder(r.Body)
|
|
||||||
if err := decoder.Decode(&requestData); err != nil {
|
|
||||||
http.Error(w, "Invalid JSON request body", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the date format
|
|
||||||
_, err := time.Parse("2006-01-02", requestData.Date)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Invalid date format. Please use yyyy-mm-dd", http.StatusBadRequest)
|
panic(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
// Process the request
|
ep := escpos.New(f)
|
||||||
response := fmt.Sprintf("Received Date: %s, Nickname: %s", requestData.Date, requestData.Nickname)
|
ep.Init()
|
||||||
|
|
||||||
// Send a JSON response
|
http.HandleFunc("/create_label", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
createDnhLabelHandler(w, r, ep)
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"message": response})
|
|
||||||
|
|
||||||
// Force immediate output to the console
|
|
||||||
log.Println(response)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start the server
|
fmt.Println("Server starting on port 8080")
|
||||||
fmt.Println("Server is running on :8080")
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
http.ListenAndServe(":8080", nil)
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createDnhLabelHandler(w http.ResponseWriter, r *http.Request, ep *escpos.Escpos) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
name := r.FormValue("name")
|
||||||
|
date := r.FormValue("date")
|
||||||
|
|
||||||
|
// Check if name is blank
|
||||||
|
if name == "" {
|
||||||
|
http.Error(w, "Name cannot be blank", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set date to today if not defined
|
||||||
|
if date == "" {
|
||||||
|
date = time.Now().Format("2006-01-02")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Here you can add the logic for creating a label using the POST data
|
||||||
|
text := fmt.Sprintf("%s - %s \n", name, date)
|
||||||
|
ep.Init()
|
||||||
|
ep.WriteRaw(pageMode())
|
||||||
|
ep.WriteRaw(pageDirection(1))
|
||||||
|
ep.WriteRaw(pagePrintArea(0, 0, int(math.Round((29.0/80.0)*512)), 850))
|
||||||
|
ep.WriteRaw(offsetY(30))
|
||||||
|
ep.SetFontSize(1, 1)
|
||||||
|
ep.Write("Technologia Incognita\n")
|
||||||
|
ep.WriteRaw(offsetY(35))
|
||||||
|
ep.SetFontSize(3, 2)
|
||||||
|
ep.SetEmphasize(1)
|
||||||
|
ep.Write("Do Not Hack\n")
|
||||||
|
ep.SetEmphasize(0)
|
||||||
|
ep.WriteRaw(offsetY(15))
|
||||||
|
ep.SetFontSize(1, 1)
|
||||||
|
ep.Write(text)
|
||||||
|
ep.WriteRaw([]byte{12}) // FF: in Page mode, prints all the data in the print buffer collectively and switches from Page mode to Standard mode.
|
||||||
|
ep.Cut()
|
||||||
|
ep.End()
|
||||||
|
}
|
41
server.go
41
server.go
|
@ -1,41 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
http.HandleFunc("/create_label", createLabelHandler)
|
|
||||||
|
|
||||||
fmt.Println("Server starting on port 8080")
|
|
||||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func createLabelHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
name := r.FormValue("name")
|
|
||||||
date := r.FormValue("date")
|
|
||||||
|
|
||||||
// Check if name is blank
|
|
||||||
if name == "" {
|
|
||||||
http.Error(w, "Name cannot be blank", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set date to today if not defined
|
|
||||||
if date == "" {
|
|
||||||
date = time.Now().Format("2006-01-02")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Here you can add the logic for creating a label using the POST data
|
|
||||||
fmt.Fprintf(w, "Label created! Name: %s, Date: %s", name, date)
|
|
||||||
}
|
|
Loading…
Reference in a new issue