This commit is contained in:
2025-12-04 23:40:25 +09:00
parent 4c32049944
commit 0afac9d320
2 changed files with 21 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"time" "time"
@@ -32,9 +33,7 @@ func (h *Handler) Handle(ctx context.Context) error {
forecast, err := openweather.Forecast(ctx, h.key, h.lat, h.lon) forecast, err := openweather.Forecast(ctx, h.key, h.lat, h.lon)
if err != nil { if err != nil {
log.Println("error in get forecast", err) return fmt.Errorf("error in get forecast: %w", err)
return err
} }
log.Println("get forecast successfully", forecast) log.Println("get forecast successfully", forecast)
@@ -42,13 +41,13 @@ func (h *Handler) Handle(ctx context.Context) error {
for _, list := range forecast.List { for _, list := range forecast.List {
weatherEvent := domain.WeatherEvent{ weatherEvent := domain.WeatherEvent{
Date: time.Unix(int64(list.Dt), 0), Date: time.Unix(int64(list.Dt), 0),
Condition: decideCondition(list.Weather[0].Main), Condition: decideCondition(list.Weather),
Temperature: &domain.Temperature{Value: domain.Celsius(list.Main.Temp)}, Temperature: &domain.Temperature{Value: domain.Celsius(list.Main.Temp)},
High: &domain.Temperature{Value: domain.Celsius(list.Main.TempMax)}, High: &domain.Temperature{Value: domain.Celsius(list.Main.TempMax)},
Low: &domain.Temperature{Value: domain.Celsius(list.Main.TempMin)}, Low: &domain.Temperature{Value: domain.Celsius(list.Main.TempMin)},
} }
message, err := json.Marshal(weatherEvent) message, err := json.Marshal(&weatherEvent)
if err != nil { if err != nil {
log.Println("error in marshal weather event", err) log.Println("error in marshal weather event", err)
@@ -66,7 +65,12 @@ func (h *Handler) Handle(ctx context.Context) error {
return nil return nil
} }
func decideCondition(main string) domain.WeatherCondition { func decideCondition(weather []openweather.Weather) domain.WeatherCondition {
if len(weather) == 0 {
return domain.WeatherConditionUnknown
}
main := weather[0].Main
switch main { switch main {
case "Thunderstorm", "Drizzle", "Rain": case "Thunderstorm", "Drizzle", "Rain":
return domain.WeatherConditionRain return domain.WeatherConditionRain

View File

@@ -28,7 +28,7 @@ type Config struct {
NATSURL string NATSURL string
} }
func NewConfig() (*Config, error) { func NewConfig() *Config {
key := os.Getenv("OPENWEATHER_API_KEY") key := os.Getenv("OPENWEATHER_API_KEY")
if key == "" { if key == "" {
log.Panic("OPENWEATHER_API_KEY is not set") log.Panic("OPENWEATHER_API_KEY is not set")
@@ -64,20 +64,22 @@ func NewConfig() (*Config, error) {
Lat: latFloat, Lat: latFloat,
Lon: lonFloat, Lon: lonFloat,
NATSURL: NATS_URL, NATSURL: NATS_URL,
}, nil }
} }
func main() { func main() {
log.Println("version", version()) log.Println("version", version())
godotenv.Load() err := godotenv.Load()
if err == nil {
config, err := NewConfig() log.Println("env loaded")
if err != nil {
log.Panic("error in create config", err)
} }
broker, err := nats.NewBroker(context.Background(), config.NATSURL, "SEVEN_SKIES_STREAM", []string{"SEVEN_SKIES_SUBJECT.>"}) config := NewConfig()
ctx := context.Background()
broker, err := nats.NewBroker(ctx, config.NATSURL, "SEVEN_SKIES_STREAM", []string{"SEVEN_SKIES_SUBJECT.>"})
if err != nil { if err != nil {
log.Panic("error in create broker", err) log.Panic("error in create broker", err)
} }
@@ -97,7 +99,7 @@ func main() {
} }
}() }()
err = handler.Handle(context.Background()) err = handler.Handle(ctx)
if err != nil { if err != nil {
log.Panic("error in task", err) log.Panic("error in task", err)
} }