MVP Created
This commit is contained in:
@@ -2,31 +2,58 @@ package camera
|
||||
|
||||
import (
|
||||
"axis_timelapse/config"
|
||||
"crypto/tls"
|
||||
"image"
|
||||
_ "image/jpeg" // Register JPEG decoder
|
||||
"image/png"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func CaptureImage() bool {
|
||||
request := fiber.Get(config.FullURL)
|
||||
var cert_type = config.CameraTLSSelfSigned
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: cert_type},
|
||||
}
|
||||
|
||||
code, body, err := request.Bytes()
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
resp, err := client.Get(config.FullURL)
|
||||
if err != nil {
|
||||
log.Error().Msgf("Error getting image.")
|
||||
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 code != 200 {
|
||||
log.Error().Msg("Unexpected status code: " + strconv.Itoa(code))
|
||||
}
|
||||
if resp.Body != nil {
|
||||
timestamp := time.Now()
|
||||
filename := timestamp.Format("/axis-2006-01-02_15-04-05.jpg")
|
||||
|
||||
timestamp := time.Now()
|
||||
filename := timestamp.Format("/axis-2006-01-02_15-04-05.jpg")
|
||||
err1 := os.WriteFile(config.StoragePath+filename, body, 0644)
|
||||
if err1 != nil {
|
||||
log.Fatal().Err(err1).Msgf("")
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user