/
machine.go
73 lines (65 loc) · 1.83 KB
/
machine.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
package cmd
import (
"fmt"
"log"
"strconv"
"time"
"github.com/masykur/absen/pkg/sf3000"
"github.com/spf13/cobra"
)
// represents the time command
var machineCommand = &cobra.Command{
Use: "machine",
Short: "Manage attandance machine",
Long: "Obtain product code and serial number of the machine"}
var machineGetCommand = &cobra.Command{
Use: "get",
Short: "Obtain attandance machine information",
Long: "Obtain product code and serial number of the machine",
}
func init() {
machineCommand.AddCommand(machineGetCommand)
machineGetCommand.AddCommand(&cobra.Command{
Use: "product-code",
Aliases: []string{"p", "prod", "product"},
Short: "Obtain machine product code",
Args: cobra.ExactArgs(0),
Run: getProductCode})
machineGetCommand.AddCommand(&cobra.Command{
Use: "serial-number",
Aliases: []string{"s", "sn"},
Short: "Obtain machine serial number",
Args: cobra.ExactArgs(0),
Run: getSerialNumber})
RootCmd.AddCommand(machineCommand)
}
// Obtain product code
func getProductCode(cmd *cobra.Command, args []string) {
servAddr := host + ":" + strconv.Itoa(port)
device := new(sf3000.Sf3000)
if ok, err := device.Connect(servAddr, nid, password, time.Duration(time.Second*20)); ok {
defer device.Close()
if productCode, err := device.GetProductCode(); err == nil {
fmt.Println(productCode)
} else {
log.Fatalln(err)
}
} else {
log.Fatalln(err)
}
}
// Obtain serial number
func getSerialNumber(cmd *cobra.Command, args []string) {
servAddr := host + ":" + strconv.Itoa(port)
device := new(sf3000.Sf3000)
if ok, err := device.Connect(servAddr, nid, password, time.Duration(time.Second*20)); ok {
defer device.Close()
if serialNumber, err := device.GetSerialNumber(); err == nil {
fmt.Println(serialNumber)
} else {
log.Fatalln(err)
}
} else {
log.Fatalln(err)
}
}