-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
162 lines (144 loc) · 3.73 KB
/
handler.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
package handler
import (
"encoding/json"
"errors"
"time"
gmg "github.com/brandenc40/green-mountain-grill"
"go.uber.org/fx"
repo "github.com/brandenc40/green-mountain-grill/server/respository"
"github.com/brandenc40/green-mountain-grill/server/respository/mapper"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
)
var Module = fx.Provide(New)
const (
contentTypeJSON = "application/json"
)
// Handler -
type Handler struct {
grill gmg.Client
logger *logrus.Logger
repo repo.Repository
currentSessionUUID uuid.UUID
}
// Params -
type Params struct {
fx.In
Logger *logrus.Logger
GrillClient gmg.Client
Repository repo.Repository
}
// New -
func New(p Params) *Handler {
h := &Handler{
grill: p.GrillClient,
logger: p.Logger,
repo: p.Repository,
currentSessionUUID: uuid.Nil,
}
return h
}
// GetGrillState -
func (c *Handler) GetGrillState(ctx *fasthttp.RequestCtx) {
state, err := c.grill.GetState()
if err != nil {
if _, ok := err.(gmg.GrillUnreachableErr); ok {
ctx.Error(err.Error(), fasthttp.StatusServiceUnavailable)
return
}
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
marshalled, err := json.Marshal(state)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
ctx.SetContentType(contentTypeJSON)
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBody(marshalled)
}
// GetGrillID -
func (c *Handler) GetGrillID(ctx *fasthttp.RequestCtx) {
id, err := c.grill.GetID()
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
ctx.SetBody(id)
ctx.SetStatusCode(fasthttp.StatusOK)
}
// GetGrillFirmware -
func (c *Handler) GetGrillFirmware(ctx *fasthttp.RequestCtx) {
id, err := c.grill.GetFirmware()
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
ctx.SetBody(id)
ctx.SetStatusCode(fasthttp.StatusOK)
}
// NewSession -
func (c *Handler) NewSession(ctx *fasthttp.RequestCtx) {
if !c.grill.IsAvailable() {
ctx.Error("grill is not available", fasthttp.StatusServiceUnavailable)
return
}
u, err := uuid.NewRandom()
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
c.currentSessionUUID = u
c.monitorGrillAsync()
ctx.SetBodyString(c.currentSessionUUID.String())
ctx.SetStatusCode(fasthttp.StatusOK)
}
func (c *Handler) GetSessionData(ctx *fasthttp.RequestCtx) {
hist, err := c.repo.GetStateHistory(c.currentSessionUUID)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
marshalled, err := json.Marshal(hist)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
ctx.SetBody(marshalled)
ctx.SetStatusCode(fasthttp.StatusOK)
}
// monitorGrillAsync - start monitoring and report errors asynchronously
func (c *Handler) monitorGrillAsync() {
go func() {
if err := c.monitorGrill(); err != nil {
c.logger.WithError(err).Error("error monitoring grill")
}
}()
}
// monitorGrill - check grill state and store to database once every minute
func (c *Handler) monitorGrill() error {
for true {
if _, err := c.storeGrillState(); err != nil {
return err
}
time.Sleep(time.Minute)
}
return nil
}
// storeGrillState - get current state and store to db
func (c *Handler) storeGrillState() (*gmg.State, error) {
if c.currentSessionUUID == uuid.Nil {
return nil, errors.New("no grill session UUID has been set")
}
state, err := c.grill.GetState()
if err != nil {
return nil, err
}
stateModel := mapper.GrillStateEntityToModel(state, c.currentSessionUUID)
if err := c.repo.InsertStateData(stateModel); err != nil {
return nil, err
}
return state, nil
}