retry connect flex

This commit is contained in:
2024-10-23 23:31:29 +07:00
parent f1dbcb4e2b
commit 1b3f5f908b
6 changed files with 50 additions and 45 deletions

View File

@ -8,8 +8,6 @@ import (
"regexp"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
var CommandNumber int = 1
@ -51,11 +49,11 @@ type FlexClient struct {
MsgChan chan string
FlexSpotChan chan FlexSpot
Repo FlexDXClusterRepository
Log *log.Logger
TCPServer *TCPServer
IsConnected bool
}
func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer, log *log.Logger) *FlexClient {
func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer) *FlexClient {
return &FlexClient{
Address: Cfg.Flex.IP,
Port: "4992",
@ -64,7 +62,7 @@ func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer, log *log.
MsgChan: TCPServer.MsgChan,
Repo: repo,
TCPServer: TCPServer,
Log: log,
IsConnected: false,
}
}
@ -73,21 +71,24 @@ func (fc *FlexClient) StartFlexClient() {
addr, err := net.ResolveTCPAddr("tcp", fc.Address+":"+fc.Port)
if err != nil {
fc.Log.Error("Cannot resolve Telnet Client address")
Log.Error("Cannot resolve Telnet Client address")
}
fc.LogWriter = bufio.NewWriter(os.Stdout)
fc.Timeout = 600 * time.Second
fc.Log.Infof("Trying to connect to flex radio at %s:%s", fc.Address, fc.Port)
Log.Infof("Trying to connect to flex radio at %s:%s", fc.Address, fc.Port)
fc.Conn, err = net.DialTCP("tcp", nil, addr)
if err != nil {
fc.Log.Errorf("Could not connect to flex radio on %s, exiting...", Cfg.Flex.IP)
os.Exit(1)
Log.Errorf("Could not connect to flex radio on %s", Cfg.Flex.IP)
Log.Error("Retrying to connect to flex radio in 5 seconds")
time.Sleep(time.Second * 5)
fc.StartFlexClient()
}
fc.Log.Infof("Connected to flex radio at %s:%s", fc.Address, fc.Port)
Log.Infof("Connected to flex radio at %s:%s", fc.Address, fc.Port)
fc.IsConnected = true
go func() {
for message := range fc.SpotChan {
@ -100,7 +101,7 @@ func (fc *FlexClient) StartFlexClient() {
err = fc.Conn.SetKeepAlive(true)
if err != nil {
fc.Log.Error("error while setting keep alive")
Log.Error("error while setting keep alive")
}
go fc.ReadLine()
@ -113,7 +114,7 @@ func (fc *FlexClient) StartFlexClient() {
fc.Write(clrSpotAllCmd)
CommandNumber++
fc.Log.Debug("Subscribed to spot on FlexRadio and Deleted all spots from panadapter")
Log.Debug("Subscribed to spot on FlexRadio and Deleted all spots from panadapter")
}
func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
@ -149,7 +150,7 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
flexSpot.Color = "#3bf908"
flexSpot.Priority = "1"
flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New DXCC ]"
flexSpot.Comment = flexSpot.Comment + " [New DXCC]"
} else if spot.DX == Cfg.SQLite.Callsign {
flexSpot.Color = "#ff0000"
flexSpot.Priority = "1"
@ -158,22 +159,22 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
flexSpot.Color = "#000000"
flexSpot.BackgroundColor = "#00c0c0"
flexSpot.Priority = "5"
flexSpot.Comment = flexSpot.Comment + " [ Worked ]"
flexSpot.Comment = flexSpot.Comment + " [Worked]"
} else if spot.NewMode && spot.NewBand {
flexSpot.Color = "#c603fc"
flexSpot.Priority = "1"
flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New Band & Mode ]"
flexSpot.Comment = flexSpot.Comment + " [New Band & Mode]"
} else if spot.NewMode && !spot.NewBand {
flexSpot.Color = "#f9a908"
flexSpot.Priority = "2"
flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New Mode ]"
flexSpot.Comment = flexSpot.Comment + " [New Mode]"
} else if spot.NewBand && !spot.NewMode {
flexSpot.Color = "#f9f508"
flexSpot.Priority = "3"
flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New Band ]"
flexSpot.Comment = flexSpot.Comment + " [New Band]"
} else if !spot.NewBand && !spot.NewMode && !spot.NewDXCC && !spot.CallsignWorked {
flexSpot.Color = "#eaeaea"
flexSpot.Priority = "5"
@ -184,7 +185,7 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
srcFlexSpot, err := fc.Repo.FindDXSameBand(flexSpot)
if err != nil {
fc.Log.Error("could not find the DX in the database: ", err)
Log.Debugf("could not find the DX in the database: ", err)
}
// send FlexSpot to HTTP Server
@ -225,7 +226,7 @@ func (fc *FlexClient) ReadLine() {
for {
message, err := fc.Reader.ReadString(byte('\n'))
if err != nil {
fc.Log.Errorf("Error reading message from flexradio closing program: %s", err)
Log.Errorf("Error reading message from flexradio closing program: %s", err)
os.Exit(1)
}
@ -238,7 +239,7 @@ func (fc *FlexClient) ReadLine() {
spot, _ := fc.Repo.FindSpotByCommandNumber(respSpot[1])
_, err := fc.Repo.UpdateFlexSpotNumberByID(respSpot[2], *spot)
if err != nil {
fc.Log.Errorf("Could not update flex spot number in database: %s", err)
Log.Errorf("Could not update flex spot number in database: %s", err)
}
}
@ -249,13 +250,13 @@ func (fc *FlexClient) ReadLine() {
if len(respTrigger) > 0 {
spot, err := fc.Repo.FindSpotByFlexSpotNumber(respTrigger[1])
if err != nil {
fc.Log.Errorf("could not find spot by flex spot number in database: %s", err)
Log.Errorf("could not find spot by flex spot number in database: %s", err)
}
msg := fmt.Sprintf(`To ALL de %s <%s> : Clicked on "%s" at %s`, Cfg.SQLite.Callsign, spot.UTCTime, spot.DX, spot.FrequencyHz)
if len(fc.TCPServer.Clients) > 0 {
fc.MsgChan <- msg
fc.Log.Infof("%s clicked on spot \"%s\" at %s", Cfg.SQLite.Callsign, spot.DX, spot.FrequencyMhz)
Log.Infof("%s clicked on spot \"%s\" at %s", Cfg.SQLite.Callsign, spot.DX, spot.FrequencyMhz)
}
}
@ -266,7 +267,7 @@ func (fc *FlexClient) ReadLine() {
if len(respDelete) > 0 {
spot, _ := fc.Repo.FindSpotByFlexSpotNumber(respDelete[1])
fc.Repo.DeleteSpotByFlexSpotNumber(respDelete[1])
fc.Log.Debugf("Spot: DX: %s - Spotter: %s - Freq: %s - Band: %s - FlexID: %v deleted from database", spot.DX, spot.SpotterCallsign, spot.FrequencyHz, spot.Band, respDelete[1])
Log.Debugf("Spot: DX: %s - Spotter: %s - Freq: %s - Band: %s - FlexID: %v deleted from database", spot.DX, spot.SpotterCallsign, spot.FrequencyHz, spot.Band, respDelete[1])
}
}
}