-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakerbot.go
81 lines (65 loc) · 2 KB
/
makerbot.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
75
76
77
78
79
80
81
// Package makerbot is a Go client library for MakerBot printers.
//
// Full README: https://github.com/tjhorner/makerbot-rpc/blob/master/README.md
package makerbot
import (
"strconv"
"strings"
"time"
"github.com/hashicorp/mdns"
)
// These constants are used to communicate with the printer
// and are apparently hard-coded
const (
makerbotClientID = "MakerWare"
makerbotClientSecret = "secret"
)
// NewClient creates a new client.
func NewClient() Client {
return Client{Connected: false, Timeout: 5 * time.Second}
}
const mdnsService = "_makerbot-jsonrpc._tcp"
// DiscoverPrinters will discover printers that are on your current
// LAN network and return them once `timeout` is up.
//
// Note that all fields are not returned in the printer's mDNS TXT
// reply. Useful fields like MachineName and IP/Port are in there,
// though, so that should be enough to initiate a connection with
// the printer.
func DiscoverPrinters(timeout time.Duration) (*[]Printer, error) {
var printers []Printer
ch := make(chan *mdns.ServiceEntry)
go func() {
for entry := range ch {
if !strings.Contains(entry.Name, "_makerbot-jsonrpc") {
continue
}
fields := *parseInfoFields(&entry.InfoFields)
vid, _ := strconv.Atoi(fields["vid"])
pid, _ := strconv.Atoi(fields["pid"])
printer := Printer{
MachineName: fields["machine_name"],
MachineType: fields["machine_type"],
APIVersion: fields["api_version"],
Serial: fields["iserial"],
MotorDriverVersion: fields["motor_driver_version"],
Vid: vid,
Pid: pid,
SSLPort: fields["ssl_port"],
BotType: fields["bot_type"],
IP: entry.AddrV4.String(),
Port: string(entry.Port),
}
printers = append(printers, printer)
}
}()
params := mdns.DefaultParams(mdnsService)
params.Timeout = timeout
params.Entries = ch
err := mdns.Query(params)
if err != nil {
return nil, err
}
close(ch)
return &printers, nil
}