mirror of
https://github.com/make-42/hayai.git
synced 2025-01-18 10:37:11 +01:00
feat: program won't crash unless something goes very very wrong
This commit is contained in:
parent
3dd8d135ff
commit
67827e958c
@ -19,6 +19,7 @@ type ConfigS struct {
|
|||||||
IssueNotification bool
|
IssueNotification bool
|
||||||
OpenWebPages bool
|
OpenWebPages bool
|
||||||
TestWarning bool
|
TestWarning bool
|
||||||
|
RetryConnectionEveryXS float64
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config ConfigS
|
var Config ConfigS
|
||||||
@ -34,6 +35,7 @@ var DefaultConfig = ConfigS{
|
|||||||
IssueNotification: true,
|
IssueNotification: true,
|
||||||
OpenWebPages: false,
|
OpenWebPages: false,
|
||||||
TestWarning: false,
|
TestWarning: false,
|
||||||
|
RetryConnectionEveryXS: 30,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
func CheckError(err error) {
|
func CheckError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
117
wolfx/wolfx.go
117
wolfx/wolfx.go
@ -81,6 +81,8 @@ type JMAEEW struct {
|
|||||||
Pond string
|
Pond string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var LastRetry time.Time
|
||||||
|
|
||||||
func Listen() {
|
func Listen() {
|
||||||
// Init sound
|
// Init sound
|
||||||
alertSound, err := alertSoundFile.Open("assets/alert-sat.wav")
|
alertSound, err := alertSoundFile.Open("assets/alert-sat.wav")
|
||||||
@ -91,65 +93,76 @@ func Listen() {
|
|||||||
defer streamer.Close()
|
defer streamer.Close()
|
||||||
// Listen
|
// Listen
|
||||||
u := url.URL{Scheme: "wss", Host: constants.WSHost, Path: "/all_eew"}
|
u := url.URL{Scheme: "wss", Host: constants.WSHost, Path: "/all_eew"}
|
||||||
log.Printf("connecting to %s", u.String())
|
|
||||||
|
|
||||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("dial:", err)
|
|
||||||
}
|
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
defer close(done)
|
|
||||||
|
|
||||||
log.Printf("connected.")
|
|
||||||
for {
|
for {
|
||||||
_, message, err := c.ReadMessage()
|
log.Printf("connecting to %s", u.String())
|
||||||
utils.CheckError(err)
|
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||||
var typeMessage TypeMessage
|
if err != nil {
|
||||||
if config.Config.TestWarning {
|
log.Print("dial:", err)
|
||||||
message = constants.TestMessage
|
time.Sleep(time.Duration(config.Config.RetryConnectionEveryXS) * time.Second)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
json.Unmarshal(message, &typeMessage)
|
|
||||||
if typeMessage.Type == "jma_eew" {
|
defer c.Close()
|
||||||
log.Printf("recv: %s", message)
|
|
||||||
var jmaeew JMAEEW
|
done := make(chan struct{})
|
||||||
json.Unmarshal(message, &jmaeew)
|
|
||||||
if !jmaeew.IsWarn && config.Config.OnlyWarnings {
|
defer close(done)
|
||||||
continue
|
|
||||||
|
log.Printf("connected.")
|
||||||
|
for {
|
||||||
|
_, message, err := c.ReadMessage()
|
||||||
|
utils.CheckError(err)
|
||||||
|
if message == nil {
|
||||||
|
log.Printf("connection lost.")
|
||||||
|
if time.Since(LastRetry) < time.Duration(config.Config.RetryConnectionEveryXS)*time.Second {
|
||||||
|
time.Sleep(time.Duration(config.Config.RetryConnectionEveryXS) * time.Second)
|
||||||
|
LastRetry = time.Now()
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
equivalentMagnitude := seismo.CalculateEquivalentMagnitude(jmaeew.Magunitude, jmaeew.Latitude, jmaeew.Longitude, config.Config.Latitude, config.Config.Longitude)
|
var typeMessage TypeMessage
|
||||||
if config.Config.IssueWarningAtAnyMagnitude || config.Config.IssueWarningAtEquivalentMagnitude < equivalentMagnitude {
|
if config.Config.TestWarning {
|
||||||
alert_body := fmt.Sprintf("Epicenter: %s\nMagnitude %0.1f\nApproximately magnitude %0.1f at your location\nStrong shaking is expected soon.\nStay calm and seek shelter nearby.\n\nOrigin time: %s JST\nAnnouncement time: %s JST\nDepth: %dkm\nCoordinates: %0.1f, %0.1f\n\nSource: %s\nStatus: %s\n\n%s", jmaeew.Hypocenter, jmaeew.Magunitude, equivalentMagnitude, jmaeew.OriginTime, jmaeew.AnnouncedTime, jmaeew.Depth, jmaeew.Latitude, jmaeew.Longitude, jmaeew.Issue.Source, jmaeew.Issue.Status, jmaeew.OriginalText)
|
message = constants.TestMessage
|
||||||
if config.Config.IssuePopup {
|
}
|
||||||
go dialog.Message("%s", alert_body).Title("Early Earthquake Warning!").Info()
|
json.Unmarshal(message, &typeMessage)
|
||||||
|
if typeMessage.Type == "jma_eew" {
|
||||||
|
log.Printf("recv: %s", message)
|
||||||
|
var jmaeew JMAEEW
|
||||||
|
json.Unmarshal(message, &jmaeew)
|
||||||
|
if !jmaeew.IsWarn && config.Config.OnlyWarnings {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
equivalentMagnitude := seismo.CalculateEquivalentMagnitude(jmaeew.Magunitude, jmaeew.Latitude, jmaeew.Longitude, config.Config.Latitude, config.Config.Longitude)
|
||||||
if config.Config.IssueNotification {
|
if config.Config.IssueWarningAtAnyMagnitude || config.Config.IssueWarningAtEquivalentMagnitude < equivalentMagnitude {
|
||||||
homedir, err := os.UserHomeDir()
|
alert_body := fmt.Sprintf("Epicenter: %s\nMagnitude %0.1f\nApproximately magnitude %0.1f at your location\nStrong shaking is expected soon.\nStay calm and seek shelter nearby.\n\nOrigin time: %s JST\nAnnouncement time: %s JST\nDepth: %dkm\nCoordinates: %0.1f, %0.1f\n\nSource: %s\nStatus: %s\n\n%s", jmaeew.Hypocenter, jmaeew.Magunitude, equivalentMagnitude, jmaeew.OriginTime, jmaeew.AnnouncedTime, jmaeew.Depth, jmaeew.Latitude, jmaeew.Longitude, jmaeew.Issue.Source, jmaeew.Issue.Status, jmaeew.OriginalText)
|
||||||
utils.CheckError(err)
|
if config.Config.IssuePopup {
|
||||||
err = beeep.Notify("Early Earthquake Warning!", alert_body, homedir+"/.icons/actions/scalable/dialog-warning.svg")
|
go dialog.Message("%s", alert_body).Title("Early Earthquake Warning!").Info()
|
||||||
utils.CheckError(err)
|
}
|
||||||
}
|
|
||||||
|
if config.Config.IssueNotification {
|
||||||
if config.Config.OpenWebPages {
|
homedir, err := os.UserHomeDir()
|
||||||
utils.OpenURL(constants.OpenURLA)
|
utils.CheckError(err)
|
||||||
utils.OpenURL(constants.OpenURLB)
|
err = beeep.Notify("Early Earthquake Warning!", alert_body, homedir+"/.icons/actions/scalable/dialog-warning.svg")
|
||||||
}
|
utils.CheckError(err)
|
||||||
|
}
|
||||||
if config.Config.IssueWarningSound {
|
|
||||||
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
if config.Config.OpenWebPages {
|
||||||
streamer.Seek(0)
|
utils.OpenURL(constants.OpenURLA)
|
||||||
speaker.Play(streamer)
|
utils.OpenURL(constants.OpenURLB)
|
||||||
for i := 0; i < 10; i++ {
|
}
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
streamer.Seek(0)
|
if config.Config.IssueWarningSound {
|
||||||
|
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
||||||
|
streamer.Seek(0)
|
||||||
|
speaker.Play(streamer)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
streamer.Seek(0)
|
||||||
|
}
|
||||||
|
speaker.Close()
|
||||||
}
|
}
|
||||||
speaker.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user