Add learned tgs

This commit is contained in:
Daniel 2024-07-28 01:08:08 -04:00
parent 4c522ff2b8
commit 1a41cf5b6a
4 changed files with 30 additions and 28 deletions

View file

@ -68,12 +68,12 @@ type System struct {
} }
type Talkgroup struct { type Talkgroup struct {
SystemID int `json:"system_id"` SystemID int `json:"system_id"`
Tgid int `json:"tgid"` Tgid int `json:"tgid"`
Name *string `json:"name"` Name *string `json:"name"`
Frequency *int32 `json:"frequency"` TgGroup *string `json:"tg_group"`
AutoCreated *bool `json:"auto_created"` Frequency *int32 `json:"frequency"`
Metadata []byte `json:"metadata"` Metadata []byte `json:"metadata"`
} }
type TalkgroupsLearned struct { type TalkgroupsLearned struct {
@ -82,6 +82,7 @@ type TalkgroupsLearned struct {
Tgid int `json:"tgid"` Tgid int `json:"tgid"`
GroupName string `json:"group_name"` GroupName string `json:"group_name"`
GroupTag *string `json:"group_tag"` GroupTag *string `json:"group_tag"`
Ignored *bool `json:"ignored"`
} }
type TalkgroupsTag struct { type TalkgroupsTag struct {

View file

@ -16,8 +16,8 @@ import (
) )
type callUploadRequest struct { type callUploadRequest struct {
Audio []byte `form:"audio"` Audio []byte `form:"audio"`
AudioName string `form:"audioName"` AudioName string
AudioType string `form:"audioType"` AudioType string `form:"audioType"`
DateTime time.Time `form:"dateTime"` DateTime time.Time `form:"dateTime"`
Frequencies []int `form:"frequencies"` Frequencies []int `form:"frequencies"`
@ -83,15 +83,16 @@ func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) {
return return
} }
if r.Form.Get("test") == "1" { if strings.Trim(r.Form.Get("test"), "\r\n") == "1" {
w.Write([]byte("Test succeeded")) // fudge the official response
http.Error(w, "incomplete call data: no talkgroup", http.StatusExpectationFailed)
return return
} }
call := new(callUploadRequest) call := new(callUploadRequest)
err = call.fill(r) err = call.fill(r)
if err != nil { if err != nil {
http.Error(w, "cannot bind upload "+err.Error(), 500) http.Error(w, "cannot bind upload "+err.Error(), http.StatusExpectationFailed)
return return
} }
@ -102,6 +103,8 @@ func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Write([]byte("Call imported successfully."))
_ = dbCall _ = dbCall
} }
@ -112,14 +115,18 @@ func (car *callUploadRequest) fill(r *http.Request) error {
for i := 0; i < rv.NumField(); i++ { for i := 0; i < rv.NumField(); i++ {
f := rv.Field(i) f := rv.Field(i)
fi := f.Interface() fi := f.Interface()
formField := rt.Field(i).Tag.Get("form") formField, has := rt.Field(i).Tag.Lookup("form")
if !has {
continue
}
switch v := fi.(type) { switch v := fi.(type) {
case []byte: case []byte:
file, _, err := r.FormFile(formField) file, hdr, err := r.FormFile(formField)
if err != nil { if err != nil {
return fmt.Errorf("get form file: %w", err) return fmt.Errorf("get form file: %w", err)
} }
car.AudioName = hdr.Filename
audioBytes, err := io.ReadAll(file) audioBytes, err := io.ReadAll(file)
if err != nil { if err != nil {
return fmt.Errorf("file read: %w", err) return fmt.Errorf("file read: %w", err)

View file

@ -1,8 +1,6 @@
package server package server
import ( import (
"fmt"
"io"
"net/http" "net/http"
"time" "time"
@ -16,7 +14,6 @@ import (
func (s *Server) setupRoutes() { func (s *Server) setupRoutes() {
r := s.r r := s.r
r.Use(middleware.Recoverer)
r.Use(middleware.WithValue(database.DBCTXKeyValue, s.db)) r.Use(middleware.WithValue(database.DBCTXKeyValue, s.db))
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
@ -28,7 +25,6 @@ func (s *Server) setupRoutes() {
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
r.Use(rateLimiter()) r.Use(rateLimiter())
r.Use(render.SetContentType(render.ContentTypeJSON)) r.Use(render.SetContentType(render.ContentTypeJSON))
// r.Use(teeRequest())
// public routes // public routes
r.Post("/auth", s.routeAuth) r.Post("/auth", s.routeAuth)
r.Post("/api/call-upload", s.routeCallUpload) r.Post("/api/call-upload", s.routeCallUpload)
@ -44,13 +40,6 @@ func (s *Server) setupRoutes() {
}) })
} }
func teeRequest() func(http.Handler) http.Handler {
return middleware.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
by, err := io.ReadAll(r.Body)
fmt.Println(string(by), err)
}))
}
func rateLimiter() func(http.Handler) http.Handler { func rateLimiter() func(http.Handler) http.Handler {
return httprate.LimitByRealIP(100, 1*time.Minute) return httprate.LimitByRealIP(100, 1*time.Minute)
} }

View file

@ -27,8 +27,8 @@ CREATE TABLE IF NOT EXISTS talkgroups(
system_id INTEGER REFERENCES systems(id) NOT NULL, system_id INTEGER REFERENCES systems(id) NOT NULL,
tgid INTEGER, tgid INTEGER,
name TEXT, name TEXT,
tg_group TEXT,
frequency INTEGER, frequency INTEGER,
auto_created BOOLEAN,
metadata JSONB, metadata JSONB,
PRIMARY KEY (system_id, tgid) PRIMARY KEY (system_id, tgid)
); );
@ -39,7 +39,8 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned(
tgid INTEGER NOT NULL, tgid INTEGER NOT NULL,
group_name TEXT NOT NULL, group_name TEXT NOT NULL,
group_tag TEXT, group_tag TEXT,
UNIQUE (system_id, tgid, group_name, group_tag) ignored BOOLEAN,
UNIQUE (system_id, tgid, group_name)
); );
CREATE OR REPLACE FUNCTION learn_talkgroup() CREATE OR REPLACE FUNCTION learn_talkgroup()
@ -47,18 +48,22 @@ RETURNS TRIGGER AS $$
BEGIN BEGIN
IF NOT EXISTS (SELECT * IF NOT EXISTS (SELECT *
FROM talkgroups FROM talkgroups
JOIN talkgroups_tags LEFT JOIN talkgroups_tags
ON talkgroups_tags.system_id = talkgroups.system_id AND talkgroups_tags.talkgroup_id = talkgroups.tgid ON talkgroups_tags.system_id = talkgroups.system_id AND talkgroups_tags.talkgroup_id = talkgroups.tgid
LEFT JOIN talkgroups_learned
ON talkgroups_learned.system_id = talkgroups.system_id AND talkgroups_learned.tgid = talkgroups.tgid
WHERE WHERE
talkgroups.system_id = NEW.system AND talkgroups.tgid = NEW.talkgroup AND talkgroups.system_id = NEW.system AND talkgroups.tgid = NEW.talkgroup AND
( (
talkgroups.name != NEW.tg_label talkgroups.name != NEW.tg_label
OR NOT (talkgroups_tags.tags @> ARRAY[NEW.tg_tag]) OR NOT (talkgroups_tags.tags @> ARRAY[NEW.tg_tag])
OR talkgroups.tg_group != NEW.tg_group
) )
AND talkgroups_learned.ignored IS NOT TRUE
) THEN ) THEN
INSERT INTO talkgroups_learned(system_id, tgid, group_name, group_tag) VALUES( INSERT INTO talkgroups_learned(system_id, tgid, group_name, group_tag) VALUES(
NEW.system, NEW.talkgroup, NEW.tg_label, NEW.tg_tag NEW.system, NEW.talkgroup, NEW.tg_label, NEW.tg_tag
) ON CONFLICT DO NOTHING; ) ON CONFLICT (system_id, tgid, group_name) DO UPDATE SET group_tag = EXCLUDED.group_tag;
END IF; END IF;
RETURN NEW; RETURN NEW;
END END