文件目录:org.jeecg.modules.demo.apimanage.service
@Transactional(rollbackFor = Exception.class)public ResponseResult addIotGreenHouseDeviceIn(Map<String, Object> map, JSONObject jsonObject) {try {JSONObject json = JSONObject.parseObject(jsonObject.toString());Nhiotgreenhousedeviceinfo nhiotgreenhousedeviceinfo = JSON.toJavaObject(json, Nhiotgreenhousedeviceinfo.class);//查询当前设备是否已经被绑定 被绑定 无法绑定String did = nhiotgreenhousedeviceinfo.getDid();/* int deviceExitsReuslt = nhiotgreenhousedeviceinfoService.selectHourseDeviceDidIsExits(did);if (deviceExitsReuslt >= 1) {return ResponseResult.error(ResponseResultEnum.DEVICE_HAS_BEEN_BOUND);}*/List<Map<String,Object>> list=nhiotgreenhousedeviceinfoService.isBind(did);if (!list.isEmpty()){return ResponseResult.error(ResponseResultEnum.DEVICE_HAS_BEEN_BOUND);}String uid = nhiotgreenhousedeviceinfo.getUid();String dpId = nhiotgreenhousedeviceinfo.getGhId();//查询用户大棚是否存在 没有大棚不予许绑定设备int hourseExitisResult = inhiotgreenhouseService.selectHourseExits(uid, dpId);if (hourseExitisResult == 0) {return ResponseResult.error(ResponseResultEnum.NOT_FOUND_GREENHOUSES_INFO);}// 添加成功后 往设备的缓存中扔数据nhiotgreenhousedeviceinfoService.save(nhiotgreenhousedeviceinfo);// 把nh_iot_greenhouse_device_info 表的所有以前这个设备的信息(大棚id和用户id) 全部更新为新的大棚//TODO 这个地方可能会更新几万条数据 或者更多int updateState=nhiotgreenhousedeviceinfoService.updateUserIdDpId(uid,dpId,did);// 添加数据后第一次处理数据 处理大棚的数据updateDpData(nhiotgreenhousedeviceinfo, 1);//把当前信息放入缓存Map<String, Object> deviceInfo = new HashMap<>();deviceInfo.put("uid", nhiotgreenhousedeviceinfo.getUid());deviceInfo.put("did", nhiotgreenhousedeviceinfo.getDid());deviceInfo.put("gh_id", nhiotgreenhousedeviceinfo.getGhId());deviceInfo.put("gh_is_auto", 0);List<Map<String, Object>> resultList = new ArrayList<>();/* if (null == redisUtil.get("deviceList")) {resultList.add(deviceInfo);// TODO redisSetredisUtil.set("deviceList", resultList);} else {resultList = (List<Map<String, Object>>) redisUtil.get("deviceList");resultList.add(deviceInfo);// TODO redisSetredisUtil.set("deviceList", resultList);}*/// 开始添加智能设备需要的表结构数据List<Nhiotdevice> nhiotdeviceList = new ArrayList<>();for (int i = 0; i < 5; i++) {Nhiotdevice nhiotdevice = new Nhiotdevice();nhiotdevice.setId(IdGenerator.uuid());nhiotdevice.setCreateBy("user");nhiotdevice.setDeviceId(did);nhiotdevice.setDeviceRuleIsUse(0);nhiotdevice.setDeviceType(i + 1);nhiotdevice.setGhId(nhiotgreenhousedeviceinfo.getGhId());nhiotdevice.setUserId(nhiotgreenhousedeviceinfo.getUid());//TODO 此处改为枚举方式直接在枚举中取出String name = "浇水";switch (i) {case 0:name = "浇水";break;case 1:name = "卷帘";break;case 2:name = "通风1";break;case 3:name = "通风2";break;case 4:name = "通风3";break;default:}nhiotdevice.setDeviceRuleName(name);nhiotdeviceList.add(nhiotdevice);}// 创建默认时间5个 农嗨云设备操作的默认时间List<nhiotgreenousedeviceconfig> nhiotgreenousedeviceconfigList = new ArrayList<>();Date date=new Date();String relay=null;for (int j = 0; j < 5; j++) {switch (j) {case 0:relay = "01";break;case 1:relay = "03";break;case 2:relay = "05";break;case 3:relay = "07";break;case 4:relay = "09";break;default:}nhiotgreenousedeviceconfig config=new nhiotgreenousedeviceconfig();config.setId(IdGenerator.uuid());config.setGhId(nhiotgreenhousedeviceinfo.getGhId());config.setUserId(nhiotgreenhousedeviceinfo.getUid());config.setRelay(relay);config.setCreateTime(date);config.setTime("50");if (relay==("09")){config.setTime("13,97,179");}config.setDeviceId(did);config.setCreateBy("jeecg");nhiotgreenousedeviceconfigList.add(config);}// 保存设备操作时间int index=inhiotgreenousedeviceconfigService.inserdefaultTime(nhiotgreenousedeviceconfigList);if (index>0){//添加默认时间成功//添加设备记录表int deviceIndex = iNhiotdeviceService.addHourseDeviceMarstInfo(nhiotdeviceList);if (deviceIndex>0){//添加最新数据//TODO 添加大棚成功,十分钟之后会更新数据,在添加成功获取一条数据保存到nh_iot_greenhouse_device_info_latest表/*** 往nh_iot_greenhouse_device_info_latest 表写入设备的最新数据 目的是 定时器十分钟更新数据 来不及* 1.根据设备id 请求http://app.sinoverse.cn:9090/api/wlw/json/3E0BF339BDF6.json 获取最新数据* 2.把数据save到 nh_iot_greenhouse_device_info_latest*/String url = "http://app.sinoverse.cn:9090/api/wlw/json/" + did + ".json";String result = HttpClientHelpers.httpClientGet(url, new HashMap<>(), "UTF-8");if(result.equals("")){return ResponseResult.error("获取设备信息失败!");}else {JSONObject jsonResult = JSONObject.parseObject(result);Nhiotgreenhousedeviceinfo deviceinfo = JSON.toJavaObject(jsonResult, Nhiotgreenhousedeviceinfo.class);//根据大棚id查询数据是否存在QueryWrapper<Nhiotgreenhousedeviceinfolatest> queryWrapper = new QueryWrapper();queryWrapper.eq("gh_id",nhiotgreenhousedeviceinfo.getGhId());Nhiotgreenhousedeviceinfolatest nhiotgreenhousedeviceinfolatest = iNhiotgreenhousedeviceinfolatestService.selectByDeviceIdDeviceInfo(queryWrapper);if(nhiotgreenhousedeviceinfolatest==null){//添加数据String uuid = IdGenerator.uuid();deviceinfo.setId(uuid);deviceinfo.setCreateTime(new Date());deviceinfo.setUid(nhiotgreenhousedeviceinfo.getUid());deviceinfo.setDataState(1);//设备正常deviceinfo.setDataOnline(1);//设备在线iNhiotgreenhousedeviceinfolatestService.saveDeviceInfo(deviceinfo);}else {return ResponseResult.error("数据添加失败!");}}return ResponseResult.success();}return ResponseResult.error("设备添加失败!");}return ResponseResult.error("默认时间添加失败!");} catch (Exception e) {e.printStackTrace();System.out.println("扫码后添加设备失败");}return ResponseResult.error(ResponseResultEnum.ADD_DEVICE_ERROR);}
