| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.DateUtils; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | |
| | | import java.util.HashMap; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService { |
| | | |
| | | @Autowired |
| | | DeviceMapper deviceMapper; |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | |
| | | @Override |
| | | public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) { |
| | | QueryWrapper<Device> wrapper = new QueryWrapper(); |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | wrapper.eq("monitor_point_id", monitorPointId); |
| | | List<Device> devices = deviceMapper.selectList(wrapper); |
| | | return devices; |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | return deviceMapper.selectList(wrapper); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getSensorsByMac(String mac) { |
| | | //从redis中获取设备因子信息 |
| | | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | | List<Sensor> sensors = device.getVersion().getSensors(); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | for (Sensor sensor : sensors) { |
| | | String sensorCode = sensor.getCode(); |
| | | String name = sensor.getName(); |
| | | result.put(sensorCode, name); |
| | | public Map<String, Object> getSensorsByMac(Map<String, Object> params) { |
| | | //设备mac |
| | | List<String> macs = (List<String>) params.remove("macs"); |
| | | List<Map<String, Object>> elementLists = new ArrayList<>(); |
| | | |
| | | for (String mac : macs) { |
| | | //从redis中获取设备因子信息 |
| | | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | | List<Sensor> sensors = device.getVersion().getSensors(); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | for (Sensor sensor : sensors) { |
| | | String sensorCode = sensor.getCode(); |
| | | String name = sensor.getName(); |
| | | map.put(sensorCode, name); |
| | | } |
| | | elementLists.add(map); |
| | | } |
| | | return result; |
| | | |
| | | Map<String, Object> map = elementLists.parallelStream() |
| | | .filter(elementList -> elementList.size() != 0) |
| | | .reduce((a, b) -> { |
| | | a.keySet().retainAll(b.keySet()); |
| | | return a; |
| | | }).orElse(new HashMap<>()); |
| | | return map; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getTrendChartData(Map<String, Object> params) { |
| | | Object type = params.get("type"); |
| | | String start = params.remove("time").toString(); |
| | | //设备mac |
| | | List<String> macs = (List<String>) params.remove("macs"); |
| | | |
| | | QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.select("mac", "name").in("mac", macs); |
| | | List<Device> devices = deviceMapper.selectList(queryWrapper); |
| | | |
| | | |
| | | //所选时间 |
| | | List<String> times = (List<String>) params.remove("times"); |
| | | //因子code |
| | | String sensorCode = params.get("sensorCode").toString(); |
| | | String end; |
| | | String timeUnits; |
| | | String dateFormat; |
| | | if ("day".equals(type)) { |
| | | end = DateUtils.getDateAddDay(start, 1); |
| | | timeUnits = "hourly"; |
| | | dateFormat = "%H"; |
| | | } else if ("month".equals(type)) { |
| | | end = DateUtils.getDateAddMonth(start, 1); |
| | | timeUnits = "daily"; |
| | | dateFormat = "%d"; |
| | | } else { |
| | | end = DateUtils.getDateAddYear(start, 1); |
| | | timeUnits = "monthly"; |
| | | dateFormat = "%m"; |
| | | //返回结果集,time=data |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | |
| | | for (String start : times) { |
| | | if ("hour".equals(type)) { |
| | | end = DateUtils.getDateAddDay(start, 1); |
| | | timeUnits = "hourly"; |
| | | dateFormat = "%Y-%m-%d %H"; |
| | | } else if ("day".equals(type)) { |
| | | end = DateUtils.getDateAddMonth(start, 1); |
| | | timeUnits = "daily"; |
| | | dateFormat = "%Y-%m-%d"; |
| | | } else { |
| | | end = DateUtils.getDateAddYear(start, 1); |
| | | timeUnits = "monthly"; |
| | | dateFormat = "%Y-%m"; |
| | | } |
| | | params.put("timeUnits", timeUnits); |
| | | params.put("start", start); |
| | | params.put("end", end); |
| | | params.put("macs", macs); |
| | | params.put("dateFormat", dateFormat); |
| | | //获取多设备指定因子数据 |
| | | List<Map<String, Object>> list = deviceMapper.getTrendChartData(params); |
| | | |
| | | for (String s : DateUtils.getTimeLag(start)) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | resultMap.put("time", s); |
| | | List<Map<String, Object>> deviceData = new ArrayList<>(); |
| | | for (Device device : devices) { |
| | | Map<String, Object> valueMap = new HashMap<>(); |
| | | valueMap.put("name", device.getName()); |
| | | valueMap.put("sensorValue", ""); |
| | | for (Map<String, Object> map : list) { |
| | | Object time = map.get("time"); |
| | | Object sensorValue = map.get(sensorCode); |
| | | Object mac = map.get("mac"); |
| | | if (s.equals(time) && device.getMac().equals(mac)) { |
| | | valueMap.put("sensorValue", sensorValue); |
| | | } |
| | | } |
| | | deviceData.add(valueMap); |
| | | } |
| | | resultMap.put("deviceData", deviceData); |
| | | result.add(resultMap); |
| | | } |
| | | } |
| | | params.put("dateFormat", dateFormat); |
| | | params.put("timeUnits", timeUnits); |
| | | params.put("start", start); |
| | | params.put("end", end); |
| | | return deviceMapper.getTrendChartData(params); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public Device getDeviceByMac(String mac) { |
| | | Map<String,Object> deviceMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE,mac); |
| | | Device device = JSON.parseObject(JSON.toJSONString(deviceMap), Device.class); |
| | | //从map获取organizationId和monitorPointId以及versionId |
| | | Map<String,Object> organizationMap = (Map<String,Object>)deviceMap.get("organization"); |
| | | Map<String,Object> monitorPointMap = (Map<String,Object>)deviceMap.get("monitorPoint"); |
| | | Map<String,Object> versionMap = (Map<String,Object>)deviceMap.get("version"); |
| | | device.setDeviceVersionId((Integer) versionMap.get("id")); |
| | | device.setOrganizationId((Integer) organizationMap.get("id")); |
| | | device.setMonitorPointId((Integer) monitorPointMap.get("id")); |
| | | //如果缓存为空则查询数据库 |
| | | if(ObjectUtils.isEmpty(device)){ |
| | | return getDeviceByMacFromDB(mac); |
| | | } |
| | | return device; |
| | | } |
| | | |
| | | private Device getDeviceByMacFromDB(String mac){ |
| | | QueryWrapper<Device> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("mac",mac); |
| | | wrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | return deviceMapper.selectOne(wrapper); |
| | | } |
| | | |
| | | |
| | | } |