First gpt batch mode working as usual
This commit is contained in:
61
main.go
61
main.go
@@ -11,39 +11,38 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type VoltageBatch struct {
|
||||
Voltages []float64 `json:"voltages"`
|
||||
}
|
||||
|
||||
var dataFile *os.File
|
||||
var berlinTZ *time.Location
|
||||
|
||||
type VoltageData struct {
|
||||
Voltage float64 `json:"voltage"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
berlinTZ, err = time.LoadLocation("Europe/Berlin")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load Berlin timezone: %v", err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initDataFile() {
|
||||
// Ensure the data/ directory exists
|
||||
if err := os.MkdirAll("data", os.ModePerm); err != nil {
|
||||
log.Fatalf("Failed to create data directory: %v", err)
|
||||
}
|
||||
_ = os.MkdirAll("data", 0755)
|
||||
|
||||
// Generate a filename with timestamp in Berlin local time
|
||||
filename := filepath.Join("data", fmt.Sprintf("voltages_%s.csv", time.Now().In(berlinTZ).Format("20060102_150405")))
|
||||
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
filename := filepath.Join(
|
||||
"data",
|
||||
fmt.Sprintf("voltages_%s.csv",
|
||||
time.Now().In(berlinTZ).Format("20060102_150405")),
|
||||
)
|
||||
|
||||
f, err := os.OpenFile(filename,
|
||||
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open data file: %v", err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
dataFile = file
|
||||
log.Printf("Logging voltage data to %s\n", filename)
|
||||
|
||||
// Write CSV header
|
||||
_, _ = dataFile.WriteString("timestamp,voltage\n")
|
||||
dataFile = f
|
||||
dataFile.WriteString("timestamp,voltage\n")
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -59,24 +58,20 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
// Parse JSON
|
||||
var v VoltageData
|
||||
if err := json.Unmarshal(body, &v); err != nil {
|
||||
var batch VoltageBatch
|
||||
if err := json.Unmarshal(body, &batch); err != nil {
|
||||
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get current timestamp in Berlin
|
||||
timestamp := time.Now().In(berlinTZ).Format("2006-01-02 15:04:05.000")
|
||||
start := time.Now().In(berlinTZ)
|
||||
|
||||
// Print voltage to console
|
||||
fmt.Printf("Received voltage: %f at %s\n", v.Voltage, timestamp)
|
||||
|
||||
// Append CSV line to file
|
||||
if dataFile != nil {
|
||||
if _, err := dataFile.WriteString(fmt.Sprintf("%s,%.6f\n", timestamp, v.Voltage)); err != nil {
|
||||
log.Printf("Failed to write to file: %v", err)
|
||||
}
|
||||
for i, v := range batch.Voltages {
|
||||
ts := start.Add(time.Duration(i) * 33 * time.Millisecond)
|
||||
dataFile.WriteString(
|
||||
fmt.Sprintf("%s,%.6f\n",
|
||||
ts.Format("2006-01-02 15:04:05.000"), v),
|
||||
)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -89,8 +84,6 @@ func main() {
|
||||
|
||||
http.HandleFunc("/data", handler)
|
||||
|
||||
addr := ":8080"
|
||||
log.Println("Listening on", addr)
|
||||
log.Fatal(http.ListenAndServe(addr, nil))
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user