-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (134 loc) · 3.64 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"sort"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
var serverHost, serverPort string
func main() {
app := cli.NewApp()
app.Name = "LG TV command-line interface"
app.Usage = "Let's you drive your LG TV from your terminal!"
app.EnableBashCompletion = true
// set the viper options to read config.yaml
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
// check if config.yaml exists
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Impossible to read conf file: %v", err)
}
serverHost = inConfigFile("ip").(string)
serverPort = inConfigFile("port").(string)
app.Commands = []cli.Command{
{
Name: "volume",
Usage: "Adjusts the TV volume. The value must be in 0-100",
Action: func(c *cli.Context) error {
volVal := c.Args().First()
volValInt, _ := strconv.Atoi(volVal)
err := sendCommand(serverHost, serverPort, fmt.Sprintf("kf 00 %.2x", volValInt))
if err != nil {
return err
}
return nil
},
},
{
Name: "mute",
Usage: "Turns on/off the TV sound. The argument must be true to mute, anything else to unmute",
Action: func(c *cli.Context) error {
if strings.ToLower(c.Args().First()) == "true" || c.Args().First() == "1" {
// mute the sound
err := sendCommand(serverHost, serverPort, "ke 00 00")
if err != nil {
return err
}
return nil
}
// unmute the sound
err := sendCommand(serverHost, serverPort, "ke 00 01")
if err != nil {
return err
}
return nil
},
},
{
Name: "poweroff",
Usage: "Turns off the TV. Needs no arguments",
Action: func(c *cli.Context) error {
err := sendCommand(serverHost, serverPort, "ka 00 00")
if err != nil {
return err
}
return nil
},
},
{
Name: "brightness",
Usage: "Adjusts the brightness of the screen. The value must be in 0-100",
Action: func(c *cli.Context) error {
brightVal := c.Args().First()
brightValInt, _ := strconv.Atoi(brightVal)
err := sendCommand(serverHost, serverPort, fmt.Sprintf("kh 00 %.2x", brightValInt))
if err != nil {
return err
}
return nil
},
},
{
Name: "contrast",
Usage: "Adjusts the contrast of the screen. The value must be in 0-100",
Action: func(c *cli.Context) error {
contrVal := c.Args().First()
contrValInt, _ := strconv.Atoi(contrVal)
err := sendCommand(serverHost, serverPort, fmt.Sprintf("kg 00 %.2x", contrValInt))
if err != nil {
return err
}
return nil
},
},
}
// Sort commands list in help panel by name
sort.Sort(cli.CommandsByName(app.Commands))
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func inConfigFile(param string) interface{} {
if viper.Get(param) == nil {
return fmt.Sprintf("%s not found in config file", param)
}
return viper.Get(param)
}
//This function sends a command string to the IP address given at the given port
func sendCommand(srv string, port string, command string) error {
// add the command to a new reader for io and append an necessited carriage return
r := strings.NewReader(command + "\n")
logrus.Debugf("connecting to TV at %s:%s", srv, port)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", srv, port))
if err != nil {
fmt.Printf("Can't connect to server: %s\n", err)
return nil
}
logrus.Debugf("sending command %s to TV", command)
// copy the reader in the io, so this "send" it to the server (copy-paste to the i buffer)
_, err = io.Copy(conn, r)
if err != nil {
return fmt.Errorf("Connection error: %d", err)
}
return nil
}