ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

python版本的海萤物联网ntp服务上线

python版本的海萤物联网ntp服务上线 文章目录python版本的海萤物联网ntp服务上线开源介绍服务读取时间服务1读取时间服务2.返回的是结构体自定义错误码示例读取时间读取时区为2的时间读取时区为-6的时间读取结构体格式时间源码main.pyconfig.pypython版本的海萤物联网ntp服务上线本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.在线文档地址开源github上的项目地址gitee上的项目地址介绍基于Python编写在海萤物联网提供校时服务。本机地址是0x2141000000000405服务服务号服务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,errtziot.call(pipe,0x2141000000000005,1,1000,bytearray())print(err:,err,time:,resp)输出err: 0 time: b2021-03-22 10:06:09 0800 MST读取时区为2的时间resp,errtziot.call(pipe,0x2141000000000005,1,1000,bytearray([2]))print(err:,err,time:,resp)输出err: 0 time: b2021-03-22 04:07:20 0200 MST读取时区为-6的时间resp,errtziot.call(pipe,0x2141000000000005,1,1000,bytearray([0x100-6]))print(err:,err,time:,resp)输出err: 0 time: b2021-03-21 20:08:14 -0600 MST读取结构体格式时间importtziotimportsbcclassAckRidGetTime(sbc.LEFormat):_fields_[# (字段名, c类型)# 时区(time_zone,sbc.c_uint8),(year,sbc.c_int16),(month,sbc.c_uint8),(day,sbc.c_uint8),(hour,sbc.c_uint8),(minute,sbc.c_uint8),(second,sbc.c_uint8),# 星期(weekday,sbc.c_uint8),]defmain():pipetziot.bind_pipe_net(0x2141000000000401,pwd,192.168.1.119,12021)whilenottziot.is_conn():passresp,errtziot.call(pipe,0x2141000000000005,2,3000,bytearray())iferr!0:returnackAckRidGetTime()ifnotack.bytearray_to_struct(resp):returnprint(ack.time_zone,ack.year,ack.month,ack.day,ack.hour,ack.minute,ack.second,ack.weekday)if__name____main__:main()输出8 2021 4 2 11 43 7 5源码最新源码请查看仓库。main.py Copyright 2021-2021 The jdh99 Authors. All rights reserved. 网络校时服务 Authors: jdh99 jdh821163.com importconfigimporttziotimportdcompyasdcomimportlaganimportsbcfromdatetimeimportdatetime,timedelta TAGntp# 应用错误码# 内部错误ERROR_CODE_INTERNAL_ERROR0x40# 接收格式错误ERROR_CODE_RX_FORMAT0x41# rid号# 读取时间.返回的是字符串RID_GET_TIME11# 读取时间.返回的是结构体RID_GET_TIME22classAckRidGetTime2(sbc.LEFormat):_fields_[# (字段名, c类型)# 时区(time_zone,sbc.c_uint8),(year,sbc.c_int16),(month,sbc.c_uint8),(day,sbc.c_uint8),(hour,sbc.c_uint8),(minute,sbc.c_uint8),(second,sbc.c_uint8),# 星期(weekday,sbc.c_uint8),]defmain():config.init()lagan.load(0)lagan.set_filter_level(lagan.LEVEL_INFO)lagan.enable_color(True)dcom.set_filter_level(lagan.LEVEL_WARN)tziot.bind_pipe_net(config.LOCAL_IA,config.local_pwd,config.LOCAL_IP,config.LOCAL_PORT)tziot.register(RID_GET_TIME1,ntp_service1)tziot.register(RID_GET_TIME2,ntp_service2)defntp_service1(pipe:int,src_ia:int,req:bytearray)-(bytearray,int):校时服务.返回值是应答和错误码.错误码为0表示回调成功,否则是错误码ip,portdcom.pipe_to_addr(pipe)iflen(req)0:time_zone8eliflen(req)1:time_zonereq[0]iftime_zone0x80:time_zone-(0x100-time_zone)else:lagan.warn(TAG,ip:%s port:%d ia:0x%x ntp failed.len is wrong:%d,ip,port,src_ia,len(req))returnNone,ERROR_CODE_RX_FORMAT nowdatetime.utcnow()timedelta(hourstime_zone)iftime_zone0:s%04d-%02d-%02d %02d:%02d:%02d %02d00 MST%(now.year,now.month,now.day,now.hour,now.minute,now.second,time_zone)else:s%04d-%02d-%02d %02d:%02d:%02d -%02d00 MST%(now.year,now.month,now.day,now.hour,now.minute,now.second,-time_zone)lagan.info(TAG,ip:%s port:%d ntp time:%s,ip,port,s)returntziot.str_to_bytearray(s),0defntp_service2(pipe:int,src_ia:int,req:bytearray)-(bytearray,int):校时服务.返回值是应答和错误码.错误码为0表示回调成功,否则是错误码ip,portdcom.pipe_to_addr(pipe)iflen(req)0:time_zone8eliflen(req)1:time_zonereq[0]iftime_zone0x80:time_zone-(0x100-time_zone)else:lagan.warn(TAG,ip:%s port:%d ia:0x%x ntp failed.len is wrong:%d,ip,port,src_ia,len(req))returnNone,ERROR_CODE_RX_FORMAT nowdatetime.utcnow()timedelta(hourstime_zone)tAckRidGetTime2()t.time_zonetime_zone t.yearnow.year t.monthnow.month t.daynow.day t.hournow.hour t.minutenow.minute t.secondnow.second t.weekdaynow.isoweekday()lagan.info(TAG,ip:%s port:%d ntp time:%04d-%02d-%02d %02d:%02d:%02d %02d00 MST,ip,port,t.year,t.month,t.day,t.hour,t.minute,t.second,t.time_zone)returnt.struct_to_bytearray(),0if__name____main__:main()config.py Copyright 2021-2021 The jdh99 Authors. All rights reserved. 配置文件 Authors: jdh99 jdh821163.com # 系统参数LOCAL_IA0x2141000000000005LOCAL_IP0.0.0.0LOCAL_PORT12931local_pwddefinit():globallocal_pwdprint(please input password:)local_pwdinput()
返回列表