Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR3] DEV-3 Measurement class #10

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import "github.com/HyperloopUPV-H8/Backend-H8/DataTransfer/podDataCreator/domain
type Measurement struct {
Name string
Value value.Value
Units Units
Ranges Ranges
}

func (m *Measurement) getDisplayString() string {
return m.Value.ToDisplayString()
}
40 changes: 40 additions & 0 deletions src/DataTransfer/podDataCreator/domain/measurement/ranges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package measurement

import (
"fmt"
"regexp"
"strconv"
"strings"
)

type Ranges struct {
safe [2]int
warning [2]int
}

var rangesExp = regexp.MustCompile(`^\[(-?\d+)\,(-?\d+)\]$`)

func NewRanges(safeRangeStr string, warningRangeStr string) Ranges {
safeRange := getRangesFromString(strings.ReplaceAll(safeRangeStr, " ", ""))
warningRange := getRangesFromString(strings.ReplaceAll(warningRangeStr, " ", ""))

return Ranges{safe: safeRange, warning: warningRange}
smorsua marked this conversation as resolved.
Show resolved Hide resolved
}

func getRangesFromString(str string) [2]int {
matches := rangesExp.FindStringSubmatch(str)

begin := getInt(matches[1])
end := getInt(matches[2])

return [2]int{int(begin), int(end)}
}

func getInt(intString string) int {
number, err := strconv.ParseInt(intString, 10, 64)

if err != nil {
fmt.Printf("error parsing int: %v\n", err)
}
return int(number)
}
11 changes: 11 additions & 0 deletions src/DataTransfer/podDataCreator/domain/measurement/value/bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package value

import (
"fmt"
)

type Bool bool

func (b *Bool) ToDisplayString() string {
return fmt.Sprintf("%v", b)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package number

import "fmt"

type Number struct {
value float64
podUnits Unit
displayUnits Unit
}

func NewNumber(podUnitString string, displayUnitString string) *Number {
podUnits := newUnit(podUnitString)
displayUnits := newUnit(displayUnitString)
return &Number{value: 0, podUnits: podUnits, displayUnits: displayUnits}
}

func (i *Number) ToDisplayString() string {
number := float64(i.value)
internationalSystemNumber := undoUnits(number, i.podUnits.operations)
result := convertToUnits(internationalSystemNumber, i.displayUnits.operations)
return fmt.Sprintf("%v", result)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package number

import (
"regexp"
"strconv"

"github.com/HyperloopUPV-H8/Backend-H8/DataTransfer/utils"
)

type Operation struct {
operator string
operand float64
}

var operationExp = regexp.MustCompile(`([+\-\/*]{1})(\d+)`)

func doOperation(number float64, operation Operation) float64 {
switch operation.operator {
case "+":
return number + operation.operand
case "-":
return number - operation.operand
case "*":
return number * operation.operand
case "/":
return number / operation.operand
default:
panic("Invalid operation")
}
}

func getOperations(ops string) []Operation {
matches := operationExp.FindAllStringSubmatch(ops, -1)
operations := make([]Operation, 0)
for _, match := range matches {
operation := getOperation(match[1], match[2])
operations = append(operations, operation)
}
return operations
}

func getOperation(operator string, operand string) Operation {
num, err := strconv.ParseFloat(operand, 32)

if err != nil {
utils.PrintParseNumberErr(err)
}

return Operation{operator: operator, operand: float32(num)}
}

func getOpositeAndReversedOperations(operations []Operation) []Operation {
newOperations := make([]Operation, len(operations))
for index, operation := range operations {
newOperations[index] = getOpositeOperation(operation)
}
newOperations = getReversedOperations(newOperations)
return newOperations
}

func getOpositeOperation(operation Operation) Operation {
opositeOperation := Operation{operand: operation.operand}
switch operation.operator {
case "+":
opositeOperation.operator = "-"
case "-":
opositeOperation.operator = "+"

case "*":
opositeOperation.operator = "/"

case "/":
opositeOperation.operator = "*"
default:
panic("Invalid operator")
}

return opositeOperation
}

func getReversedOperations(operations []Operation) []Operation {
length := len(operations)
reversedOperations := make([]Operation, length)

for index, operation := range operations {
reversedOperations[length-1-index] = operation
}

return reversedOperations
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package number
smorsua marked this conversation as resolved.
Show resolved Hide resolved

import (
"regexp"
)

type Unit struct {
name string
operations []Operation
}

var unitExp = regexp.MustCompile(`^([a-zA-Z]+)#((?:[+\-\/*]{1}\d+)+)#$`)

func newUnit(unitStr string) Unit {
matches := unitExp.FindStringSubmatch(unitStr)
unit := Unit{
name: matches[1],
operations: getOperations(matches[2]),
}
return unit
}

func convertToUnits(number float64, operations []Operation) float64 {
result := number
for _, operation := range operations {
result = doOperation(result, operation)
}
return result
}

func undoUnits(number float64, operations []Operation) float64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Porque es necesario revertir operaciones para devolver valores anteriores? para qué es este método?

newOperations := getOpositeAndReversedOperations(operations)
return convertToUnits(number, newOperations)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package value

import "fmt"

type String string

func (s *String) ToDisplayString() string {
return fmt.Sprintf("%v",*s)
}
35 changes: 35 additions & 0 deletions src/DataTransfer/podDataCreator/domain/measurement/value/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package value

import "github.com/HyperloopUPV-H8/Backend-H8/DataTransfer/podDataCreator/domain/measurement/value/number"

type Value interface {
ToDisplayString() string
}

func NewDefault(valueType string, podUnits string, displayUnits string) Value {
switch valueType {
case "bool":
return new(Bool)
smorsua marked this conversation as resolved.
Show resolved Hide resolved
default:
if isEnum(valueType) {
return new(String)
jmaralo marked this conversation as resolved.
Show resolved Hide resolved
} else if isNumber(valueType) {
return number.NewNumber(podUnits, displayUnits)
} else {
panic("Invalid type")
}
}
}

func isNumber(valueType string) bool {
switch valueType {
case "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float32", "float64":
return true
default:
return false
}
}
// TODO: Esta función esta hecha pero no se donde colocarla, por ahora dejo este placeholder
func isEnum(va string) bool {
return true
}
12 changes: 9 additions & 3 deletions src/DataTransfer/podDataCreator/domain/podData.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ type PodData struct {
}

func (podData *PodData) UpdatePacket(pu packetparser.PacketUpdate) {
packet := podData.getPacket(pu.Id)
packet.updatePacket(pu)
}

func (podData *PodData) getPacket(id uint16) *Packet {
packet := new(Packet)
for _, board := range podData.Boards {
packet, hasPacket := board.Packets[pu.Id]
foundPacket, hasPacket := board.Packets[id]
if hasPacket {
packet.updatePacket(pu)
packet = foundPacket
}
}

return packet
}
14 changes: 0 additions & 14 deletions src/DataTransfer/podDataCreator/infra/excelAdapter/excelAdapter.go

This file was deleted.

17 changes: 8 additions & 9 deletions src/DataTransfer/podDataCreator/podDataCreator.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package podDataCreator

import (
excelRetreiver "github.com/HyperloopUPV-H8/Backend-H8/DataTransfer/excelRetreiver"
domain "github.com/HyperloopUPV-H8/Backend-H8/DataTransfer/podDataCreator/domain"
"github.com/HyperloopUPV-H8/Backend-H8/DataTransfer/podDataCreator/infra/excelAdapter"
"github.com/HyperloopUPV-H8/Backend-H8/Shared/excelAdapter/dto"
)

func Run() domain.PodData {
structure := excelRetreiver.GetStructure()
podData := domain.PodData{}
podData.Boards = getBoards(structure)
func Invoke(boardDTOs map[string]dto.BoardDTO) domain.PodData {
podData := domain.PodData{
Boards: getBoards(boardDTOs),
}

return podData
}

func getBoards(structure excelRetreiver.Structure) map[string]*domain.Board {
func getBoards(boardDTOs map[string]dto.BoardDTO) map[string]*domain.Board {
boards := make(map[string]*domain.Board)
for name, sheet := range structure.Sheets {
for name, boardDTO := range boardDTOs {
board := &domain.Board{
Name: name,
Packets: excelAdapter.GetPackets(sheet.Tables),
Packets: boardDTO.GetPackets(),
}
boards[board.Name] = board
}
Expand Down
Loading