2018-09-22 19:22:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Jeffrey Walter <jeffreydwalter@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
|
|
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
package arlo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-22 19:22:42 +00:00
|
|
|
"io"
|
2018-09-19 19:12:06 +00:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-05-27 08:49:40 +00:00
|
|
|
)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
func FloatToHex(x float64) string {
|
|
|
|
var result []byte
|
|
|
|
quotient := int(x)
|
|
|
|
fraction := x - float64(quotient)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
for quotient > 0 {
|
|
|
|
quotient = int(x / 16)
|
|
|
|
remainder := int(x - (float64(quotient) * 16))
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
if remainder > 9 {
|
|
|
|
result = append([]byte{byte(remainder + 55)}, result...)
|
|
|
|
} else {
|
|
|
|
for _, c := range strconv.Itoa(int(remainder)) {
|
|
|
|
result = append([]byte{byte(c)}, result...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x = float64(quotient)
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
if fraction == 0 {
|
|
|
|
return string(result)
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
result = append(result, '.')
|
|
|
|
|
|
|
|
for fraction > 0 {
|
|
|
|
fraction = fraction * 16
|
|
|
|
integer := int(fraction)
|
|
|
|
fraction = fraction - float64(integer)
|
|
|
|
|
|
|
|
if integer > 9 {
|
|
|
|
result = append(result, byte(integer+55))
|
|
|
|
} else {
|
|
|
|
for _, c := range strconv.Itoa(int(integer)) {
|
|
|
|
result = append(result, byte(c))
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
return string(result)
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genTransId() string {
|
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
random := rand.New(rand.NewSource(time.Now().UnixNano()))
|
2018-09-19 19:12:06 +00:00
|
|
|
e := random.Float64() * math.Pow(2, 32)
|
|
|
|
ms := time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
return fmt.Sprintf("%s!%s!%s", TransIdPrefix, strings.ToLower(FloatToHex(e)), strconv.Itoa(int(ms)))
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
2018-12-11 23:48:51 +00:00
|
|
|
|
|
|
|
func (a *Arlo) DownloadFile(url string, w io.Writer) error {
|
2018-12-25 22:31:46 +00:00
|
|
|
resp, err := http.Get(url)
|
2018-12-11 23:48:51 +00:00
|
|
|
if err != nil {
|
2020-05-27 08:49:40 +00:00
|
|
|
return fmt.Errorf("getting %s: %v", url, err)
|
2018-12-11 23:48:51 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
_, err = io.Copy(w, resp.Body)
|
|
|
|
if err != nil {
|
2020-05-27 08:49:40 +00:00
|
|
|
return fmt.Errorf("copying body to writer: %v", err)
|
2018-12-11 23:48:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|