av/input/gvctrl/utils.go

55 lines
1.3 KiB
Go

/*
DESCRIPTION
utils.go provides helper functions for functionality found in both gvctrl.go
request.go.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
LICENSE
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package gvctrl
import (
"crypto/md5"
"encoding/hex"
"math"
"strconv"
"strings"
)
func md5Hex(s string) string {
h := md5.New()
h.Write([]byte(s))
return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}
func closestValIdx(v int, l []int) int {
var idx int
for i := range l {
if math.Abs(float64(l[i]-v)) < math.Abs(float64(l[idx]-v)) {
idx = i
}
}
return idx
}
func closestValStr(v int, l []int) string {
return strconv.Itoa(l[closestValIdx(v, l)] * 1000)
}