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.

71 lines
1.7 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 (
"fmt"
"github.com/gogf/gf/v2/errors/gerror"
"log"
)
// 日出日落时间偏差调节
// SunRiseSet 日出日落时间偏差调节(单位:分钟)
type SunRiseSet struct {
Rise int `json:"rise"` // 日出偏差(-60~60分钟
Set int `json:"set"` // 日落偏差(-60~60分钟
}
func SunRiseSetRead(deviceId string) (*SunRiseSet, error) {
cmd, err := ReadCmd(deviceId, sunRiseSet, "读取日出日落时间偏差")
if err != nil {
return nil, err
}
data := cmd.Data
srs := new(SunRiseSet)
log.Printf("日出日落偏差% X", data)
if int(data[0]) > 60 {
srs.Rise = -(256 - int(data[0]))
} else {
srs.Rise = int(data[0])
}
if int(data[1]) > 60 {
srs.Set = -(256 - int(data[1]))
} else {
srs.Set = int(data[1])
}
log.Println(srs.Rise, srs.Set)
return srs, nil
}
func (srs *SunRiseSet) Write(deviceId string) error {
return SunRiseSetWrite(deviceId, srs)
}
// SunRiseSetWrite 写入日出日落时间偏差设置
func SunRiseSetWrite(deviceId string, srs *SunRiseSet) error {
// 1. 参数校验
if srs.Rise < -60 || srs.Rise > 60 {
return fmt.Errorf("日出偏差值超出范围(-60~60分钟当前值%d", srs.Rise)
}
if srs.Set < -60 || srs.Set > 60 {
return fmt.Errorf("日落偏差值超出范围(-60~60分钟当前值%d", srs.Set)
}
// 2. 准备写入数据2字节
data := []byte{
byte(srs.Rise), // 日出偏差
byte(srs.Set), // 日落偏差
}
// 4. 发送命令
resp, err := WriteCmd(deviceId, sunRiseSet, data, "写入日出日落时间偏差")
if err != nil {
return fmt.Errorf("设备通信失败: %v", err)
}
// 5. 验证设备响应
if !resp.Success {
return gerror.New("设备返回写入失败")
}
return nil
}