summaryrefslogtreecommitdiff
path: root/src/main.go
blob: af82e8faded16bf328801ae9754e6e5605b647f7 (plain)
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
// vim: shiftwidth=4 tabstop=4 noexpandtab

package main

import (
	"flag"
	"log"
	"os"
	"io/ioutil"
	"encoding/json"
)

var (
	camera Machine
)

type config struct {
	WebConfig webConfig `json:"webserver"`
}

func main() {

	// read command line arguments
	configPath := readFlags()

	// set up log and print startup message
	log.SetFlags(0)  // disable timestamp because systemd takes care of that
	log.Println("Starting birdscan")

	// read config file
	cfg := readConfig(configPath)

	// setup camera state machine
	camera = newCamStateMachine()

	// start goroutines
	server := NewWebServer(&cfg.WebConfig)
	go server.run()  // http server

	// run camera state machine
	camera.run()
}

func readFlags() string {
	var retval string
	flag.StringVar(&retval, "c", "/etc/birdscan/config.json", "Path to birdscan configuration file")
	flag.Parse()
	return retval
}

func readConfig(path string) config {

	log.Printf("Reading config file %s", path)
	var retval config

	// open the config file
	configFile, err := os.Open(path)
	defer configFile.Close()
	if err != nil {
		log.Fatalf("Could not open configuration file %s", path)
	}

	// read byte content
	byteData, err := ioutil.ReadAll(configFile)
	if err != nil {
		log.Fatalf("Could not read configuration file %s", path)
	}

	// parse content to config structs
	err = json.Unmarshal(byteData, &retval)
	if err != nil {
		log.Fatalf("Could not parse configuration file %s", path)
	}

	return retval
}