NTP client for node js / particle

NTP 封包內容

0~3 Bytes:
   0                   1                   2                   3
   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |LI | VN  |Mode |    Stratum     |     Poll      |  Precision   |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4~7 Bytes:
  Estimated Error
8~11 Bytes:
  Estimated Drift Rate
12~15 Bytes:
  Reference Clock Identifier
16~23 Bytes:
  Reference Timestamp
24~31 Bytes:
  Originate Timestamp
32~39 Bytes:
  Receive Timestamp
40~47 Bytes:
  Transit Timestamp

for node js (參考網路程式碼)

//refer: https://github.com/moonpyk/node-ntp-client/blob/master/lib/ntp-client.js
 
var NTPClient=function(server,port){
  var self=this;
  var construct=function (server,port){
    self.server=server;
    self.port=port;
    self.t1=self.now();
    self.last_time=self.now();
    update();
  }
  self.now=function(){
    return new Date().getTime()/1000.0;
  }
  var intParer=function(packetBuffer,offset){
    var intpart = 0
    for (var i = 0; i <= 3; i++) intpart = 256 * intpart + packetBuffer[offset + i]; return intpart; } var timestampParser=function(packetBuffer,offset){ var intpart = intParer(packetBuffer,offset); var fractpart = intParer(packetBuffer,offset+4); //2208988800 = (70 * 365 + 17) * 24 * 60 * 60 是 1970與1900之秒差 if(intpart>2208988800)
      intpart-=2208988800;    
    return parseInt(intpart)+fractpart/0x100000000;
  }
  var update=function(){
    var ntpData = new Buffer(48);   //ntpData= 0x1B,0,0,0,0,~,0  //length:48, 24~31 bytes為本地端時間
    ntpData[0] = 0x1B;
    for (var i = 1; i < 48; i++) ntpData[i] = 0; var udp = require('dgram'); var client = udp.createSocket("udp4"); var timeout = setTimeout(function () { console.log('[error]','timeout'); client.close(); },3000); var errorFired = false; client.on('error', function (err) { if (errorFired) { return; } errorFired = true; console.log('[error]','timeout'); clearTimeout(timeout); }); client.send(ntpData, 0, ntpData.length, port, server, function (err) { if (err) { if (errorFired) { return; } clearTimeout(timeout); errorFired = true; client.close(); return; } client.once('message', function (msg) { clearTimeout(timeout); client.close(); self.t1=self.now(); self.last_time=timestampParser(msg,40); console.log('======================',(self.last_time-self.t1)); }); }); } var dt=function(){ return self.now()-self.t1; }; self.time=function(){ if(dt()>6)
      update();
    return self.last_time+dt();
  }
  construct(server,port);
};
 
 
 
var server = "time.stdtime.gov.tw"; //Stratum=2 (第二階時間伺服器)
//server="time.nc7j.com"; //Stratum=1 (第一階時間伺服器)
server="10.31.160.1"; //Stratum=3
var port = 123;
 
ntp=new NTPClient(server,port);
console.log(ntp.time());
setInterval(function(){
  console.log(ntp.now(),ntp.time());
},100);

for paritcle

class NTPClient{
  private:
    double last_time;
    double time_diff;
    unsigned long t1;
    unsigned long t2;
    byte address[4];
    int port;
    UDP udp;
    unsigned long intParser(byte* packetBuffer, int offset){
      unsigned long highWord = (packetBuffer[offset+0] >> 8) + packetBuffer[offset+1];
      unsigned long lowWord = (packetBuffer[offset+2] >> 8) + packetBuffer[offset+3];
      unsigned long intpart = highWord >> 16 | lowWord;
      return intpart;
    }
    double timestampParser(byte* packetBuffer, int offset){
      unsigned long intpart = intParser(packetBuffer,offset);
      unsigned long fractpart=intParser(packetBuffer,offset+4);
      //2208988800 = (70 * 365 + 17) * 24 * 60 * 60  是 1970與1900之秒差
      if(intpart>2208988800)
        intpart-=2208988800;
      return (int)(intpart)+(double)fractpart/0x100000000;
    }
    double update(){
      #define NTP_PACKET_SIZE 48
      byte packetBuffer[NTP_PACKET_SIZE]={0};
      memset(packetBuffer, 0, NTP_PACKET_SIZE);
 
      packetBuffer[0]=0x1B;
 
      this->udp.beginPacket(this->address, this->port);
      this->udp.write(packetBuffer, NTP_PACKET_SIZE);
      this->udp.endPacket();
 
      while(1){
          if ( this->udp.parsePacket() ) {
              t1=micros();
              this->udp.read(packetBuffer, NTP_PACKET_SIZE);
              this->last_time=timestampParser(packetBuffer,40);
              return this->last_time;
          }
          delay(10);
      }
      return 0;
    }
    double dt(){
      return (micros()-t1)/1000000.0;
    }
  public:
    NTPClient(byte address[], int port=123){
      Time.zone(8);
      memcpy(this->address, address, 4);
      this->port=port;
      this->udp.begin(8888);    //隨便一個 port
      this->update();
    }
    ~NTPClient(){
 
    }
    double time(){
      if(this->dt()>600)  //每10分鐘強制校時
        this->update();
      return this->last_time+this->dt();
    }
};
 
NTPClient *ntp;
byte address[] = {118,163,81,61}; //time.stdtime.gov.tw
ntp=new NTPClient(address,123);