-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
122 lines (102 loc) · 3.18 KB
/
server.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
package main
import (
"./mbus"
"bytes"
"code.google.com/p/go.net/websocket"
"encoding/json"
"flag"
"html/template"
"io"
"log"
"net/http"
"runtime"
"time"
)
var sniffingTTY = flag.String("snifferTTY", "/dev/ttyUSB0", "sniffing device")
var sendingTTY = flag.String("senderTTY", "/dev/ttyUSB1", "sender device")
var DBUser = flag.String("DBUser", "root", "DB username")
var DBPass = flag.String("DBPass", "root", "DB password")
var DBName = flag.String("DBName", "capturedFrames", "DB name")
var DemoMode = flag.Bool("DemoMode", false, "Insert the sent data directly into the DB in case the sender is not working properly")
// Initialize the application
func main() {
runtime.GOMAXPROCS(4)
// Read config flags
flag.Parse()
// Initialize the database
setupDB()
// Initialize the sending device
initializeSender()
// Start the data reading from the serial device
go readData()
// Start the MUC webservice
go mucService()
// Start the sniffer webservice
snifferService()
}
// Starts the MUC service on port 8080
func mucService() {
http.HandleFunc("/webui", mucLogUIHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
// Starts the sniffer webservice on port 80
func snifferService() {
http.HandleFunc("/", mainHandler)
http.HandleFunc("/send", sendFrameHandler)
http.HandleFunc("/export", exportDump)
http.HandleFunc("/import", importDump)
http.HandleFunc("/truncate", truncateSQL)
http.HandleFunc("/sendPartiallyHandler", sendPartiallyHandler)
http.HandleFunc("/statusSniffer", statusSnifferHandler)
http.Handle("/socket", websocket.Handler(socketHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("./templates"))))
if err := http.ListenAndServe(":80", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
var rootTemplate = template.Must(template.ParseFiles("templates/layout.html"))
rootTemplate.Execute(w, snifferActive)
}
// Our socket handler for new sniffed data
// TODO: add a last ID
func socketHandler(c *websocket.Conn) {
lastID := 0
frames, ID := getNewFrames(lastID)
json.NewEncoder(c).Encode(frames)
lastID = ID
for {
frames, ID := getNewFrames(lastID)
if len(frames) > 0 {
json.NewEncoder(c).Encode(frames)
lastID = ID
}
}
}
// Export a dump as JSON
func exportDump(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename=\"dump-"+time.Now().String()+".json\"")
frames, _ := getNewFrames(0)
json.NewEncoder(w).Encode(frames)
}
// Import a dump
func importDump(w http.ResponseWriter, r *http.Request) {
importedData, _, _ := r.FormFile("import")
defer importedData.Close()
var buf bytes.Buffer
io.Copy(&buf, importedData)
var frame []mbus.Frame
json.Unmarshal(buf.Bytes(), &frame)
for i := range frame {
addNewFrame(frame[i].Value, frame[i].Time)
}
w.Header().Set("Refresh", "0; url=http://"+r.Host)
}
// Truncate SQL
func truncateSQL(w http.ResponseWriter, r *http.Request) {
truncateTable()
w.Header().Set("Refresh", "0; url=http://"+r.Host)
}