DeviceAlarmServiceImplTest.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.gb28181.bean.DeviceAlarm;
  3. import com.genersoft.iot.vmp.service.IDeviceAlarmService;
  4. import com.genersoft.iot.vmp.utils.DateUtil;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. import javax.annotation.Resource;
  9. import java.time.Instant;
  10. import java.time.LocalDateTime;
  11. import java.time.ZoneOffset;
  12. import java.time.temporal.TemporalAccessor;
  13. import java.util.Date;
  14. @SpringBootTest
  15. @RunWith(SpringRunner.class)
  16. class DeviceAlarmServiceImplTest {
  17. @Resource
  18. private IDeviceAlarmService deviceAlarmService;
  19. @org.junit.jupiter.api.Test
  20. void getAllAlarm() {
  21. // deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111",null,null,null, null, null);
  22. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, null, null, null, null,
  23. // null, null).getSize());
  24. //
  25. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", null, null, null,
  26. // null, null).getSize());
  27. //
  28. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "1", null, null,
  29. // null, null).getSize());
  30. //
  31. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "2", null, null,
  32. // null, null).getSize());
  33. //
  34. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "3", null, null,
  35. // null, null).getSize());
  36. //
  37. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "4", null, null,
  38. // null, null).getSize());
  39. //
  40. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "5", null, null,
  41. // null, null).getSize());
  42. //
  43. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", null, "1", null,
  44. // null, null).getSize());
  45. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", null, "1", null,
  46. // null, null).getSize());
  47. }
  48. @org.junit.jupiter.api.Test
  49. void add() {
  50. for (int i = 0; i < 1000; i++) {
  51. DeviceAlarm deviceAlarm = new DeviceAlarm();
  52. deviceAlarm.setDeviceId("11111111111111111111");
  53. deviceAlarm.setAlarmDescription("test_" + i);
  54. /**
  55. * 报警方式 , 1为电话报警, 2为设备报警, 3为短信报警, 4为 GPS报警, 5为视频报警, 6为设备故障报警,
  56. * * 7其他报警;可以为直接组合如12为电话报警或 设备报警-
  57. */
  58. deviceAlarm.setAlarmMethod((int)(Math.random()*7 + 1) + "");
  59. Instant date = randomDate("2021-01-01 00:00:00", "2021-06-01 00:00:00");
  60. deviceAlarm.setAlarmTime(DateUtil.formatter.format(date));
  61. /**
  62. * 报警级别, 1为一级警情, 2为二级警情, 3为三级警情, 4为四级 警情-
  63. */
  64. deviceAlarm.setAlarmPriority((int)(Math.random()*4 + 1) + "");
  65. deviceAlarm.setLongitude(116.325);
  66. deviceAlarm.setLatitude(39.562);
  67. deviceAlarmService.add(deviceAlarm);
  68. }
  69. }
  70. @org.junit.jupiter.api.Test
  71. void clearAlarmBeforeTime() {
  72. deviceAlarmService.clearAlarmBeforeTime(null,null, null);
  73. }
  74. private Instant randomDate(String beginDate, String endDate) {
  75. try {
  76. //构造开始日期
  77. LocalDateTime start = LocalDateTime.parse(beginDate, DateUtil.formatter);
  78. //构造结束日期
  79. LocalDateTime end = LocalDateTime.parse(endDate, DateUtil.formatter);
  80. //getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
  81. if (start.isAfter(end)) {
  82. return null;
  83. }
  84. long date = random(start.toInstant(ZoneOffset.of("+8")).toEpochMilli(), end.toInstant(ZoneOffset.of("+8")).toEpochMilli());
  85. return Instant.ofEpochMilli(date);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return null;
  90. }
  91. private static long random(long begin, long end) {
  92. long rtn = begin + (long) (Math.random() * (end - begin));
  93. //如果返回的是开始时间和结束时间,则递归调用本函数查找随机值
  94. if (rtn == begin || rtn == end) {
  95. return random(begin, end);
  96. }
  97. return rtn;
  98. }
  99. }