add http, store skylab defintions in generated file as string

This commit is contained in:
saji 2023-06-23 16:20:28 -05:00
parent 3373eb3d06
commit aa0b61405b
7 changed files with 64 additions and 45 deletions

View file

@ -3,7 +3,6 @@ package cli
import ( import (
"fmt" "fmt"
imgui "github.com/AllenDang/cimgui-go"
"github.com/kschamplin/gotelem" "github.com/kschamplin/gotelem"
"github.com/kschamplin/gotelem/mprpc" "github.com/kschamplin/gotelem/mprpc"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -31,14 +30,8 @@ Connects to a gotelem server or relay. Can be used to
Action: client, Action: client,
} }
func loop() {
imgui.ShowDemoWindow()
}
func client(ctx *cli.Context) error { func client(ctx *cli.Context) error {
backend := imgui.CreateBackend()
backend.CreateWindow("hi there", 1200, 900, 0)
backend.Run(loop)
return nil return nil
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net" "net"
"net/http"
"os" "os"
"sync" "sync"
"time" "time"
@ -51,9 +52,10 @@ type service interface {
// can be extended on certain platforms (see cli/socketcan.go) // can be extended on certain platforms (see cli/socketcan.go)
// or if certain features are present (see cli/sqlite.go) // or if certain features are present (see cli/sqlite.go)
var serveThings = []service{ var serveThings = []service{
&XBeeService{}, &xBeeService{},
&CanLoggerService{}, &canLoggerService{},
&rpcService{}, &rpcService{},
&httpService{},
} }
func serve(cCtx *cli.Context) error { func serve(cCtx *cli.Context) error {
@ -141,17 +143,17 @@ func handleCon(conn net.Conn, broker *gotelem.JBroker, l *slog.Logger, done <-ch
// this spins up a new can socket on vcan0 and broadcasts a packet every second. for testing. // this spins up a new can socket on vcan0 and broadcasts a packet every second. for testing.
type CanLoggerService struct { type canLoggerService struct {
} }
func (c *CanLoggerService) String() string { func (c *canLoggerService) String() string {
return "CanLoggerService" return "CanLoggerService"
} }
func (c *CanLoggerService) Status() { func (c *canLoggerService) Status() {
} }
func (c *CanLoggerService) Start(cCtx *cli.Context, broker *gotelem.JBroker, l *slog.Logger) (err error) { func (c *canLoggerService) Start(cCtx *cli.Context, broker *gotelem.JBroker, l *slog.Logger) (err error) {
rxCh, err := broker.Subscribe("canDump") rxCh, err := broker.Subscribe("canDump")
if err != nil { if err != nil {
return err return err
@ -182,19 +184,19 @@ func (c *CanLoggerService) Start(cCtx *cli.Context, broker *gotelem.JBroker, l *
} }
} }
// XBeeService provides data over an Xbee device, either by serial or TCP // xBeeService provides data over an Xbee device, either by serial or TCP
// based on the url provided in the xbee flag. see the description for details. // based on the url provided in the xbee flag. see the description for details.
type XBeeService struct { type xBeeService struct {
session *xbee.Session session *xbee.Session
} }
func (x *XBeeService) String() string { func (x *xBeeService) String() string {
return "hello" return "hello"
} }
func (x *XBeeService) Status() { func (x *xBeeService) Status() {
} }
func (x *XBeeService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *slog.Logger) (err error) { func (x *xBeeService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *slog.Logger) (err error) {
if cCtx.String("xbee") == "" { if cCtx.String("xbee") == "" {
logger.Info("not using xbee") logger.Info("not using xbee")
return return
@ -233,3 +235,22 @@ func (x *XBeeService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *
} }
} }
type httpService struct {
}
func (h *httpService) String() string {
return "HttpService"
}
func (h *httpService) Status() {
}
func (h *httpService) Start(cCtx *cli.Context, broker *gotelem.JBroker, logger *slog.Logger) (err error) {
r := gotelem.TelemRouter(logger)
http.ListenAndServe(":8080", r)
return
}

1
go.mod
View file

@ -3,7 +3,6 @@ module github.com/kschamplin/gotelem
go 1.20 go 1.20
require ( require (
github.com/AllenDang/cimgui-go v0.0.0-20230502145512-97518c13c52b
github.com/go-chi/chi/v5 v5.0.8 github.com/go-chi/chi/v5 v5.0.8
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/mattn/go-sqlite3 v1.14.16 github.com/mattn/go-sqlite3 v1.14.16

2
go.sum
View file

@ -1,5 +1,3 @@
github.com/AllenDang/cimgui-go v0.0.0-20230502145512-97518c13c52b h1:EpLVWHcO5auIf8X0STrno4WAWThu6s6QgJ1ywJa5Tmo=
github.com/AllenDang/cimgui-go v0.0.0-20230502145512-97518c13c52b/go.mod h1:iNfbIyOBN8k3XScMxULbrwYbPsXEAUD0Jb6UwrspQb8=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0= github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=

View file

@ -5,6 +5,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -17,36 +18,36 @@ import (
// SkylabFile is a yaml file from skylab. // SkylabFile is a yaml file from skylab.
type SkylabFile struct { type SkylabFile struct {
Packets []PacketDef Packets []PacketDef `json:"packets"`
Boards []BoardSpec Boards []BoardDef `json:"boards"`
} }
type BoardSpec struct { type BoardDef struct {
Name string Name string `json:"name"`
Transmit []string Transmit []string `json:"transmit"`
Recieve []string Receive []string `json:"receive"`
} }
// data field. // data field.
type DataField struct { type FieldDef struct {
Name string Name string `json:"name"`
Type string Type string `json:"type"`
Units string // mostly for documentation Units string `json:"units"`
Conversion float32 Conversion float32 `json:"conversion"`
Bits []struct { Bits []struct {
Name string Name string `json:"name"`
} } `json:"bits"`
} }
// a PacketDef is a full can packet. // a PacketDef is a full can packet.
type PacketDef struct { type PacketDef struct {
Name string Name string `json:"name"`
Description string Description string `json:"description"`
Id uint32 Id uint32 `json:"id"`
BigEndian bool Endian string `json:"endian"`
Repeat int Repeat int `json:"repeat"`
Offset int Offset int `json:"offset"`
Data []DataField Data []FieldDef `json:"data"`
} }
// we need to generate bitfield types. // we need to generate bitfield types.
@ -83,7 +84,7 @@ func MapType(ctype string) string {
return typeMap[ctype] return typeMap[ctype]
} }
func (d *DataField) ToStructMember(parentName string) string { func (d *FieldDef) ToStructMember(parentName string) string {
if d.Type == "bitfield" { if d.Type == "bitfield" {
bfStructName := parentName + toCamelInitCase(d.Name, true) bfStructName := parentName + toCamelInitCase(d.Name, true)
@ -93,7 +94,7 @@ func (d *DataField) ToStructMember(parentName string) string {
} }
} }
func (d *DataField) MakeMarshal(offset int) string { func (d *FieldDef) MakeMarshal(offset int) string {
fieldName := toCamelInitCase(d.Name, true) fieldName := toCamelInitCase(d.Name, true)
if d.Type == "uint8_t" || d.Type == "int8_t" { if d.Type == "uint8_t" || d.Type == "int8_t" {
@ -117,7 +118,7 @@ func (d *DataField) MakeMarshal(offset int) string {
return "panic(\"failed to do it\")\n" return "panic(\"failed to do it\")\n"
} }
func (d *DataField) MakeUnmarshal(offset int) string { func (d *FieldDef) MakeUnmarshal(offset int) string {
fieldName := toCamelInitCase(d.Name, true) fieldName := toCamelInitCase(d.Name, true)
if d.Type == "uint8_t" || d.Type == "int8_t" { if d.Type == "uint8_t" || d.Type == "int8_t" {
@ -316,6 +317,7 @@ func main() {
"strJoin": strJoin, "strJoin": strJoin,
"mapf": mapf, "mapf": mapf,
"maptype": MapType, "maptype": MapType,
"json": json.Marshal,
} }
tmpl, err := template.New("golang.go.tmpl").Funcs(fnMap).ParseGlob("templates/*.go.tmpl") tmpl, err := template.New("golang.go.tmpl").Funcs(fnMap).ParseGlob("templates/*.go.tmpl")

File diff suppressed because one or more lines are too long

View file

@ -165,3 +165,6 @@ func FromJson (id uint32, raw []byte) (Packet, error) {
{{template "packet" .}} {{template "packet" .}}
{{- end}} {{- end}}
// The json representation that was used to generate this data.
// can be used to share the parsing data for i.e dynamic python gui.
const SkylabDefinitions = `{{json . | printf "%s" }}`