diff --git a/fastbee-common/src/main/java/com/fastbee/common/model/vo/TreeItemVo.java b/fastbee-common/src/main/java/com/fastbee/common/model/vo/TreeItemVo.java new file mode 100644 index 0000000..7d719f0 --- /dev/null +++ b/fastbee-common/src/main/java/com/fastbee/common/model/vo/TreeItemVo.java @@ -0,0 +1,19 @@ +package com.fastbee.common.model.vo; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class TreeItemVo { + + private Long id; + private String name; + //经度 + private Float longitude; + //纬度 + private Float latitude; + private Integer status = 1;//状态:1-未激活,2-禁用,3-在线,4-离线 + private List children = new ArrayList<>(); +} diff --git a/fastbee-common/src/main/java/com/fastbee/common/model/vo/iot/GisDeviceListVo.java b/fastbee-common/src/main/java/com/fastbee/common/model/vo/iot/GisDeviceListVo.java new file mode 100644 index 0000000..bf32857 --- /dev/null +++ b/fastbee-common/src/main/java/com/fastbee/common/model/vo/iot/GisDeviceListVo.java @@ -0,0 +1,19 @@ +package com.fastbee.common.model.vo.iot; + +import com.fastbee.common.model.vo.TreeItemVo; +import lombok.Data; + +import java.util.List; + +/** + * Gis设备列表 + */ +@Data +public class GisDeviceListVo { + + private Integer count; + private Integer onLineCount; + private List productContainDeviceList; + + +} diff --git a/fastbee-common/src/main/java/com/fastbee/common/utils/DateUtils.java b/fastbee-common/src/main/java/com/fastbee/common/utils/DateUtils.java index 6ec1ca3..40e0f31 100644 --- a/fastbee-common/src/main/java/com/fastbee/common/utils/DateUtils.java +++ b/fastbee-common/src/main/java/com/fastbee/common/utils/DateUtils.java @@ -21,10 +21,11 @@ import org.apache.commons.lang3.time.DateFormatUtils; public class DateUtils extends org.apache.commons.lang3.time.DateUtils { public static String YYYY = "yyyy"; - + public static String YYYYMM = "yyyyMM"; public static String YYYY_MM = "yyyy-MM"; public static String YYYY_MM_DD = "yyyy-MM-dd"; + public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH"; public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; diff --git a/fastbee-open-api/src/main/java/com/fastbee/data/controller/DeviceController.java b/fastbee-open-api/src/main/java/com/fastbee/data/controller/DeviceController.java index c3c30bf..d62a924 100644 --- a/fastbee-open-api/src/main/java/com/fastbee/data/controller/DeviceController.java +++ b/fastbee-open-api/src/main/java/com/fastbee/data/controller/DeviceController.java @@ -481,4 +481,25 @@ public class DeviceController extends BaseController return rspData; } + /** + * 所有设备点数据 + * + * @return + */ + @GetMapping("/devicePointInfo") + @ApiOperation("所有设备点数据") + public AjaxResult devicePointInfo(Device device) { + return AjaxResult.success(deviceService.devicePointInfo(device)); + } + + /** + * 查询设备历史数据曲线图 + */ + @GetMapping("/getDeviceLogAllCurves") + @ApiOperation("查询设备历史数据曲线图") + public AjaxResult getDeviceLogAllCurves(Long deviceId,String beginTime ,String endTime) { + ArrayList data = deviceService.getDeviceLogAllCurves(deviceId,beginTime ,endTime); + return success(data); + } + } diff --git a/fastbee-open-api/src/main/java/com/fastbee/data/controller/GisDeviceController.java b/fastbee-open-api/src/main/java/com/fastbee/data/controller/GisDeviceController.java new file mode 100644 index 0000000..d51c005 --- /dev/null +++ b/fastbee-open-api/src/main/java/com/fastbee/data/controller/GisDeviceController.java @@ -0,0 +1,48 @@ +package com.fastbee.data.controller; + +import com.fastbee.common.core.controller.BaseController; +import com.fastbee.common.core.domain.AjaxResult; +import com.fastbee.data.service.gis.IGisDeviceService; +import com.fastbee.iot.domain.Device; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = "GIS设备") +@RestController +@RequestMapping("/gis/device") +public class GisDeviceController extends BaseController { + @Autowired + private IGisDeviceService gisDeviceService; + + /** + * 获取设备数量和列表 + */ + @ApiOperation("获取设备数量和列表") + @GetMapping("/totalAndList") + public AjaxResult totalAndList(Device device) { + return AjaxResult.success(gisDeviceService.totalAndList(device)); + } + + /** + * 根据设备id获取实时数据 + */ + @GetMapping(value = "/getRealTimeDataByDevicerId") + public AjaxResult getRealTimeDataByDevicerId(Long deviceId) + { + return success(gisDeviceService.getRealTimeDataByDevicerId(deviceId)); + } + + /** + * 根据设备id获取流量水量 + */ + @GetMapping(value = "/getDeviceLogCurves") + public AjaxResult getDeviceLogCurves(Long deviceId) + { + return success(gisDeviceService.getDeviceLogCurves(deviceId)); + } + +} diff --git a/fastbee-open-api/src/main/java/com/fastbee/data/service/gis/IGisDeviceService.java b/fastbee-open-api/src/main/java/com/fastbee/data/service/gis/IGisDeviceService.java new file mode 100644 index 0000000..264c4b7 --- /dev/null +++ b/fastbee-open-api/src/main/java/com/fastbee/data/service/gis/IGisDeviceService.java @@ -0,0 +1,13 @@ +package com.fastbee.data.service.gis; + +import com.fastbee.common.model.vo.iot.GisDeviceListVo; +import com.fastbee.iot.domain.Device; + +public interface IGisDeviceService { + + public GisDeviceListVo totalAndList(Device device); + + Object getRealTimeDataByDevicerId(Long deviceId); + + Object getDeviceLogCurves(Long deviceId); +} diff --git a/fastbee-open-api/src/main/java/com/fastbee/data/service/gis/impl/GisDeviceServiceImpl.java b/fastbee-open-api/src/main/java/com/fastbee/data/service/gis/impl/GisDeviceServiceImpl.java new file mode 100644 index 0000000..6c9fb18 --- /dev/null +++ b/fastbee-open-api/src/main/java/com/fastbee/data/service/gis/impl/GisDeviceServiceImpl.java @@ -0,0 +1,260 @@ +package com.fastbee.data.service.gis.impl; + +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.fastbee.common.model.vo.TreeItemVo; +import com.fastbee.common.model.vo.iot.GisDeviceListVo; +import com.fastbee.common.utils.DateUtils; +import com.fastbee.common.utils.SecurityUtils; +import com.fastbee.common.utils.StringUtils; +import com.fastbee.data.service.gis.IGisDeviceService; +import com.fastbee.iot.domain.Device; +import com.fastbee.iot.domain.DeviceLog; +import com.fastbee.iot.domain.Product; +import com.fastbee.iot.domain.ThingsModel; +import com.fastbee.iot.mapper.DeviceMapper; +import com.fastbee.iot.mapper.ProductMapper; +import com.fastbee.iot.service.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.time.LocalDateTime; +import java.util.*; +import java.util.stream.Collectors; + +@Service +public class GisDeviceServiceImpl implements IGisDeviceService { + + private final DeviceMapper deviceMapper; + private final ProductMapper productMapper; + + @Autowired + public IDeviceService iDeviceService; + @Autowired + public IProductService iProductService; + @Autowired + private IDeviceLogService logService; + +// @Resource +// DeviceInfoCacheService deviceInfoCacheService; + @Autowired + private IAlertService iAlertService; + @Autowired + private IThingsModelService thingsModelService; + + public GisDeviceServiceImpl(@Qualifier("deviceMapper") DeviceMapper deviceMapper, @Qualifier("productMapper") ProductMapper productMapper) { + this.deviceMapper = deviceMapper; + this.productMapper = productMapper; + } + + @Override + public GisDeviceListVo totalAndList(Device device) { + List productEntities = productMapper.selectProductList(new Product()); + Long userId = SecurityUtils.getUserId(); + List productContainDeviceListVos = new ArrayList<>(); + int count = 0; + int onLineCount = 0; + GisDeviceListVo deviceListVo = new GisDeviceListVo(); + for (Product productEntity : productEntities) { + TreeItemVo treeItemVo = new TreeItemVo(); + treeItemVo.setId(1000000 + productEntity.getProductId()); + treeItemVo.setName(productEntity.getProductName()); + List deviceEntities = new ArrayList<>(); + Device deviceEntity = new Device(); + deviceEntity.setProductId(productEntity.getProductId()); + deviceEntity.setStatus(device.getStatus()); + deviceEntity.setDeviceNameOrSerialNumber(device.getDeviceNameOrSerialNumber()); + if (userId.equals(1L)) { + deviceEntities = deviceMapper.selectDeviceListNotUser(deviceEntity); + } else { + deviceEntities = deviceMapper.selectDeviceList(deviceEntity); + } + count += deviceEntities.size(); + List treeItemVoList = new ArrayList<>(); + for (Device deviceEntity1 : deviceEntities) { + TreeItemVo devItem = new TreeItemVo(); + devItem.setName(deviceEntity1.getDeviceName()); + devItem.setId(deviceEntity1.getDeviceId()); + devItem.setStatus(deviceEntity1.getStatus()); + devItem.setLatitude(deviceEntity1.getLatitude() != null ?deviceEntity1.getLatitude().floatValue():null); + devItem.setLongitude(deviceEntity1.getLongitude()!= null ?deviceEntity1.getLongitude().floatValue():null); + treeItemVoList.add(devItem); + if (deviceEntity1.getStatus() == 3) { + onLineCount++; + } + } + treeItemVo.setChildren(treeItemVoList); + productContainDeviceListVos.add(treeItemVo); + } + deviceListVo.setProductContainDeviceList(productContainDeviceListVos); + deviceListVo.setCount(count); + deviceListVo.setOnLineCount(onLineCount); + return deviceListVo; + } + + @Override + public Object getRealTimeDataByDevicerId(Long deviceId) { + HashMap rMap = new HashMap() { + }; + if (deviceId == null) { + throw new RuntimeException("请上传devicerId"); + } + Device device = iDeviceService.selectDeviceByDeviceId(deviceId); + if (device == null) { + throw new RuntimeException("未查到该设备"); + } + ThingsModel thingsModel1 = new ThingsModel(); + thingsModel1.setProductId(device.getProductId()); + List thingsModelEntities = thingsModelService.selectThingsModelList(thingsModel1); +// List thingsModelValueItems = deviceInfoCacheService.getDeviceInfoCache(device.getDeviceId()); +// List thingsModelValueItems = deviceInfoCacheService.getCacheDeviceStatus(device.getProductId(), +// device.getSerialNumber()); + DateTime dateTime = DateUtil.date(new Date()); + List deviceLogsAll = new ArrayList<>(); + List deviceLogs = logService.selectDeviceLogList(new DeviceLog() {{ + setSerialNumber(device.getSerialNumber()); + setBeginTime(DateUtil.beginOfDay(DateUtil.offsetDay(dateTime, -1)).toString()); + setEndTime(DateUtil.endOfDay(dateTime).toString()); + }}); + if (deviceLogs != null) { + deviceLogsAll.addAll(deviceLogs); + } + + Map> collect = deviceLogsAll.stream().sorted(Comparator + .comparing(DeviceLog::getCreateTime, Comparator + .nullsFirst(Comparator.naturalOrder())).reversed()).collect(Collectors.groupingBy(t -> t.getIdentity())); + for (ThingsModel thingsModel : thingsModelEntities) { + HashMap hashMap = new HashMap() {{ + put("upType", 0); + put("identifier", thingsModel.getIdentifier()); + put("unit", ""); + put("value", 0); + put("name", thingsModel.getModelName()); + }}; +// {"max": 100, "min": 0, "step": 1, "type": "decimal", "unit": "m/s"} + if(StringUtils.isNotEmpty(thingsModel.getSpecs())){ + String specs = thingsModel.getSpecs(); + JSONObject parse = (JSONObject) JSON.parse(specs); + if(parse.containsKey("unit")){ + hashMap.put("unit", parse.get("unit")); + } + } + + if (hashMap.size() > 1) { + List deviceLogs1 = collect.get(thingsModel.getIdentifier()); + if (deviceLogs1 != null) { + if (Float.parseFloat(deviceLogs1.get(0).getLogValue()) == Float.parseFloat(deviceLogs1.get(1).getLogValue())) + { + hashMap.put("upType", 1); + } else if (Float.parseFloat(deviceLogs1.get(0).getLogValue()) < Float.parseFloat(deviceLogs1.get(1).getLogValue())) { + hashMap.put("upType", -1); + } + hashMap.put("value", deviceLogs1.get(0).getLogValue()); + } + } else if (hashMap.size() > 0) { + List deviceLogs2 = collect.get(thingsModel.getIdentifier()); + if (deviceLogs2 != null) { + hashMap.put("value", deviceLogs2.get(0).getLogValue()); + } + } + rMap.put(thingsModel.getIdentifier(), hashMap); + } + return rMap; + } + + @Override + public Object getDeviceLogCurves(Long deviceId) { + if (deviceId == null) { + throw new RuntimeException("请上传deviceId"); + } + + Device device = iDeviceService.selectDeviceByDeviceId(deviceId); + if (device == null) { + throw new RuntimeException("未查到该设备"); + } +// List alertList = getAlertListForDevice(deviceId, device.getProductId()); +// List waterLevelAlert = new ArrayList<>(); +// List rainfallAlert = new ArrayList<>(); +// +// if (!alertList.isEmpty()) { +// alertList.forEach(alert -> parseAlert(alert, waterLevelAlert, rainfallAlert)); +// } + Map sumFlow = getDataList(device, "dataSumFlow"); + Map insFlow = getDataList(device, "dataInsFlow"); + Map rMap = new HashMap<>(); + rMap.put("time", insFlow.get("time")); + rMap.put("sumFlow", sumFlow.get("data")); + rMap.put("insFlow", insFlow.get("data")); +// rMap.put("sumFlowAlert", waterLevelAlert); +// rMap.put("insFlowAlert", rainfallAlert); + return rMap; + } + +// private List getAlertListForDevice(Long deviceId, Long productId) { +// AlertEntity alert = new AlertEntity(); +// alert.setDeviceId(deviceId); +// alert.setProductId(productId); +// return iAlertService.selectAlertList(alert); +// } + +// private void parseAlert(AlertEntity alert, List waterLevelAlert, List rainfallAlert) { +// JSONArray triggers = JSON.parseArray(alert.getTriggers()); +// for (Object obj : triggers) { +// Map trigger = (Map) obj; +// Float value = Float.valueOf(trigger.get("value").toString()); +// if (alert.getAlertType() == 1) { +// waterLevelAlert.add(new DeviceAlertDto(alert.getAlertName(), "#FF0000", value)); +// } else if (alert.getAlertType() == 2) { +// rainfallAlert.add(new DeviceAlertDto(alert.getAlertName(), "#FFFF00", value)); +// } +// } +// } + + private HashMap getDataList(Device device, String identity) { + HashMap dataListMap = new HashMap<>(); + HashMap kvHashMap = new HashMap<>(); + kvHashMap.put("identity", identity); + kvHashMap.put("serialNumber", device.getSerialNumber()); + Date endTime = new Date(); + kvHashMap.put("endTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,endTime)); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date()); + calendar.add(Calendar.HOUR,-23); + kvHashMap.put("beginTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH,calendar.getTime())+":00:00"); + List> reMap = logService.selectDayData(kvHashMap); + Map> create_time = new HashMap<>(); + if (reMap != null) { + create_time = reMap.stream().collect(Collectors.toMap(t -> { + return DateUtil.date((LocalDateTime) t.get("create_time")).toString("HH"); + }, t -> t, (t, t1) -> t1)); + } + DateTime date = DateUtil.date(); + ArrayList jiangyvliang = new ArrayList<>(); + ArrayList time = new ArrayList<>(); + dataListMap.put("time", time); + dataListMap.put("data", jiangyvliang); + for (int i = 0; i < 24; i++) { + String s = DateUtil.offsetHour(DateUtil.offsetHour(date, -23), i).toString("HH"); + time.add(s); + Map map = create_time.get(s); + if (map != null) { + if(identity.equals("dataSumFlow")){ + jiangyvliang.add(Float.parseFloat(create_time.get(s).get("maxLogValue").toString()) + - Float.parseFloat(create_time.get(s).get("minLogValue").toString())); + }else{ + jiangyvliang.add(create_time.get(s).get("avgLogValue")); + } + } else { + jiangyvliang.add(0); + } + } + return dataListMap; + + } + +} diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/Device.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/Device.java index 0a6b418..594506b 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/Device.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/Device.java @@ -136,6 +136,12 @@ public class Device extends BaseEntity @ApiModelProperty("设备摘要") private String summary; + /** + * 设备名称编号搜索 + */ + @ApiModelProperty("设备名称编号搜索") + private String deviceNameOrSerialNumber; + /** 分组ID,用于分组查询 **/ @ApiModelProperty("分组ID,用于分组查询") private Long groupId; diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/ThingsModel.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/ThingsModel.java index b76d6c5..a1fbdc5 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/ThingsModel.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/domain/ThingsModel.java @@ -134,5 +134,11 @@ public class ThingsModel extends BaseEntity private String language; private List modelIdList; + public ThingsModel() { + + } + public ThingsModel(Long productId) { + this.productId = productId; + } } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceLogMapper.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceLogMapper.java index 60efcaf..563e283 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceLogMapper.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceLogMapper.java @@ -12,7 +12,9 @@ import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 设备日志Mapper接口 @@ -127,4 +129,17 @@ public interface DeviceLogMapper * @return com.fastbee.common.core.domain.AjaxResult */ List countThingsModelInvoke(DataCenterParam dataCenterParam); + + /** + * 创建数据库表 + * + * @param tableName 表名称 + */ + int createTable(@Param("tableName") String tableName, @Param("id") Long id); + + List> selectDayData(Map kvHashMap); + + void createTableByDate(String ymTableName); + + List selectDataList(HashMap kvHashMap); } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceMapper.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceMapper.java index e5e88d8..53fc3ba 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceMapper.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/DeviceMapper.java @@ -93,7 +93,7 @@ public interface DeviceMapper */ public int updateDeviceThingsModelValue(ThingsModelValuesInput input); - + public List selectDeviceListNotUser(Device deviceEntity); /** * 查询设备列表 * diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/TableMapper.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/TableMapper.java new file mode 100644 index 0000000..c88f4dc --- /dev/null +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/mapper/TableMapper.java @@ -0,0 +1,23 @@ +package com.fastbee.iot.mapper; + + +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public interface TableMapper { + + /** + * 查询collectstatusPmon + * + * @param tableName 表名 + * @return collectstatusPmon + */ + public void createTable(String tableName); + + + public String getTableByName(String tableName); + + List getTableNameList(String[] tableNames); +} diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceLogService.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceLogService.java index 7ac380a..be48b9b 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceLogService.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceLogService.java @@ -8,6 +8,8 @@ import com.fastbee.iot.model.MonitorModel; import java.util.Date; import com.fastbee.iot.model.ThingsModelLogCountVO; import com.fastbee.iot.model.param.DataCenterParam; + +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -106,4 +108,6 @@ public interface IDeviceLogService * @return com.fastbee.common.core.domain.AjaxResult */ List countThingsModelInvoke(DataCenterParam dataCenterParam); + + List> selectDayData(HashMap kvHashMap); } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceService.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceService.java index 4361310..8602997 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceService.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/IDeviceService.java @@ -13,6 +13,7 @@ import com.fastbee.common.core.thingsModel.ThingsModelValuesInput; import com.fastbee.iot.model.dto.ThingsModelDTO; import org.quartz.SchedulerException; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -329,4 +330,9 @@ public interface IDeviceService */ List listDeviceGroupByGroupIds(List groupIds); + List> devicePointInfo(Device device); + + + ArrayList getDeviceLogAllCurves(Long deviceId, String beginTime, String endTime); + } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceLogServiceImpl.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceLogServiceImpl.java index 67bf692..92bae75 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceLogServiceImpl.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceLogServiceImpl.java @@ -1,11 +1,13 @@ package com.fastbee.iot.service.impl; +import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson2.JSONObject; import com.fastbee.common.utils.DateUtils; import com.fastbee.common.utils.StringUtils; import com.fastbee.iot.domain.Device; import com.fastbee.iot.domain.DeviceLog; import com.fastbee.iot.domain.ThingsModel; +import com.fastbee.iot.mapper.TableMapper; import com.fastbee.iot.model.DeviceReport; import com.fastbee.iot.model.HistoryModel; import com.fastbee.iot.service.IDeviceService; @@ -44,6 +46,10 @@ public class DeviceLogServiceImpl implements IDeviceLogService { @Autowired private IDeviceService deviceService; + @Autowired + private TableMapper tableMapper; + + private String tableName = "iot_device_log_"; /** * 查询设备日志 * @@ -216,4 +222,26 @@ public class DeviceLogServiceImpl implements IDeviceLogService { public List countThingsModelInvoke(DataCenterParam dataCenterParam) { return logService.countThingsModelInvoke(dataCenterParam); } + + + @Override + public List> selectDayData(HashMap kvHashMap) { + String serialNumber = (String) kvHashMap.get("serialNumber"); + if (kvHashMap.get("tableName") == null) { + Date date = new Date(); + String ym = DateUtils.parseDateToStr(DateUtils.YYYYMM, date); + kvHashMap.put("tableName", tableName + ym); + } + if (kvHashMap.get("tableName2") == null) { + String ym = DateUtils.parseDateToStr(DateUtils.YYYYMM, DateUtil.offsetMonth(new Date(), -1)); + kvHashMap.put("tableName2", tableName + ym); + } + if (tableMapper.getTableByName(kvHashMap.get("tableName").toString()) == null) { + deviceLogMapper.createTable(kvHashMap.get("tableName").toString(),1L); + } + if (tableMapper.getTableByName(kvHashMap.get("tableName2").toString()) == null) { + deviceLogMapper.createTable(kvHashMap.get("tableName2").toString(),1L); + } + return deviceLogMapper.selectDayData(kvHashMap); + } } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceServiceImpl.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceServiceImpl.java index c8b2ffe..23743ec 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceServiceImpl.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/service/impl/DeviceServiceImpl.java @@ -29,7 +29,7 @@ import com.fastbee.iot.mapper.*; import com.fastbee.iot.model.*; import com.fastbee.iot.model.ThingsModelItem.Datatype; import com.fastbee.iot.model.ThingsModelItem.EnumItem; -import com.fastbee.iot.model.ThingsModelItem.ThingsModel; +import com.fastbee.iot.domain.ThingsModel; import com.fastbee.iot.model.ThingsModels.*; import com.fastbee.iot.model.dto.ThingsModelDTO; import com.fastbee.iot.model.gateWay.SubDeviceListVO; @@ -623,11 +623,11 @@ public class DeviceServiceImpl implements IDeviceService { // 排序 thingsList = thingsList.stream().sorted(Comparator.comparing(ThingsModelValueItem::getOrder).reversed()).collect(Collectors.toList()); // 数组类型物模型里面对象赋值 - List[] arrayParams = new List[dataType.getArrayCount()]; + List[] arrayParams = new List[dataType.getArrayCount()]; for (int i = 0; i < dataType.getArrayCount(); i++) { - List thingsModels = new ArrayList<>(); + List thingsModels = new ArrayList<>(); for (int j = 0; j < thingsList.size(); j++) { - ThingsModel thingsModel = new ThingsModel(); + com.fastbee.iot.model.ThingsModelItem.ThingsModel thingsModel = new com.fastbee.iot.model.ThingsModelItem.ThingsModel(); BeanUtils.copyProperties(thingsList.get(j), thingsModel); String shadow = thingsList.get(j).getShadow(); if (StringUtils.isNotEmpty(shadow) && !shadow.equals("")) { @@ -1592,5 +1592,75 @@ public class DeviceServiceImpl implements IDeviceService { return deviceMapper.listDeviceGroupByGroupIds(groupIds); } + /** + * 所有设备点数据 + * + * @return + */ + @Override + public List> devicePointInfo(Device devices) { + ArrayList> resultList = new ArrayList<>(); + List deviceList = selectDeviceList(devices); + + for (Device device : deviceList) { + HashMap itemMap = new HashMap<>(); + resultList.add(itemMap); + Integer integer = 0; + + itemMap.put("device",device); + if (device.getStatus() != null) { + if (device.getStatus().equals(4)) { + integer = 1; + } + } + itemMap.put("online",integer); + + } + return resultList; + } + + @Override + public ArrayList getDeviceLogAllCurves(Long deviceId, String beginTime, String endTime) { + if (deviceId == null) { + throw new RuntimeException("请上传devicerId"); + } + + Device device = selectDeviceByDeviceId(deviceId); + if (device == null) { + throw new RuntimeException("未查到该设备"); + } + + ArrayList list = new ArrayList<>(); + HashMap kvHashMap = new HashMap<>(); + kvHashMap.put("serialNumber", device.getSerialNumber()); + kvHashMap.put("beginTime", beginTime); + kvHashMap.put("endTime", endTime); + List thingsModels = thingsModelService.selectThingsModelList(new ThingsModel(device.getProductId())); + List deviceLogs= logService.selectDataList(kvHashMap); + Map> collect = deviceLogs != null ? deviceLogs.stream().collect(Collectors.groupingBy(t -> t.getIdentity())): new HashMap(); + for (ThingsModel modelDevice : thingsModels) { + HashMap dataListMap = new HashMap<>(); + + List deviceLogs1 = collect.get(modelDevice.getIdentifier()); + dataListMap.put("name", modelDevice.getModelName()); + if (StringUtils.isNotEmpty(modelDevice.getSpecs())) { + cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(modelDevice.getSpecs()); + dataListMap.put("unit",object.get("unit")); + }else{ + dataListMap.put("unit",""); + } + if (deviceLogs1 != null) { + System.out.println(deviceLogs1); + dataListMap.put("time",deviceLogs1.stream().map(t->t.getCreateTime()).collect(Collectors.toList())); + dataListMap.put("data",deviceLogs1.stream().map(t->t.getLogValue()).collect(Collectors.toList())); + }else{ + dataListMap.put("time",new ArrayList<>()); + dataListMap.put("data",new ArrayList<>()); + } + list.add(dataListMap); + } + return list ; + } + } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/ILogService.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/ILogService.java index 66c3224..0397b25 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/ILogService.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/ILogService.java @@ -12,6 +12,7 @@ import com.fastbee.iot.tdengine.service.model.TdLogDto; import org.springframework.stereotype.Service; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -71,4 +72,6 @@ public interface ILogService { List countThingsModelInvoke(DataCenterParam dataCenterParam); DeviceLog selectLastReport(DeviceLog deviceLog); + + List selectDataList(HashMap kvHashMap); } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/MySqlLogServiceImpl.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/MySqlLogServiceImpl.java index 5e4959d..11ac4ed 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/MySqlLogServiceImpl.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/MySqlLogServiceImpl.java @@ -1,8 +1,11 @@ package com.fastbee.iot.tdengine.service.impl; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; import com.fastbee.common.utils.DateUtils; import com.fastbee.iot.domain.Device; import com.fastbee.iot.domain.DeviceLog; +import com.fastbee.iot.mapper.TableMapper; import com.fastbee.iot.model.DeviceStatistic; import com.fastbee.iot.model.HistoryModel; import com.fastbee.iot.model.ThingsModelLogCountVO; @@ -11,9 +14,9 @@ import com.fastbee.iot.tdengine.service.ILogService; import com.fastbee.iot.mapper.DeviceLogMapper; import com.fastbee.iot.model.MonitorModel; import com.fastbee.iot.tdengine.service.model.TdLogDto; +import org.springframework.beans.factory.annotation.Autowired; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; /** @@ -25,7 +28,9 @@ import java.util.stream.Collectors; public class MySqlLogServiceImpl implements ILogService { private DeviceLogMapper deviceLogMapper; - + private String tableName = "iot_device_log_"; + @Autowired + private TableMapper tableMapper; public MySqlLogServiceImpl(DeviceLogMapper _deviceLogMapper){ this.deviceLogMapper=_deviceLogMapper; } @@ -116,4 +121,35 @@ public class MySqlLogServiceImpl implements ILogService { public DeviceLog selectLastReport(DeviceLog deviceLog) { return deviceLogMapper.selectLastReport(deviceLog); } + + @Override + public List selectDataList(HashMap kvHashMap) { + if (kvHashMap.get("endTime") != null) { + DateTime endTime = DateUtil.parse(kvHashMap.get("endTime").toString()); + String yyyyMM = endTime.toString("yyyyMM"); + String yyyyMM2 = DateUtil.offsetMonth(endTime, -1).toString("yyyyMM"); + if (tableMapper.getTableByName(tableName + yyyyMM) == null) { + deviceLogMapper.createTableByDate(tableName + yyyyMM); + } + if (tableMapper.getTableByName(tableName + yyyyMM2) == null) { + deviceLogMapper.createTableByDate(tableName + yyyyMM2); + } + kvHashMap.put("tableName", tableName + yyyyMM); + kvHashMap.put("tableName2", tableName + yyyyMM2); + } + if (kvHashMap.get("tableName") == null) { + Date date = new Date(); + String ym = DateUtils.parseDateToStr(DateUtils.YYYYMM, date); + String ym2 = DateUtils.parseDateToStr(DateUtils.YYYYMM, DateUtil.offsetMonth(new Date(), -1)); + if (tableMapper.getTableByName(tableName + ym) == null) { + deviceLogMapper.createTableByDate(tableName + ym); + } + if (tableMapper.getTableByName(tableName + ym2) == null) { + deviceLogMapper.createTableByDate(tableName + ym2); + } + kvHashMap.put("tableName", tableName + ym); + kvHashMap.put("tableName2", tableName + ym2); + } + return deviceLogMapper.selectDataList(kvHashMap); + } } diff --git a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/TdengineLogServiceImpl.java b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/TdengineLogServiceImpl.java index 30a6af5..819976f 100644 --- a/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/TdengineLogServiceImpl.java +++ b/fastbee-service/fastbee-iot-service/src/main/java/com/fastbee/iot/tdengine/service/impl/TdengineLogServiceImpl.java @@ -15,6 +15,8 @@ import com.fastbee.iot.tdengine.service.model.TdLogDto; import com.fastbee.iot.util.SnowflakeIdWorker; import org.springframework.context.ApplicationContext; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -157,4 +159,9 @@ public class TdengineLogServiceImpl implements ILogService { public DeviceLog selectLastReport(DeviceLog deviceLog) { return null; } + + @Override + public List selectDataList(HashMap kvHashMap) { + return Collections.emptyList(); + } } diff --git a/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/DeviceLogMapper.xml b/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/DeviceLogMapper.xml index e255d92..8685562 100644 --- a/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/DeviceLogMapper.xml +++ b/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/DeviceLogMapper.xml @@ -262,6 +262,154 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + CREATE TABLE `${tableName}` + ( + `log_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '设备日志ID', + `identity` varchar(64) NOT NULL COMMENT '标识符', + `log_type` tinyint(1) NOT NULL COMMENT '类型(1=属性上报,2=调用功能,3=事件上报,4=设备升级,5=设备上线,6=设备离线)', + `log_value` decimal(18, 3) default 0 comment '日志值', + `device_id` bigint(20) DEFAULT NULL COMMENT '设备ID', + `device_name` varchar(64) DEFAULT NULL COMMENT '设备名称', + `serial_number` varchar(64) DEFAULT NULL COMMENT '设备编号', + `is_monitor` tinyint(1) unsigned zerofill DEFAULT '0' COMMENT '是否监测数据(1=是,0=否)', + `mode` tinyint(1) unsigned zerofill DEFAULT '0' COMMENT '模式(1=影子模式,2=在线模式,3=其他)', + `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID', + `user_name` varchar(30) DEFAULT '' COMMENT '用户昵称', + `tenant_id` bigint(20) DEFAULT NULL COMMENT '租户ID', + `tenant_name` varchar(30) DEFAULT '' COMMENT '租户名称', + `create_by` varchar(64) DEFAULT '' COMMENT '创建者', + `create_time` datetime DEFAULT NULL COMMENT '创建时间', + `remark` varchar(200) DEFAULT NULL COMMENT '备注', + PRIMARY KEY (`log_id`) USING BTREE, + KEY `iot_device_log_index_serial_number` (`serial_number`) USING BTREE, + KEY `iot_device_log_index_tenant_id` (`tenant_id`) USING BTREE, + KEY `iot_device_log_index_user_id` (`user_id`) USING BTREE, + KEY `iot_device_log_index_identity_device_id` (`identity`, `device_id`) USING BTREE, + KEY `iot_device_log_index_identity_createTime` (`identity`, `create_time`) USING BTREE + ) ENGINE = InnoDB + AUTO_INCREMENT = 1 + DEFAULT CHARSET = utf8 + ROW_FORMAT = DYNAMIC COMMENT ='设备日志'; + + + + + + CREATE TABLE ${tableName} + ( + `log_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '设备监测信息ID', + `identity` varchar(64) NOT NULL COMMENT '标识符', + `model_name` varchar(255) DEFAULT NULL COMMENT '物模型名称', + `log_type` tinyint(1) NOT NULL COMMENT '类型(1=属性上报,2=调用功能,3=事件上报,4=设备升级,5=设备上线,6=设备离线)', + `log_value` varchar(64) NOT NULL COMMENT '日志值', + `device_id` bigint(20) DEFAULT NULL COMMENT '设备ID', + `device_name` varchar(64) DEFAULT NULL COMMENT '设备名称', + `serial_number` varchar(64) DEFAULT NULL COMMENT '设备编号', + `is_monitor` tinyint(1) unsigned zerofill NOT NULL DEFAULT '0' COMMENT '是否监测数据(1=是,0=否)', + `mode` tinyint(1) unsigned zerofill NOT NULL DEFAULT '0' COMMENT '模式(1=影子模式,2=在线模式,3=其他)', + `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID', + `user_name` varchar(30) DEFAULT '' COMMENT '用户昵称', + `tenant_id` bigint(20) DEFAULT NULL COMMENT '租户ID', + `tenant_name` varchar(30) DEFAULT '' COMMENT '租户名称', + `create_by` varchar(64) DEFAULT '' COMMENT '创建者', + `create_time` datetime DEFAULT NULL COMMENT '创建时间', + `remark` varchar(200) DEFAULT NULL COMMENT '备注', + PRIMARY KEY (`log_id`) USING BTREE, + KEY `iot_device_log_index_serial_number` (`serial_number`) USING BTREE, + KEY `iot_device_log_index_tenant_id` (`tenant_id`) USING BTREE, + KEY `iot_device_log_index_user_id` (`user_id`) USING BTREE, + KEY `iot_device_log_index_device_id` (`device_id`) USING BTREE, + KEY `index_serialNumber_createTime` (`serial_number`,`create_time`) USING BTREE, + KEY `index_isMonitor_serialNumber_createTime` (`serial_number`,`is_monitor`,`create_time`) USING BTREE + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='设备日志'; + + + + update iot_device set things_model_value=#{stringValue} where device_id = #{deviceId} diff --git a/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/TableMapper.xml b/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/TableMapper.xml new file mode 100644 index 0000000..64b561c --- /dev/null +++ b/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/TableMapper.xml @@ -0,0 +1,34 @@ + + + + + + + CREATE TABLE IF NOT EXISTS `${tableName}` + ( + `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主键', + `group_id` int(0) NULL DEFAULT NULL COMMENT '组号', + `username` varchar(20) NULL DEFAULT NULL COMMENT '用户名', + `password` varchar(20) NULL DEFAULT NULL COMMENT '密码', + PRIMARY KEY (`id`) + ) ENGINE = InnoDB + AUTO_INCREMENT = 9 + CHARACTER SET = utf8mb4 COMMENT ='用于测试的用户表'; + + + + + + +