forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (146 loc) · 3.89 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
// Version 1.7
// Supports Windows, Linux, Mac, and Raspberry Pi, Beagle Bone Black
package main
import (
"flag"
"go/build"
"log"
"net/http"
//"path/filepath"
"text/template"
)
var (
version = "1.74"
versionFloat = float32(1.74)
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
)
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 main() {
// setup logging
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
//getList()
flag.Parse()
f := flag.Lookup("addr")
log.Println("Version:" + version)
log.Print("Started server and websocket on localhost" + f.Value.String())
//homeTempl = template.Must(template.ParseFiles(filepath.Join(*assets, "home.html")))
if !*verbose {
log.Println("Verbose mode not specified, stopping all further logging")
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()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/ws", wsHandler)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("Error ListenAndServe:", err)
}
}
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>
`