106 lines
4.9 KiB
Java
106 lines
4.9 KiB
Java
import com.ruoyi.RuoYiApplication;
|
||
import com.ruoyi.common.constant.IotConstants;
|
||
import com.ruoyi.common.constant.ServiceConstants;
|
||
import com.ruoyi.common.utils.map.GeoUtils;
|
||
import com.ruoyi.common.utils.onenet.Token;
|
||
import com.ruoyi.system.domain.AsDevice;
|
||
import com.ruoyi.system.domain.EtOperatingArea;
|
||
import com.ruoyi.system.service.IAsDeviceService;
|
||
import com.ruoyi.system.service.IEtOperatingAreaService;
|
||
import com.ruoyi.system.service.ISysConfigService;
|
||
import lombok.SneakyThrows;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.junit.Test;
|
||
import org.junit.runner.RunWith;
|
||
import org.locationtech.jts.geom.Geometry;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.boot.test.context.SpringBootTest;
|
||
import org.springframework.test.context.junit4.SpringRunner;
|
||
|
||
import javax.annotation.Resource;
|
||
import java.math.BigDecimal;
|
||
|
||
|
||
@Slf4j
|
||
@RunWith(SpringRunner.class)
|
||
@SpringBootTest(classes = RuoYiApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||
public class MainTests {
|
||
|
||
@Resource
|
||
private IAsDeviceService asDeviceService;
|
||
|
||
@Autowired
|
||
private ISysConfigService sysConfigService;
|
||
|
||
@Resource
|
||
private IEtOperatingAreaService etOperatingAreaService;
|
||
|
||
@Test
|
||
@SneakyThrows
|
||
public void testSelectActivityById() {
|
||
// 记录开始时间
|
||
long startTime = System.nanoTime();
|
||
|
||
AsDevice device = asDeviceService.selectAsDeviceByMac("75D30EA71454");
|
||
BigDecimal lon = new BigDecimal("120.25709");
|
||
BigDecimal lat = new BigDecimal("27.105487");
|
||
|
||
EtOperatingArea area = etOperatingAreaService.selectEtOperatingAreaByAreaId(device.getAreaId());
|
||
String isAdminUnlocking = device.getIsAdminUnlocking();// 是否是管理员开锁:0-否;1-是
|
||
|
||
String nearBoundaryDistanceConfig = sysConfigService.selectConfigByKey("near.boundary.distance");// 靠近运营区边界时的播报距离
|
||
log.info("靠近运营区边界时的播报距离==================:" + nearBoundaryDistanceConfig);
|
||
double nearBoundaryDistance = Double.parseDouble(nearBoundaryDistanceConfig);
|
||
String exceedArea = sysConfigService.selectConfigByKey("exceed.area.distance");// 超出运营区外断电距离
|
||
log.info("超出运营区外断电距离================:" + exceedArea);
|
||
int exceedDistance = Integer.parseInt(exceedArea);
|
||
|
||
|
||
// 创建多边形对象
|
||
Geometry polygon = GeoUtils.fromWkt(area.getBoundary());
|
||
// 是否在缩短后的运营区边界内
|
||
boolean inPolygonWithTolerance = GeoUtils.isInPolygonWithShorten(lon.toString(), lat.toString(), polygon, nearBoundaryDistance);
|
||
if(!inPolygonWithTolerance && !isAdminUnlocking.equals("1")){//没有在缩短后的运营区边界内
|
||
boolean inPolygonWithTolerance1 = GeoUtils.isInPolygonWithTolerance(lon.toString(), lat.toString(), polygon, 0);// 是否在运营区内
|
||
boolean isNearBoundary = GeoUtils.isInPolygonWithTolerance(lon.toString(), lat.toString(), polygon, exceedDistance );
|
||
if(inPolygonWithTolerance1){//是否在运营区边界内
|
||
// 在靠近运营区边界时发警报
|
||
log.info("靠近运营区边界发警告命令--SN:" + device.getSn());
|
||
asDeviceService.sendCommand(device.getMac(), Token.getToken(), IotConstants.COMMAND_PLAY2, "靠近运营区边界", null, null);
|
||
}else if(isNearBoundary){ // 是否在超出运营区边界多少米内
|
||
//在20米范围内,发报警
|
||
log.info("超出运营区"+exceedDistance+"米内发送警告命令--SN:" + device.getSn());
|
||
asDeviceService.sendCommand(device.getMac(), Token.getToken(), IotConstants.COMMAND_PLAY3, "超出运营区"+exceedDistance+"米内",null,null);
|
||
}else{
|
||
// 超出运营区外断电
|
||
String areaOutOutage = area.getAreaOutOutage();
|
||
if (areaOutOutage.equals("1")) { // 超出营运区断电
|
||
log.info("超出营运区断电命令--SN:" + device.getSn());
|
||
asDeviceService.sendCommand(device.getMac(), Token.getToken(), IotConstants.COMMAND_QLOSE+IotConstants.COMMAND_FREQUENCY_5, "超出营运区断电",null,null);
|
||
device.setLockStatus(ServiceConstants.LOCK_STATUS_CLOSE);
|
||
int updateAsDevice = asDeviceService.updateAsDevice(device);
|
||
if (updateAsDevice > 0) {
|
||
log.info("禁行区内断电--更新设备锁状态成功SN:" + device.getSn());
|
||
}
|
||
}
|
||
}
|
||
}else{
|
||
log.info("在运营区域内");
|
||
}
|
||
|
||
// 记录结束时间
|
||
long endTime = System.nanoTime();
|
||
|
||
// 计算执行时间(以毫秒为单位)
|
||
long duration = (endTime - startTime) / 1_000_000;
|
||
|
||
// 输出执行时间
|
||
System.out.println("Execution time: " + duration + " milliseconds");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|