文件目录:org.jeecg.modules.demo.apimanage.service
    image.png

    1. @Transactional(rollbackFor = Exception.class)
    2. public ResponseResult addIotGreenHouseDeviceIn(Map<String, Object> map, JSONObject jsonObject) {
    3. try {
    4. JSONObject json = JSONObject.parseObject(jsonObject.toString());
    5. Nhiotgreenhousedeviceinfo nhiotgreenhousedeviceinfo = JSON.toJavaObject(json, Nhiotgreenhousedeviceinfo.class);
    6. //查询当前设备是否已经被绑定 被绑定 无法绑定
    7. String did = nhiotgreenhousedeviceinfo.getDid();
    8. /* int deviceExitsReuslt = nhiotgreenhousedeviceinfoService.selectHourseDeviceDidIsExits(did);
    9. if (deviceExitsReuslt >= 1) {
    10. return ResponseResult.error(ResponseResultEnum.DEVICE_HAS_BEEN_BOUND);
    11. }*/
    12. List<Map<String,Object>> list=nhiotgreenhousedeviceinfoService.isBind(did);
    13. if (!list.isEmpty()){
    14. return ResponseResult.error(ResponseResultEnum.DEVICE_HAS_BEEN_BOUND);
    15. }
    16. String uid = nhiotgreenhousedeviceinfo.getUid();
    17. String dpId = nhiotgreenhousedeviceinfo.getGhId();
    18. //查询用户大棚是否存在 没有大棚不予许绑定设备
    19. int hourseExitisResult = inhiotgreenhouseService.selectHourseExits(uid, dpId);
    20. if (hourseExitisResult == 0) {
    21. return ResponseResult.error(ResponseResultEnum.NOT_FOUND_GREENHOUSES_INFO);
    22. }
    23. // 添加成功后 往设备的缓存中扔数据
    24. nhiotgreenhousedeviceinfoService.save(nhiotgreenhousedeviceinfo);
    25. // 把nh_iot_greenhouse_device_info 表的所有以前这个设备的信息(大棚id和用户id) 全部更新为新的大棚
    26. //TODO 这个地方可能会更新几万条数据 或者更多
    27. int updateState=nhiotgreenhousedeviceinfoService.updateUserIdDpId(uid,dpId,did);
    28. // 添加数据后第一次处理数据 处理大棚的数据
    29. updateDpData(nhiotgreenhousedeviceinfo, 1);
    30. //把当前信息放入缓存
    31. Map<String, Object> deviceInfo = new HashMap<>();
    32. deviceInfo.put("uid", nhiotgreenhousedeviceinfo.getUid());
    33. deviceInfo.put("did", nhiotgreenhousedeviceinfo.getDid());
    34. deviceInfo.put("gh_id", nhiotgreenhousedeviceinfo.getGhId());
    35. deviceInfo.put("gh_is_auto", 0);
    36. List<Map<String, Object>> resultList = new ArrayList<>();
    37. /* if (null == redisUtil.get("deviceList")) {
    38. resultList.add(deviceInfo);
    39. // TODO redisSet
    40. redisUtil.set("deviceList", resultList);
    41. } else {
    42. resultList = (List<Map<String, Object>>) redisUtil.get("deviceList");
    43. resultList.add(deviceInfo);
    44. // TODO redisSet
    45. redisUtil.set("deviceList", resultList);
    46. }*/
    47. // 开始添加智能设备需要的表结构数据
    48. List<Nhiotdevice> nhiotdeviceList = new ArrayList<>();
    49. for (int i = 0; i < 5; i++) {
    50. Nhiotdevice nhiotdevice = new Nhiotdevice();
    51. nhiotdevice.setId(IdGenerator.uuid());
    52. nhiotdevice.setCreateBy("user");
    53. nhiotdevice.setDeviceId(did);
    54. nhiotdevice.setDeviceRuleIsUse(0);
    55. nhiotdevice.setDeviceType(i + 1);
    56. nhiotdevice.setGhId(nhiotgreenhousedeviceinfo.getGhId());
    57. nhiotdevice.setUserId(nhiotgreenhousedeviceinfo.getUid());
    58. //TODO 此处改为枚举方式直接在枚举中取出
    59. String name = "浇水";
    60. switch (i) {
    61. case 0:
    62. name = "浇水";
    63. break;
    64. case 1:
    65. name = "卷帘";
    66. break;
    67. case 2:
    68. name = "通风1";
    69. break;
    70. case 3:
    71. name = "通风2";
    72. break;
    73. case 4:
    74. name = "通风3";
    75. break;
    76. default:
    77. }
    78. nhiotdevice.setDeviceRuleName(name);
    79. nhiotdeviceList.add(nhiotdevice);
    80. }
    81. // 创建默认时间5个 农嗨云设备操作的默认时间
    82. List<nhiotgreenousedeviceconfig> nhiotgreenousedeviceconfigList = new ArrayList<>();
    83. Date date=new Date();
    84. String relay=null;
    85. for (int j = 0; j < 5; j++) {
    86. switch (j) {
    87. case 0:
    88. relay = "01";
    89. break;
    90. case 1:
    91. relay = "03";
    92. break;
    93. case 2:
    94. relay = "05";
    95. break;
    96. case 3:
    97. relay = "07";
    98. break;
    99. case 4:
    100. relay = "09";
    101. break;
    102. default:
    103. }
    104. nhiotgreenousedeviceconfig config=new nhiotgreenousedeviceconfig();
    105. config.setId(IdGenerator.uuid());
    106. config.setGhId(nhiotgreenhousedeviceinfo.getGhId());
    107. config.setUserId(nhiotgreenhousedeviceinfo.getUid());
    108. config.setRelay(relay);
    109. config.setCreateTime(date);
    110. config.setTime("50");
    111. if (relay==("09")){
    112. config.setTime("13,97,179");
    113. }
    114. config.setDeviceId(did);
    115. config.setCreateBy("jeecg");
    116. nhiotgreenousedeviceconfigList.add(config);
    117. }
    118. // 保存设备操作时间
    119. int index=inhiotgreenousedeviceconfigService.inserdefaultTime(nhiotgreenousedeviceconfigList);
    120. if (index>0){
    121. //添加默认时间成功
    122. //添加设备记录表
    123. int deviceIndex = iNhiotdeviceService.addHourseDeviceMarstInfo(nhiotdeviceList);
    124. if (deviceIndex>0){
    125. //添加最新数据
    126. //TODO 添加大棚成功,十分钟之后会更新数据,在添加成功获取一条数据保存到nh_iot_greenhouse_device_info_latest表
    127. /**
    128. * 往nh_iot_greenhouse_device_info_latest 表写入设备的最新数据 目的是 定时器十分钟更新数据 来不及
    129. * 1.根据设备id 请求http://app.sinoverse.cn:9090/api/wlw/json/3E0BF339BDF6.json 获取最新数据
    130. * 2.把数据save到 nh_iot_greenhouse_device_info_latest
    131. */
    132. String url = "http://app.sinoverse.cn:9090/api/wlw/json/" + did + ".json";
    133. String result = HttpClientHelpers.httpClientGet(url, new HashMap<>(), "UTF-8");
    134. if(result.equals("")){
    135. return ResponseResult.error("获取设备信息失败!");
    136. }else {
    137. JSONObject jsonResult = JSONObject.parseObject(result);
    138. Nhiotgreenhousedeviceinfo deviceinfo = JSON.toJavaObject(jsonResult, Nhiotgreenhousedeviceinfo.class);
    139. //根据大棚id查询数据是否存在
    140. QueryWrapper<Nhiotgreenhousedeviceinfolatest> queryWrapper = new QueryWrapper();
    141. queryWrapper.eq("gh_id",nhiotgreenhousedeviceinfo.getGhId());
    142. Nhiotgreenhousedeviceinfolatest nhiotgreenhousedeviceinfolatest = iNhiotgreenhousedeviceinfolatestService.selectByDeviceIdDeviceInfo(queryWrapper);
    143. if(nhiotgreenhousedeviceinfolatest==null){
    144. //添加数据
    145. String uuid = IdGenerator.uuid();
    146. deviceinfo.setId(uuid);
    147. deviceinfo.setCreateTime(new Date());
    148. deviceinfo.setUid(nhiotgreenhousedeviceinfo.getUid());
    149. deviceinfo.setDataState(1);//设备正常
    150. deviceinfo.setDataOnline(1);//设备在线
    151. iNhiotgreenhousedeviceinfolatestService.saveDeviceInfo(deviceinfo);
    152. }else {
    153. return ResponseResult.error("数据添加失败!");
    154. }
    155. }
    156. return ResponseResult.success();
    157. }
    158. return ResponseResult.error("设备添加失败!");
    159. }
    160. return ResponseResult.error("默认时间添加失败!");
    161. } catch (Exception e) {
    162. e.printStackTrace();
    163. System.out.println("扫码后添加设备失败");
    164. }
    165. return ResponseResult.error(ResponseResultEnum.ADD_DEVICE_ERROR);
    166. }