You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package m9z
import (
"encoding/binary"
"fmt"
"time"
)
type SunTimes struct {
Sunrise time.Time // 日出时间(转换为当日时间对象)
Sunset time.Time // 日落时间
RawSunrise int16 // 原始日出分钟数(从午夜开始的分钟偏移)
RawSunset int16 // 原始日落分钟数
}
func SunRiseTimeRead(deviceId string) (*SunTimes, error) {
cmd, err := ReadCmd(deviceId, sunRiseTime, "读取日出日落时间")
if err != nil {
return nil, err
}
data := cmd.Data
// 数据长度校验至少需要4字节
if len(data) < 4 {
return nil, fmt.Errorf("invalid data length: %d, expect at least 4 bytes", len(data))
}
st := &SunTimes{}
// 1. 解析日出时间小端序int16
st.RawSunrise = int16(binary.LittleEndian.Uint16(data[0:2]))
// 2. 解析日落时间小端序int16
st.RawSunset = int16(binary.LittleEndian.Uint16(data[2:4]))
// 转换为当日时间假设基于UTC的当天
toDayFmt := time.Now().Format(time.DateOnly)
location, _ := time.LoadLocation("Asia/Shanghai")
baseTime, _ := time.ParseInLocation(time.DateOnly, toDayFmt, location)
st.Sunrise = baseTime.Add(time.Duration(st.RawSunrise) * time.Minute)
st.Sunset = baseTime.Add(time.Duration(st.RawSunset) * time.Minute)
return st, nil
}