forked from pier-oliviert/irrigation
/
irrigation.go
133 lines (108 loc) · 2.72 KB
/
irrigation.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
package main
import (
"errors"
"flag"
"github.com/globocom/config"
"github.com/gorilla/pat"
"github.com/pothibo/irrigation/db"
"github.com/pothibo/irrigation/gpio"
"github.com/pothibo/irrigation/models"
"github.com/pothibo/irrigation/scheduler"
"log"
"net/http"
"os"
"strconv"
)
var configFile string
func init() {
flag.Bool("server", true, "Start the server with port: 7777")
flag.Bool("initdb", true, "Initialize the database.")
flag.Bool("activate", true, "Activate the relays.")
flag.Bool("help", true, "Show this help menu.")
flag.StringVar(&configFile, "c", "/srv/http/irrigation/config.yml", "Configuration file")
}
func main() {
config.ReadConfigFile(configFile)
path, err := config.GetString("database")
if err != nil {
log.Panicln("No database file specified in config file.")
os.Exit(1)
}
db.Init(path)
models.RegisterEntry()
models.RegisterValve()
models.RegisterSchedule()
flag.Parse()
flag.Visit(actionFlag)
flag.PrintDefaults()
}
func actionFlag(flag *flag.Flag) {
switch {
case flag.Name == "server":
err := launchServer(flag)
if err != nil {
log.Panicln(err)
os.Exit(1)
}
case flag.Name == "initdb":
err := db.Create()
if err != nil {
log.Panicln(err)
}
os.Exit(1)
case flag.Name == "activate":
err := activateRelay()
if err != nil {
log.Fatalln(err)
}
os.Exit(1)
}
}
func launchServer(flag *flag.Flag) error {
path, err := config.GetString("assets")
if err != nil {
return errors.New("No assets folder specified in config file.")
}
scheduler.Run()
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(path))))
r := pat.New()
r.Post("/schedules/{scheduleId}", updateSchedule)
r.Get("/schedules/{scheduleId}/edit", editSchedule)
r.Get("/schedules/new", newSchedule)
r.Post("/schedules", createSchedule)
r.Get("/valves/{valveId}/edit", editValve)
r.Get("/valves/{valveId}/open", openValve)
r.Get("/valves/{valveId}/close", closeValve)
r.Post("/valves/{valveId}", updateValve)
r.Get("/valves/{valveId}", showValve)
r.Get("/manual", manual)
r.Get("/", homepage)
http.Handle("/", r)
initializeTemplates(path)
err = http.ListenAndServe(":7777", nil)
return err
}
func activateRelay() error {
for _, valve := range Valves() {
err := gpio.Activate(valve.RelayId)
if err != nil {
return err
}
}
return nil
}
func Valves() map[int]*models.Valve {
relays := make(map[int]*models.Valve)
valves, err := config.GetList("valves")
if err != nil {
log.Fatalf("Could not load the valves id: %v", err)
}
for _, value := range valves {
valve, err := strconv.Atoi(value)
if err != nil {
log.Printf("Valve %s could not be configured. Ignoring")
}
relays[valve] = models.FirstValveOrCreate(valve)
}
return relays
}