资讯动态

海萤物联网ntp服务上线

发布时间:2026/8/13 21:08:31 来源:尧图企业网站定制
文章目录海萤物联网ntp服务上线开源介绍服务读取时间服务1读取时间服务2.返回的是结构体自定义错误码示例读取时间读取时区为2的时间读取时区为-6的时间读取结构体格式时间源码main.goconfig.go海萤物联网ntp服务上线本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.在线文档地址开源github上的项目地址gitee上的项目地址介绍基于Golang编写在海萤物联网提供校时服务。本机地址是0x2141000000000404服务服务号服务1读取时间12读取时间2.返回的是结构体读取时间服务1CON请求空或者带符号的1个字节。当CON请求为空时则默认为读取的是北京时间时区8。也可以带1个字节表示时区号。这个字节是有符号的int8。小技巧可以使用0x100减去正值即负值。比如8对应的无符号数是0x100-8248。ACK应答当前时间的字符串当前时间字符串的格式2006-01-02 15:04:05 -0700 MST读取时间服务2.返回的是结构体CON请求格式与读取时间服务1一致ACK应答struct{// 时区uint8 TimeZone uint16 Year uint8 Month uint8 Day uint8 Hour uint8 Minute uint8 Second// 星期uint8 Weekday}自定义错误码错误码含义0x40内部错误0x41接收格式错误示例读取时间resp,err:tziot.Call(pipe,0x2141000000000004,1,1000,[]uint8{})fmt.Println(err:,err,time:,string(resp))输出err: 0 time: 2021-03-20 06:34:18 0800 CST读取时区为2的时间resp,err:tziot.Call(pipe,0x2141000000000004,1,1000,[]uint8{2})fmt.Println(err:,err,time:,string(resp))输出err: 0 time: 2021-03-20 00:36:31 0200 CST读取时区为-6的时间resp,err:tziot.Call(pipe,0x2141000000000004,1,1000,[]uint8{0x100-6})fmt.Println(err:,err,time:,string(resp))输出err: 0 time: 2021-03-19 16:36:06 -0600 CST读取结构体格式时间resp,_:tziot.Call(pipe,0x2141000000000004,2,3000,[]uint8{8})varack AckRidGetTime2_dcom.BytesToStruct(resp,ack)fmt.Println(ack)输出{8 2021 3 30 12 11 28 2}源码最新源码请查看仓库。main.go// Copyright 2021-2021 The jdh99 Authors. All rights reserved.// 网络校时服务// Authors: jdh99 jdh821163.compackagemainimport(github.com/jdhxyy/dcomgithub.com/jdhxyy/lagangithub.com/jdhxyy/tziotntp/configtime)consttagntp// 应用错误码const(// 内部错误errorCodeInternalError0x40// 接收格式错误errorCodeRxFormat0x41)// rid号const(// 读取时间.返回的是字符串ridGetTime11// 读取时间.返回的是结构体ridGetTime22)// ACK格式typeAckRidGetTime2struct{// 时区TimeZoneuint8Yearuint16Monthuint8Dayuint8Houruint8Minuteuint8Seconduint8// 星期Weekdayuint8}funcmain(){err:lagan.Load(0)iferr!nil{panic(err)}lagan.EnableColor(true)lagan.SetFilterLevel(lagan.LevelInfo)_,errtziot.BindPipeNet(config.LocalIA,config.LocalPwd,config.LocalIP,config.LocalPort)iferr!nil{panic(err)return}tziot.Register(ridGetTime1,ntpService1)tziot.Register(ridGetTime2,ntpService2)select{}}// ntpService1 校时服务// 返回值是应答和错误码.错误码为0表示回调成功,否则是错误码funcntpService1(pipeuint64,srcIAuint64,req[]uint8)([]uint8,int){addr:dcom.PipeToAddr(pipe)vartimeZoneintiflen(req)0{timeZone8}elseiflen(req)1{timeZoneint(int8(req[0]))}else{lagan.Warn(tag,addr:%v ia:0x%x ntp failed.len is wrong:%d,addr,srcIA,len(req))returnnil,errorCodeRxFormat}t:getTime(timeZone)lagan.Info(tag,addr:%v ia:0x%x ntp time:%v,addr,srcIA,t)return[]uint8(t.Format(2006-01-02 15:04:05 -0700 MST)),0}funcgetTime(timeZoneint)time.Time{t:time.Now().UTC()secondsEastOfUTC:int((time.Duration(timeZone)*time.Hour).Seconds())loc:time.FixedZone(CST,secondsEastOfUTC)tt.In(loc)returnt}// ntpService2 校时服务// 返回值是应答和错误码.错误码为0表示回调成功,否则是错误码funcntpService2(pipeuint64,srcIAuint64,req[]uint8)([]uint8,int){addr:dcom.PipeToAddr(pipe)vartimeZoneintiflen(req)0{timeZone8}elseiflen(req)1{timeZoneint(int8(req[0]))}else{lagan.Warn(tag,addr:%v ia:0x%x ntp failed.len is wrong:%d,addr,srcIA,len(req))returnnil,errorCodeRxFormat}t:getTime(timeZone)lagan.Info(tag,addr:%v ia:0x%x ntp time:%v,addr,srcIA,t)varack AckRidGetTime2 ack.TimeZoneuint8(timeZone)ack.Yearuint16(t.Year())ack.Monthuint8(t.Month())ack.Dayuint8(t.Day())ack.Houruint8(t.Hour())ack.Minuteuint8(t.Minute())ack.Seconduint8(t.Second())ack.Weekdayuint8(t.Weekday())data,err:dcom.StructToBytes(ack)iferr!nil{lagan.Error(tag,addr:%v ia:0x%x ntp failed.struct to bytes error:%v,addr,srcIA,err)returnnil,errorCodeInternalError}returndata,0}config.go// Copyright 2021-2021 The jdh99 Authors. All rights reserved.// 配置文件// Authors: jdh99 jdh821163.compackageconfigimportfmt// 系统参数const(LocalIA0x2141000000000004LocalIP0.0.0.0LocalPort12930)varLocalPwdstringfuncinit(){fmt.Println(please input password:)_,err:fmt.Scanln(LocalPwd)iferr!nil{panic(err)}}

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价