itd/internal/types/types.go

143 lines
2.5 KiB
Go
Raw Normal View History

2021-08-21 08:19:49 +00:00
package types
2021-11-23 19:12:16 +00:00
import (
"fmt"
"strconv"
)
const (
ReqTypeHeartRate = iota
ReqTypeBattLevel
ReqTypeFwVersion
ReqTypeFwUpgrade
ReqTypeBtAddress
ReqTypeNotify
ReqTypeSetTime
2021-08-26 04:18:24 +00:00
ReqTypeWatchHeartRate
ReqTypeWatchBattLevel
2021-10-22 20:21:14 +00:00
ReqTypeMotion
ReqTypeWatchMotion
ReqTypeStepCount
ReqTypeWatchStepCount
ReqTypeCancel
2021-11-23 06:04:09 +00:00
ReqTypeFS
)
const (
UpgradeTypeArchive = iota
UpgradeTypeFiles
)
2021-11-23 06:04:09 +00:00
const (
FSTypeWrite = iota
FSTypeRead
FSTypeMove
FSTypeDelete
FSTypeList
FSTypeMkdir
)
type ReqDataFS struct {
2021-11-23 19:12:16 +00:00
Type int `json:"type"`
2021-11-23 06:04:09 +00:00
Files []string `json:"files"`
2021-11-23 19:12:16 +00:00
Data string `json:"data,omitempty"`
2021-11-23 06:04:09 +00:00
}
2021-08-21 08:19:49 +00:00
type ReqDataFwUpgrade struct {
Type int
Files []string
}
type Response struct {
2021-10-23 05:14:01 +00:00
Type int `json:"type"`
2021-08-21 08:19:49 +00:00
Value interface{} `json:"value,omitempty"`
Message string `json:"msg,omitempty"`
ID string `json:"id,omitempty"`
2021-08-21 08:19:49 +00:00
Error bool `json:"error"`
}
type Request struct {
Type int `json:"type"`
2021-08-21 08:19:49 +00:00
Data interface{} `json:"data,omitempty"`
}
type ReqDataNotify struct {
Title string
Body string
}
2021-08-26 04:18:24 +00:00
type DFUProgress struct {
Received int64 `mapstructure:"recvd"`
Total int64 `mapstructure:"total"`
Sent int64 `mapstructure:"sent"`
2021-08-26 04:18:24 +00:00
}
2021-10-22 20:42:33 +00:00
type MotionValues struct {
X int16
Y int16
Z int16
}
2021-11-23 06:04:09 +00:00
type FileInfo struct {
2021-11-23 19:12:16 +00:00
Name string `json:"name"`
Size int64 `json:"size"`
IsDir bool `json:"isDir"`
}
func (fi FileInfo) String() string {
var isDirChar rune
if fi.IsDir {
isDirChar = 'd'
} else {
isDirChar = '-'
}
// Get human-readable value for file size
val, unit := bytesHuman(fi.Size)
prec := 0
// If value is less than 10, set precision to 1
if val < 10 {
prec = 1
}
// Convert float to string
valStr := strconv.FormatFloat(val, 'f', prec, 64)
// Return string formatted like so:
// - 10 kB file
// or:
// d 0 B .
return fmt.Sprintf(
"%c %3s %-2s %s",
isDirChar,
valStr,
unit,
fi.Name,
)
}
// bytesHuman returns a human-readable string for
// the amount of bytes inputted.
func bytesHuman(b int64) (float64, string) {
const unit = 1000
// Set possible units prefixes (PineTime flash is 4MB)
units := [2]rune{'k', 'M'}
// If amount of bytes is less than smallest unit
if b < unit {
// Return unchanged with unit "B"
return float64(b), "B"
}
div, exp := int64(unit), 0
// Get decimal values and unit prefix index
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
// Create string for full unit
unitStr := string([]rune{units[exp], 'B'})
// Return decimal with unit string
return float64(b) / float64(div), unitStr
}