Skip to content

Commit

Permalink
Added initial version of the user interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ocrease committed Aug 16, 2018
1 parent 16a9ea0 commit bdf5d24
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 39 deletions.
36 changes: 27 additions & 9 deletions cmd/vboxanalyser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"

"github.com/ocrease/vboxanalyser/file"
"github.com/ocrease/vboxanalyser/s2"
"github.com/ocrease/vboxanalyser/pkg/server"
"github.com/ocrease/vboxanalyser/pkg/vbo"
"github.com/pkg/browser"
)

const VboxExtension = ".vbo"
Expand All @@ -18,15 +18,33 @@ func main() {
channel := flag.String("c", "LOT_Engine_Spd", "Specify the channel to analyse - rpm, speedKph, speedMph")
threshold := flag.Float64("t", 8300, "Specify the RPM threshold")

_ = channel
_ = threshold
//staticPath := flag.String("webDir", "./web/vboxanalyser/src/", "directory for web resources")

flag.Parse()

// explorer := fe.FileExplorer{}

// files, err := explorer.GetDirectoryContents("C:/Racing/2018-03-16 Snetterton300")
// if err != nil {
// log.Fatal(err)
// }

// for _, f := range files {
// fmt.Printf("%v/%v\n", f.BasePath, f.Path)
// }

path, _ := filepath.Abs(*dir)
fmt.Printf("Analysing .vbo files in: %v\n", path)

err := filepath.Walk(*dir, createFileProcessor(*channel, *threshold))
if err != nil {
log.Fatal(err)
}
// err := filepath.Walk(*dir, createFileProcessor(*channel, *threshold))
// if err != nil {
// log.Fatal(err)
// }

browser.OpenURL("http://localhost:8080")
server.NewServer().Start()

}

Expand All @@ -37,14 +55,14 @@ func createFileProcessor(channel string, threshold float64) func(string, os.File
}
if !info.IsDir() {
if filepath.Ext(path) == VboxExtension {
file := file.ParseFile(path)
file := vbo.ParseFile(path)
//fmt.Printf("%v - num points %v, num columns %v\n", path, len(file.Data.Rows), len(file.Columns))
v, err := file.MaxValue(channel)
if err != nil {
fmt.Printf("%v - %v\n", path, err)
}
if v > threshold {
fmt.Printf("%v - %v laps - %v\n", path, s2.NumLaps(&file), v)
fmt.Printf("%v - %v laps - %v\n", path, vbo.NumLaps(&file), v)
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions pkg/file/fileexplorer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package file

import (
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"syscall"
)

func getRootPaths() (drives []string) {
switch runtime.GOOS {
case "windows":
kernel32, _ := syscall.LoadLibrary("kernel32.dll")
getLogicalDrivesHandle, _ := syscall.GetProcAddress(kernel32, "GetLogicalDrives")

if ret, _, callErr := syscall.Syscall(uintptr(getLogicalDrivesHandle), 0, 0, 0, 0); callErr != 0 {
drives = append(drives, "C:")
} else {
drives = bitsToDrives(uint32(ret))
}
default:
drives = append(drives, subdirectoriesOf("/")...)
}
return
}

func bitsToDrives(bitMap uint32) (drives []string) {
availableDrives := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

for i := range availableDrives {
if bitMap&1 == 1 {
drives = append(drives, availableDrives[i]+":")
}
bitMap >>= 1
}

return
}

type Explorer struct {
}

// func (fe *Explorer) GetDirectoryContents(path string) ([]FileDescriptor, error) {
// var dirs []FileDescriptor
// if len(path) == 0 {
// for _, v := range getRootPaths() {
// dirs = append(dirs, FileDescriptor{Path: v, IsDir: true})
// }
// } else {
// files, err := ioutil.ReadDir(path)
// if err != nil {
// return nil, err
// }

// for _, f := range files {
// if f.IsDir() {
// dirs = append(dirs, FileDescriptor{BasePath: path, Path: f.Name(), IsDir: true})
// }
// }
// }
// return dirs, nil
// }

func (fe *Explorer) GetDirectory(path string) (dirs []Directory) {
var subDirs []string
if len(path) == 0 {
subDirs = getRootPaths()
} else {
subDirs = subdirectoriesOf(path)
}
for _, v := range subDirs {
dirs = append(dirs, Directory{Parent: path, Name: v, SubDirectories: subdirectoriesOf(path + v + "/")})
}
return dirs
}

func subdirectoriesOf(path string) []string {
dirs := make([]string, 0)
fmt.Printf("Looking for directories under %v\n", path)
files, err := ioutil.ReadDir(filepath.Clean(path))
if err != nil {
fmt.Printf("Error occured %v\n", err)
return dirs
}
for _, f := range files {
if f.IsDir() {
dirs = append(dirs, f.Name())
}
}
return dirs
}
17 changes: 17 additions & 0 deletions pkg/file/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package file

type FileDescriptor struct {
BasePath string `json:"basePath"`
Path string `json:"path"`
IsDir bool `json:"isDir"`
}

type Service interface {
GetDirectory(dirPath string) []Directory
}

type Directory struct {
Parent string `json:"parent"`
Name string `json:"name"`
SubDirectories []string `json:"subdirectories"`
}
111 changes: 111 additions & 0 deletions pkg/server/rice-box.go

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package server

import (
"encoding/json"
"log"
"net/http"

rice "github.com/GeertJohan/go.rice"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"

"github.com/ocrease/vboxanalyser/pkg/file"
"github.com/ocrease/vboxanalyser/pkg/vbo"
)

type Server struct {
router *mux.Router
fs file.Service
analyser *vbo.Analyser
}

func NewServer() *Server {
s := Server{router: mux.NewRouter(), fs: new(file.Explorer), analyser: new(vbo.Analyser)}

s.router.HandleFunc("/api/directory", s.directoryList).Methods("GET")
s.router.HandleFunc("/api/analyse", s.analyseDirectory).Methods("GET")
s.router.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("../../ui").HTTPBox()))

return &s
}

func (s *Server) Start() {
log.Println("Listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", handlers.CORS()(s.router)))
}

func (s *Server) directoryList(w http.ResponseWriter, r *http.Request) {
path := r.FormValue("path")
json.NewEncoder(w).Encode(s.fs.GetDirectory(path))
}

func (s *Server) analyseDirectory(w http.ResponseWriter, r *http.Request) {
path := r.FormValue("path")
json.NewEncoder(w).Encode(s.analyser.AnalyseDirectory(path))
}
77 changes: 77 additions & 0 deletions pkg/vbo/analyser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package vbo

import (
"fmt"
"os"
"path/filepath"
)

type Analyser struct{}

const VboxExtension = ".vbo"

func (a *Analyser) AnalyseDirectory(path string) []FileSummary {
summaries := make([]FileSummary, 0)
fmt.Printf("Analysing .vbo files in: %v\n", path)
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {

if filepath.Ext(path) == VboxExtension {
fmt.Printf("Analysing %v\n", path)

file := ParseFile(path)
rpm, err := file.MaxValue("LOT_Engine_Spd")
if err != nil {
rpm = 0
}
vel, err := file.MaxValue("velocity")
if err != nil {
vel = 0
}
summaries = append(summaries, FileSummary{Path: path, NumLaps: NumLaps(&file), MaxVelocity: vel, MaxRpm: rpm})
}
}
return nil
})
if err != nil {
fmt.Printf("Error processing directory %v - %v\n", path, err)
}
fmt.Printf("Analysing %v .vbo files in: %v\n", len(summaries), path)
return summaries
}

// func createFileProcessor(summaries *[]FileSummary) func(string, os.FileInfo, error) error {
// return func(path string, info os.FileInfo, err error) error {
// if err != nil {
// return err
// }
// if !info.IsDir() {

// if filepath.Ext(path) == VboxExtension {
// fmt.Printf("Analysing %v\n", path)

// file := ParseFile(path)
// rpm, err := file.MaxValue("rpm")
// if err != nil {
// rpm = 0
// }
// vel, err := file.MaxValue("velocity")
// if err != nil {
// vel = 0
// }
// summaries = append(summaries, FileSummary{Path: path, NumLaps: NumLaps(&file), MaxVelocity: vel, MaxRpm: rpm})
// }
// }
// return nil
// }
// }

type FileSummary struct {
Path string `json:"path"`
NumLaps uint32 `json:"numlaps"`
MaxVelocity float64 `json:"maxvelocity"`
MaxRpm float64 `json:"maxrpm"`
}
14 changes: 6 additions & 8 deletions file/file.go → pkg/vbo/file.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package file
package vbo

import (
"bufio"
"os"
"strconv"
"strings"

"github.com/ocrease/vboxanalyser"
)

//ParseFile creates a VboFile representation
func ParseFile(path string) vboxanalyser.VboFile {
func ParseFile(path string) File {
data, _ := os.Open(path)
defer data.Close()

vboFile := vboxanalyser.VboFile{Path: path, Columns: make(map[string]int)}
vboFile := File{Path: path, Columns: make(map[string]int)}

var section string

Expand All @@ -31,7 +29,7 @@ func ParseFile(path string) vboxanalyser.VboFile {
section = "columns"
case "[data]":
section = "data"
vboFile.Data = &vboxanalyser.VboFileData{MaxValues: make([]float64, len(vboFile.Columns))}
vboFile.Data = &Data{MaxValues: make([]float64, len(vboFile.Columns))}
case "[laptiming]":
section = "laptiming"
default:
Expand All @@ -46,7 +44,7 @@ func ParseFile(path string) vboxanalyser.VboFile {
return vboFile
}

func processRow(section string, line string, vboFile *vboxanalyser.VboFile) {
func processRow(section string, line string, vboFile *File) {
switch section {
case "data":
vboFile.CreateDataRow(strings.Fields(line))
Expand All @@ -57,7 +55,7 @@ func processRow(section string, line string, vboFile *vboxanalyser.VboFile) {
if fields[0] == "Start" {
lon1, _ := strconv.ParseFloat(fields[1], 64)
lat1, _ := strconv.ParseFloat(fields[2], 64)
vboFile.Start = vboxanalyser.LatLng{lat1 / 60, lon1 * -1 / 60}
vboFile.Start = LatLng{lat1 / 60, lon1 * -1 / 60}
}
case "columns":
for i, v := range strings.Fields(line) {
Expand Down
10 changes: 4 additions & 6 deletions s2/s2.go → pkg/vbo/s2.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package s2
package vbo

import (
"github.com/golang/geo/s1"
"github.com/ocrease/vboxanalyser"

"github.com/golang/geo/s2"
)

Expand All @@ -13,9 +11,9 @@ const (

var gateWidth = s1.ChordAngleFromAngle(s1.Angle(12.5 / EarthRadius))

func NumLaps(f *vboxanalyser.VboFile) int {
func NumLaps(f *File) uint32 {
rows := f.Data.Rows
var numLaps = 0
var numLaps uint32
var crossingStartLine = false
for i, v := range rows {
if i > 0 {
Expand All @@ -40,7 +38,7 @@ func NumLaps(f *vboxanalyser.VboFile) int {

}

// func Distance(f *vboxanalyser.VboFile) float64 {
// func Distance(f *vbo.File) float64 {
// //latLons := make([]s2.LatLng, len(file.data.rows))
// var distance float64
// for i, v := range f.Data.Rows {
Expand Down
Loading

0 comments on commit bdf5d24

Please sign in to comment.