package com.telpo.dipperposition.service.impl; import com.telpo.dipperposition.common.HexConvert; import com.telpo.dipperposition.common.SocketClient; import com.telpo.dipperposition.service.IDipperAstTimeAsyncTaskService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.time.LocalDateTime; /** * @program: DipperDataAsyncTaskServiceImpl * @description: 发送SDBP-AST-TIME获取时间信息 * @author: king * @create: 2021-01-10 14:01 */ @Service @Slf4j public class DipperAstTimeAsyncTaskServiceImpl implements IDipperAstTimeAsyncTaskService { @Override @Async("asyncServiceExecutor") public String pushAstTime() { // (1) 发送SDBP-AST-TIME // String sendResult = pushTimeToDipper(); //if (sendResult == null) { // log.error("取不到时间。"); // return null; //} // (2) 获取时间信息 return pushTimeToDipper(); //return sendResult; } private String pushTimeToDipper() { // 创建Socket客户端实例; // SocketClient client = new SocketClient(astServer, timeAstPort, astTimeout); // 时间和位置不是从服务器获取,而是本地生成(其中时间误差不超过3s) // 23 3E 04 02 10 00 20 E1 07 09 14 0C 22 38 00 00 00 00 00 28 6B EE 22 98 // 23 3E 为同步头 // 04 02 为识别码 // 10 00 表示长度为 16 // --日期-- // 20 E1 表示闰秒改正数为 // E1 07 表示年为 2017 年(十六进制 07E1 转为十进制) // 09 表示 9 月 // 14 表示 20 日 // 0C 22 38 00 00 00 00 00 表示 UTC时间,为12时34分56.0秒(小数秒建议固定为 0) // 00 28 6B EE 表示 4 秒的时间精度(十六进制 EE6B2800 转为十进制为 4000000000,乘以比 例因子 10-9就是 4 秒) // 00 2F 为校验和 // TODO astTimeCmd 组装 String astTimeCmd = "233E0402"; astTimeCmd += "1000"; astTimeCmd += "20E1"; LocalDateTime now = LocalDateTime.now(); int year = now.getYear(); int month = now.getMonthValue(); int day = now.getDayOfMonth(); String hexYearString = Integer.toHexString(year); hexYearString = "0" + hexYearString; astTimeCmd += hexYearString.substring(2,2) + hexYearString.substring(0,2); String hexMonthString = Integer.toHexString(month); hexMonthString = "0" + hexMonthString; astTimeCmd += hexMonthString; String hexDayString = Integer.toHexString(day); if (day < 16) { hexDayString = "0" + hexDayString; } astTimeCmd += hexDayString; int hour = now.getHour(); int minitor = now.getMinute(); int second = now.getSecond(); String hexHourString = Integer.toHexString(hour); if (hour < 16) { hexHourString = "0" + hexHourString; } astTimeCmd += hexHourString; String hexMinitorString = Integer.toHexString(minitor); if (minitor < 16) { hexMinitorString = "0" + hexMinitorString; } astTimeCmd += hexMinitorString; String hexSecondString = Integer.toHexString(second); if (second < 16) { hexSecondString = "0" + hexSecondString; } astTimeCmd += hexSecondString; astTimeCmd += "0000000000"; astTimeCmd += "00286BEE"; //String hexIn = HexConvert.convertStringToHex(astTimeCmd) + HexConvert.makeChecksum(astTimeCmd); String hexIn = astTimeCmd + HexConvert.makeChecksum(astTimeCmd); //String ackAckCheckRef = "233E0101020004020A1D"; //String sendResult = client.sendCmd(hexIn, ackAckCheckRef); //client.closeConnection(); return hexIn; } }