Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/mwuertinger/hal/pkg/config"
"github.com/mwuertinger/hal/pkg/device"
"github.com/mwuertinger/hal/pkg/frontend"
"github.com/mwuertinger/hal/pkg/mqtt"
"github.com/mwuertinger/hal/pkg/persistence"
"github.com/mwuertinger/hal/pkg/timer"
)

func main() {
Expand All @@ -35,6 +37,11 @@ func main() {
log.Fatalf("persistence.Start: %v", err)
}

timerSvc := timer.NewService()
if err := timerSvc.Start(); err != nil {
log.Fatalf("timerSvc.Start: %v", err)
}

mqttBroker := mqtt.New()
if err := mqttBroker.Connect(c.Mqtt); err != nil {
log.Fatalf("mqttBroker.Connect: %v", err)
Expand All @@ -49,6 +56,17 @@ func main() {
log.Fatalf("frontend.Start: %v", err)
}

// TODO remove
var switches []device.Switch
for _, dev := range device.List() {
switches = append(switches, dev.(device.Switch))
}
timerSvc.AddJob(timer.Job{
Timestamp: time.Date(2018, 10, 26, 5, 0, 0, 0, time.UTC),
Status: true,
Switches: switches,
})

log.Println("Server ready")

// Wait for receiving a signal.
Expand Down
73 changes: 73 additions & 0 deletions pkg/timer/timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package timer

import (
"errors"
"github.com/mwuertinger/hal/pkg/device"
"log"
"math/rand"
"sync"
"time"
)

type Service interface {
Start() error
AddJob(job Job) (uint64, error)
}

type Job struct {
ID uint64
Timestamp time.Time // execution time
Switches []device.Switch // list of switches
Status bool // target status
}

type service struct {
mu sync.Mutex // protects everything below
initialized bool
jobs map[uint64]Job
}

func NewService() Service {
return &service{jobs: map[uint64]Job{}}
}

func (s *service) Start() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.initialized {
return errors.New("already initialized")
}

go func() {
for now := range time.Tick(time.Minute) {
s.mu.Lock()
log.Print("Timer.jobs: ", s.jobs)
for id, job := range s.jobs {
if job.Timestamp.Before(now) {
log.Print("Timer: ", job)
for _, sw := range job.Switches {
sw.Switch(job.Status)
}
delete(s.jobs, id)
}
}
s.mu.Unlock()
}
}()
return nil
}

func (s *service) AddJob(job Job) (uint64, error) {
job.ID = rand.Uint64()

// defensive copying
switches := make([]device.Switch, len(job.Switches))
copy(switches, job.Switches)
job.Switches = switches

s.mu.Lock()
defer s.mu.Unlock()
s.jobs[job.ID] = job

return job.ID, nil
}