forked from macdylan/sm2uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector_sacp.go
74 lines (62 loc) · 1.19 KB
/
connector_sacp.go
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
package main
import (
"log"
"net"
"os"
"time"
"github.com/gosuri/uilive"
)
const (
SACPPort = "8888"
SACPTimeout = 5
)
type SACPConnector struct {
printer *Printer
conn net.Conn
}
func (sc *SACPConnector) Ping(p *Printer) bool {
// if !p.Sacp {
// return false
// }
if ping(p.IP, SACPPort, 3) {
sc.printer = p
return true
}
return false
}
func (sc *SACPConnector) Connect() (err error) {
conn, err := SACP_connect(sc.printer.IP, SACPTimeout*time.Second)
if conn != nil {
sc.conn = conn
}
return err
}
func (sc *SACPConnector) Disconnect() error {
if sc.conn != nil {
SACP_disconnect(sc.conn, SACPTimeout*time.Second)
sc.conn.Close()
}
return nil
}
func (sc *SACPConnector) Upload(payload *Payload) (err error) {
content, err := payload.GetContent(NoFix)
if !NoFix {
if err != nil {
log.Printf("G-Code fix error(ignored): %s", err)
} else if payload.ShouldBeFix() {
log.Printf("G-Code fixed")
}
}
w := uilive.New()
w.Start()
log.SetOutput(w)
defer func() {
w.Stop()
log.SetOutput(os.Stderr)
}()
err = SACP_start_upload(sc.conn, payload.Name, content, SACPTimeout*time.Second)
return
}
func init() {
Connector.RegisterHandler(&SACPConnector{})
}