Added the record command
Very basic, but it will do for testing
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/ESilva15/BeamNGMockOg/mockserver"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func recordAction(cmd *cobra.Command, args []string) {
|
||||||
|
outputFile, _ := cmd.Flags().GetString("output")
|
||||||
|
address, _ := cmd.Flags().GetString("address")
|
||||||
|
port, _ := cmd.Flags().GetInt("port")
|
||||||
|
|
||||||
|
if err := mockserver.Record(address, port, outputFile); err != nil {
|
||||||
|
fmt.Printf("Something went wrong while recording the file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// shelf
|
||||||
|
var recordCmd = &cobra.Command{
|
||||||
|
Use: "record",
|
||||||
|
Short: "record -o <path-to-bin-file>",
|
||||||
|
Long: `record will store the data from the given UDP server to the
|
||||||
|
filepath given by -i`,
|
||||||
|
Args: nil,
|
||||||
|
Run: recordAction,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(recordCmd)
|
||||||
|
|
||||||
|
recordCmd.PersistentFlags().StringP("output", "o", "output.bin", "output file for recording")
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package mockserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/gob"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
bngsdk "github.com/ESilva15/gobngsdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Record records data from the UDP connection created by address and port
|
||||||
|
func Record(address string, port int, filePath string) error {
|
||||||
|
ticker := time.NewTicker(time.Second / 60)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
// Create the output file
|
||||||
|
bin, err := os.Create(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the BeamNGSDK instance
|
||||||
|
beam, err := bngsdk.Init(address, port)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer beam.Close()
|
||||||
|
|
||||||
|
enc := gob.NewEncoder(bin)
|
||||||
|
for {
|
||||||
|
err := beam.ReadData()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := enc.Encode(beam.Data); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
<-ticker.C
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user