Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 12 additions & 100 deletions cmd/application/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,14 @@
package main

import (
"fmt"
"github.com/GetSky/WeatherAlertBTA/config"
"github.com/GetSky/WeatherAlertBTA/internal/application"
"github.com/GetSky/WeatherAlertBTA/internal/infrastructure"
"time"
)

var cnf *config.Conf

var weatherSrv application.WeatherService
var chartSrv application.ChartService
var notifySrv application.NotifyService
var scheduleSrv application.ScheduleService

var (
workActive bool
windAlertActive bool
lastWindAlertTime time.Time
)
var tracker *application.WeatherTracker

func init() {
var err error
Expand All @@ -31,98 +20,21 @@ func init() {
return
}

chartSrv = infrastructure.NewChartService(cnf.ChartWeatherUrl)
weatherSrv = infrastructure.NewWeatherService(cnf.WeatherUrl)
notifySrv = infrastructure.NewTelegramNotifyService(cnf.BotToken, cnf.TelegramChat)
scheduleSrv = infrastructure.NewScheduleService(cnf.TimeReserveBeforeDusk)
chartSrv := infrastructure.NewChartService(cnf.ChartWeatherUrl)
weatherSrv := infrastructure.NewWeatherService(cnf.WeatherUrl)
notifySrv := infrastructure.NewTelegramNotifyService(cnf.BotToken, cnf.TelegramChat)
scheduleSrv := infrastructure.NewScheduleService(cnf.TimeReserveBeforeDusk)

tracker = application.NewWeatherTracker(
application.NewTurnOnState(scheduleSrv, notifySrv, chartSrv, weatherSrv, cnf.WindThreshold, cnf.DelayTime),
application.NewTurnOffState(scheduleSrv, notifySrv),
)

}

func main() {
for {
isWorkTime, _ := scheduleSrv.IsWorkNow()
if isWorkTime != workActive {
workActive = isWorkTime
checkWorkStatus()
}
if isWorkTime {
checkWeather()
} else {
}
tracker.Check()
time.Sleep(cnf.PollInterval)
}
}

func checkWorkStatus() {
switch workActive {
case true:
dusk, dawn, err := scheduleSrv.GetNautical(time.Now())
if err != nil {
fmt.Printf("Main → %v\n", err)
return
}
err = notifySrv.SendWorkStarted(dusk, dawn)
if err != nil {
fmt.Printf("Main → %v\n", err)
return
}
case false:
err := notifySrv.SendWorkEnded()
if err != nil {
fmt.Printf("Main → %v\n", err)
return
}
}
}

func checkWeather() {
chart, err := chartSrv.GetUpdatedChart()
if err != nil {
fmt.Printf("ChartService → %v\n", err)
return
}

weather, err := weatherSrv.GetLatestWeather()
if err != nil {
fmt.Printf("WeatherService → %v\n", err)
return
}

if weather.WindSpeed >= cnf.WindThreshold {
weather.Hazardous = true
lastWindAlertTime = time.Now()
windAlertActive = true

err = notifySrv.SendUpdate(chart, weather)
if err != nil {
fmt.Printf("Main → %v\n", err)
return
}

fmt.Println("Wind alert sent successfully.")

} else {
if windAlertActive {
duration := time.Since(lastWindAlertTime)
if duration > cnf.DelayTime {
weather.Hazardous = false
err = notifySrv.SendUpdate(chart, weather)
if err != nil {
fmt.Printf("Main → %v\n", err)
return
}

windAlertActive = false
fmt.Println("Wind speed below threshold message sent.")
} else {
fmt.Println("The wind speed is below the threshold, but the time has not come to cancel the alert.")
}
} else {
weather.Hazardous = false
err = notifySrv.SendUpdate(chart, weather)
if err != nil {
fmt.Printf("Main → %v\n", err)
return
}
}
}
}
40 changes: 40 additions & 0 deletions internal/application/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Alexander Getmansky <alex@getsky.tech>
// Licensed under the Apache License, Version 2.0

package application

import "fmt"

type State interface {
check() error
SetTracker(tracker *WeatherTracker)
}

type WeatherTracker struct {
turnedOn State
turnedOff State

currentState State
}

func NewWeatherTracker(turnedOn State, turnedOff State) *WeatherTracker {
t := &WeatherTracker{
turnedOn: turnedOn,
turnedOff: turnedOff,
}
turnedOn.SetTracker(t)
turnedOff.SetTracker(t)
t.switchState(turnedOff)
return t
}

func (t *WeatherTracker) switchState(s State) {
t.currentState = s
}

func (t *WeatherTracker) Check() {
err := t.currentState.check()
if err != nil {
fmt.Printf("Tracker → %v\n", err)
}
}
51 changes: 51 additions & 0 deletions internal/application/turn_off_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 Alexander Getmansky <alex@getsky.tech>
// Licensed under the Apache License, Version 2.0

package application

import (
"time"
)

type TurnOffState struct {
tracker *WeatherTracker
scheduleSrv ScheduleService
notifySrv NotifyService
}

func NewTurnOffState(schedule ScheduleService, notify NotifyService) *TurnOffState {
return &TurnOffState{
scheduleSrv: schedule,
notifySrv: notify,
}
}

func (t *TurnOffState) SetTracker(tracker *WeatherTracker) {
t.tracker = tracker
}

func (t *TurnOffState) check() error {
isWorkTime, err := t.scheduleSrv.IsWorkNow()
if err != nil {
return err
}

if isWorkTime == false {
return nil
}

dusk, dawn, err := t.scheduleSrv.GetNautical(time.Now())
if err != nil {
return err
}

err = t.notifySrv.SendWorkStarted(dusk, dawn)
if err != nil {
return err
}

t.tracker.switchState(t.tracker.turnedOn)
t.tracker.Check()

return nil
}
120 changes: 120 additions & 0 deletions internal/application/turn_on_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2024 Alexander Getmansky <alex@getsky.tech>
// Licensed under the Apache License, Version 2.0

package application

import (
"fmt"
"time"
)

type TurnOnState struct {
tracker *WeatherTracker
scheduleSrv ScheduleService
notifySrv NotifyService
chartSrv ChartService
weatherSrv WeatherService

windThreshold float64
delayTime time.Duration

lastWindAlertTime time.Time
windAlertActive bool
}

func NewTurnOnState(
schedule ScheduleService,
notify NotifyService,
chart ChartService,
weather WeatherService,
windThreshold float64,
delayTime time.Duration,
) *TurnOnState {
return &TurnOnState{
scheduleSrv: schedule,
notifySrv: notify,
chartSrv: chart,
weatherSrv: weather,
windThreshold: windThreshold,
delayTime: delayTime,
}
}

func (t *TurnOnState) SetTracker(tracker *WeatherTracker) {
t.tracker = tracker
}

func (t *TurnOnState) check() error {
isWorkTime, err := t.scheduleSrv.IsWorkNow()
if err != nil {
return err
}

if isWorkTime == false {
err = t.notifySrv.SendWorkEnded()
if err != nil {
return err
}

t.tracker.switchState(t.tracker.turnedOff)

return nil
}

err = t.checkWeather()
if err != nil {
return err
}

return nil
}

func (t *TurnOnState) checkWeather() error {
chart, err := t.chartSrv.GetUpdatedChart()
if err != nil {
return err
}

weather, err := t.weatherSrv.GetLatestWeather()
if err != nil {
return err
}

if weather.WindSpeed >= t.windThreshold {
weather.Hazardous = true
t.lastWindAlertTime = time.Now()
t.windAlertActive = true

err = t.notifySrv.SendUpdate(chart, weather)
if err != nil {
return err
}

fmt.Println("Wind alert sent successfully.")

} else {
if t.windAlertActive {
duration := time.Since(t.lastWindAlertTime)
if duration > t.delayTime {
weather.Hazardous = false
err = t.notifySrv.SendUpdate(chart, weather)
if err != nil {
return err
}

t.windAlertActive = false
fmt.Println("Wind speed below threshold message sent.")
} else {
fmt.Println("The wind speed is below the threshold, but the time has not come to cancel the alert.")
}
} else {
weather.Hazardous = false
err = t.notifySrv.SendUpdate(chart, weather)
if err != nil {
return err
}
}
}

return nil
}