Added received time and APRS API
This commit is contained in:
82
collector.go
82
collector.go
@@ -2,7 +2,6 @@ package hamview
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -173,22 +172,22 @@ func (c *Collector) processRadio(ctx context.Context, received *Radio) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) processAPRSPacket(ctx context.Context, received *Packet) {
|
||||
radio, err := c.getRadioByID(ctx, received.RadioID)
|
||||
func (c *Collector) processAPRSPacket(ctx context.Context, packet *Packet) {
|
||||
radio, err := c.getRadioByID(ctx, packet.RadioID)
|
||||
if err != nil {
|
||||
Logger.Warnf("collector: process %s packet: can't find radio %q: %v", received.Protocol, received.RadioID, err)
|
||||
Logger.Warnf("collector: process %s packet: can't find radio %q: %v", packet.Protocol, packet.RadioID, err)
|
||||
return
|
||||
}
|
||||
|
||||
decoded, err := aprs.Parse(string(received.Raw))
|
||||
decoded, err := aprs.Parse(string(packet.Raw))
|
||||
if err != nil {
|
||||
Logger.Warnf("collector: invalid %s packet: %v", received.Protocol, err)
|
||||
Logger.Warnf("collector: invalid %s packet: %v", packet.Protocol, err)
|
||||
return
|
||||
}
|
||||
|
||||
Logger.Tracef("collector: process %s packet (%d bytes)",
|
||||
received.Protocol,
|
||||
len(received.Raw))
|
||||
packet.Protocol,
|
||||
len(packet.Raw))
|
||||
|
||||
engine := schema.Query(ctx)
|
||||
station := new(schema.APRSStation)
|
||||
@@ -198,10 +197,10 @@ func (c *Collector) processAPRSPacket(ctx context.Context, received *Packet) {
|
||||
return
|
||||
} else if has {
|
||||
cols := []string{"last_heard_at"}
|
||||
station.LastHeardAt = received.Time
|
||||
station.LastHeardAt = packet.Time
|
||||
if decoded.Latitude != 0 {
|
||||
station.LastLatitude = sql.NullFloat64{Float64: decoded.Latitude, Valid: true}
|
||||
station.LastLongitude = sql.NullFloat64{Float64: decoded.Longitude, Valid: true}
|
||||
station.LastLatitude = &decoded.Latitude
|
||||
station.LastLongitude = &decoded.Longitude
|
||||
cols = append(cols, "last_latitude", "last_longitude")
|
||||
}
|
||||
if _, err = engine.ID(station.ID).Cols(cols...).Update(station); err != nil {
|
||||
@@ -212,18 +211,12 @@ func (c *Collector) processAPRSPacket(ctx context.Context, received *Packet) {
|
||||
station = &schema.APRSStation{
|
||||
Call: strings.ToUpper(decoded.Source.String()),
|
||||
Symbol: decoded.Symbol,
|
||||
FirstHeardAt: received.Time,
|
||||
LastHeardAt: received.Time,
|
||||
FirstHeardAt: packet.Time,
|
||||
LastHeardAt: packet.Time,
|
||||
}
|
||||
if decoded.Latitude != 0 {
|
||||
station.LastLatitude = sql.NullFloat64{
|
||||
Float64: decoded.Latitude,
|
||||
Valid: decoded.Latitude != 0,
|
||||
}
|
||||
station.LastLongitude = sql.NullFloat64{
|
||||
Float64: decoded.Longitude,
|
||||
Valid: decoded.Longitude != 0,
|
||||
}
|
||||
station.LastLatitude = &decoded.Latitude
|
||||
station.LastLongitude = &decoded.Longitude
|
||||
}
|
||||
if station.ID, err = engine.Insert(station); err != nil {
|
||||
Logger.Warnf("collector: can't insert APRS station: %v", err)
|
||||
@@ -231,7 +224,7 @@ func (c *Collector) processAPRSPacket(ctx context.Context, received *Packet) {
|
||||
}
|
||||
}
|
||||
|
||||
packet := &schema.APRSPacket{
|
||||
save := &schema.APRSPacket{
|
||||
RadioID: radio.ID,
|
||||
StationID: station.ID,
|
||||
Source: station.Call,
|
||||
@@ -239,20 +232,15 @@ func (c *Collector) processAPRSPacket(ctx context.Context, received *Packet) {
|
||||
Path: decoded.Path.String(),
|
||||
Comment: decoded.Comment,
|
||||
Symbol: string(decoded.Symbol[:]),
|
||||
Raw: string(received.Raw),
|
||||
Raw: string(packet.Raw),
|
||||
ReceivedAt: packet.Time,
|
||||
}
|
||||
if decoded.Latitude != 0 {
|
||||
packet.Latitude = sql.NullFloat64{
|
||||
Float64: decoded.Latitude,
|
||||
Valid: decoded.Latitude != 0,
|
||||
}
|
||||
packet.Longitude = sql.NullFloat64{
|
||||
Float64: decoded.Longitude,
|
||||
Valid: decoded.Longitude != 0,
|
||||
}
|
||||
save.Latitude = &decoded.Latitude
|
||||
save.Longitude = &decoded.Longitude
|
||||
}
|
||||
|
||||
if _, err = engine.Insert(packet); err != nil {
|
||||
if _, err = engine.Insert(save); err != nil {
|
||||
Logger.Warnf("collector: can't insert APRS packet: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -287,22 +275,20 @@ func (c *Collector) processMeshCorePacket(ctx context.Context, packet *Packet) {
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
save = &schema.MeshCorePacket{
|
||||
RadioID: radio.ID,
|
||||
SNR: packet.SNR,
|
||||
RSSI: packet.RSSI,
|
||||
RouteType: uint8(parsed.RouteType),
|
||||
PayloadType: uint8(parsed.PayloadType),
|
||||
Version: parsed.Version,
|
||||
Hash: hex.EncodeToString(parsed.Hash()),
|
||||
Path: parsed.Path,
|
||||
Payload: parsed.Payload,
|
||||
ChannelHash: channelHash,
|
||||
Raw: packet.Raw,
|
||||
ReceivedAt: packet.Time,
|
||||
}
|
||||
)
|
||||
save := &schema.MeshCorePacket{
|
||||
RadioID: radio.ID,
|
||||
SNR: packet.SNR,
|
||||
RSSI: packet.RSSI,
|
||||
RouteType: uint8(parsed.RouteType),
|
||||
PayloadType: uint8(parsed.PayloadType),
|
||||
Version: parsed.Version,
|
||||
Hash: hex.EncodeToString(parsed.Hash()),
|
||||
Path: parsed.Path,
|
||||
Payload: parsed.Payload,
|
||||
ChannelHash: channelHash,
|
||||
Raw: packet.Raw,
|
||||
ReceivedAt: packet.Time,
|
||||
}
|
||||
if _, err = schema.Query(ctx).Insert(save); err != nil {
|
||||
Logger.Warnf("collector: error storing packet: %v", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user