This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
/
blcu.go
72 lines (59 loc) · 1.69 KB
/
blcu.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
package blcu
import (
"net"
"github.com/HyperloopUPV-H8/h9-backend/internal/vehicle/models"
wsModels "github.com/HyperloopUPV-H8/h9-backend/internal/ws_handle/models"
"github.com/rs/zerolog"
trace "github.com/rs/zerolog/log"
)
type BLCU struct {
addr net.TCPAddr
boardToId map[string]uint16
ackChannel chan struct{}
sendOrder func(models.Order) error
config BLCUConfig
trace zerolog.Logger
}
func NewBLCU(laddr net.TCPAddr, boardIds map[string]uint16, config BLCUConfig) BLCU {
trace.Info().Str("addr", laddr.String()).Msg("New BLCU")
return BLCU{
addr: laddr,
boardToId: boardIds,
ackChannel: make(chan struct{}, BLCU_ACK_CHAN_BUF),
trace: trace.With().Str("component", BLCU_COMPONENT_NAME).Logger(),
config: config,
sendOrder: func(o models.Order) error { return nil },
}
}
func (blcu *BLCU) HandlerName() string {
return BLCU_HANDLER_NAME
}
func (blcu *BLCU) SetSendOrder(sendOrder func(o models.Order) error) {
blcu.sendOrder = sendOrder
}
func (blcu *BLCU) UpdateMessage(client wsModels.Client, msg wsModels.Message) {
blcu.trace.Debug().Str("topic", msg.Topic).Str("client", client.Id()).Msg("Update message")
switch msg.Topic {
case blcu.config.Topics.Upload:
err := blcu.upload(client, msg.Payload)
if err != nil {
blcu.notifyUploadFailure(client)
} else {
blcu.notifyUploadSuccess(client)
}
case blcu.config.Topics.Download:
board, data, err := blcu.download(client, msg.Payload)
if err != nil {
blcu.notifyDownloadFailure(client)
} else {
blcu.notifyDownloadSuccess(client, data)
blcu.writeDownloadFile(board, data)
}
}
}
func (blcu *BLCU) NotifyAck() {
select {
case blcu.ackChannel <- struct{}{}:
default:
}
}