cml04-plb018/internal/helpers/helpers.go

90 lines
2.0 KiB
Go
Raw Normal View History

2024-08-20 15:50:52 +02:00
package helpers
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"git.espin.casa/albert/cml04-plb018/internal/types"
)
var (
mux = sync.RWMutex{}
contador = map[types.PEtiquetado]string{
types.Ata12: "ata12.dat",
types.Ata345: "ata345.dat",
}
)
// EsAmericano comprueba si el producto es americano y lo devuelve como boolean
func EsAmericano(producto string) bool {
if strings.HasPrefix(producto, "W") {
return true
}
return false
}
// MetrosAPies convierte una longitud en metros a pies
func MetrosAPies(m float64) float64 {
return m * 3.28084
}
// PiesAMetros convierte una longitud en pies a metros
func PiesAMetros(p float64) float64 {
return p / 3.28084
}
// NroPaqueteSiguiente incrementa el contador de paquetes para un puesto de etiquetado
// y lo devuelve como string
func NroPaqueteSiguiente(pe types.PEtiquetado) (string, error) {
mux.Lock()
defer mux.Unlock()
// Read actual value
contador, ok := contador[pe]
if !ok {
return "", fmt.Errorf("puesto de etiquetado desconocido")
}
data, err := os.ReadFile(contador)
if err != nil {
return "", err
}
nm, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return "", err
}
// increase value
nm++
// Write new matricula value in file
newData := strconv.FormatUint(nm, 10)
// Write new matricula value in file
if err = os.WriteFile(contador, []byte(newData), 0644); err != nil {
return "", err
}
// return new value
return strconv.FormatUint(nm, 10), nil
}
// NroPaqueteActual devuelve el contador de paquetes para un puesto de etiquetado
// como string
func NroPaqueteActual(pe types.PEtiquetado) (string, error) {
mux.RLock()
defer mux.RUnlock()
// Read actual value
contador, ok := contador[pe]
if !ok {
return "", fmt.Errorf("puesto de etiquetado desconocido")
}
data, err := os.ReadFile(contador)
if err != nil {
return "", err
}
nm, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return "", err
}
// return new value
return strconv.FormatUint(nm, 10), nil
}