|
|
@ -0,0 +1,172 @@ |
|
|
|
|
|
package com.topsail.scheduletask.task; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
|
import com.topsail.scheduletask.mapper.InformLogDao; |
|
|
|
|
|
import com.topsail.scheduletask.mapper.TopsailTransmitLogDao; |
|
|
|
|
|
import com.topsail.scheduletask.pojo.DeviceOnlineInfo; |
|
|
|
|
|
import com.topsail.scheduletask.pojo.FocusDeviceOnline; |
|
|
|
|
|
import com.topsail.scheduletask.pojo.TopsailProductData; |
|
|
|
|
|
import com.topsail.scheduletask.service.AmqpService; |
|
|
|
|
|
import com.topsail.scheduletask.util.MaiSenderlUtil; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
import org.springframework.core.env.Environment; |
|
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling; |
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent; |
|
|
|
|
|
import org.springframework.context.event.EventListener; |
|
|
|
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
|
import java.util.*; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
@Component |
|
|
|
|
|
@Configuration //1.主要用于标记配置类,兼备Component的效果。 |
|
|
|
|
|
@EnableScheduling // 2.开启定时任务 |
|
|
|
|
|
public class ProductDataScheduleTask { |
|
|
|
|
|
final static SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss"); |
|
|
|
|
|
@Autowired |
|
|
|
|
|
AmqpService amqpService; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
TopsailTransmitLogDao topsailTransmitLogDao; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
InformLogDao informLogDao; |
|
|
|
|
|
|
|
|
|
|
|
//1.每2小时定时生成数据 |
|
|
|
|
|
@Scheduled(cron = "0 0 */2 * * ?") |
|
|
|
|
|
// @Scheduled(fixedRate=5000) |
|
|
|
|
|
private void productData() { |
|
|
|
|
|
doProductData(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @EventListener(ApplicationReadyEvent.class) |
|
|
|
|
|
// private void initProductData() { |
|
|
|
|
|
// doProductData(); |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
private void doProductData() { |
|
|
|
|
|
//1.查询需要生成数据的设备 |
|
|
|
|
|
List<TopsailProductData> productDataDeviceList = topsailTransmitLogDao.getProductDataDevice(1); |
|
|
|
|
|
if (productDataDeviceList != null && productDataDeviceList.size() > 0) { |
|
|
|
|
|
for (TopsailProductData topsailProductData : productDataDeviceList) { |
|
|
|
|
|
String imei = topsailProductData.getImei(); |
|
|
|
|
|
String sampleData = topsailProductData.getSampleData(); |
|
|
|
|
|
if (sampleData != null) { |
|
|
|
|
|
|
|
|
|
|
|
JSONObject object = JSON.parseObject(sampleData); |
|
|
|
|
|
object.put("Imei", imei); |
|
|
|
|
|
object.put("IMEI", imei); |
|
|
|
|
|
Calendar calendar = Calendar.getInstance(); |
|
|
|
|
|
calendar.setTime(new Date()); |
|
|
|
|
|
long timeInMillis = calendar.getTimeInMillis(); |
|
|
|
|
|
object.put("timestamp", timeInMillis); |
|
|
|
|
|
String format = sdf.format(calendar.getTime()); |
|
|
|
|
|
object.put("sendTime", format); |
|
|
|
|
|
String dataBody = generateDeviceData(imei, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), 99, 23); |
|
|
|
|
|
// object.put("dataBody", dataBody); |
|
|
|
|
|
// amqpService.SendExchange("aepProfile", object.toJSONString()); |
|
|
|
|
|
String s = object.toJSONString(); |
|
|
|
|
|
//将字符串7470736C01001D0438363233333530353834333436363030260623140523641401403267BC4C696F74替换为dataBody |
|
|
|
|
|
s = s.replace("7470736C01001D0438363233333530353839343438303930260623163752641C0140344983C2696F74", dataBody); |
|
|
|
|
|
System.out.println(s); |
|
|
|
|
|
s = s.replace("862335058944809", imei); |
|
|
|
|
|
s = s.replace("862335058944809", imei); |
|
|
|
|
|
s = s.replace("260623163752", format); |
|
|
|
|
|
amqpService.SendMessage("aepmq", s); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
/** |
|
|
|
|
|
* 根据以下样例进行开发 |
|
|
|
|
|
* 要求:不同的设备号imei,不同是时间生成不同的源数据 |
|
|
|
|
|
* 源数据样例: |
|
|
|
|
|
* 7470736C01001D0438363233333530353834333436363030260623140523641401403267BC4C696F74 |
|
|
|
|
|
* 源数据共82个字符,其中是17到46位是IMEI,49到60位是时间 |
|
|
|
|
|
* 源数据分割后如下: |
|
|
|
|
|
* 7470736C01001D04 |
|
|
|
|
|
* 383632333335303538343334363630 (IMEI:862335058434660) |
|
|
|
|
|
* 30 |
|
|
|
|
|
* 260623140523(时间:26年6月23日14时5分23秒) |
|
|
|
|
|
* 641401403267BC4C696F74 |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 生成设备源数据 |
|
|
|
|
|
* |
|
|
|
|
|
* @param imei 设备IMEI号(15位) |
|
|
|
|
|
* @param year 年份(20-99表示2020-2099) |
|
|
|
|
|
* @param month 月份(1-12) |
|
|
|
|
|
* @param day 日期(1-31) |
|
|
|
|
|
* @param hour 小时(0-23) |
|
|
|
|
|
* @param minute 分钟(0-59) |
|
|
|
|
|
* @param second 秒(0-59) |
|
|
|
|
|
* @param battery 电量百分比(0-100) |
|
|
|
|
|
* @param signal 信号强度(0-30) |
|
|
|
|
|
* @return 生成的十六进制源数据字符串 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static String generateDeviceData(String imei, int year, int month, int day, |
|
|
|
|
|
int hour, int minute, int second, |
|
|
|
|
|
int battery, int signal) { |
|
|
|
|
|
// 协议头:7470736C = "tpsl" |
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
// sb.append("7470736C"); |
|
|
|
|
|
sb.append("7470736C01001D04"); |
|
|
|
|
|
|
|
|
|
|
|
// 消息类型和设备类型:01001D04 |
|
|
|
|
|
// 01: 消息类型 |
|
|
|
|
|
// 001D: 数据长度(29字节) |
|
|
|
|
|
// 04: 设备类型(无线远程闷盖采集终端) |
|
|
|
|
|
// sb.append("01001D04"); |
|
|
|
|
|
|
|
|
|
|
|
// IMEI转换为十六进制ASCII (15位IMEI + 1位预留0) |
|
|
|
|
|
String imeiHex = stringToAscii(imei); |
|
|
|
|
|
sb.append(imeiHex); |
|
|
|
|
|
|
|
|
|
|
|
// 预留位 |
|
|
|
|
|
sb.append("30"); |
|
|
|
|
|
|
|
|
|
|
|
// 时间戳转换为BCD格式 (6字节) |
|
|
|
|
|
String timeBCD = String.format("%02d%02d%02d%02d%02d%02d", |
|
|
|
|
|
year % 100, month, day, hour, minute, second); |
|
|
|
|
|
// sb.append("260623140523"); |
|
|
|
|
|
sb.append(timeBCD); |
|
|
|
|
|
// |
|
|
|
|
|
// // 电量 (1字节) |
|
|
|
|
|
// sb.append(String.format("%02x", battery)); |
|
|
|
|
|
// |
|
|
|
|
|
// // 信号强度 (1字节) |
|
|
|
|
|
// sb.append(String.format("%02x", signal)); |
|
|
|
|
|
|
|
|
|
|
|
// 固定尾部数据 |
|
|
|
|
|
// sb.append("1401403267BC4C696F74"); |
|
|
|
|
|
sb.append("6414014032670000696F74"); |
|
|
|
|
|
|
|
|
|
|
|
return sb.toString().toUpperCase(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将Imei和Imsi,server_ip,server_port,APN转换成3132等形式 |
|
|
|
|
|
public static String stringToAscii(String value) { |
|
|
|
|
|
StringBuffer sbu = new StringBuffer(); |
|
|
|
|
|
char[] chars = value.toCharArray(); |
|
|
|
|
|
for (int i = 0; i < chars.length; i++) { |
|
|
|
|
|
sbu.append(String.format("%02x", (int) chars[i])); |
|
|
|
|
|
} |
|
|
|
|
|
return sbu.toString(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
Calendar calendar = Calendar.getInstance(); |
|
|
|
|
|
calendar.setTime(new Date()); |
|
|
|
|
|
String format = sdf.format(calendar.getTime()); |
|
|
|
|
|
System.out.println(format); |
|
|
|
|
|
String s1 = generateDeviceData("866150058927993", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), 99, 23); |
|
|
|
|
|
// String s2 = generateDeviceData("866150058859295", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), 99, 23); |
|
|
|
|
|
System.out.println(s1); |
|
|
|
|
|
// System.out.println(s2); |
|
|
|
|
|
} |
|
|
|
|
|
} |