adding discovery

This commit is contained in:
Gregory Salaun 2024-10-30 22:40:25 +07:00
parent 4454822ad4
commit 8b3658b5af
3 changed files with 108 additions and 42 deletions

View File

@ -36,6 +36,7 @@ type Config struct {
} `yaml:"cluster"`
Flex struct {
Discover bool `yaml:"discovery"`
IP string `yaml:"ip"`
SpotLife string `yaml:"spot_life"`
} `yaml:"flex"`

View File

@ -18,8 +18,9 @@ cluster:
command: #SET/NOFILTER
login_prompt: "Please enter your call:"
flex:
discovery: true
ip: 10.10.10.120 #113.161.103.129
spot_life: 300
spot_life: 600
clublog:
api: 5767f19333363a9ef432ee9cd4141fe76b8adf38
telnetserver:

View File

@ -37,6 +37,14 @@ type FlexSpot struct {
Worked bool
}
type Discovery struct {
IP string
NickName string
Model string
Serial string
Version string
}
type FlexClient struct {
Address string
Port string
@ -54,7 +62,6 @@ type FlexClient struct {
func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer, SpotChanToFlex chan TelnetSpot) *FlexClient {
return &FlexClient{
Address: Cfg.Flex.IP,
Port: "4992",
SpotChanToFlex: SpotChanToFlex,
MsgChan: TCPServer.MsgChan,
@ -65,54 +72,72 @@ func NewFlexClient(repo FlexDXClusterRepository, TCPServer *TCPServer, SpotChanT
}
func (fc *FlexClient) StartFlexClient() {
var err error
addr, err := net.ResolveTCPAddr("tcp", fc.Address+":"+fc.Port)
if err != nil {
Log.Error("Cannot resolve Telnet Client address")
}
if Cfg.Flex.IP == "" && !Cfg.Flex.Discover {
Log.Errorln("You must either turn FlexRadio Discovery on or provide an IP address for the Flex")
fc.LogWriter = bufio.NewWriter(os.Stdout)
fc.Timeout = 600 * time.Second
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 {
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()
}
Log.Infof("Connected to flex radio at %s:%s", fc.Address, fc.Port)
fc.IsConnected = true
go func() {
for message := range fc.SpotChanToFlex {
fc.SendSpottoFlex(message)
} else if Cfg.Flex.Discover {
ok, d := DiscoverFlexRadio()
if ok {
fc.Address = d.IP
Log.Infof("Found: %s with Nick: %s, Version: %s, Serial: %s - using IP: %s", d.Model, d.NickName, d.Version, d.Serial, d.IP)
} else {
Log.Errorln("Could not discover any FlexRadio on the network, please provide an IP address in the config file.")
}
}()
fc.Reader = bufio.NewReader(fc.Conn)
fc.Writer = bufio.NewWriter(fc.Conn)
err = fc.Conn.SetKeepAlive(true)
if err != nil {
Log.Error("error while setting keep alive")
} else if Cfg.Flex.IP != "" {
fc.Address = Cfg.Flex.IP
}
go fc.ReadLine()
if fc.Address != "" {
addr, err := net.ResolveTCPAddr("tcp", fc.Address+":"+fc.Port)
if err != nil {
Log.Error("Cannot resolve Telnet Client address")
}
subSpotAllCmd := fmt.Sprintf("C%v|sub spot all", CommandNumber)
fc.Write(subSpotAllCmd)
CommandNumber++
fc.LogWriter = bufio.NewWriter(os.Stdout)
clrSpotAllCmd := fmt.Sprintf("C%v|spot clear", CommandNumber)
fc.Write(clrSpotAllCmd)
CommandNumber++
fc.Timeout = 600 * time.Second
Log.Debug("Subscribed to spot on FlexRadio and Deleted all spots from panadapter")
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 {
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()
}
Log.Infof("Connected to flex radio at %s:%s", fc.Address, fc.Port)
fc.IsConnected = true
go func() {
for message := range fc.SpotChanToFlex {
fc.SendSpottoFlex(message)
}
}()
fc.Reader = bufio.NewReader(fc.Conn)
fc.Writer = bufio.NewWriter(fc.Conn)
err = fc.Conn.SetKeepAlive(true)
if err != nil {
Log.Error("error while setting keep alive")
}
go fc.ReadLine()
subSpotAllCmd := fmt.Sprintf("C%v|sub spot all", CommandNumber)
fc.Write(subSpotAllCmd)
CommandNumber++
clrSpotAllCmd := fmt.Sprintf("C%v|spot clear", CommandNumber)
fc.Write(clrSpotAllCmd)
CommandNumber++
Log.Debug("Subscribed to spot on FlexRadio and Deleted all spots from panadapter")
} else {
Log.Errorln("You must either turn FlexRadio Discovery on or provide an IP address for the Flex")
}
}
func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
@ -273,3 +298,42 @@ func (fc *FlexClient) Write(data string) (n int, err error) {
}
return
}
func DiscoverFlexRadio() (bool, *Discovery) {
if Cfg.Flex.Discover {
Log.Infoln("FlexRadio Discovery is turned on...searching for radio on the network")
pc, err := net.ListenPacket("udp4", ":4992")
if err != nil {
Log.Errorln("Could not receive UDP packets to discover FlexRadio")
}
defer pc.Close()
buf := make([]byte, 1024)
for {
n, _, err := pc.ReadFrom(buf)
if err != nil {
Log.Errorln("Could not read data on UDP port 4992")
}
discoverRe := regexp.MustCompile(`discovery_protocol_version=.*\smodel=(.*)\sserial=(.*)\sversion=(.*)\snickname=(.*)\scallsign=.*\sip=(.*)\sport=.*\s+`)
match := discoverRe.FindStringSubmatch(string(buf[:n]))
if len(match) > 0 {
d := Discovery{
NickName: match[4],
Model: match[1],
Serial: match[2],
Version: match[3],
IP: match[5],
}
return true, &d
}
}
} else {
Log.Infoln("FlexRadio Discovery is turned off...using IP provided in the config file")
}
return false, nil
}