From c4054b3a23e27cb8a0c19b7f484df7c9eef981c2 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 2 May 2024 22:18:48 +0100 Subject: [PATCH] Added configuration --- config/config.go | 78 ++++++++++++++++++++++++++++++++ device/socketDevice.go | 4 +- go.mod | 1 + go.sum | 3 ++ thermalPrinter/thermalPrinter.go | 15 +++--- 5 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..cd7892b --- /dev/null +++ b/config/config.go @@ -0,0 +1,78 @@ +package config + +import ( + "log" + "os" + "slices" + "sync" + "thermalPrinter/device" + + "gopkg.in/yaml.v2" +) + +var ( + instance *Configuration + once sync.Once + validUseValues = []string{ + "socket", + "usb", + } +) + +type usbDev struct { + Path string `yaml:"path"` +} + +type socketDev struct { + Host string `yaml:"host"` + Port string `yaml:"port"` +} + +type Configuration struct { + Use string `yaml:"use"` + Usb *usbDev `yaml:"usb"` + Socket *socketDev `yaml:"socket"` +} + +func GetInstance() *Configuration { + once.Do(func() { + instance = &Configuration{} + instance.loadConfiguration() + }) + + return instance +} + +func (c *Configuration) loadConfiguration() { + confPath := "/home/esilva/Desktop/projetos/thermal_printer/config.yaml" + + file, err := os.ReadFile(confPath) + if err != nil { + log.Fatalf("Unable to open configuration file [%s]: %s", confPath, + err.Error()) + } + + err = yaml.Unmarshal(file, &instance) + if err != nil { + log.Fatalf("Error parsing config file: %s", err.Error()) + } + + if !slices.Contains(validUseValues, instance.Use) { + log.Fatalf("Unusual value for `Use`.") + } +} + +func (c *Configuration) GetWriteableDevice() device.WriteableDevice { + if c.Use == "usb" { + return &device.PrinterUSB{ + Path: c.Usb.Path, + } + } else if c.Use == "socket" { + return &device.PrinterSocket{ + Host: c.Socket.Host, + Port: c.Socket.Port, + } + } + + return nil +} diff --git a/device/socketDevice.go b/device/socketDevice.go index b28538b..b3adac9 100644 --- a/device/socketDevice.go +++ b/device/socketDevice.go @@ -21,7 +21,9 @@ func (p *PrinterSocket) Write(data []byte) (int, error) { written, err := conn.Write(data) if err != nil { - fmt.Println("ERROR: ", err) + errStr := fmt.Sprintf("Error: %s", err) + return written, errors.New(errStr) } + return written, err } diff --git a/go.mod b/go.mod index a4b14a4..11a8131 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.2 require ( github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da github.com/spf13/cobra v1.8.0 + gopkg.in/yaml.v2 v2.4.0 ) require ( diff --git a/go.sum b/go.sum index e4f3efa..81e6838 100644 --- a/go.sum +++ b/go.sum @@ -8,5 +8,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/thermalPrinter/thermalPrinter.go b/thermalPrinter/thermalPrinter.go index 5421cc3..013258d 100644 --- a/thermalPrinter/thermalPrinter.go +++ b/thermalPrinter/thermalPrinter.go @@ -1,8 +1,10 @@ package thermalprinter import ( + "fmt" "sync" + "thermalPrinter/config" "thermalPrinter/device" encodings "thermalPrinter/encoding" ) @@ -19,7 +21,10 @@ type TP struct { func (p *TP) Print(message string) { processed := p.TextProcessor.Process(message) - p.Device.Write(processed) + _, err := p.Device.Write(processed) + if err != nil { + fmt.Println("Err: ", err) + } } func GetInstance(customDev device.WriteableDevice) *TP { @@ -27,13 +32,11 @@ func GetInstance(customDev device.WriteableDevice) *TP { var p encodings.Parser p.Initialize() - devSocket := device.PrinterSocket{ - Host: "", - Port: "", - } + cfg := config.GetInstance() + device := cfg.GetWriteableDevice() instance = &TP{ - Device: &devSocket, + Device: device, TextProcessor: p, } })