-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
101 lines (84 loc) · 2.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"bartekpacia/bit-festival-2023/algo"
"github.com/gorilla/mux"
)
type ReqData struct {
Ampacity float64 `json:"ampacity"`
MaxPower float64 `json:"maxPower"`
VeinsUnderLoad int `json:"veinsUnderLoad"`
Placements algo.CableLocation `json:"placements"`
Temperature float64 `json:"temperature"`
}
type RespData struct {
CableType string `json:"cableType"`
VeinsNumber int `json:"veinsNumber"`
CrossSection float64 `json:"crossSection"`
InsulationType string `json:"insulationType"`
Placements algo.CableLocation `json:"placements"`
Temperature float64 `json:"temperature"`
}
func handlePost(w http.ResponseWriter, r *http.Request) {
var req ReqData
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Printf("%+v\n", req)
var resp RespData
I_ost, err := algo.CalcTemp(req.Ampacity, req.MaxPower, req.Temperature, req.VeinsUnderLoad)
if err != nil {
log.Fatalln(err)
}
fmt.Println("I_ost:", I_ost)
przekroj, err := algo.MatchCrossection(I_ost, req.VeinsUnderLoad, req.Placements)
if err != nil {
log.Fatalln("failed to match crossection:", err)
}
resp.CrossSection = przekroj
resp.CableType = "YDY"
resp.VeinsNumber = req.VeinsUnderLoad
resp.InsulationType = "PVC"
resp.Placements = req.Placements
resp.Temperature = req.Temperature
jData, err := json.Marshal(resp)
if err != nil {
// handle error
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(jData)
}
func main() {
algo.LoadPath = "algo/"
err := algo.Init()
if err != nil {
log.Fatalln(err)
}
I_obl := 18.0 // A
temp := 27.0 // °C
P := 0.0
fmt.Printf("I_obl: %f A, temp: %f °C\n", I_obl, temp)
I_ost, err := algo.CalcTemp(I_obl, P, temp, 3)
if err != nil {
log.Fatalln(err)
}
fmt.Println("I_ost:", I_ost)
przekroj, err := algo.MatchCrossection(I_ost, 3, algo.A1)
if err != nil {
log.Fatalln("failed to match crossection:", err)
}
fmt.Println("przekroj:", przekroj)
r := mux.NewRouter()
r.HandleFunc("/", handlePost).Methods("POST")
srv := &http.Server{
Addr: ":8080",
Handler: r,
}
srv.ListenAndServe()
}