build script tweaks and fixes to TUI client. Succesfully displays only changed device info

main
Keegan 3 years ago
parent 602c515205
commit 588ecc8275

@ -1,14 +1,24 @@
#!/bin/bash #!/bin/bash
echo "Purging old builds"
rm -v bin/* 2>/dev/null
echo "Removing Logs"
rm -v bin/log/* 2>/dev/null
echo "Building reactor binaries"
env GOOS=linux GOARCH=arm GOARM=7 go build -o bin/reactor_linux_arm cmd/reactor/main.go env GOOS=linux GOARCH=arm GOARM=7 go build -o bin/reactor_linux_arm cmd/reactor/main.go
env GOOS=linux GOARCH=arm64 go build -o bin/reactor_linux_arm64 cmd/reactor/main.go env GOOS=linux GOARCH=arm64 go build -o bin/reactor_linux_arm64 cmd/reactor/main.go
echo "Building tui binaries"
env GOOS=linux GOARCH=arm GOARM=7 go build -o bin/tui_linux_arm cmd/tui/main.go env GOOS=linux GOARCH=arm GOARM=7 go build -o bin/tui_linux_arm cmd/tui/main.go
env GOOS=linux GOARCH=arm64 go build -o bin/tui_linux_arm64 cmd/tui/main.go env GOOS=linux GOARCH=arm64 go build -o bin/tui_linux_arm64 cmd/tui/main.go
env GOOS=linux GOARCH=amd64 go build -o bin/tui_linux_amd64 cmd/tui/main.go env GOOS=linux GOARCH=amd64 go build -o bin/tui_linux_amd64 cmd/tui/main.go
echo "Building server binary"
env GOOS=linux GOARCH=amd64 go build -o bin/server_linux_amd64 cmd/server/main.go env GOOS=linux GOARCH=amd64 go build -o bin/server_linux_amd64 cmd/server/main.go
tar -czvf pireactor.tar.gz -C bin reactor_linux_arm64 echo "Compressing binaries for distrubution"
tar -czvf bbreactor.tar.gz -C bin reactor_linux_arm tar -czf pireactor.tar.gz -C bin reactor_linux_arm64
tar -czvf server.tar.gz -C bin server_linux_amd64 tar -czf bbreactor.tar.gz -C bin reactor_linux_arm
tar -czvf tui.tar.gz -C bin tui_linux_amd64 tui_linux_arm tui_linux_arm64 tar -czf server.tar.gz -C bin server_linux_amd64
tar -czf tui.tar.gz -C bin tui_linux_amd64 tui_linux_arm tui_linux_arm64

@ -9,6 +9,13 @@ import (
"strconv" "strconv"
) )
func getLogType() string {
if t, ok := os.LookupEnv("LOGTYPE"); ok {
return t
}
return "DEFAULT"
}
func getVerbosity() int { func getVerbosity() int {
v := os.Getenv("VERBOSE") v := os.Getenv("VERBOSE")
level := 0 level := 0
@ -16,7 +23,7 @@ func getVerbosity() int {
var err error var err error
level, err = strconv.Atoi(v) level, err = strconv.Atoi(v)
if err != nil { if err != nil {
log.Fatalf("Invalid Vverboity %v", v) log.Fatalf("Invalid Verbosity %v", v)
} }
} }
return level return level
@ -39,6 +46,7 @@ var debugStart time.Time
var debugVerbosity int var debugVerbosity int
func init() { func init() {
debugVerbosity = getVerbosity() debugVerbosity = getVerbosity()
debugStart = time.Now() debugStart = time.Now()
if debugVerbosity > 0 { if debugVerbosity > 0 {
@ -50,8 +58,9 @@ func init() {
os.Exit(1) os.Exit(1)
} }
} }
filename := time.Now().Format("01-02T15:04:05") logtype := getLogType() // start with "REACTOR" etc
filename += ".log" timestamp := time.Now().Format("Tue 15:04:05")
filename := fmt.Sprintf("%s %s.log", logtype, timestamp)
f, err := os.OpenFile(path+filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664) f, err := os.OpenFile(path+filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

@ -111,15 +111,10 @@ func (r *ReactorManager) Monitor(conn *grpc.ClientConn) {
r.Exit() r.Exit()
break; break;
} }
//r.devstatus.Lock()
for _,v := range resp.GetDevices() { for _,v := range resp.GetDevices() {
d := &DeviceInfo{Id:uint32(v.GetAddr()),Type:v.GetType(),Status:v.GetStatus(),Data:v.GetData()} d := &DeviceInfo{Id:uint32(v.GetAddr()),Type:v.GetType(),Status:v.GetStatus(),Data:v.GetData()}
go r.UpdateDevice(d) go r.UpdateDevice(d)
//go r.DevsMon.Send(d)
//r.Devs[d.Id] = d
} }
//r.devstatus.Unlock()
logging.Debug(logging.DPing, "RMA %v Reactor Reached", r.Id)
time.Sleep(r.Hb) // time between sensor pings time.Sleep(r.Hb) // time between sensor pings
} }
} }

@ -2,7 +2,7 @@ package server
import ( import (
"sync" "sync"
"fmt" _ "fmt"
"FRMS/internal/pkg/logging" "FRMS/internal/pkg/logging"
) )
// allows for multiple readers/writers // allows for multiple readers/writers
@ -52,9 +52,9 @@ func NewStatusMonitor(t string, id uint32, sys *SystemViewer) *StatusMonitor {
} else { } else {
// tui status monitor // tui status monitor
sbuf := NewBuffer() sbuf := NewBuffer()
sm.ReactorChan, sbuf["Reactor"] = sys.AddListener(id,0) //sm.ReactorChan, sbuf["Reactor"] = sys.AddListener(id,0)
sm.DevBuf = &devbuf{Buf:sbuf} // makes it easier to work with sm.DevBuf = &devbuf{Buf:sbuf} // makes it easier to work with
go sm.Listen(sm.ReactorChan) go sm.UpdateListener(id,0)
} }
return sm return sm
} }
@ -70,7 +70,7 @@ func (s *StatusMonitor) GenerateIds() {
func (s *StatusMonitor) Send(d *DeviceInfo, dtype string) { func (s *StatusMonitor) Send(d *DeviceInfo, dtype string) {
d.TransactionId = <-s.TransactionId d.TransactionId = <-s.TransactionId
logging.Debug(logging.DClient,"SYS 01 Updating %s Info (%s, %s)", d.Type, d.Status, d.Data) logging.Debug(logging.DClient,"SYS 01 Sending update for: %s (%s)", d.Type, d.Status)
if dtype == "Reactor" { if dtype == "Reactor" {
s.ReactorChan <-d s.ReactorChan <-d
} else { } else {
@ -83,6 +83,9 @@ func (s *StatusMonitor) GetBuffer() []*DeviceInfo {
s.DevBuf.Lock() s.DevBuf.Lock()
defer s.DevBuf.Unlock() defer s.DevBuf.Unlock()
res := []*DeviceInfo{} res := []*DeviceInfo{}
if len(s.DevBuf.Buf["Reactor"]) != 0 || len(s.DevBuf.Buf["Device"]) != 0 {
logging.Debug(logging.DClient,"Clearing buff %v", s.DevBuf.Buf)
}
for _, devs := range s.DevBuf.Buf { for _, devs := range s.DevBuf.Buf {
for _, dev := range devs { for _, dev := range devs {
// loops over reactors then devices // loops over reactors then devices
@ -98,16 +101,18 @@ func (s *StatusMonitor) UpdateListener(tid, reactorId uint32) {
defer s.DevBuf.Unlock() defer s.DevBuf.Unlock()
// clearing proper buffer // clearing proper buffer
if reactorId == 0 { if reactorId == 0 {
s.DevBuf.Buf["Reactors"] = make(map[uint32]*DeviceInfo) logging.Debug(logging.DClient,"SYS 01 Adding %v as reactor listener", tid)
s.ReactorChan, s.DevBuf.Buf["Reactors"] = s.SystemViewer.AddListener(tid, reactorId) s.DevBuf.Buf["Reactor"] = make(map[uint32]*DeviceInfo)
s.ReactorChan, s.DevBuf.Buf["Reactor"] = s.SystemViewer.AddListener(tid, reactorId)
go s.Listen(s.ReactorChan) go s.Listen(s.ReactorChan)
} else { } else {
s.DevBuf.Buf["Devices"] = make(map[uint32]*DeviceInfo) // clearing old devices logging.Debug(logging.DClient,"SYS 01 Adding %v as reactor %v listener", tid, reactorId)
s.DevBuf.Buf["Device"] = make(map[uint32]*DeviceInfo) // clearing old devices
if s.DevBuf.ReactorId != reactorId && s.DevBuf.ReactorId != 0{ if s.DevBuf.ReactorId != reactorId && s.DevBuf.ReactorId != 0{
go s.SystemViewer.RemoveListener(s.DevBuf.ReactorId, tid) go s.SystemViewer.RemoveListener(s.DevBuf.ReactorId, tid)
} }
s.DevBuf.ReactorId = reactorId s.DevBuf.ReactorId = reactorId
s.DevChan, s.DevBuf.Buf["Devices"] = s.SystemViewer.AddListener(tid, reactorId) s.DevChan, s.DevBuf.Buf["Device"] = s.SystemViewer.AddListener(tid, reactorId)
go s.Listen(s.DevChan) go s.Listen(s.DevChan)
} }
} }
@ -115,19 +120,19 @@ func (s *StatusMonitor) UpdateListener(tid, reactorId uint32) {
func (s *StatusMonitor) UpdateBuffer(d *DeviceInfo, dtype string, ch chan *DeviceInfo) { func (s *StatusMonitor) UpdateBuffer(d *DeviceInfo, dtype string, ch chan *DeviceInfo) {
s.DevBuf.Lock() s.DevBuf.Lock()
defer s.DevBuf.Unlock() defer s.DevBuf.Unlock()
logging.Debug(logging.DClient,"SYS 01 Dev %v update requested", d)
if dev, exists := s.DevBuf.Buf[dtype][d.Id]; exists { if dev, exists := s.DevBuf.Buf[dtype][d.Id]; exists {
// already a device in the buffer // already a device in the buffer
fmt.Printf("Old: %v,\nNew: %v \n", dev, d)
if dev.TransactionId > d.TransactionId { if dev.TransactionId > d.TransactionId {
logging.Debug(logging.DClient,"SYS 01 Update Processed. Old: %v, New: %v \n", dev, d)
d = dev // not sure if i can do this lol d = dev // not sure if i can do this lol
fmt.Printf("Old: %v,\nNew: %v \n", dev, d)
} }
} }
if ch == s.ReactorChan || ch == s.DevChan { if ch == s.ReactorChan || ch == s.DevChan {
// hacky way to check if the device came from a listener of a current channel // hacky way to check if the device came from a listener of a current channel
s.DevBuf.Buf[dtype][d.Id] = d s.DevBuf.Buf[dtype][d.Id] = d
} else { } else {
fmt.Printf("%v out of date\n",d) logging.Debug(logging.DClient,"Dev out of date!")
} }
} }
@ -157,7 +162,6 @@ type listeners struct {
type lischan struct { type lischan struct {
sync.WaitGroup sync.WaitGroup
WaitChan chan struct{}
StatusChan chan *DeviceInfo StatusChan chan *DeviceInfo
} }
@ -167,6 +171,11 @@ type syslayout struct {
sync.RWMutex sync.RWMutex
} }
func NewLisChan(ch chan *DeviceInfo) *lischan {
l := &lischan{StatusChan:ch}
return l
}
func NewInfoStream() *InfoStream { func NewInfoStream() *InfoStream {
dch := make(chan *DeviceInfo) dch := make(chan *DeviceInfo)
s := &InfoStream{Stream:dch} s := &InfoStream{Stream:dch}
@ -186,15 +195,14 @@ func (s *InfoStream) AddSender() chan *DeviceInfo {
} }
func (s *InfoStream) Listen() { func (s *InfoStream) Listen() {
for { for deviceInfo := range s.Stream {
deviceInfo := <-s.Stream
go s.Update(deviceInfo) go s.Update(deviceInfo)
go s.Echo(deviceInfo)
} }
} }
func (s *InfoStream) Update(d *DeviceInfo) { func (s *InfoStream) Update(d *DeviceInfo) {
s.Layout.Lock() s.Layout.Lock()
defer s.Layout.Unlock()
if dev, ok := s.Layout.Devs[d.Id]; !ok { if dev, ok := s.Layout.Devs[d.Id]; !ok {
d.Index = s.Layout.uint32 d.Index = s.Layout.uint32
s.Layout.uint32 += 1 s.Layout.uint32 += 1
@ -222,14 +230,15 @@ func (s *InfoStream) AddListener(id uint32, ch chan *DeviceInfo) map[uint32]*Dev
// if i get a memory leak ill eat my shoe // if i get a memory leak ill eat my shoe
s.listeners.Lock() s.listeners.Lock()
defer s.listeners.Unlock() defer s.listeners.Unlock()
if lis, ok := s.listeners.Listeners[id]; ok { if _, ok := s.listeners.Listeners[id]; ok {
// exists // exists
go lis.Exit() go s.RemoveListener(id)
delete(s.listeners.Listeners,id)
} }
s.listeners.Listeners[id] = &lischan{StatusChan:ch} s.listeners.Listeners[id] = NewLisChan(ch)
s.Layout.RLock() logging.Debug(logging.DClient,"SYS 01 Getting Devices")
defer s.Layout.RUnlock() //s.Layout.RLock()
//defer s.Layout.RUnlock()
logging.Debug(logging.DClient,"SYS 01 Returning Devices %v", s.Layout.Devs)
return s.Layout.Devs return s.Layout.Devs
} }
@ -237,26 +246,14 @@ func (l *listeners) RemoveListener(id uint32) {
l.Lock() l.Lock()
defer l.Unlock() defer l.Unlock()
if lis, ok := l.Listeners[id]; ok { if lis, ok := l.Listeners[id]; ok {
go func(){
lis.Wait()
close(lis.WaitChan)
}()
go lis.Exit()
delete(l.Listeners,id) delete(l.Listeners,id)
go func(ls *lischan){
ls.Wait()
close(ls.StatusChan)
}(lis)
} }
} }
func (l *lischan) Exit() {
for {
select {
case <-l.WaitChan:
close(l.StatusChan)
break;
case <-l.StatusChan:
// dump buffer
}
}
}
// status buffer maintaince // status buffer maintaince
type SystemViewer struct { type SystemViewer struct {
@ -309,7 +306,7 @@ func (s *SystemViewer) AddListener(id, rid uint32) (chan *DeviceInfo, map[uint32
} }
} }
func (s *SystemViewer) RemoveListener(tid, rid uint32) { func (s *SystemViewer) RemoveListener(rid, tid uint32) {
// removes chan for specific tid and rid // removes chan for specific tid and rid
s.DeviceStream.Lock() s.DeviceStream.Lock()
defer s.DeviceStream.Unlock() defer s.DeviceStream.Unlock()

@ -123,6 +123,7 @@ func (t *TUIManager) GetDevices(ctx context.Context, req *pb.GetDevicesRequest)
devices := []*pb.Dev{} devices := []*pb.Dev{}
resp := &pb.GetDevicesResponse{ClientId:t.Id,Devices:devices} resp := &pb.GetDevicesResponse{ClientId:t.Id,Devices:devices}
if req.GetReactorId() > 0 || req.GetRefresh() { if req.GetReactorId() > 0 || req.GetRefresh() {
logging.Debug(logging.DClient,"TMA %v client requested devs from %v",t.Id,req.GetReactorId())
resp.ReactorId = req.GetReactorId() resp.ReactorId = req.GetReactorId()
t.StatusMon.UpdateListener(t.Id, req.GetReactorId()) t.StatusMon.UpdateListener(t.Id, req.GetReactorId())
} }
@ -131,7 +132,9 @@ func (t *TUIManager) GetDevices(ctx context.Context, req *pb.GetDevicesRequest)
for _, v := range devs { for _, v := range devs {
resp.Devices = append(resp.Devices, &pb.Dev{Id:v.Id,Type:v.Type,Status:v.Status,Data:v.Data,Index:v.Index}) resp.Devices = append(resp.Devices, &pb.Dev{Id:v.Id,Type:v.Type,Status:v.Status,Data:v.Data,Index:v.Index})
} }
if len(resp.Devices) > 0 {
logging.Debug(logging.DClient,"TMA %v sending %v devices to client" ,t.Id, len(resp.Devices)) logging.Debug(logging.DClient,"TMA %v sending %v devices to client" ,t.Id, len(resp.Devices))
}
return resp, nil return resp, nil
} }

@ -144,7 +144,7 @@ func NewDisplay(rc,dc chan uint32) *Display {
d := &Display{} d := &Display{}
d.App = tview.NewApplication() d.App = tview.NewApplication()
d.Flex = tview.NewFlex() d.Flex = tview.NewFlex()
d.DeviceList = tview.NewList() d.DeviceList = tview.NewList().SetSelectedFocusOnly(true)
d.ReactorList = tview.NewList() d.ReactorList = tview.NewList()
d.ReactorList.AddItem("Refresh","Press (r) to refresh manually", 114, nil) d.ReactorList.AddItem("Refresh","Press (r) to refresh manually", 114, nil)
d.ReactorList.AddItem("Quit","Press (q) to quit",113,func() { d.ReactorList.AddItem("Quit","Press (q) to quit",113,func() {
@ -188,6 +188,7 @@ func (d *Display) DisplayDevices(devs map[uint32]*Device) {
// going to clear on every reactor selection to simplify // going to clear on every reactor selection to simplify
// can probably just load from SM to save system resources on spam reloading // can probably just load from SM to save system resources on spam reloading
for _, dev := range devs { for _, dev := range devs {
logging.Debug(logging.DClient,"Displaying device %v",dev)
txt := fmt.Sprintf("0x%x %v %v",dev.Id,dev.Status,dev.Type) txt := fmt.Sprintf("0x%x %v %v",dev.Id,dev.Status,dev.Type)
indx := int(dev.Index) indx := int(dev.Index)
for indx >= d.DeviceList.GetItemCount() { for indx >= d.DeviceList.GetItemCount() {

Loading…
Cancel
Save