@ -5,9 +5,11 @@ import (
"log"
"log"
"sync"
"sync"
"strconv"
"strconv"
"strings"
"time"
"time"
"github.com/rivo/tview"
"github.com/rivo/tview"
_ "github.com/gdamore/tcell/v2"
_ "github.com/gdamore/tcell/v2"
"os"
)
)
type Device struct {
type Device struct {
@ -27,14 +29,14 @@ type TUI struct {
Err chan error
Err chan error
}
}
func NewTUI ( ip string , port int , ch chan error ) * TUI {
func NewTUI ( ip string , port int , ifconfig string , ch chan error ) * TUI {
//r := make(map[uint32]*Reactor)
//r := make(map[uint32]*Reactor)
t := & TUI { }
t := & TUI { }
//l := new(LocalView)
//l := new(LocalView)
//l.Reactors = r
//l.Reactors = r
//t.LocalView = l
//t.LocalView = l
t . Err = ch
t . Err = ch
client := NewTUIClient ( ip , port )
client := NewTUIClient ( ip , port , ifconfig )
t . TUIClient = client
t . TUIClient = client
return t
return t
}
}
@ -74,16 +76,11 @@ func (t *TUI) Monitor() {
select {
select {
case reactor := <- t . SelectedReactor :
case reactor := <- t . SelectedReactor :
// reactor has been selected in tui, grabbing devs
// reactor has been selected in tui, grabbing devs
fmt . Printf ( "ReactorSelected %v\n" , reactor )
t . App . QueueUpdateDraw ( func ( ) {
t . App . QueueUpdateDraw ( func ( ) {
t . UpdateDevices ( reactor )
t . UpdateDevices ( reactor )
} )
} )
<< << << < HEAD
case <- t . SelectedDevice :
case device : <- t . SelectedDevice :
fmt . Println ( "DeviceSelected" )
// TODO
// TODO
== == == =
>> >> >> > 03 b99d8d16e9ca33d05553b749613e30ab9be204
case <- timer :
case <- timer :
// time to ping for status
// time to ping for status
t . App . QueueUpdateDraw ( func ( ) {
t . App . QueueUpdateDraw ( func ( ) {
@ -96,6 +93,15 @@ func (t *TUI) Monitor() {
func ( t * TUI ) UpdateDevices ( r ... uint32 ) {
func ( t * TUI ) UpdateDevices ( r ... uint32 ) {
// get devices for the reactor and update the tui
// get devices for the reactor and update the tui
var id uint32
var id uint32
// see if there is a page being displayed
if name , _ := t . Display . DevicePages . GetFrontPage ( ) ; name != "" {
if tmp , err := strconv . ParseUint ( name , 10 , 32 ) ; err != nil {
log . Fatal ( err )
} else {
id = uint32 ( tmp )
}
}
// overwrite if called as a func
if len ( r ) > 0 {
if len ( r ) > 0 {
id = r [ 0 ]
id = r [ 0 ]
}
}
@ -103,7 +109,7 @@ func (t *TUI) UpdateDevices(r ...uint32) {
if err != nil {
if err != nil {
log . Fatal ( err )
log . Fatal ( err )
}
}
if len ( r ) > 0 {
if id != 0 {
// reactor specificed split devs
// reactor specificed split devs
reactors := make ( map [ uint32 ] * Device )
reactors := make ( map [ uint32 ] * Device )
devices := make ( map [ uint32 ] * Device )
devices := make ( map [ uint32 ] * Device )
@ -139,9 +145,14 @@ func NewDisplay(rc,dc chan uint32) *Display {
d . Flex = tview . NewFlex ( )
d . Flex = tview . NewFlex ( )
lists := make ( map [ string ] * tview . List )
lists := make ( map [ string ] * tview . List )
d . DeviceList = lists
d . DeviceList = lists
d . ReactorList = tview . NewList ( ) . ShowSecondaryText ( false )
d . ReactorList = tview . NewList ( ) //.ShowSecondaryText(false)
d . ReactorList . AddItem ( "Quit" , "Press (q) to quit" , 113 , func ( ) {
d . App . Stop ( )
os . Exit ( 0 )
} )
d . DevicePages = tview . NewPages ( )
d . DevicePages = tview . NewPages ( )
d . ReactorList . SetTitle ( "Reactors" ) . SetBorder ( true )
d . ReactorList . SetTitle ( "Reactors" ) . SetBorder ( true )
d . ReactorList . SetSelectedFunc ( d . SelectReactor )
d . DevicePages . SetTitle ( "Devices" ) . SetBorder ( true )
d . DevicePages . SetTitle ( "Devices" ) . SetBorder ( true )
d . SelectedReactor = rc
d . SelectedReactor = rc
d . SelectedDevice = dc
d . SelectedDevice = dc
@ -160,49 +171,53 @@ func (d *Display) DisplayReactors(r map[uint32]*Device) {
//d.Lock()
//d.Lock()
//defer d.Unlock()
//defer d.Unlock()
// locking may break the hell out of this gonna trust tview
// locking may break the hell out of this gonna trust tview
for id , reactor := range r {
for _ , reactor := range r {
txt := fmt . Sprintf ( "%v %v %v ", reactor . Id , reactor . Status , reactor . Data )
txt := fmt . Sprintf ( "%v %v ", reactor . Id , reactor . Status )
if d . ReactorList . GetItemCount ( ) > int ( reactor . Index ) {
if d . ReactorList . GetItemCount ( ) > int ( reactor . Index ) + 1 {
d . ReactorList . RemoveItem ( int ( reactor . Index ) )
d . ReactorList . RemoveItem ( int ( reactor . Index ) )
}
}
d . ReactorList . InsertItem ( int ( reactor . Index ) , txt , string ( id ) , rune ( 49 + reactor . Index ) , nil )
d . ReactorList . InsertItem ( int ( reactor . Index ) , txt , reactor . Data , rune ( 49 + reactor . Index ) , nil )
}
}
}
}
func ( d * Display ) DisplayDevices ( devs map [ uint32 ] * Device , rid uint32 ) {
func ( d * Display ) DisplayDevices ( devs map [ uint32 ] * Device , rid uint32 ) {
//d.Lock()
//d.Lock()
reactorPage := string ( rid )
reactorPage := strconv . FormatUint ( uint64 ( rid ) , 10 )
var reactorList * tview . List
var reactorList * tview . List
if reactorList , ok := d . DeviceList [ reactorPage ] ; ! ok {
var ok bool
reactorList = tview . NewList ( )
if reactorList , ok = d . DeviceList [ reactorPage ] ; ! ok {
reactorList = tview . NewList ( ) . ShowSecondaryText ( false )
d . DeviceList [ reactorPage ] = reactorList
d . DeviceList [ reactorPage ] = reactorList
d . DevicePages . AddPage ( reactorPage , reactorList , true , false )
}
}
//d.Unlock()
//d.Unlock()
for _ , dev := range devs {
for _ , dev := range devs {
txt := fmt . Sprintf ( " %v %v at %x%x", dev . Type , dev . Status , dev . Id , dev . Data ) // sensor alive at 0x0 data
txt := fmt . Sprintf ( " 0x%x %v %v", dev . Id , dev . Status , dev . Type ) // sensor alive at 0x0 data
if reactorList . GetItemCount ( ) > int ( dev . Index ) {
if reactorList . GetItemCount ( ) > int ( dev . Index ) {
reactorList . RemoveItem ( int ( dev . Index ) )
reactorList . RemoveItem ( int ( dev . Index ) )
}
}
reactorList . InsertItem ( int ( dev . Index ) , txt , string ( dev . Id ) , rune ( 49 + dev . Index ) , nil )
reactorList . InsertItem ( int ( dev . Index ) , txt , dev . Data , 0 , nil )
}
}
d . DevicePages . SwitchToPage ( reactorPage )
d . DevicePages . SwitchToPage ( reactorPage )
}
}
func ( d * Display ) SelectReactor ( index int , main , i d string , r rune ) {
func ( d * Display ) SelectReactor ( index int , main , data string , r rune ) {
// called when reactor in list in selected
// called when reactor in list in selected
fmt . Println ( "SELECTED" )
if main != "Quit" {
if id , err := strconv . Atoi ( id ) ; err != nil {
maintxt := strings . Split ( main , " " )
log . Fatal ( err )
id := maintxt [ 0 ]
} else {
if id , err := strconv . ParseUint ( id , 10 , 32 ) ; err != nil {
d . SelectedReactor <- uint32 ( id )
log . Fatal ( err )
} else {
d . SelectedReactor <- uint32 ( id )
}
}
}
}
}
func ( d * Display ) SelectDevice ( index int , main , id string , r rune ) {
func ( d * Display ) SelectDevice ( index int , main , id string , r rune ) {
// called when device is selected in sub menu
// called when device is selected in sub menu
fmt . Println ( "SELECTED DEV" )
if id , err := strconv . ParseUint ( id , 10 , 32 ) ; err != nil {
if id , err := strconv . Atoi ( id ) ; err != nil {
log . Fatal ( err )
log . Fatal ( err )
} else {
} else {
d . SelectedDevice <- uint32 ( id )
d . SelectedDevice <- uint32 ( id )