-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (190 loc) · 5.44 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"log"
"os"
"time"
"github.com/nikoksr/konfetty"
"github.com/nikoksr/konfetty/examples"
)
type SmartHomeConfig struct {
General GeneralConfig
Rooms []RoomConfig
Routines []RoutineConfig
}
type GeneralConfig struct {
HomeName string
OwnerName string
TimeZone string
EnergyMode string
MaintenanceInterval time.Duration
}
type BaseDevice struct {
Name string
Type string
Enabled bool
Location string
}
type LightDevice struct {
BaseDevice
Brightness int
ColorTemp int
}
type ThermostatDevice struct {
BaseDevice
TargetTemp float64
Mode string
}
type RoomConfig struct {
Name string
Devices []any // Can be LightDevice or ThermostatDevice
}
type RoutineConfig struct {
Name string
StartTime string
Days []string
Actions []Action
}
type Action struct {
DeviceName string
Command string
Value any
}
func main() {
// Initial config (simulating loaded from file)
cfg := &SmartHomeConfig{
General: GeneralConfig{
HomeName: "My Smart Home",
OwnerName: "John Doe",
},
Rooms: []RoomConfig{
{
Name: "Living Room",
Devices: []any{
LightDevice{
BaseDevice: BaseDevice{Name: "Main Light", Type: "light"},
Brightness: 80,
},
ThermostatDevice{
BaseDevice: BaseDevice{Name: "AC", Type: "thermostat"},
},
},
},
},
Routines: []RoutineConfig{
{
Name: "Morning Routine",
StartTime: "07:00",
Days: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"},
Actions: []Action{
{DeviceName: "Main Light", Command: "setBrightness", Value: 100},
},
},
},
}
// Use konfetty to process the config
cfg, err := konfetty.FromStruct(cfg).
WithDefaults(
// Default for all BaseDevice instances
BaseDevice{
Enabled: false,
Location: "Unknown",
},
// Specific defaults for LightDevice instances
LightDevice{
BaseDevice: BaseDevice{
Type: "light", // Overwrite the default for BaseDevice.Type for LightDevice instances
},
Brightness: 50,
ColorTemp: 3000,
},
// Specific defaults for ThermostatDevice instances
ThermostatDevice{
BaseDevice: BaseDevice{
Enabled: true, // All ThermostatDevice instances are enabled by default
Type: "thermostat", // Overwrite the default for BaseDevice.Type for ThermostatDevice instances
},
TargetTemp: 22.0,
Mode: "auto",
},
// General config defaults
GeneralConfig{
TimeZone: "UTC",
EnergyMode: "balanced",
MaintenanceInterval: 30 * 24 * time.Hour, // 30 days
},
).
WithTransformer(func(c *SmartHomeConfig) {
// Example transformation: Set all lights to 20% brightness if EnergyMode is "saving"
if c.General.EnergyMode != "saving" {
return
}
for i, room := range c.Rooms {
for j, device := range room.Devices {
if light, ok := device.(LightDevice); ok {
light.Brightness = 20
c.Rooms[i].Devices[j] = light
}
}
}
}).
Build()
if err != nil {
log.Fatalf("Error building config: %v", err)
}
// Use the final config as needed...
examples.PrettyPrint(os.Stdout, cfg)
// The processed config would look like this:
//
// {
// "General": {
// "HomeName": "My Smart Home", // Kept original value
// "OwnerName": "John Doe", // Kept original value
// "TimeZone": "UTC", // Applied from defaults
// "EnergyMode": "balanced", // Applied from defaults
// "MaintenanceInterval": 2592000000000000 // Applied from defaults (30 days in nanoseconds)
// },
// "Rooms": [
// {
// "Name": "Living Room", // Kept original value
// "Devices": [
// {
// "Name": "Main Light", // Kept original value
// "Type": "light", // Applied from LightDevice default
// "Enabled": false, // Applied from BaseDevice default
// "Location": "Unknown", // Applied from BaseDevice default
// "Brightness": 80, // Kept original value
// "ColorTemp": 3000 // Applied from LightDevice default
// },
// {
// "Name": "AC", // Kept original value
// "Type": "thermostat", // Applied from ThermostatDevice default
// "Enabled": true, // Applied from ThermostatDevice default
// "Location": "Unknown", // Applied from BaseDevice default
// "TargetTemp": 22, // Applied from ThermostatDevice default
// "Mode": "auto" // Applied from ThermostatDevice default
// }
// ]
// }
// ],
// "Routines": [
// {
// "Name": "Morning Routine", // Kept original value
// "StartTime": "07:00", // Kept original value
// "Days": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ], // Kept original values
// "Actions": [
// {
// "DeviceName": "Main Light", // Kept original value
// "Command": "setBrightness", // Kept original value
// "Value": 100 // Kept original value
// }
// ]
// }
// ]
// }
}