-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
325 lines (254 loc) · 7.39 KB
/
data.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"errors"
"fmt"
"log"
"os"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type ResetFrequency string
const (
Minutes ResetFrequency = "minutes"
Hourly ResetFrequency = "hourly"
Daily ResetFrequency = "daily"
Weekly ResetFrequency = "weekly"
Monthly ResetFrequency = "monthly"
// Add more frequencies as needed
)
type Habit struct {
ID uint `gorm:"primarykey"`
Name string `gorm:"size:255"`
ResetFrequency ResetFrequency `gorm:"type:varchar(10)"`
ResetValue int `gorm:"type:int"`
Group string `gorm:"size:255"`
IsActive bool `gorm:"type:boolean"`
LastComplete time.Time `gorm:"type:datetime"`
NeedsCompletion bool `gorm:"type:boolean"`
StartHour int `gorm:"type:int"` // Value between 0-23
StartMinute int `gorm:"type:int"` // Value between 0-23
EndHour int `gorm:"type:int"`
EndMinute int `gorm:"type:int"` // Value between 0-23
}
func (h Habit) getUniqueId() string {
isDevStr := GetEnvWithDefault("IS_DEV", "false")
return fmt.Sprintf("%d-%s", h.ID, isDevStr)
}
func (h Habit) getMQTTTopic(topic string, msgType string) string {
if msgType != "state" && msgType != "config" {
log.Fatalf("only 'state' and 'config' are supported")
}
fullTopic := "homeassistant/binary_sensor/" + topic + "/" + h.Name + "/" + msgType
publishTopic := lowerAndReplaceSpaces(fullTopic)
println(publishTopic)
return publishTopic
}
func (h *Habit) getDeviceName() (name string, id string) {
var deviceName string
isDevStr := GetEnvWithDefault("IS_DEV", "false")
if isDevStr == "true" {
deviceName = h.Group + "_dev"
} else {
deviceName = h.Group
}
deviceId := lowerAndReplaceSpaces(deviceName)
return deviceName, deviceId
}
type HabitUpdates struct {
ID *uint
Name *string
ResetFrequency *ResetFrequency
ResetValue *int
Group *string
IsActive *bool
StartHour *int
StartMinute *int
EndHour *int
EndMinute *int
}
type Note struct {
ID uint `gorm:"primarykey"`
Title string `gorm:"type:varchar(1024)"`
Content string `gorm:"type:varchar(1024)"`
OnlyRelevantOnDay string `gorm:"type:varchar(255)"`
}
func (h *Note) getDeviceName() (name string, id string) {
var deviceName string
isDevStr := GetEnvWithDefault("IS_DEV", "false")
if isDevStr == "true" {
deviceName = "Notes_DEV"
} else {
deviceName = "Notes"
}
deviceId := lowerAndReplaceSpaces(deviceName)
return deviceName, deviceId
}
func (n *Note) getMQTTTopic(postfix string) string {
noteTitle := lowerAndReplaceSpaces(n.Title)
//publishTopic := strings.ToLower(thisTopic)
fullTopic := "homeassistant/sensor/notes/" + noteTitle + "/" + postfix
return fullTopic
}
type Database struct {
db *gorm.DB
}
func NewDatabase() (*Database, func(), error) {
dbpath := GetEnvWithDefault("DB_PATH", "db/habits.db")
if _, err := os.Stat("db"); os.IsNotExist(err) {
log.Print("making db dir")
if err := os.Mkdir("db", 0755); err != nil {
log.Fatalf("Failed to create directory: %v", err)
}
} else {
log.Print("db dir exists")
}
if _, err := os.Stat(dbpath); os.IsNotExist(err) {
log.Print("Database file does not exist")
}
db, err := gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
if err != nil {
return nil, nil, err
}
if err := db.AutoMigrate(&Habit{}); err != nil {
return nil, nil, err
}
if err := db.AutoMigrate(&Note{}); err != nil {
return nil, nil, err
}
closeFunc := func() {
dbSession, err := db.DB()
if err != nil {
fmt.Printf("Failed to get database session: %s", err)
return
}
if err := dbSession.Close(); err != nil {
fmt.Printf("Error closing database: %s", err)
}
}
return &Database{
db: db,
}, closeFunc, nil
}
func (d *Database) checkAndUpdateHabits() error {
log.Printf("checking")
db, closeDB, err := NewDatabase()
if err != nil {
log.Printf("Error initializing database: %s", err)
}
defer closeDB()
rows, err := db.GetAllHabits(true)
if err != nil {
log.Printf("Error fetching habits: %s", err)
}
for _, habit := range rows {
fmt.Println("Checking :" + habit.Name)
if needsCompletion(habit) {
fmt.Println("ACTION_NEEDED")
habit.NeedsCompletion = true
err := db.SetHabitNeedCompletion(habit.ID, true)
if err != nil {
log.Printf("ERROR ERROR saving check habit: %s", err)
return err
}
// Handle what to do if habit needs completion. For instance, notify the user.
} else {
// This extended update might not be needed always(just after config update)
habit.NeedsCompletion = false
err := db.SetHabitNeedCompletion(habit.ID, false)
if err != nil {
log.Printf("ERROR ERROR saving check habit: %s", err)
return err
}
fmt.Println("ALL GOOD" + habit.Name)
}
}
return nil
}
func (d *Database) CreateHabit(h *Habit) error {
return d.db.Create(h).Error
}
func (d *Database) GetAllHabits(isActive ...bool) ([]Habit, error) {
var habits []Habit
if d.db == nil {
return nil, errors.New("database is not initialized")
}
db := d.db // Define and initialize the db variable
if len(isActive) > 0 && isActive[0] {
db = db.Where("is_active = ?", true)
}
if err := db.Find(&habits).Error; err != nil {
return nil, err
}
return habits, nil
}
func (d *Database) GetHabitByID(id uint) (*Habit, error) {
fmt.Printf(`GetHabitByID`)
var habit Habit
if err := d.db.First(&habit, id).Error; err != nil {
return nil, err
}
return &habit, nil
}
func (d *Database) DeleteHabitByID(id uint) error {
return d.db.Delete(&Habit{}, id).Error
}
func (d *Database) DeleteNoteByID(id uint) error {
return d.db.Delete(&Note{}, id).Error
}
func (d *Database) CreateNote(n *Note) error {
return d.db.Create(n).Error
}
func (d *Database) getAllNotes() ([]Note, error) {
var notes []Note
if d.db == nil {
return nil, errors.New("database is not initialized")
}
if err := d.db.Find(¬es).Error; err != nil {
return nil, err
}
return notes, nil
}
func (d *Database) GetNoteByID(id uint) (*Note, error) {
if d.db == nil {
return nil, errors.New("database is not initialized")
}
var note Note
result := d.db.First(¬e, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil // or return nil, result.Error if you want to return the not found error
}
return nil, result.Error
}
return ¬e, nil
}
func (d *Database) EditHabit(id uint, updatedHabit *HabitUpdates) error {
return d.db.Model(&Habit{}).Where("id = ?", id).Updates(updatedHabit).Error
}
func (d *Database) SetHabitNeedCompletion(id uint, NeedsCompletion bool) error {
return d.db.Model(&Habit{}).Where("id = ?", id).Updates(map[string]interface{}{"NeedsCompletion": NeedsCompletion}).Error
}
func (d *Database) CompleteHabit(id uint) error {
preRow, err := d.GetHabitByID(id)
if err != nil {
panic(err)
}
preRow.LastComplete = time.Now()
return d.db.Model(&Habit{}).Where("id = ?", id).Updates(preRow).Error
}
func (d *Database) SetGroup(id uint, group string) error {
preRow, err := d.GetHabitByID(id)
if err != nil {
panic(err)
}
preRow.Group = group
return d.db.Model(&Habit{}).Where("id = ?", id).Updates(preRow).Error
}
func (d *Database) GetAllGroups() ([]string, error) {
var groupNames []string
if err := d.db.Raw("SELECT DISTINCT IFNULL(`group`, '') FROM habits").Scan(&groupNames).Error; err != nil {
return nil, err
}
return groupNames, nil
}