Skip to content

Commit

Permalink
set device status on cron wake/shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousm4x committed Jan 29, 2023
1 parent 969d154 commit 1f433dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion backend/cronjobs/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func RunPing(app *pocketbase.PocketBase) {
CronPing.Run()
}

func RunWakeShutdown() {
func RunWakeShutdown(app *pocketbase.PocketBase) {
CronWakeShutdown = cron.New()
for _, device := range Devices {
wake_cron := device.GetString("wake_cron")
Expand All @@ -85,17 +85,29 @@ func RunWakeShutdown() {

if wake_cron_enabled && wake_cron != "" {
CronWakeShutdown.AddFunc(wake_cron, func() {
device.Set("status", "pending")
app.Dao().SaveRecord(device)
if err := networking.WakeDevice(device); err != nil {
logger.Error.Println(err)
device.Set("status", "offline")
app.Dao().SaveRecord(device)
}
device.Set("status", "online")
app.Dao().SaveRecord(device)
})
}

if shutdown_cron_enabled && shutdown_cron != "" {
CronWakeShutdown.AddFunc(shutdown_cron, func() {
device.Set("status", "pending")
app.Dao().SaveRecord(device)
if err := networking.ShutdownDevice(device); err != nil {
logger.Error.Println(err)
device.Set("status", "online")
app.Dao().SaveRecord(device)
}
device.Set("status", "offline")
app.Dao().SaveRecord(device)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions backend/pb/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func StartPocketBase(distDirFS fs.FS) {

// run cronjobs
go cronjobs.RunPing(App)
go cronjobs.RunWakeShutdown()
go cronjobs.RunWakeShutdown(App)

// restart ping cronjobs or wake/shutdown cronjobs on model update
// add event hook before starting server.
Expand All @@ -94,7 +94,7 @@ func StartPocketBase(distDirFS fs.FS) {
for _, job := range cronjobs.CronWakeShutdown.Entries() {
cronjobs.CronWakeShutdown.Remove(job.ID)
}
go cronjobs.RunWakeShutdown()
go cronjobs.RunWakeShutdown(App)
}
return nil
})
Expand Down

0 comments on commit 1f433dd

Please sign in to comment.