Skip to content

Commit aee709e

Browse files
authored
Merge branch 'develop' into backend/logger-implementation-hotfix
2 parents a8f57ed + 9ed19c8 commit aee709e

11 files changed

Lines changed: 620 additions & 136 deletions

File tree

.github/workflows/build-backend.yaml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
SECRET:
77
required: true
88
workflow_dispatch:
9+
pull_request:
10+
paths:
11+
- backend/**
912

1013
env:
1114
SECRET: ${{ secrets.SECRET }}
@@ -26,7 +29,9 @@ jobs:
2629
- name: "Install packages"
2730
run: apk update && apk add --no-cache libpcap-dev musl-dev gcc go
2831

29-
- uses: actions/checkout@v3
32+
- uses: actions/checkout@v4
33+
with:
34+
sparse-checkout: backend
3035

3136
- name: "Create output path"
3237
working-directory: "${{env.BACKEND_DIR}}"
@@ -47,10 +52,12 @@ jobs:
4752
go build -ldflags '-linkmode external -extldflags "-static"' -o ../output/backend-linux-64
4853
4954
- name: "Upload build"
50-
uses: actions/upload-artifact@v3
55+
uses: actions/upload-artifact@v4
5156
with:
5257
name: backend-linux
5358
path: "${{env.BACKEND_DIR}}/output/*"
59+
retention-days: 3
60+
compression-level: 9
5461

5562
build-backend-windows:
5663
name: "Build backend for windows"
@@ -90,10 +97,12 @@ jobs:
9097
go build -o ..\output\backend-windows-64.exe
9198
9299
- name: "Upload build"
93-
uses: actions/upload-artifact@v3
100+
uses: actions/upload-artifact@v4
94101
with:
95102
name: backend-windows
96103
path: "${{env.BACKEND_DIR}}\\output\\*"
104+
retention-days: 3
105+
compression-level: 9
97106

98107
build-backend-mac:
99108
name: "Build backend for macOS"
@@ -142,7 +151,9 @@ jobs:
142151
go build -o ../output/backend-macos-m1-64
143152
144153
- name: "Upload build"
145-
uses: actions/upload-artifact@v3
154+
uses: actions/upload-artifact@v4
146155
with:
147156
name: backend-macos
148157
path: "${{env.BACKEND_DIR}}/output/*"
158+
retention-days: 3
159+
compression-level: 9

.github/workflows/build-ethernet-view.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name: Build ethernet view
33
on:
44
workflow_call:
55
workflow_dispatch:
6+
pull_request:
7+
paths:
8+
- ethernet-view/**
9+
- common-front/**
610

711
jobs:
812
build-ethernet-view:
@@ -14,7 +18,11 @@ jobs:
1418
COMMON_DIR: ./common-front
1519

1620
steps:
17-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
22+
with:
23+
sparse-checkout: |
24+
ethernet-view
25+
common-front
1826
1927
- name: "Install common front dependencies"
2028
working-directory: "${{env.COMMON_DIR}}"
@@ -36,7 +44,9 @@ jobs:
3644

3745

3846
- name: "Upload build"
39-
uses: actions/upload-artifact@v3
47+
uses: actions/upload-artifact@v4
4048
with:
4149
name: ethernet-view
4250
path: "${{env.FRONTEND_DIR}}/static/*"
51+
retention-days: 3
52+
compression-level: 9
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Test backend
2+
3+
on:
4+
push:
5+
paths:
6+
- backend/**
7+
pull_request:
8+
paths:
9+
- backend/**
10+
workflow_dispatch:
11+
12+
env:
13+
SECRET: ${{ secrets.SECRET }}
14+
15+
jobs:
16+
test-backend:
17+
name: "Test backend"
18+
runs-on: ubuntu-latest
19+
20+
env:
21+
BACKEND_DIR: ./backend
22+
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
27+
- name: Setup Go
28+
uses: actions/setup-go@v4
29+
with:
30+
go-version: 1.21
31+
cache: false
32+
33+
34+
- name: Install Dependencies
35+
run: |
36+
sudo apt-get update && sudo apt-get install -y libpcap-dev
37+
38+
- name: "Load secret"
39+
working-directory: "${{env.BACKEND_DIR}}"
40+
run: |
41+
./load-secret.sh "$SECRET"
42+
43+
- name: Test with Go
44+
working-directory: "${{env.BACKEND_DIR}}"
45+
run: go test -v -timeout 30s ./...

backend/cmd/main.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/HyperloopUPV-H8/h9-backend/internal/data_transfer"
2121
"github.com/HyperloopUPV-H8/h9-backend/internal/excel"
2222
"github.com/HyperloopUPV-H8/h9-backend/internal/excel/ade"
23+
"github.com/HyperloopUPV-H8/h9-backend/internal/excel/utils"
2324
"github.com/HyperloopUPV-H8/h9-backend/internal/info"
2425
"github.com/HyperloopUPV-H8/h9-backend/internal/message_transfer"
2526
"github.com/HyperloopUPV-H8/h9-backend/internal/order_transfer"
@@ -445,27 +446,29 @@ func getTransportDecEnc(info info.Info, podData pod_data.PodData) (*presentation
445446
for i, measurement := range packet.Measurements {
446447
switch meas := measurement.(type) {
447448
case pod_data.NumericMeasurement:
449+
podOps := getOps(meas.PodUnits)
450+
displayOps := getOps(meas.DisplayUnits)
448451
switch meas.Type {
449452
case "uint8":
450-
descriptor[i] = data.NewNumericDescriptor[uint8](data.ValueName(meas.Id))
453+
descriptor[i] = data.NewNumericDescriptor[uint8](data.ValueName(meas.Id), podOps, displayOps)
451454
case "uint16":
452-
descriptor[i] = data.NewNumericDescriptor[uint16](data.ValueName(meas.Id))
455+
descriptor[i] = data.NewNumericDescriptor[uint16](data.ValueName(meas.Id), podOps, displayOps)
453456
case "uint32":
454-
descriptor[i] = data.NewNumericDescriptor[uint32](data.ValueName(meas.Id))
457+
descriptor[i] = data.NewNumericDescriptor[uint32](data.ValueName(meas.Id), podOps, displayOps)
455458
case "uint64":
456-
descriptor[i] = data.NewNumericDescriptor[uint64](data.ValueName(meas.Id))
459+
descriptor[i] = data.NewNumericDescriptor[uint64](data.ValueName(meas.Id), podOps, displayOps)
457460
case "int8":
458-
descriptor[i] = data.NewNumericDescriptor[int8](data.ValueName(meas.Id))
461+
descriptor[i] = data.NewNumericDescriptor[int8](data.ValueName(meas.Id), podOps, displayOps)
459462
case "int16":
460-
descriptor[i] = data.NewNumericDescriptor[int16](data.ValueName(meas.Id))
463+
descriptor[i] = data.NewNumericDescriptor[int16](data.ValueName(meas.Id), podOps, displayOps)
461464
case "int32":
462-
descriptor[i] = data.NewNumericDescriptor[int32](data.ValueName(meas.Id))
465+
descriptor[i] = data.NewNumericDescriptor[int32](data.ValueName(meas.Id), podOps, displayOps)
463466
case "int64":
464-
descriptor[i] = data.NewNumericDescriptor[int64](data.ValueName(meas.Id))
467+
descriptor[i] = data.NewNumericDescriptor[int64](data.ValueName(meas.Id), podOps, displayOps)
465468
case "float32":
466-
descriptor[i] = data.NewNumericDescriptor[float32](data.ValueName(meas.Id))
469+
descriptor[i] = data.NewNumericDescriptor[float32](data.ValueName(meas.Id), podOps, displayOps)
467470
case "float64":
468-
descriptor[i] = data.NewNumericDescriptor[float64](data.ValueName(meas.Id))
471+
descriptor[i] = data.NewNumericDescriptor[float64](data.ValueName(meas.Id), podOps, displayOps)
469472
default:
470473
panic(fmt.Sprintf("unexpected numeric type for %s: %s", meas.Id, meas.Type))
471474
}
@@ -509,6 +512,17 @@ func getTransportDecEnc(info info.Info, podData pod_data.PodData) (*presentation
509512
return decoder, encoder
510513
}
511514

515+
func getOps(units utils.Units) data.ConversionDescriptor {
516+
output := make(data.ConversionDescriptor, len(units.Operations))
517+
for i, operation := range units.Operations {
518+
output[i] = data.Operation{
519+
Operator: operation.Operator,
520+
Operand: operation.Operand,
521+
}
522+
}
523+
return output
524+
}
525+
512526
type TransportAPI struct {
513527
OnNotification func(abstraction.TransportNotification)
514528
OnConnectionUpdate func(abstraction.TransportTarget, bool)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package data
2+
3+
type valueConverter func(Value) Value
4+
type valueReverter func(Value) Value
5+
6+
type ConversionDescriptor []Operation
7+
8+
type Operation struct {
9+
Operator string
10+
Operand float64
11+
}
12+
13+
func newConvertNumeric[N numeric](podConversion, displayConversion ConversionDescriptor) valueConverter {
14+
return func(value Value) Value {
15+
numeric := value.(NumericValue[N])
16+
for _, operation := range append(podConversion, displayConversion...) {
17+
switch operation.Operator {
18+
case "+":
19+
numeric.inner += N(operation.Operand)
20+
case "-":
21+
numeric.inner -= N(operation.Operand)
22+
case "*":
23+
numeric.inner *= N(operation.Operand)
24+
case "/":
25+
numeric.inner /= N(operation.Operand)
26+
}
27+
}
28+
return numeric
29+
}
30+
}
31+
32+
func newRevertNumeric[N numeric](podConversion, displayConversion ConversionDescriptor) valueReverter {
33+
return func(value Value) Value {
34+
numeric := value.(NumericValue[N])
35+
for i := len(displayConversion) - 1; i >= 0; i-- {
36+
operation := displayConversion[i]
37+
switch operation.Operator {
38+
case "+":
39+
numeric.inner -= N(operation.Operand)
40+
case "-":
41+
numeric.inner += N(operation.Operand)
42+
case "*":
43+
numeric.inner /= N(operation.Operand)
44+
case "/":
45+
numeric.inner *= N(operation.Operand)
46+
}
47+
}
48+
49+
for i := len(podConversion) - 1; i >= 0; i-- {
50+
operation := podConversion[i]
51+
switch operation.Operator {
52+
case "+":
53+
numeric.inner -= N(operation.Operand)
54+
case "-":
55+
numeric.inner += N(operation.Operand)
56+
case "*":
57+
numeric.inner /= N(operation.Operand)
58+
case "/":
59+
numeric.inner *= N(operation.Operand)
60+
}
61+
}
62+
63+
return numeric
64+
}
65+
}
66+
67+
func convertBoolean(value Value) Value {
68+
return value
69+
}
70+
71+
func revertBoolean(value Value) Value {
72+
return value
73+
}
74+
75+
func convertEnum(value Value) Value {
76+
return value
77+
}
78+
79+
func revertEnum(value Value) Value {
80+
return value
81+
}

backend/pkg/transport/packet/data/descriptors.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,40 @@ type Descriptor []valueDescriptor
1111

1212
// valueDescriptor describes a value of a packet
1313
type valueDescriptor struct {
14-
Name ValueName
15-
Type valueType
16-
decode valueDecoder
17-
encode valueEncoder
14+
Name ValueName
15+
Type valueType
16+
decode valueDecoder
17+
encode valueEncoder
18+
convert valueConverter
19+
revert valueReverter
1820
}
1921

2022
// Decode decodes the next value from the reader using its decoding method
2123
func (descriptor *valueDescriptor) Decode(endianness binary.ByteOrder, reader io.Reader) (Value, error) {
22-
return descriptor.decode(endianness, reader)
24+
value, err := descriptor.decode(endianness, reader)
25+
if err != nil {
26+
return value, err
27+
}
28+
return descriptor.convert(value), nil
2329
}
2430

2531
// Encode encodes the provided value into the writer using its encoding method
2632
func (descriptor *valueDescriptor) Encode(endianness binary.ByteOrder, value Value, writer io.Writer) error {
27-
return descriptor.encode(endianness, value, writer)
33+
return descriptor.encode(endianness, descriptor.revert(value), writer)
2834
}
2935

3036
// NewNumericDescriptor creates a new NumericDescriptor
3137
//
3238
// name is the name for the value
33-
func NewNumericDescriptor[N numeric](name ValueName) valueDescriptor {
39+
func NewNumericDescriptor[N numeric](name ValueName, podUnits, displayUnits ConversionDescriptor) valueDescriptor {
3440
var n N
3541
return valueDescriptor{
36-
Name: name,
37-
Type: valueType(reflect.TypeOf(n).Name()),
38-
decode: decodeNumeric[N],
39-
encode: encodeNumeric[N],
42+
Name: name,
43+
Type: valueType(reflect.TypeOf(n).Name()),
44+
decode: decodeNumeric[N],
45+
encode: encodeNumeric[N],
46+
convert: newConvertNumeric[N](podUnits, displayUnits),
47+
revert: newRevertNumeric[N](podUnits, displayUnits),
4048
}
4149
}
4250

@@ -45,10 +53,12 @@ func NewNumericDescriptor[N numeric](name ValueName) valueDescriptor {
4553
// name is the name for the value
4654
func NewBooleanDescriptor(name ValueName) valueDescriptor {
4755
return valueDescriptor{
48-
Name: name,
49-
Type: BoolType,
50-
decode: decodeBool,
51-
encode: encodeBool,
56+
Name: name,
57+
Type: BoolType,
58+
decode: decodeBool,
59+
encode: encodeBool,
60+
convert: convertBoolean,
61+
revert: revertBoolean,
5262
}
5363
}
5464

@@ -57,9 +67,11 @@ func NewBooleanDescriptor(name ValueName) valueDescriptor {
5767
// name is the name for the value and descriptor is the variants the enum has
5868
func NewEnumDescriptor(name ValueName, descriptor EnumDescriptor) valueDescriptor {
5969
return valueDescriptor{
60-
Name: name,
61-
Type: EnumType,
62-
decode: newDecodeEnum(name, descriptor),
63-
encode: newEncodeEnum(name, descriptor),
70+
Name: name,
71+
Type: EnumType,
72+
decode: newDecodeEnum(name, descriptor),
73+
encode: newEncodeEnum(name, descriptor),
74+
convert: convertEnum,
75+
revert: revertEnum,
6476
}
6577
}

0 commit comments

Comments
 (0)