-
Notifications
You must be signed in to change notification settings - Fork 174
/
main.go
executable file
·323 lines (281 loc) · 9.27 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Version 1.86
// Supports Windows, Linux, Mac, and Raspberry Pi, Beagle Bone Black
package main
import (
"flag"
"go/build"
"log"
"net/http"
//"path/filepath"
"errors"
"fmt"
"net"
"os"
//"net/http/pprof"
//"runtime"
"io"
"runtime/debug"
"text/template"
"time"
)
var (
version = "1.87"
versionFloat = float32(1.87)
addr = flag.String("addr", ":8989", "http service address")
//assets = flag.String("assets", defaultAssetPath(), "path to assets")
//verbose = flag.Bool("v", true, "show debug logging")
verbose = flag.Bool("v", false, "show debug logging")
//homeTempl *template.Template
isLaunchSelf = flag.Bool("ls", false, "launch self 5 seconds later")
// regular expression to sort the serial port list
// typically this wouldn't be provided, but if the user wants to clean
// up their list with a regexp so it's cleaner inside their end-user interface
// such as ChiliPeppr, this can make the massive list that Linux gives back
// to you be a bit more manageable
regExpFilter = flag.String("regex", "", "Regular expression to filter serial port list")
// allow garbageCollection()
//isGC = flag.Bool("gc", false, "Is garbage collection on? Off by default.")
//isGC = flag.Bool("gc", true, "Is garbage collection on? Off by default.")
gcType = flag.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
// whether to do buffer flow debugging
bufFlowDebugType = flag.String("bufflowdebug", "off", "off = (default) We do not send back any debug JSON, on = We will send back a JSON response with debug info based on the configuration of the buffer flow that the user picked")
// hostname. allow user to override, otherwise we look it up
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
)
type NullWriter int
func (NullWriter) Write([]byte) (int, error) { return 0, nil }
func defaultAssetPath() string {
//p, err := build.Default.Import("gary.burd.info/go-websocket-chat", "", build.FindOnly)
p, err := build.Default.Import("github.com/johnlauer/serial-port-json-server", "", build.FindOnly)
if err != nil {
return "."
}
return p.Dir
}
func homeHandler(c http.ResponseWriter, req *http.Request) {
homeTemplate.Execute(c, req.Host)
}
func launchSelfLater() {
log.Println("Going to launch myself 5 seconds later.")
time.Sleep(2 * 1000 * time.Millisecond)
log.Println("Done waiting 5 secs. Now launching...")
}
func main() {
flag.Parse()
// setup logging
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
// see if we are supposed to wait 5 seconds
if *isLaunchSelf {
launchSelfLater()
}
//getList()
f := flag.Lookup("addr")
log.Println("Version:" + version)
// hostname
hn, _ := os.Hostname()
if *hostname == "unknown-hostname" {
*hostname = hn
}
log.Println("Hostname:", *hostname)
// turn off garbage collection
// this is dangerous, as u could overflow memory
//if *isGC {
if *gcType == "std" {
log.Println("Garbage collection is on using Standard mode, meaning we just let Golang determine when to garbage collect.")
} else if *gcType == "max" {
log.Println("Garbage collection is on for MAXIMUM real-time collecting on each send/recv from serial port. Higher CPU, but less stopping of the world to garbage collect since it is being done on a constant basis.")
} else {
log.Println("Garbage collection is off. Memory use will grow unbounded. You WILL RUN OUT OF RAM unless you send in the gc command to manually force garbage collection. Lower CPU, but progressive memory footprint.")
debug.SetGCPercent(-1)
}
ip, err := externalIP()
if err != nil {
log.Println(err)
}
log.Print("Starting server and websocket on " + ip + "" + f.Value.String())
//homeTempl = template.Must(template.ParseFiles(filepath.Join(*assets, "home.html")))
log.Println("The Serial Port JSON Server is now running.")
log.Println("If you are using ChiliPeppr, you may go back to it and connect to this server.")
// see if they provided a regex filter
if len(*regExpFilter) > 0 {
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
}
//GetDarwinMeta()
if !*verbose {
log.Println("You can enter verbose mode to see all logging by starting with the -v command line switch.")
log.SetOutput(new(NullWriter)) //route all logging to nullwriter
}
// list serial ports
portList, _ := GetList()
metaports, _ := GetMetaList()
/*if errSys != nil {
log.Printf("Got system error trying to retrieve serial port list. Err:%v\n", errSys)
log.Fatal("Exiting")
}*/
go func() {
time.Sleep(300 * time.Millisecond)
log.SetOutput(io.Writer(os.Stdout))
log.Println("Your serial ports:")
if len(portList) == 0 {
log.Println("\tThere are no serial ports to list.")
}
for _, element := range portList {
// if we have meta data for this port, use it
setMetaDataForOsSerialPort(&element, metaports)
log.Printf("\t%v\n", element)
}
if !*verbose {
//log.Println("You can enter verbose mode to see all logging by starting with the -v command line switch.")
log.SetOutput(new(NullWriter)) //route all logging to nullwriter
}
}()
// launch the hub routine which is the singleton for the websocket server
go h.run()
// launch our serial port routine
go sh.run()
// launch our dummy data routine
//go d.run()
// Setup GPIO server
// Ignore GPIO for now, but it would be nice to get GPIO going natively
//gpio.PreInit()
// when the app exits, clean up our gpio ports
//defer gpio.CleanupGpio()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/ws", wsHandler)
if err := http.ListenAndServe(*addr, nil); err != nil {
fmt.Printf("Error trying to bind to port: %v, so exiting...", err)
log.Fatal("Error ListenAndServe:", err)
}
}
func externalIP() (string, error) {
//log.Println("Getting external IP")
ifaces, err := net.Interfaces()
if err != nil {
log.Println("Got err getting external IP addr")
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
//log.Println("Iface down")
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
//log.Println("Loopback")
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
log.Println("Got err on iface.Addrs()")
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
//log.Println("Ip was nil or loopback")
continue
}
ip = ip.To4()
if ip == nil {
//log.Println("Was not ipv4 addr")
continue // not an ipv4 address
}
//log.Println("IP is ", ip.String())
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHtml))
// If you navigate to this server's homepage, you'll get this HTML
// so you can directly interact with the serial port server
const homeTemplateHtml = `<!DOCTYPE html>
<html>
<head>
<title>Serial Port Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var conn;
var msg = $("#msg");
var log = $("#log");
function appendLog(msg) {
var d = log[0]
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
msg.appendTo(log)
if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight;
}
}
$("#form").submit(function() {
if (!conn) {
return false;
}
if (!msg.val()) {
return false;
}
conn.send(msg.val() + "\n");
msg.val("");
return false
});
if (window["WebSocket"]) {
conn = new WebSocket("ws://{{$}}/ws");
conn.onclose = function(evt) {
appendLog($("<div><b>Connection closed.</b></div>"))
}
conn.onmessage = function(evt) {
appendLog($("<div/>").text(evt.data))
}
} else {
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
}
});
</script>
<style type="text/css">
html {
overflow: hidden;
}
body {
overflow: hidden;
padding: 0;
margin: 0;
width: 100%;
height: 100%;
background: gray;
}
#log {
background: white;
margin: 0;
padding: 0.5em 0.5em 0.5em 0.5em;
position: absolute;
top: 0.5em;
left: 0.5em;
right: 0.5em;
bottom: 3em;
overflow: auto;
}
#form {
padding: 0 0.5em 0 0.5em;
margin: 0;
position: absolute;
bottom: 1em;
left: 0px;
width: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<div id="log"></div>
<form id="form">
<input type="submit" value="Send" />
<input type="text" id="msg" size="64"/>
</form>
</body>
</html>
`