First actual commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package device
|
||||
|
||||
type WriteableDevice interface {
|
||||
Write(data []byte) (int, error)
|
||||
}
|
||||
|
||||
type PrinterDevice struct {
|
||||
Device WriteableDevice
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type PrinterSocket struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
||||
func (p *PrinterSocket) Write(data []byte) (int, error) {
|
||||
conn, err := net.Dial("tcp", p.Host+":"+p.Port)
|
||||
if err != nil {
|
||||
errStr := fmt.Sprintf("Error: %s", err)
|
||||
return 0, errors.New(errStr)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
written, err := conn.Write(data)
|
||||
if err != nil {
|
||||
fmt.Println("ERROR: ", err)
|
||||
}
|
||||
return written, err
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type PrinterUSB struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (p *PrinterUSB) Write(data []byte) (int, error) {
|
||||
printer, err := os.OpenFile(p.Path, os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer printer.Close()
|
||||
|
||||
return printer.Write(data)
|
||||
}
|
||||
Reference in New Issue
Block a user