retry connect flex

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

View File

@ -63,19 +63,19 @@ func (c *TCPClient) StartClient() {
addr, err := net.ResolveTCPAddr("tcp", c.Address+":"+c.Port) addr, err := net.ResolveTCPAddr("tcp", c.Address+":"+c.Port)
if err != nil { if err != nil {
c.Log.Error("cannot resolve Telnet Client address:", err) c.Log.Error("Cannot resolve Telnet Client address:", err)
} }
c.setDefaultParams() c.setDefaultParams()
c.Conn, err = net.DialTCP("tcp", nil, addr) c.Conn, err = net.DialTCP("tcp", nil, addr)
if err != nil { if err != nil {
c.Log.Error("cannot connect to Telnet Client:", err) c.Log.Error("Cannot connect to Telnet Client:", err)
} }
c.Log.Infof("connected to DX cluster %s:%s", c.Address, c.Port) c.Log.Infof("Connected to DX cluster %s:%s", c.Address, c.Port)
err = c.Conn.SetKeepAlive(true) err = c.Conn.SetKeepAlive(true)
if err != nil { if err != nil {
c.Log.Error("error while setting keep alive:", err) c.Log.Error("Error while setting keep alive:", err)
} }
c.Reader = bufio.NewReader(c.Conn) c.Reader = bufio.NewReader(c.Conn)
@ -100,32 +100,32 @@ func (c *TCPClient) Close() {
func (c *TCPClient) SetFilters() { func (c *TCPClient) SetFilters() {
if Cfg.Cluster.FT8 { if Cfg.Cluster.FT8 {
c.Write([]byte("set/ft8\r\n")) c.Write([]byte("set/ft8\r\n"))
c.Log.Debug("FT8 is on as defined in the config file") c.Log.Info("FT8: On")
} }
if Cfg.Cluster.Skimmer { if Cfg.Cluster.Skimmer {
c.Write([]byte("set/skimmer\r\n")) c.Write([]byte("set/skimmer\r\n"))
c.Log.Debug("Skimmer is on as defined in the config file") c.Log.Info("Skimmer: On")
} }
if Cfg.Cluster.FT4 { if Cfg.Cluster.FT4 {
c.Write([]byte("set/ft4\r\n")) c.Write([]byte("set/ft4\r\n"))
c.Log.Debug("FT4 is on as defined in the config file") c.Log.Info("FT4: On")
} }
if !Cfg.Cluster.FT8 { if !Cfg.Cluster.FT8 {
c.Write([]byte("set/noft8\r\n")) c.Write([]byte("set/noft8\r\n"))
c.Log.Debug("FT8 is off as defined in the config file") c.Log.Info("FT8: Off")
} }
if !Cfg.Cluster.FT4 { if !Cfg.Cluster.FT4 {
c.Write([]byte("set/noft4\r\n")) c.Write([]byte("set/noft4\r\n"))
c.Log.Debug("FT4 is off as defined in the config file") c.Log.Info("FT4: Off")
} }
if !Cfg.Cluster.Skimmer { if !Cfg.Cluster.Skimmer {
c.Write([]byte("set/noskimmer\r\n")) c.Write([]byte("set/noskimmer\r\n"))
c.Log.Debug("Skimmer is off as defined in the config file") c.Log.Info("Skimmer: Off")
} }
} }
@ -147,11 +147,11 @@ func (c *TCPClient) ReadLine() {
if Cfg.Cluster.Command != "" { if Cfg.Cluster.Command != "" {
c.WriteString(Cfg.Cluster.Command) c.WriteString(Cfg.Cluster.Command)
} }
c.Log.Info("start receiving spots") c.Log.Info("Start receiving spots")
} }
// start := time.Now() // start := time.Now()
ProcessTelnetSpot(spotRe, message, c.SpotChan, c.Countries) go ProcessTelnetSpot(spotRe, message, c.SpotChan, c.Countries)
// elapsed := time.Since(start) // elapsed := time.Since(start)
// Log.Infof("Total time for processing spot: %s", elapsed) // Log.Infof("Total time for processing spot: %s", elapsed)
@ -159,7 +159,7 @@ func (c *TCPClient) ReadLine() {
if len(c.TCPServer.Clients) > 0 { if len(c.TCPServer.Clients) > 0 {
if count == 0 { if count == 0 {
// wait 3 seconds before sending messages to allow the client to connect // wait 3 seconds before sending messages to allow the client to connect
time.Sleep(time.Second * 3) time.Sleep(time.Second * 5)
count++ count++
} }
c.MsgChan <- message c.MsgChan <- message

View File

@ -47,12 +47,12 @@ func (s *TCPServer) StartServer() {
s.LogWriter = bufio.NewWriter(os.Stdout) s.LogWriter = bufio.NewWriter(os.Stdout)
s.Listener, err = net.Listen("tcp", Cfg.TelnetServer.Host+":"+Cfg.TelnetServer.Port) s.Listener, err = net.Listen("tcp", Cfg.TelnetServer.Host+":"+Cfg.TelnetServer.Port)
if err != nil { if err != nil {
s.Log.Info("could not create telnet server") s.Log.Info("Could not create telnet server")
} }
defer s.Listener.Close() defer s.Listener.Close()
s.Log.Infof("telnet server listening on %s:%s", Cfg.TelnetServer.Host, Cfg.TelnetServer.Port) s.Log.Infof("Telnet server listening on %s:%s", Cfg.TelnetServer.Host, Cfg.TelnetServer.Port)
go func() { go func() {
for message := range s.MsgChan { for message := range s.MsgChan {
@ -62,9 +62,9 @@ func (s *TCPServer) StartServer() {
for { for {
s.Conn, err = s.Listener.Accept() s.Conn, err = s.Listener.Accept()
s.Log.Info("client connected", s.Conn.RemoteAddr().String()) s.Log.Info("Client connected", s.Conn.RemoteAddr().String())
if err != nil { if err != nil {
s.Log.Error("could not accept connections to telnet server") s.Log.Error("Could not accept connections to telnet server")
continue continue
} }
s.Mutex.Lock() s.Mutex.Lock()

BIN
flex.sqlite Normal file

Binary file not shown.

View File

@ -8,8 +8,6 @@ import (
"regexp" "regexp"
"strings" "strings"
"time" "time"
log "github.com/sirupsen/logrus"
) )
var CommandNumber int = 1 var CommandNumber int = 1
@ -51,11 +49,11 @@ type FlexClient struct {
MsgChan chan string MsgChan chan string
FlexSpotChan chan FlexSpot FlexSpotChan chan FlexSpot
Repo FlexDXClusterRepository Repo FlexDXClusterRepository
Log *log.Logger
TCPServer *TCPServer TCPServer *TCPServer
IsConnected bool
} }
func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer, log *log.Logger) *FlexClient { func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer) *FlexClient {
return &FlexClient{ return &FlexClient{
Address: Cfg.Flex.IP, Address: Cfg.Flex.IP,
Port: "4992", Port: "4992",
@ -64,7 +62,7 @@ func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer, log *log.
MsgChan: TCPServer.MsgChan, MsgChan: TCPServer.MsgChan,
Repo: repo, Repo: repo,
TCPServer: TCPServer, TCPServer: TCPServer,
Log: log, IsConnected: false,
} }
} }
@ -73,21 +71,24 @@ func (fc *FlexClient) StartFlexClient() {
addr, err := net.ResolveTCPAddr("tcp", fc.Address+":"+fc.Port) addr, err := net.ResolveTCPAddr("tcp", fc.Address+":"+fc.Port)
if err != nil { 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.LogWriter = bufio.NewWriter(os.Stdout)
fc.Timeout = 600 * time.Second 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) fc.Conn, err = net.DialTCP("tcp", nil, addr)
if err != nil { if err != nil {
fc.Log.Errorf("Could not connect to flex radio on %s, exiting...", Cfg.Flex.IP) Log.Errorf("Could not connect to flex radio on %s", Cfg.Flex.IP)
os.Exit(1) 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() { go func() {
for message := range fc.SpotChan { for message := range fc.SpotChan {
@ -100,7 +101,7 @@ func (fc *FlexClient) StartFlexClient() {
err = fc.Conn.SetKeepAlive(true) err = fc.Conn.SetKeepAlive(true)
if err != nil { if err != nil {
fc.Log.Error("error while setting keep alive") Log.Error("error while setting keep alive")
} }
go fc.ReadLine() go fc.ReadLine()
@ -113,7 +114,7 @@ func (fc *FlexClient) StartFlexClient() {
fc.Write(clrSpotAllCmd) fc.Write(clrSpotAllCmd)
CommandNumber++ 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) { func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
@ -149,7 +150,7 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
flexSpot.Color = "#3bf908" flexSpot.Color = "#3bf908"
flexSpot.Priority = "1" flexSpot.Priority = "1"
flexSpot.BackgroundColor = "#000000" flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New DXCC ]" flexSpot.Comment = flexSpot.Comment + " [New DXCC]"
} else if spot.DX == Cfg.SQLite.Callsign { } else if spot.DX == Cfg.SQLite.Callsign {
flexSpot.Color = "#ff0000" flexSpot.Color = "#ff0000"
flexSpot.Priority = "1" flexSpot.Priority = "1"
@ -158,22 +159,22 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
flexSpot.Color = "#000000" flexSpot.Color = "#000000"
flexSpot.BackgroundColor = "#00c0c0" flexSpot.BackgroundColor = "#00c0c0"
flexSpot.Priority = "5" flexSpot.Priority = "5"
flexSpot.Comment = flexSpot.Comment + " [ Worked ]" flexSpot.Comment = flexSpot.Comment + " [Worked]"
} else if spot.NewMode && spot.NewBand { } else if spot.NewMode && spot.NewBand {
flexSpot.Color = "#c603fc" flexSpot.Color = "#c603fc"
flexSpot.Priority = "1" flexSpot.Priority = "1"
flexSpot.BackgroundColor = "#000000" flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New Band & Mode ]" flexSpot.Comment = flexSpot.Comment + " [New Band & Mode]"
} else if spot.NewMode && !spot.NewBand { } else if spot.NewMode && !spot.NewBand {
flexSpot.Color = "#f9a908" flexSpot.Color = "#f9a908"
flexSpot.Priority = "2" flexSpot.Priority = "2"
flexSpot.BackgroundColor = "#000000" flexSpot.BackgroundColor = "#000000"
flexSpot.Comment = flexSpot.Comment + " [ New Mode ]" flexSpot.Comment = flexSpot.Comment + " [New Mode]"
} else if spot.NewBand && !spot.NewMode { } else if spot.NewBand && !spot.NewMode {
flexSpot.Color = "#f9f508" flexSpot.Color = "#f9f508"
flexSpot.Priority = "3" flexSpot.Priority = "3"
flexSpot.BackgroundColor = "#000000" 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 { } else if !spot.NewBand && !spot.NewMode && !spot.NewDXCC && !spot.CallsignWorked {
flexSpot.Color = "#eaeaea" flexSpot.Color = "#eaeaea"
flexSpot.Priority = "5" flexSpot.Priority = "5"
@ -184,7 +185,7 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
srcFlexSpot, err := fc.Repo.FindDXSameBand(flexSpot) srcFlexSpot, err := fc.Repo.FindDXSameBand(flexSpot)
if err != nil { 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 // send FlexSpot to HTTP Server
@ -225,7 +226,7 @@ func (fc *FlexClient) ReadLine() {
for { for {
message, err := fc.Reader.ReadString(byte('\n')) message, err := fc.Reader.ReadString(byte('\n'))
if err != nil { 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) os.Exit(1)
} }
@ -238,7 +239,7 @@ func (fc *FlexClient) ReadLine() {
spot, _ := fc.Repo.FindSpotByCommandNumber(respSpot[1]) spot, _ := fc.Repo.FindSpotByCommandNumber(respSpot[1])
_, err := fc.Repo.UpdateFlexSpotNumberByID(respSpot[2], *spot) _, err := fc.Repo.UpdateFlexSpotNumberByID(respSpot[2], *spot)
if err != nil { 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 { if len(respTrigger) > 0 {
spot, err := fc.Repo.FindSpotByFlexSpotNumber(respTrigger[1]) spot, err := fc.Repo.FindSpotByFlexSpotNumber(respTrigger[1])
if err != nil { 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) 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 { if len(fc.TCPServer.Clients) > 0 {
fc.MsgChan <- msg 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 { if len(respDelete) > 0 {
spot, _ := fc.Repo.FindSpotByFlexSpotNumber(respDelete[1]) spot, _ := fc.Repo.FindSpotByFlexSpotNumber(respDelete[1])
fc.Repo.DeleteSpotByFlexSpotNumber(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])
} }
} }
} }

View File

@ -68,7 +68,7 @@ func main() {
TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port, log) TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port, log)
FlexClient := NewFlexClient(*fRepo, TCPServer, log) FlexClient := NewFlexClient(*fRepo, TCPServer)
TCPClient := NewTCPClient(TCPServer, FlexClient, log, Countries) TCPClient := NewTCPClient(TCPServer, FlexClient, log, Countries)
HTTPServer := NewHTTPServer(*cRepo, *fRepo, FlexClient, TCPServer, log) HTTPServer := NewHTTPServer(*cRepo, *fRepo, FlexClient, TCPServer, log)

12
spot.go
View File

@ -63,11 +63,15 @@ func ProcessTelnetSpot(re *regexp.Regexp, spotRaw string, SpotChan chan TelnetSp
default: default:
spot.NewDXCC = true spot.NewDXCC = true
} }
} else if len(contactsMode) == 0 { }
if len(contactsMode) == 0 {
spot.NewMode = true spot.NewMode = true
} else if len(contactsBand) == 0 { }
if len(contactsBand) == 0 {
spot.NewBand = true spot.NewBand = true
} else if len(contactsCall) > 0 { }
if len(contactsCall) > 0 {
spot.CallsignWorked = true spot.CallsignWorked = true
} }
@ -104,7 +108,7 @@ func ProcessTelnetSpot(re *regexp.Regexp, spotRaw string, SpotChan chan TelnetSp
spot.DX, spot.Spotter, spot.Frequency, spot.Band, spot.Mode, spot.Comment, spot.Time, spot.DXCC) spot.DX, spot.Spotter, spot.Frequency, spot.Band, spot.Mode, spot.Comment, spot.Time, spot.DXCC)
} }
} else { } else {
Log.Errorf("Could not decode: %s", strings.Trim(spotRaw, "\n")) Log.Debugf("Could not decode: %s", strings.Trim(spotRaw, "\n"))
} }
} }