-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub.go
193 lines (165 loc) · 4.04 KB
/
hub.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package hub
import (
"embed"
"encoding/json"
"fmt"
"html/template"
"net/url"
"os"
"github.com/merliot/dean"
"github.com/merliot/device"
"github.com/merliot/device/models"
)
//go:embed css go.mod images js template
var fs embed.FS
type Child struct {
Model string
Name string
DeployParams string `json:"DeployParams,omitempty"`
Online bool `json:"-"`
device.Devicer `json:"-"`
}
type Children map[string]*Child // keyed by id
type Hub struct {
*device.Device
Version string
Demo bool
models.Models `json:"-"`
Children
server *dean.Server
templates *template.Template
}
var targets = []string{"x86-64", "rpi"}
func New(id, model, name string) dean.Thinger {
fmt.Println("NEW HUB\r")
h := &Hub{}
h.Device = device.New(id, model, name, fs, targets).(*device.Device)
h.Version = version
h.Models = make(models.Models)
h.Children = make(Children)
h.CompositeFs.AddFS(fs)
h.templates = h.CompositeFs.ParseFS("template/*")
return h
}
func (h *Hub) SetServer(server *dean.Server) {
h.server = server
}
func (h *Hub) SetBackup(backup string) {
if backup == "" {
return
}
u, err := url.Parse(backup)
if err != nil {
fmt.Println(backup, "is not a valid URL:", err)
return
}
ws := "ws://"
if u.Scheme == "https" {
ws = "wss://"
}
dialURL := ws + u.Host + "/ws/?ping-period=4"
h.SetDialURLs(dialURL)
}
func (h *Hub) SetDemo(demo bool) {
h.Locked = (demo == true)
h.Demo = demo
}
func (h *Hub) RegisterModel(model string, maker dean.ThingMaker) {
if h.server != nil {
h.server.RegisterModel(model, maker)
}
h.Models[model] = models.New(model, maker)
}
func (h *Hub) GenerateUf2s(dir string) error {
for _, model := range h.Models {
if err := model.Modeler.GenerateUf2s(dir); err != nil {
return err
}
}
return nil
}
func (h *Hub) getState(msg *dean.Msg) {
h.Path = "state"
msg.Marshal(h).Reply()
}
func (h *Hub) online(msg *dean.Msg, online bool) {
var thing dean.ThingMsgConnect
msg.Unmarshal(&thing)
if child, ok := h.Children[thing.Id]; ok {
child.Online = online
msg.Broadcast()
}
}
func (h *Hub) connect(online bool) func(*dean.Msg) {
return func(msg *dean.Msg) {
h.online(msg, online)
}
}
func (h *Hub) createdThing(msg *dean.Msg) {
var child dean.ThingMsgCreated
msg.Unmarshal(&child)
h.Children[child.Id] = &Child{Model: child.Model, Name: child.Name}
child.Path = "created/device"
msg.Marshal(&child).Broadcast()
}
func (h *Hub) deletedThing(msg *dean.Msg) {
var child dean.ThingMsgDeleted
msg.Unmarshal(&child)
delete(h.Children, child.Id)
child.Path = "deleted/device"
msg.Marshal(&child).Broadcast()
}
func (h *Hub) restart(msg *dean.Msg) {
fmt.Println("RESTART")
os.Exit(0)
}
func (h *Hub) Subscribers() dean.Subscribers {
return dean.Subscribers{
"get/state": h.getState,
"connected": h.connect(true),
"disconnected": h.connect(false),
"created/thing": h.createdThing,
"deleted/thing": h.deletedThing,
"restart": h.restart,
}
}
func (h *Hub) loadDevice(thinger dean.Thinger, id, deployParams string) {
device := thinger.(device.Devicer)
device.SetDeployParams(deployParams)
device.CopyWifiAuth(h.WifiAuth)
device.SetWsScheme(h.WsScheme)
device.SetDialURLs(h.DialURLs)
device.SetLocked(h.Locked)
h.Children[id].Devicer = device
if h.Demo {
thinger.Setup()
thinger.SetFlag(dean.ThingFlagMetal)
thinger.SetOnline(true)
injector := h.server.NewInjector(id)
go thinger.Run(injector)
}
}
func (h *Hub) LoadDevices(devices string) {
var children Children
// If devices is empty, try loading from devices.json file
if devices == "" {
data, err := os.ReadFile("devices.json")
if err != nil {
return
}
devices = string(data)
}
err := json.Unmarshal([]byte(devices), &children)
if err != nil {
fmt.Printf("Error parsing devices: %s\n", err)
return
}
for id, child := range children {
thinger, err := h.server.CreateThing(id, child.Model, child.Name)
if err != nil {
fmt.Printf("Skipping: error creating device Id '%s': %s\n", id, err)
continue
}
h.loadDevice(thinger, id, child.DeployParams)
}
}