Skip to content
This repository was archived by the owner on Mar 22, 2025. It is now read-only.

Commit 6b864a8

Browse files
authored
Merge pull request #76 from lmas/dev
Merge in code (mostly) from sprint 1
2 parents 8a07616 + cfe6465 commit 6b864a8

File tree

19 files changed

+2816
-507
lines changed

19 files changed

+2816
-507
lines changed

.github/workflows/main.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Tests and Linters
1+
name: Linters, Spellcheck, and Tests
22

33
on:
44
push:
@@ -7,7 +7,7 @@ on:
77
jobs:
88
Linters:
99
runs-on: ubuntu-latest
10-
timeout-minutes: 10
10+
timeout-minutes: 2
1111
steps:
1212
- uses: actions/checkout@v4
1313
- name: Setup go
@@ -19,15 +19,24 @@ jobs:
1919
- name: Run linters
2020
run: make lint
2121

22+
Spellcheck:
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 2
25+
steps:
26+
- uses: actions/checkout@v4
27+
- uses: crate-ci/[email protected]
28+
2229
Tests:
2330
runs-on: ubuntu-latest
24-
timeout-minutes: 10
31+
timeout-minutes: 2
2532
steps:
2633
- uses: actions/checkout@v4
2734
- name: Setup go
2835
uses: actions/setup-go@v5
2936
with:
3037
go-version: 1.23
38+
- name: Install dependencies
39+
run: make deps
3140
- name: Run tests
3241
run: make test
3342
- name: Report stats

Comfortstat/Comfortstat.go

Lines changed: 94 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020
// instantiate the System
2121
sys := components.NewSystem("Comfortstat", ctx)
2222

23-
// Instatiate the Capusle
23+
// Instantiate the Capsule
2424
sys.Husk = &components.Husk{
2525
Description: " is a controller for a consumed servo motor position based on a consumed temperature",
2626
Certificate: "ABCD",
@@ -31,6 +31,9 @@ func main() {
3131

3232
// instantiate a template unit asset
3333
assetTemplate := initTemplate()
34+
// Calling initAPI() starts the pricefeedbackloop that fetches the current electricity price for the particular hour
35+
initAPI()
36+
time.Sleep(1 * time.Second)
3437
assetName := assetTemplate.GetName()
3538
sys.UAssets[assetName] = &assetTemplate
3639

@@ -45,8 +48,8 @@ func main() {
4548
if err := json.Unmarshal(raw, &uac); err != nil {
4649
log.Fatalf("Resource configuration error: %+v\n", err)
4750
}
48-
ua, cleanup := newUnitAsset(uac, &sys, servsTemp)
49-
defer cleanup()
51+
ua, startup := newUnitAsset(uac, &sys, servsTemp)
52+
startup()
5053
sys.UAssets[ua.GetName()] = &ua
5154
}
5255

@@ -66,117 +69,164 @@ func main() {
6669
time.Sleep(2 * time.Second) // allow the go routines to be executed, which might take more time than the main routine to end
6770
}
6871

69-
// TODO: change the namne, will get one function for each of the four cases
70-
// Serving handles the resources services. NOTE: it exepcts those names from the request URL path
72+
// Serving handles the resources services. NOTE: it expects those names from the request URL path
7173
func (t *UnitAsset) Serving(w http.ResponseWriter, r *http.Request, servicePath string) {
7274
switch servicePath {
73-
case "min_temperature":
74-
t.set_minTemp(w, r)
75-
case "max_temperature":
76-
t.set_maxTemp(w, r)
77-
case "max_price":
78-
t.set_maxPrice(w, r)
79-
case "min_price":
80-
t.set_minPrice(w, r)
81-
case "SEK_price":
82-
t.set_SEKprice(w, r)
83-
case "desired_temp":
84-
t.set_desiredTemp(w, r)
75+
case "MinTemperature":
76+
t.httpSetMinTemp(w, r)
77+
case "MaxTemperature":
78+
t.httpSetMaxTemp(w, r)
79+
case "MaxPrice":
80+
t.httpSetMaxPrice(w, r)
81+
case "MinPrice":
82+
t.httpSetMinPrice(w, r)
83+
case "SEKPrice":
84+
t.httpSetSEKPrice(w, r)
85+
case "DesiredTemp":
86+
t.httpSetDesiredTemp(w, r)
87+
case "userTemp":
88+
t.httpSetUserTemp(w, r)
89+
case "Region":
90+
t.httpSetRegion(w, r)
8591
default:
8692
http.Error(w, "Invalid service request [Do not modify the services subpath in the configurration file]", http.StatusBadRequest)
8793
}
8894
}
8995

90-
func (rsc *UnitAsset) set_SEKprice(w http.ResponseWriter, r *http.Request) {
96+
func (rsc *UnitAsset) httpSetSEKPrice(w http.ResponseWriter, r *http.Request) {
9197
switch r.Method {
9298
case "GET":
93-
signalErr := rsc.getSEK_price()
99+
signalErr := rsc.getSEKPrice()
94100
usecases.HTTPProcessGetRequest(w, r, &signalErr)
95101
default:
96102
http.Error(w, "Method is not supported.", http.StatusNotFound)
97103
}
98104
}
99105

100-
// TODO: split up this function to two sepreate function that sets on max and min price.
101-
func (rsc *UnitAsset) set_minTemp(w http.ResponseWriter, r *http.Request) {
106+
// All these functions below handles HTTP "PUT" or "GET" requests to modefy or retrieve the MAX/MIN temprature/price and desierd temperature
107+
// For the PUT case - the "HTTPProcessSetRequest(w, r)" is called to prosses the data given from the user and if no error,
108+
// call the set functions in things.go with the value witch updates the value in the struct
109+
func (rsc *UnitAsset) httpSetMinTemp(w http.ResponseWriter, r *http.Request) {
102110
switch r.Method {
103111
case "PUT":
104112
sig, err := usecases.HTTPProcessSetRequest(w, r)
105113
if err != nil {
106-
log.Println("Error with the setting request of the position ", err)
114+
//log.Println("Error with the setting request of the position ", err)
115+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
116+
return
117+
107118
}
108-
rsc.setMin_temp(sig)
119+
rsc.setMinTemp(sig)
109120
case "GET":
110-
signalErr := rsc.getMin_temp()
121+
signalErr := rsc.getMinTemp()
111122
usecases.HTTPProcessGetRequest(w, r, &signalErr)
112123
default:
113124
http.Error(w, "Method is not supported.", http.StatusNotFound)
114125
}
115126
}
116-
func (rsc *UnitAsset) set_maxTemp(w http.ResponseWriter, r *http.Request) {
127+
func (rsc *UnitAsset) httpSetMaxTemp(w http.ResponseWriter, r *http.Request) {
128+
switch r.Method {
129+
case "PUT":
130+
sig, err := usecases.HTTPProcessSetRequest(w, r)
131+
if err != nil {
132+
//log.Println("Error with the setting request of the position ", err)
133+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
134+
return
135+
}
136+
rsc.setMaxTemp(sig)
137+
case "GET":
138+
signalErr := rsc.getMaxTemp()
139+
usecases.HTTPProcessGetRequest(w, r, &signalErr)
140+
default:
141+
http.Error(w, "Method is not supported.", http.StatusNotFound)
142+
}
143+
}
144+
145+
func (rsc *UnitAsset) httpSetMinPrice(w http.ResponseWriter, r *http.Request) {
117146
switch r.Method {
118147
case "PUT":
119148
sig, err := usecases.HTTPProcessSetRequest(w, r)
120149
if err != nil {
121-
log.Println("Error with the setting request of the position ", err)
150+
//log.Println("Error with the setting request of the position ", err)
151+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
152+
return
122153
}
123-
rsc.setMax_temp(sig)
154+
rsc.setMinPrice(sig)
124155
case "GET":
125-
signalErr := rsc.getMax_temp()
156+
signalErr := rsc.getMinPrice()
126157
usecases.HTTPProcessGetRequest(w, r, &signalErr)
127158
default:
128159
http.Error(w, "Method is not supported.", http.StatusNotFound)
160+
129161
}
130162
}
131163

132-
// LOOK AT: I guess that we probable only need to if there is a PUT from user?
133-
// LOOK AT: so not the GET!
134-
// For PUT - the "HTTPProcessSetRequest(w, r)" is called to prosses the data given from the user and if no error, call set_minMaxprice with the value
135-
// wich updates the value in thge struct
136-
func (rsc *UnitAsset) set_minPrice(w http.ResponseWriter, r *http.Request) {
164+
func (rsc *UnitAsset) httpSetMaxPrice(w http.ResponseWriter, r *http.Request) {
137165
switch r.Method {
138166
case "PUT":
139167
sig, err := usecases.HTTPProcessSetRequest(w, r)
140168
if err != nil {
141-
log.Println("Error with the setting request of the position ", err)
169+
//log.Println("Error with the setting request of the position ", err)
170+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
171+
return
142172
}
143-
rsc.setMin_price(sig)
173+
rsc.setMaxPrice(sig)
144174
case "GET":
145-
signalErr := rsc.getMin_price()
175+
signalErr := rsc.getMaxPrice()
146176
usecases.HTTPProcessGetRequest(w, r, &signalErr)
147177
default:
148178
http.Error(w, "Method is not supported.", http.StatusNotFound)
149179

150180
}
151181
}
152182

153-
func (rsc *UnitAsset) set_maxPrice(w http.ResponseWriter, r *http.Request) {
183+
func (rsc *UnitAsset) httpSetDesiredTemp(w http.ResponseWriter, r *http.Request) {
154184
switch r.Method {
155185
case "PUT":
156186
sig, err := usecases.HTTPProcessSetRequest(w, r)
157187
if err != nil {
158-
log.Println("Error with the setting request of the position ", err)
188+
//log.Println("Error with the setting request of the position ", err)
189+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
190+
return
159191
}
160-
rsc.setMax_price(sig)
192+
rsc.setDesiredTemp(sig)
161193
case "GET":
162-
signalErr := rsc.getMax_price()
194+
signalErr := rsc.getDesiredTemp()
163195
usecases.HTTPProcessGetRequest(w, r, &signalErr)
164196
default:
165197
http.Error(w, "Method is not supported.", http.StatusNotFound)
198+
}
199+
200+
}
166201

202+
func (rsc *UnitAsset) httpSetUserTemp(w http.ResponseWriter, r *http.Request) {
203+
switch r.Method {
204+
case "PUT":
205+
sig, err := usecases.HTTPProcessSetRequest(w, r)
206+
if err != nil {
207+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
208+
return
209+
}
210+
rsc.setUserTemp(sig)
211+
case "GET":
212+
signalErr := rsc.getUserTemp()
213+
usecases.HTTPProcessGetRequest(w, r, &signalErr)
214+
default:
215+
http.Error(w, "Method is not supported.", http.StatusNotFound)
167216
}
168217
}
169218

170-
func (rsc *UnitAsset) set_desiredTemp(w http.ResponseWriter, r *http.Request) {
219+
func (rsc *UnitAsset) httpSetRegion(w http.ResponseWriter, r *http.Request) {
171220
switch r.Method {
172221
case "PUT":
173222
sig, err := usecases.HTTPProcessSetRequest(w, r)
174223
if err != nil {
175-
log.Println("Error with the setting request of the position ", err)
224+
http.Error(w, "request incorrectly formatted", http.StatusBadRequest)
225+
return
176226
}
177-
rsc.setDesired_temp(sig)
227+
rsc.setRegion(sig)
178228
case "GET":
179-
signalErr := rsc.getDesired_temp()
229+
signalErr := rsc.getRegion()
180230
usecases.HTTPProcessGetRequest(w, r, &signalErr)
181231
default:
182232
http.Error(w, "Method is not supported.", http.StatusNotFound)

0 commit comments

Comments
 (0)