61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package camera
|
|
|
|
import (
|
|
"axis_timelapse/config"
|
|
"crypto/tls"
|
|
"image"
|
|
_ "image/jpeg" // Register JPEG decoder
|
|
"image/png"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func CaptureImage() bool {
|
|
var cert_type = config.CameraTLSSelfSigned
|
|
tr := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: cert_type},
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
resp, err := client.Get(config.FullURL)
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msgf("")
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
log.Error().Msg("Unexpected status code: " + strconv.Itoa(resp.StatusCode))
|
|
return false
|
|
}
|
|
|
|
if resp.Body != nil {
|
|
timestamp := time.Now()
|
|
filename := timestamp.Format("/axis-2006-01-02_15-04-05.png")
|
|
|
|
img, _, err := image.Decode(resp.Body)
|
|
if err != nil {
|
|
log.Error().Msgf("Error decoding image.")
|
|
}
|
|
|
|
outFile, err := os.Create(config.StoragePath + filename)
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msgf("")
|
|
}
|
|
defer outFile.Close()
|
|
|
|
err = png.Encode(outFile, img)
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msgf("")
|
|
} else {
|
|
log.Info().Msg("New image created: " + config.StoragePath + filename)
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|