222 lines
5.9 KiB
Go
222 lines
5.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
var config = readConfig("")
|
|
|
|
// our main function
|
|
func main() {
|
|
|
|
// Ausgeben der Config-Optionen
|
|
fmt.Println("Host: " + config.Host)
|
|
fmt.Println("Post: " + config.Port)
|
|
fmt.Println("Template: " + config.Template)
|
|
fmt.Println("MQTTServer: " + config.MQTTServer)
|
|
fmt.Println("MQTTPort: " + strconv.Itoa(config.MQTTPort))
|
|
fmt.Println("OS: " + config.OS)
|
|
|
|
router := mux.NewRouter()
|
|
//router.HandleFunc("/_api/md/{pagename:.*}", getRawPage).Methods("GET")
|
|
router.HandleFunc("/_api/config", getConfig).Methods("GET")
|
|
router.HandleFunc("/_api/aufnahme", getAufnahme).Methods("GET")
|
|
router.HandleFunc("/_api/aufnahme", postAufnahme).Methods("POST")
|
|
router.HandleFunc("/_api/kamerabild", getKamerabild).Methods("GET")
|
|
router.HandleFunc("/_api/fotoliste", getFotoliste).Methods("GET")
|
|
router.PathPrefix("/pic/").Handler(http.StripPrefix("/pic/", http.FileServer(http.Dir("pic")))).Methods("GET")
|
|
router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET")
|
|
|
|
log.Fatal(http.ListenAndServe(":"+config.Port, router))
|
|
|
|
}
|
|
|
|
func getConfig(w http.ResponseWriter, r *http.Request) {
|
|
json.NewEncoder(w).Encode(config)
|
|
}
|
|
|
|
func getAufnahme(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// raspistill -t 99999999 -p '100,140,800,600'
|
|
if config.OS == "linux" {
|
|
cmd := exec.Command("killall", "raspistill")
|
|
err := cmd.Run()
|
|
check(err)
|
|
|
|
//cmd = exec.Command("/usr/bin/raspistill", "-t 1 -o /home/pi/fbox/pic/tmp.jpg")
|
|
//cmd = exec.Command("bash", "-c", `/usr/bin/raspistill -p '100,140,800,600' -t 1 -o /home/pi/fbox/pic/tmp.jpg`)
|
|
cmd = exec.Command("bash", "-c", `/usr/bin/raspistill -op 0 -p '100,140,800,600' -t 1 -o /home/pi/fbox/pic/tmp.jpg`)
|
|
err = cmd.Run()
|
|
check(err)
|
|
} else {
|
|
copyfile("./pic/Testbild.jpg", "./pic/tmp.jpg")
|
|
}
|
|
|
|
json.NewEncoder(w).Encode("tmp.jpg")
|
|
}
|
|
|
|
func postAufnahme(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))
|
|
currentTime := time.Now()
|
|
Ordner := currentTime.Format("2006-01-02")
|
|
if !directoryExists(Ordner) {
|
|
os.MkdirAll(path.Join("/home/pi/fbox/pic", Ordner), os.ModePerm)
|
|
}
|
|
|
|
//fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))
|
|
Foto := currentTime.Format("150405")
|
|
copyfile("./pic/tmp.jpg", path.Join("/home/pi/fbox/pic", Ordner, Foto+".jpg"))
|
|
|
|
json.NewEncoder(w).Encode(path.Join(Ordner, Foto+".jpg"))
|
|
}
|
|
|
|
func getKamerabild(w http.ResponseWriter, r *http.Request) {
|
|
if config.OS == "linux" {
|
|
//cmd := exec.Command("/usr/bin/raspistill", "-t 99999999 -p '100,140,800,600'")
|
|
cmd := exec.Command("bash", "-c", `/usr/bin/raspistill -t 99999999 -p '100,140,800,600'`)
|
|
err := cmd.Run()
|
|
check(err)
|
|
}
|
|
|
|
json.NewEncoder(w).Encode("OK")
|
|
}
|
|
|
|
func getFotoliste(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//Livestream der Kamera beenden
|
|
if config.OS == "linux" {
|
|
cmd := exec.Command("killall", "raspistill")
|
|
err := cmd.Run()
|
|
check(err)
|
|
}
|
|
|
|
// Datum der config auslesen
|
|
f, err := os.Open("./config.json")
|
|
check(err)
|
|
statinfo, err := f.Stat()
|
|
check(err)
|
|
ConfigTime := statinfo.ModTime()
|
|
//fmt.Println(ConfigTime)
|
|
|
|
// Dateien herausfinden die neuer als das Config-Datm sind
|
|
//picfiles, err := ioutil.ReadDir("./pic")
|
|
//check(err)
|
|
var files []string
|
|
|
|
filepath.Walk("./pic", func(filepath string, file os.FileInfo, err error) error {
|
|
tmpPath := strings.Replace(filepath, "\\", "/", -1)
|
|
|
|
if file.Mode().IsRegular() {
|
|
//if file.ModTime() > ConfigTime {
|
|
//fmt.Print(file.Name() + ": ")
|
|
//fmt.Println(file.ModTime())
|
|
if file.Name() != "Testbild.jpg" && file.Name() != "TestbildKamera.jpg" && file.Name() != "tmp.jpg" {
|
|
if inTimeSpan(ConfigTime, time.Now(), file.ModTime()) {
|
|
files = append(files, tmpPath)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
/*
|
|
for _, file := range picfiles {
|
|
if file.Mode().IsRegular() {
|
|
//if file.ModTime() > ConfigTime {
|
|
fmt.Print(file.Name() + ": ")
|
|
fmt.Println(file.ModTime())
|
|
if inTimeSpan(ConfigTime, time.Now(), file.ModTime()) {
|
|
files = append(files, file.Name())
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
json.NewEncoder(w).Encode(files)
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
// Typen
|
|
//--------------------------------------------------------------------------
|
|
type Configuration struct {
|
|
Host string
|
|
Port string
|
|
Template string
|
|
MQTTServer string
|
|
MQTTPort int
|
|
OS string
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
// Hilfsfunktionen
|
|
//--------------------------------------------------------------------------
|
|
func check(e error) {
|
|
if e != nil {
|
|
fmt.Println(e)
|
|
}
|
|
}
|
|
|
|
func fileExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|
|
|
|
func directoryExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return info.IsDir()
|
|
}
|
|
|
|
func copyfile(source string, destination string) {
|
|
input, err := ioutil.ReadFile(source)
|
|
check(err)
|
|
err = ioutil.WriteFile(destination, input, 0644)
|
|
check(err)
|
|
}
|
|
|
|
func readConfig(filename string) *Configuration {
|
|
// initialize conf with default values.
|
|
conf := &Configuration{
|
|
Host: "http://127.0.0.1",
|
|
Port: "8000",
|
|
Template: "default",
|
|
MQTTServer: "127.0.0.1",
|
|
MQTTPort: 8080,
|
|
OS: runtime.GOOS,
|
|
}
|
|
|
|
b, err := ioutil.ReadFile("./config.json")
|
|
if err != nil {
|
|
return conf
|
|
}
|
|
if err = json.Unmarshal(b, conf); err != nil {
|
|
return conf
|
|
}
|
|
return conf
|
|
}
|
|
|
|
func inTimeSpan(start, end, check time.Time) bool {
|
|
return check.After(start) && check.Before(end)
|
|
}
|