-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (123 loc) · 3.1 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
package main
import (
"encoding/json"
"flag"
"io"
"io/ioutil"
"net"
"net/http"
"sync/atomic"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket"
"github.com/julienschmidt/httprouter"
"github.com/thinxer/semikami"
"golang.org/x/net/context"
)
var (
flagAPI = flag.String("api", ":8080", "API Binding Address")
)
func bigEndian(hi, lo byte) int {
return (int(hi) << 8) + int(lo)
}
func CORS(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
w.Header().Add("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return nil
}
return ctx
}
func WriteJSON(w http.ResponseWriter, v interface{}) error {
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(v)
}
type M map[string]interface{}
var (
currentPM25 int32
currentPM100 int32
)
func WSHandler(c *websocket.Conn) {
closed := false
ret := websocket.CloseError{
Code: websocket.CloseNormalClosure,
}
const writeWait = 10 * time.Second
defer func() {
log.Infof("Close websocket [%s]", ret.Error())
c.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(ret.Code, ret.Text), time.Now().Add(writeWait))
c.Close()
}()
go func() {
for {
_, r, err := c.NextReader()
if err != nil {
break
}
io.Copy(ioutil.Discard, r)
}
log.Infof("Pipe closed")
closed = true
}()
for !closed {
signal := M{
"pm2.5": atomic.LoadInt32(¤tPM25),
"pm10": atomic.LoadInt32(¤tPM100),
}
if err := c.WriteJSON(signal); err != nil && err != websocket.ErrCloseSent {
if _, ok := err.(net.Error); !ok {
log.Warnf("Error writing to client: %s", err.Error())
}
closed = true
}
time.Sleep(2 * time.Second)
}
}
func main() {
flag.Parse()
go func() {
for {
err := SerialWaitSignal()
if err != nil {
log.Error(err)
continue
}
data, err := SerialReadFrame()
if err != nil {
log.Error(err)
continue
}
pm10 := bigEndian(data[0], data[1])
pm25 := bigEndian(data[2], data[3])
pm100 := bigEndian(data[4], data[5])
log.Infof("PM1.0: %d, PM2.5: %d, PM10: %d", pm10, pm25, pm100)
atomic.StoreInt32(¤tPM25, int32(pm25))
atomic.StoreInt32(¤tPM100, int32(pm100))
}
}()
router := httprouter.New()
k := kami.New(nil, router).With(CORS)
k.Get("/realtime", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
upg := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// accept any origin
return true
},
}
ws, err := upg.Upgrade(w, r, nil)
if err != nil {
log.Warnf("Error to upgrade: %s", err.Error())
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
go WSHandler(ws)
})
k.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/viewer.html", http.StatusMovedPermanently)
})
http.Handle("/", k.Handler())
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
log.Infof("Listen at %s", *flagAPI)
panic(http.ListenAndServe(*flagAPI, http.DefaultServeMux))
}