diff --git a/xiuosiot-backend/pom.xml b/xiuosiot-backend/pom.xml index dc0ebe6..c0b2ecf 100644 --- a/xiuosiot-backend/pom.xml +++ b/xiuosiot-backend/pom.xml @@ -181,6 +181,7 @@ org.springframework.boot spring-boot-maven-plugin + com.aiit.xiuos.XiuosApplication org.projectlombok @@ -189,6 +190,15 @@ + + + maven-compiler-plugin + + 1.8 + 1.8 + true + + org.mybatis.generator diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/DingUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/DingUtil.java index 5301ed9..1a1ba8d 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/DingUtil.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/DingUtil.java @@ -1,5 +1,5 @@ package com.aiit.xiuos.Utils; -import com.alibaba.fastjson.JSON; + import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.google.common.collect.Maps; diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmqxApiUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmqxApiUtil.java new file mode 100644 index 0000000..ae3f1e7 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmqxApiUtil.java @@ -0,0 +1,39 @@ +package com.aiit.xiuos.Utils; + +import com.aiit.xiuos.mqtt.MqttConfiguration; +import com.alibaba.fastjson.JSONObject; + +import okhttp3.Credentials; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.checkerframework.checker.units.qual.A; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.IOException; +@Component +public class EmqxApiUtil { + @Autowired + MqttConfiguration mqttConfiguration; + + + public String getClientInfo(String clientId){ + try { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url(mqttConfiguration.getApiurl()+"/api/v5/clients/"+clientId) + .header("Content-Type", "application/json") + .header("Authorization", Credentials.basic(mqttConfiguration.getApikey(), mqttConfiguration.getSecretkey())) + .build(); + + Response response = client.newCall(request).execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/HexUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/HexUtil.java index a145ae5..294962e 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/HexUtil.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/HexUtil.java @@ -2,6 +2,7 @@ package com.aiit.xiuos.Utils; import com.alibaba.fastjson.JSONObject; +import java.io.RandomAccessFile; import java.math.BigInteger; import java.util.ArrayList; import java.util.BitSet; @@ -168,6 +169,7 @@ public class HexUtil { bit = bs[i] & 0x0f; // 低四位, 与操作 0000 1111 sb.append(chars[bit]); sb.append(' ');//每个Byte之间空格分隔 + } return sb.toString().trim(); } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/OTAFileUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/OTAFileUtil.java new file mode 100644 index 0000000..cf1782e --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/OTAFileUtil.java @@ -0,0 +1,56 @@ +package com.aiit.xiuos.Utils; + +import cn.hutool.core.io.FileUtil; + +import java.io.*; +import java.util.ArrayList; + +public class OTAFileUtil { + public static byte[] getFile(String fileName, int offset,int size) { + byte[] buffer = new byte[size]; + File file = new File(System.getProperty("user.dir") + "/otafiles/" + fileName); + byte[] chunk; + RandomAccessFile randomAccessFile =null; + try { + randomAccessFile = new RandomAccessFile(file, "r"); + int bytesRead; + randomAccessFile.seek(offset); + if ((bytesRead = randomAccessFile.read(buffer)) != -1) { + if (bytesRead < size) { + chunk = new byte[bytesRead]; + System.arraycopy(buffer, 0, chunk, 0, bytesRead); + } else { + chunk = new byte[size]; + System.arraycopy(buffer, 0, chunk, 0, size); + } + return chunk; + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if(randomAccessFile!=null){ + try{ + randomAccessFile.close(); + System.out.println("文件流关闭"+"offset="+offset); + }catch (IOException e){ + e.printStackTrace(); + } + } + } + return null; + } + + public static void main(String rgs[]) { + + byte[] chunk = OTAFileUtil.getFile("2_ota_refresh.svg", 0,1024); + System.out.println(); + String filePath = System.getProperty("user.dir") + "/otafiles/" + "2_ota_refresh.svg"; + + FileUtil.del(filePath); + + + + + } + +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/XiuosApplication.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/XiuosApplication.java index 6025855..4e734b1 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/XiuosApplication.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/XiuosApplication.java @@ -15,7 +15,11 @@ import org.springframework.scheduling.annotation.EnableScheduling; public class XiuosApplication { public static void main(String[] args) { - SpringApplication.run(XiuosApplication.class, args); + try { + SpringApplication.run(XiuosApplication.class, args); + } catch (Throwable e) { + e.printStackTrace(); + } } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/DeviceController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/DeviceController.java index 469ba01..f2d3e71 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/DeviceController.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/DeviceController.java @@ -87,7 +87,7 @@ public class DeviceController { @GetMapping("/getRunStatusCount") public ResultRespons getRunStatusCount(HttpServletRequest request){ UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); - List> list=deviceInfoService.getDeviceRunStautsCount(userInfo.getOrg()); + List> list=deviceInfoService.getDeviceRunStatusCount(userInfo.getOrg()); if(list!=null){ return new ResultRespons(Constant.SUCCESS_CODE,"查询设备成功!",list); } @@ -97,9 +97,6 @@ public class DeviceController { @GetMapping("/updateDeviceStatus") public ResultRespons updateDeviceStatus(HttpServletRequest request){ UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); - if(userInfo==null){ - return new ResultRespons(Constant.SessionTimeOut_CODE,"用户尚未登录,请先登录!"); - } List list=deviceInfoService.selectActiveDevice(userInfo.getOrg()); if(list!=null){ return new ResultRespons(Constant.SUCCESS_CODE,"查询设备成功!",list); diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/EmqxApiController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/EmqxApiController.java new file mode 100644 index 0000000..c65ed55 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/EmqxApiController.java @@ -0,0 +1,31 @@ +package com.aiit.xiuos.controller; + +import com.aiit.xiuos.Utils.Constant; +import com.aiit.xiuos.Utils.EmqxApiUtil; +import com.aiit.xiuos.Utils.ResultRespons; +import org.apache.commons.lang.StringUtils; +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@RequestMapping("/api") +public class EmqxApiController { + @Autowired + EmqxApiUtil emqxApiUtil; + @GetMapping("/getClient") + public ResultRespons selectDevice(@RequestParam("clientId") String clientId){ + String res= emqxApiUtil.getClientInfo(clientId); + if(StringUtils.contains(res,"clientid")){ + return new ResultRespons(Constant.SUCCESS_CODE,"查询设备状态成功"); + }else{ + return new ResultRespons(Constant.ERROR_CODE,"设备不在线!"); + + } + + } +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/FirmwareController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/FirmwareController.java index 62c2904..41fe479 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/FirmwareController.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/FirmwareController.java @@ -103,11 +103,9 @@ public class FirmwareController { String fileName = firmwareInfo.getFileName(); String version = firmwareInfo.getFileVersion(); String filePath = System.getProperty("user.dir") + "/otafiles/" + version+"_"+fileName; - Boolean flag = FileUtil.del(filePath); - + Boolean flag = FileUtil.del(filePath); if (flag) { int res = firmwareInfoService.deleteFirmware(id); - if (1 == res) { otaInfoService.deleteOtaInfo(fileName,version); return new ResultRespons(Constant.SUCCESS_CODE, "删除成功"); @@ -116,7 +114,7 @@ public class FirmwareController { } } else { - return new ResultRespons(Constant.ERROR_CODE, "固件不存在"); + return new ResultRespons(Constant.ERROR_CODE, "删除失败"); } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/LoraDeviceController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/LoraDeviceController.java new file mode 100644 index 0000000..3df3384 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/LoraDeviceController.java @@ -0,0 +1,87 @@ +package com.aiit.xiuos.controller; + +import com.aiit.xiuos.Utils.Constant; +import com.aiit.xiuos.Utils.ResultRespons; +import com.aiit.xiuos.model.LoraDeviceInfo; +import com.aiit.xiuos.model.UserInfo; +import com.aiit.xiuos.service.LoraDeviceInfoService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.Map; + +@RestController +@Slf4j +@RequestMapping("/loraDevice") +public class LoraDeviceController { + @Autowired + LoraDeviceInfoService loraDeviceInfoService; + @PostMapping("/add") + public ResultRespons addInfo (@RequestBody LoraDeviceInfo loraDeviceInfo, HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + loraDeviceInfo.setOrg(userInfo.getOrg()); + int res = loraDeviceInfoService.addInfo(loraDeviceInfo); + if(1==res){ + return new ResultRespons(Constant.SUCCESS_CODE,"新增lora设备信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"新增lora设备信息失败!"); + } + + @PostMapping("/update") + public ResultRespons updateInfo (@RequestBody LoraDeviceInfo loraDeviceInfo) { + int res = loraDeviceInfoService.updateInfo(loraDeviceInfo); + if(res>=0){ + return new ResultRespons(Constant.SUCCESS_CODE,"更新lora设备信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"更新lora设备信息失败!"); + } + + @PostMapping("/delete") + public ResultRespons deleteInfo (@RequestBody LoraDeviceInfo loraDeviceInfo) { + int res = loraDeviceInfoService.deleteInfo(loraDeviceInfo.getDevEui()); + if(1==res){ + return new ResultRespons(Constant.SUCCESS_CODE,"删除lora设备信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"删除lora设备信息失败!"); + } + + @GetMapping("/select") + public ResultRespons getInfo (@RequestParam("gatewayid") String id,HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + List res =null; + if(StringUtils.isEmpty(id)){ + res = loraDeviceInfoService.selectInfo(userInfo.getOrg()); + }else{ + res = loraDeviceInfoService.selectInfoByGateway(id,userInfo.getOrg()); + } + + if(null!=res){ + return new ResultRespons(Constant.SUCCESS_CODE,"查询lora设备信息成功!",res); + } + return new ResultRespons(Constant.ERROR_CODE,"查询lora设备信息失败!"); + } + + @GetMapping("/getStatus") + public ResultRespons getStatus (HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + List> statusList = loraDeviceInfoService.getDeviceRunStatusCount(userInfo.getOrg()); + if(null!=statusList){ + return new ResultRespons(Constant.SUCCESS_CODE,"查询lora设备信息成功!",statusList); + } + return new ResultRespons(Constant.ERROR_CODE,"查询lora设备信息失败!"); + } + + @PostMapping("/offline") + public ResultRespons setDeviceOffLine (HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + int res = loraDeviceInfoService.setDeviceOffline(userInfo.getOrg()); + if(res>=0){ + return new ResultRespons(Constant.SUCCESS_CODE,"更新lora设备信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"更新lora设备信息失败!"); + } +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/LoraGatewayController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/LoraGatewayController.java new file mode 100644 index 0000000..7808711 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/LoraGatewayController.java @@ -0,0 +1,82 @@ +package com.aiit.xiuos.controller; + +import com.aiit.xiuos.Utils.Constant; +import com.aiit.xiuos.Utils.ResultRespons; +import com.aiit.xiuos.model.LoraDeviceInfo; +import com.aiit.xiuos.model.LoraGatewayInfo; +import com.aiit.xiuos.model.UserInfo; +import com.aiit.xiuos.service.LoraDeviceInfoService; +import com.aiit.xiuos.service.LoraGatewayInfoService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.Map; + +@RestController +@Slf4j +@RequestMapping("/loraGateway") +public class LoraGatewayController { + @Autowired + LoraGatewayInfoService loraGatewayInfoService; + @PostMapping("/add") + public ResultRespons addInfo (@RequestBody LoraGatewayInfo loraGatewayInfo, HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + loraGatewayInfo.setOrg(userInfo.getOrg()); + int res = loraGatewayInfoService.addGatewayInfo(loraGatewayInfo); + if(1==res){ + return new ResultRespons(Constant.SUCCESS_CODE,"新增lora网关信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"新增lora网关信息失败!"); + } + + @PostMapping("/update") + public ResultRespons updateInfo (@RequestBody LoraGatewayInfo loraGatewayInfo) { + int res = loraGatewayInfoService.updateGatewayInfo(loraGatewayInfo); + if(res>=0){ + return new ResultRespons(Constant.SUCCESS_CODE,"更新lora设备信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"更新lora设备信息失败!"); + } + + @PostMapping("/delete") + public ResultRespons deleteInfo (@RequestBody LoraGatewayInfo loraGatewayInfo) { + int res = loraGatewayInfoService.deleteGatewayInfo(loraGatewayInfo.getGatewayid()); + if(1==res){ + return new ResultRespons(Constant.SUCCESS_CODE,"删除lora网关设备信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"删除lora网关设备信息失败!"); + } + + @GetMapping("/select") + public ResultRespons getInfo (HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + List res = loraGatewayInfoService.selectGatewayInfo(userInfo.getOrg()); + if(null!=res){ + return new ResultRespons(Constant.SUCCESS_CODE,"查询lora网关信息成功!",res); + } + return new ResultRespons(Constant.ERROR_CODE,"查询lora网关信息失败!"); + } + + @GetMapping("/getStatus") + public ResultRespons getStatus (HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + List> statusList = loraGatewayInfoService.getGatewayRunStatusCount(userInfo.getOrg()); + if(null!=statusList){ + return new ResultRespons(Constant.SUCCESS_CODE,"查询lora设备信息成功!",statusList); + } + return new ResultRespons(Constant.ERROR_CODE,"查询lora设备信息失败!"); + } + + @PostMapping("/offline") + public ResultRespons setGatewayOffLine (HttpServletRequest request) { + UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); + int res = loraGatewayInfoService.setGatewayOffline(userInfo.getOrg()); + if(res>=0){ + return new ResultRespons(Constant.SUCCESS_CODE,"更新lora网关信息成功!"); + } + return new ResultRespons(Constant.ERROR_CODE,"更新lora网关信息失败!"); + } +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/OTAController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/OTAController.java index 9f559ba..ded69a1 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/OTAController.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/OTAController.java @@ -1,79 +1,105 @@ package com.aiit.xiuos.controller; -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.util.IdUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.crypto.SecureUtil; + import com.aiit.xiuos.Utils.Constant; import com.aiit.xiuos.Utils.ResultRespons; +import com.aiit.xiuos.model.FirmwareInfo; +import com.aiit.xiuos.model.OtaInfo; +import com.aiit.xiuos.model.UserInfo; +import com.aiit.xiuos.mqtt.MyMqttClient; +import com.aiit.xiuos.service.FirmwareInfoService; +import com.aiit.xiuos.service.OtaInfoService; +import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.OutputStream; -import java.net.URLEncoder; +import javax.servlet.http.HttpServletRequest; import java.util.List; @RestController @RequestMapping("/ota") @Slf4j public class OTAController { + @Autowired + private MyMqttClient myMqttClient; + @Autowired + OtaInfoService otaInfoService; + @Autowired + FirmwareInfoService firmwareInfoService; - /* -上传接口 - */ -//MultipartFile用于接受前端传过来的file对象 - @PostMapping("/upload") - public ResultRespons upload(MultipartFile file) throws IOException { - String md5 = SecureUtil.md5(file.getInputStream()); - System.out.println("文件的md5是:"+md5); - String filename = file.getOriginalFilename();//获取文件的名称 -// String flag = IdUtil.fastSimpleUUID();//通过Hutool工具包的IdUtil类获取uuid作为前缀 - String rootFilePath = System.getProperty("user.dir") + "/otafiles/" + filename; - FileUtil.writeBytes(file.getBytes(), rootFilePath);//使用Hutool工具包将我们接收到文件保存到rootFilePath中 - return new ResultRespons(Constant.SUCCESS_CODE, "上传成功", filename); + @GetMapping("/getAll") + public ResultRespons getAllJob(@RequestParam("fileName") String fileName,@RequestParam("fileVersion") String fileVersion,HttpServletRequest request) { + UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user"); + List otaInfos = otaInfoService.getALL(fileName,fileVersion ,userInfo.getOrg()); + return new ResultRespons(Constant.SUCCESS_CODE, "查询成功", otaInfos); + } + + @PostMapping("/overJob") + public ResultRespons overJob(@RequestBody OtaInfo otaInfo) { + otaInfo.setStatus(0); + otaInfoService.updateOtaInfo(otaInfo); + return new ResultRespons(Constant.SUCCESS_CODE, "更新成功"); + } + + @PostMapping("/reTryJob") + public ResultRespons reTryJob(@RequestBody OtaInfo otaInfo) { + String clientId =otaInfo.getDeviceId(); + String version =otaInfo.getFileVersion(); + String fileName =otaInfo.getFileName(); + FirmwareInfo firmwareInfo= firmwareInfoService.getByNameAndVersion(fileName,version); + String md5 = firmwareInfo.getFileMd5(); + long size = firmwareInfo.getFileSize(); + int id =firmwareInfo.getId(); + JSONObject payload =new JSONObject(); + payload.put("fileId",id); + payload.put("version",version); + payload.put("md5",md5); + payload.put("fileSize",size); + myMqttClient.publish("ota/"+clientId+"/update",payload.toJSONString()); + //状态置为进行中 + otaInfo.setStatus(2); + otaInfoService.updateOtaInfo(otaInfo); + return new ResultRespons(Constant.SUCCESS_CODE, "开始下发"); } - /* - 下载接口 - */ - @GetMapping("/download/{name}") - public ResultRespons getFiles(@PathVariable String name, HttpServletResponse response) { - OutputStream os;//新建一个输出流对象 - String basePath = System.getProperty("user.dir") + "/otafiles/"; //定义文件上传的根路径 - List fileNames = FileUtil.listFileNames(basePath);//获取所有的文件名称 - String fileName = fileNames.stream().filter(filename -> filename.contains(name)).findAny().orElse("");//找到跟参数一致的文件 - try { - if (StrUtil.isNotEmpty(fileName)) { - response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); - response.setContentType("application/octet-stream"); - byte[] bytes = FileUtil.readBytes(basePath + fileName);//通过文件的路径读取文件字节流 - os = response.getOutputStream();//通过response的输出流返回文件 - os.write(bytes); - os.flush(); - os.close(); - return new ResultRespons(Constant.SUCCESS_CODE, "下载成功"); - }else{ - return new ResultRespons(Constant.ERROR_CODE, "文件名不存在"); - } - } catch (Exception e) { - return new ResultRespons(Constant.ERROR_CODE, "下载失败"); + + @PostMapping("/addJob") + public ResultRespons addJob(@RequestBody OtaInfo otaInfo, HttpServletRequest request){ + OtaInfo lastOta = otaInfoService.getJobById(otaInfo.getDeviceId()); + if(lastOta!=null){ + return new ResultRespons(Constant.ERROR_CODE,"新增升级任务失败,存在进行中的任务,请检查!"); } + UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user"); + otaInfo.setOrg(userInfo.getOrg()); + otaInfo.setCurrentProcess(0); + //立即执行 + if(otaInfo.getUpdateType()==0){ + String clientId =otaInfo.getDeviceId(); + String version =otaInfo.getFileVersion(); + String fileName =otaInfo.getFileName(); + FirmwareInfo firmwareInfo= firmwareInfoService.getByNameAndVersion(fileName,version); + String md5 = firmwareInfo.getFileMd5(); + long size = firmwareInfo.getFileSize(); + int id =firmwareInfo.getId(); + JSONObject payload =new JSONObject(); + payload.put("fileId",id); + payload.put("version",version); + payload.put("md5",md5); + payload.put("fileSize",size); + myMqttClient.publish(1,false,"ota/"+clientId+"/update",payload.toJSONString()); + //状态置为进行中 + otaInfo.setStatus(2); + }else{ + //状态设置未开始 + otaInfo.setStatus(3); + } + int res = otaInfoService.addOtaInfo(otaInfo); + if(1==res){ + return new ResultRespons(Constant.SUCCESS_CODE,"新增升级任务成功"); + } + return new ResultRespons(Constant.ERROR_CODE,"新增升级任务失败"); } - - @PostMapping("/delete/{name}") - public ResultRespons deleteFile(@PathVariable String name) throws IOException { - String filePath =System.getProperty("user.dir") + "/otafiles/"+name; - Boolean flag =FileUtil.isFile(filePath); - if(flag){ - FileUtil.del(filePath); - return new ResultRespons(Constant.SUCCESS_CODE, "删除成功"); - }else{ - return new ResultRespons(Constant.ERROR_CODE, "文件不存在"); - } - } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ProtocolProductInfoController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ProtocolProductInfoController.java index 1ed7bfd..f8c43b0 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ProtocolProductInfoController.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ProtocolProductInfoController.java @@ -5,9 +5,12 @@ import com.aiit.xiuos.model.DeviceInfo; import com.aiit.xiuos.model.ProtocolProductInfo; import com.aiit.xiuos.model.UserInfo; import com.aiit.xiuos.model.VO.ProtocolProductVo; +import com.aiit.xiuos.mqtt.MqttConfiguration; +import com.aiit.xiuos.mqtt.MyMqttClient; import com.aiit.xiuos.service.DeviceInfoService; import com.aiit.xiuos.service.ProtocolService; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -27,13 +30,12 @@ import java.util.Map; public class ProtocolProductInfoController { @Autowired private ProtocolService protocolService; + @Autowired + MyMqttClient myMqttClient; @PostMapping("/add") public ResultRespons addProtocolProduct(@RequestBody ProtocolProductVo protocolProductVo, HttpServletRequest request) throws ParseException { UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); - if(userInfo==null){ - return new ResultRespons(Constant.SessionTimeOut_CODE,"用户尚未登录,请先登录!"); - } ProtocolProductInfo productInfo = protocolService.getProtocolByName(protocolProductVo.getProductName()); if (productInfo != null) { @@ -62,9 +64,6 @@ public class ProtocolProductInfoController { @GetMapping("/selectAll") public ResultRespons selectProtocolProductList(HttpServletRequest request) throws ParseException { UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user"); - if(userInfo==null){ - return new ResultRespons(Constant.SessionTimeOut_CODE,"用户尚未登录,请先登录!"); - } List protocolProductInfos = protocolService.getProtocolAll(userInfo.getOrg()); List protocolProductVos= new ArrayList<>(); if (protocolProductInfos != null && protocolProductInfos.size() >= 0) { @@ -119,4 +118,13 @@ public class ProtocolProductInfoController { } return new ResultRespons(Constant.ERROR_CODE, "导出csv成功!"); } + + @PostMapping("/publish") + public ResultRespons publish(@RequestBody Map jsonMap) { + String clientId =jsonMap.get("clientId").toString(); + String jsonString =jsonMap.get("jsonString").toString(); + log.info("发送的配方消息是:"+jsonString); + myMqttClient.publish("protocol/"+clientId+"/files",jsonString); + return new ResultRespons(Constant.SUCCESS_CODE, "协议已推送!"); + } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/FirmwareInfoMapper.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/FirmwareInfoMapper.java index ddac908..151abbe 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/FirmwareInfoMapper.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/FirmwareInfoMapper.java @@ -21,15 +21,15 @@ public interface FirmwareInfoMapper { int updateByPrimaryKey(FirmwareInfo record); - @Select("select * from firmware_info where org=#{org}") + @Select("select * from firmware_info where org=#{org} order by id desc") List getAll(@Param("org") String org); - @Select("select * from firmware_info where org=#{org} and verify=#{verify}") + @Select("select * from firmware_info where org=#{org} and verify=#{verify} order by id desc") List getByVerify(@Param("org") String org,@Param("verify") String verify); - @Select("select * from firmware_info where file_name=#{name}") + @Select("select * from firmware_info where file_name=#{name} order by id desc") List getByName(@Param("name") String name); - @Select("select * from firmware_info where file_name=#{name} and file_version=#{version}") + @Select("select * from firmware_info where file_name=#{name} and file_version=#{version} ") FirmwareInfo getByNameAndVersion(@Param("name") String name,@Param("version") String version); } \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/LoraDeviceInfoMapper.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/LoraDeviceInfoMapper.java new file mode 100644 index 0000000..a0d5c33 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/LoraDeviceInfoMapper.java @@ -0,0 +1,38 @@ +package com.aiit.xiuos.dao.mappers; + +import com.aiit.xiuos.model.LoraDeviceInfo; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +@Repository +public interface LoraDeviceInfoMapper { + int deleteByPrimaryKey(String deveui); + + int insert(LoraDeviceInfo record); + + int insertSelective(LoraDeviceInfo record); + + LoraDeviceInfo selectByPrimaryKey(String deveui); + + int updateByPrimaryKeySelective(LoraDeviceInfo record); + + int updateByPrimaryKey(LoraDeviceInfo record); + + @Select("select * from lora_device_info where org=#{org}") + List selectAll(@Param("org") String org); + + @Select("select * from lora_device_info where gatewayId=#{id} and org=#{org}") + List selectById(@Param("id") String id,@Param("org") String org); + + + @Select("select d.status ,count(d.status) from lora_device_info d where org = #{org} GROUP BY d.status") + List> getDeviceRunStatusCount(@Param("org") String org); + + @Update("update lora_device_info set status= 2 where status = 1 and org = #{org} ") + int setDeviceOffLine(@Param("org") String org); +} \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/LoraGatewayInfoMapper.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/LoraGatewayInfoMapper.java new file mode 100644 index 0000000..ff0a7bc --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/LoraGatewayInfoMapper.java @@ -0,0 +1,35 @@ +package com.aiit.xiuos.dao.mappers; + +import com.aiit.xiuos.model.LoraDeviceInfo; +import com.aiit.xiuos.model.LoraGatewayInfo; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +@Repository +public interface LoraGatewayInfoMapper { + int deleteByPrimaryKey(String gatewayid); + + int insert(LoraGatewayInfo record); + + int insertSelective(LoraGatewayInfo record); + + LoraGatewayInfo selectByPrimaryKey(String gatewayid); + + int updateByPrimaryKeySelective(LoraGatewayInfo record); + + int updateByPrimaryKey(LoraGatewayInfo record); + + @Select("select * from lora_gateway_info where org=#{org}") + List selectAll(@Param("org") String org); + + @Select("select d.status ,count(d.status) from lora_gateway_info d where org = #{org} GROUP BY d.status") + List> getGatewayRunStatusCount(@Param("org") String org); + + @Update("update lora_gateway_info set status= 2 where status = 1 and org = #{org} ") + int setGatewayOffLine(@Param("org") String org); +} \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/OtaInfoMapper.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/OtaInfoMapper.java index 5e5e1c0..8d63bd1 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/OtaInfoMapper.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/OtaInfoMapper.java @@ -1,7 +1,14 @@ package com.aiit.xiuos.dao.mappers; +import com.aiit.xiuos.model.FirmwareInfo; import com.aiit.xiuos.model.OtaInfo; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.springframework.stereotype.Repository; +import java.util.List; +@Repository public interface OtaInfoMapper { int deleteByPrimaryKey(Integer id); @@ -14,4 +21,16 @@ public interface OtaInfoMapper { int updateByPrimaryKeySelective(OtaInfo record); int updateByPrimaryKey(OtaInfo record); + + @Select("select * from ota_info where file_name=#{name} and file_version =#{version} and org=#{org} order by create_time desc") + List getAll(@Param("name") String name,@Param("version") String version,@Param("org") String org); + + @Select("select * from ota_info where update_type=1 and status =3") + List getJob(); + + @Select("select * from ota_info where device_id =#{device_id} and status =2") + OtaInfo getOtaInfo(@Param("device_id") String deviceId); + + @Delete("delete from ota_info where file_name=#{name} and file_version =#{version}") + int deleteOtaInfo(@Param("name") String name,@Param("version") String version); } \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/DeviceInfo.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/DeviceInfo.java index 2f1d021..7d91cae 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/DeviceInfo.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/DeviceInfo.java @@ -6,10 +6,10 @@ import lombok.Data; @Data @Builder public class DeviceInfo { - private String id; - private String no; + private String id; + private String productname; private String type; @@ -56,9 +56,9 @@ public class DeviceInfo { private String org; - public DeviceInfo(String id, String no, String productname, String type, Integer activestatus, String updatetime, String devicedesc, Integer runstatus, String statusdesc, String kernel, String webversion, String ipaddr, String netmask, String gateway, String dnsserver0, String dnsserver1, String topic, String serveraddr, String serverport, String username, String clientid, String privateserveraddr, String privateserverport, String privateserverusername, String org) { - this.id = id; + public DeviceInfo(String no, String id, String productname, String type, Integer activestatus, String updatetime, String devicedesc, Integer runstatus, String statusdesc, String kernel, String webversion, String ipaddr, String netmask, String gateway, String dnsserver0, String dnsserver1, String topic, String serveraddr, String serverport, String username, String clientid, String privateserveraddr, String privateserverport, String privateserverusername, String org) { this.no = no; + this.id = id; this.productname = productname; this.type = type; this.activestatus = activestatus; diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/LoraDeviceInfo.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/LoraDeviceInfo.java new file mode 100644 index 0000000..f1ddcb9 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/LoraDeviceInfo.java @@ -0,0 +1,57 @@ +package com.aiit.xiuos.model; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class LoraDeviceInfo { + private String devEui; + + private String joinEui; + + private String name; + + private String appKey; + + private Boolean supportOtaa; + + private Boolean supportClassc; + + private String gatewayid; + + private String remark1; + + private String remark2; + + private String org; + + private String remark3; + + private String remark4; + + private Integer status; + + private String remark5; + + public LoraDeviceInfo(String devEui, String joinEui, String name, String appKey, Boolean supportOtaa, Boolean supportClassc, String gatewayid, String remark1, String remark2, String org, String remark3, String remark4, Integer status, String remark5) { + this.devEui = devEui; + this.joinEui = joinEui; + this.name = name; + this.appKey = appKey; + this.supportOtaa = supportOtaa; + this.supportClassc = supportClassc; + this.gatewayid = gatewayid; + this.remark1 = remark1; + this.remark2 = remark2; + this.org = org; + this.remark3 = remark3; + this.remark4 = remark4; + this.status = status; + this.remark5 = remark5; + } + + public LoraDeviceInfo() { + super(); + } +} \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/LoraGatewayInfo.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/LoraGatewayInfo.java new file mode 100644 index 0000000..fdb6172 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/LoraGatewayInfo.java @@ -0,0 +1,51 @@ +package com.aiit.xiuos.model; + +import java.util.Date; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class LoraGatewayInfo { + private String gatewayid; + + private String name; + + private Integer deviceNum; + @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") + private Date lastConnect; + + private Integer status; + + private String org; + + private String remark1; + + private String remark2; + + private String remark3; + + private String remark4; + + private String remark5; + + public LoraGatewayInfo(String gatewayid, String name, Integer deviceNum, Date lastConnect, Integer status, String org, String remark1, String remark2, String remark3, String remark4, String remark5) { + this.gatewayid = gatewayid; + this.name = name; + this.deviceNum = deviceNum; + this.lastConnect = lastConnect; + this.status = status; + this.org = org; + this.remark1 = remark1; + this.remark2 = remark2; + this.remark3 = remark3; + this.remark4 = remark4; + this.remark5 = remark5; + } + + public LoraGatewayInfo() { + super(); + } +} \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/OtaInfo.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/OtaInfo.java index 5aed9ea..365f62e 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/OtaInfo.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/OtaInfo.java @@ -1,6 +1,8 @@ package com.aiit.xiuos.model; import java.util.Date; + +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Builder; import lombok.Data; @@ -16,7 +18,7 @@ public class OtaInfo { private Integer currentProcess; private Integer status; - + @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date createTime; private Integer updateType; @@ -25,7 +27,9 @@ public class OtaInfo { private String fileVersion; - public OtaInfo(Integer id, String fileName, String deviceId, Integer currentProcess, Integer status, Date createTime, Integer updateType, Date timeout, String fileVersion) { + private String org; + + public OtaInfo(Integer id, String fileName, String deviceId, Integer currentProcess, Integer status, Date createTime, Integer updateType, Date timeout, String fileVersion, String org) { this.id = id; this.fileName = fileName; this.deviceId = deviceId; @@ -35,6 +39,7 @@ public class OtaInfo { this.updateType = updateType; this.timeout = timeout; this.fileVersion = fileVersion; + this.org = org; } public OtaInfo() { diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MqttConfiguration.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MqttConfiguration.java index 546cdaa..6562afe 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MqttConfiguration.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MqttConfiguration.java @@ -3,20 +3,16 @@ package com.aiit.xiuos.mqtt; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; - @Component @ConfigurationProperties("mqtt") @Setter @Getter @Slf4j public class MqttConfiguration { - @Autowired - private MyMqttClient myMqttClient; /** * 用户名 @@ -47,12 +43,19 @@ public class MqttConfiguration { */ private int keepalive; + private String apikey; + private String secretkey; + private String apiurl; + @Bean public MyMqttClient getMqttPushClient() { + MyMqttClient myMqttClient =new MyMqttClient(); myMqttClient.connect(hostUrl, clientId, username, password, timeout, keepalive); // 以/#结尾表示订阅所有以test开头的主题 myMqttClient.subscribe(defaultTopic, 0); - log.info("订阅成功"+defaultTopic); + myMqttClient.subscribe("$SYS/brokers/+/clients/+/#", 1); + log.info("订阅成功,clientId:"+clientId); return myMqttClient; } + } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MsgReceiveHandle.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MsgReceiveHandle.java index e246aa2..7cca8c0 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MsgReceiveHandle.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MsgReceiveHandle.java @@ -1,4 +1,112 @@ package com.aiit.xiuos.mqtt; +import com.aiit.xiuos.Utils.MyUtils; +import com.aiit.xiuos.Utils.OTAFileUtil; +import com.aiit.xiuos.Utils.SpringUtil; +import com.aiit.xiuos.model.DeviceInfo; +import com.aiit.xiuos.model.FirmwareInfo; +import com.aiit.xiuos.model.OtaInfo; +import com.aiit.xiuos.service.DeviceInfoService; +import com.aiit.xiuos.service.FirmwareInfoService; +import com.aiit.xiuos.service.OtaInfoService; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +@Slf4j public class MsgReceiveHandle { + + private OtaInfoService otaInfoService=SpringUtil.getBeanByClass(OtaInfoService.class); + + private FirmwareInfoService firmwareInfoService=SpringUtil.getBeanByClass(FirmwareInfoService.class); + + private DeviceInfoService deviceInfoService=SpringUtil.getBeanByClass(DeviceInfoService.class); + + + private MyMqttClient myMqttClient= SpringUtil.getBeanByClass(MyMqttClient.class); + + public void handleConnected(JSONObject msgJson) { + String deviceId =msgJson.getString("clientid"); + System.out.println("收到设备上线信息,设备ID:" + msgJson.getString("clientid")); + DeviceInfo deviceInfo = deviceInfoService.selectbyNo(deviceId); + if(deviceInfo==null){ + log.info("平台不存在该设备id,请检查"); + return; + } + deviceInfo.setUpdatetime(MyUtils.getTime()); + deviceInfo.setActivestatus(1); + deviceInfoService.updateDeviceInfo(deviceInfo); + } + + public void handleDisConnected(JSONObject msgJson) { + String deviceId =msgJson.getString("clientid"); + System.out.println("收到设备下线信息,设备ID:" + msgJson.getString("clientid")); + DeviceInfo deviceInfo = deviceInfoService.selectbyNo(deviceId); + if(deviceInfo==null){ + log.info("平台不存在该设备id,请检查"); + return; + } + deviceInfo.setUpdatetime(MyUtils.getTime()); + deviceInfo.setActivestatus(0); + deviceInfoService.updateDeviceInfo(deviceInfo); + } + + public void handleOtaFile(JSONObject msgJson) { + int fileId = msgJson.getIntValue("fileId"); + int fileOffset = msgJson.getIntValue("fileOffset"); + int size = msgJson.getIntValue("size"); + String clientId = msgJson.getString("clientId"); + OtaInfo otaInfo = otaInfoService.getJobById(clientId); + if(otaInfo!=null){ + otaInfo.setCurrentProcess(fileOffset+size); + otaInfoService.updateOtaInfo(otaInfo); + } + FirmwareInfo firmwareInfo = firmwareInfoService.getById(fileId); + String version =firmwareInfo.getFileVersion(); + String name = firmwareInfo.getFileName(); + String realName = version+"_"+name; + byte[] chunk = OTAFileUtil.getFile(realName, fileOffset,size); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("file",chunk); + myMqttClient.publish(1,false,"ota/" + clientId + "/files", chunk); + } + + public void handleOtaVersion(JSONObject msgJson) { + String deviceId = msgJson.getString("clientId"); + String version = msgJson.getString("version"); + DeviceInfo deviceInfo = deviceInfoService.selectbyNo(deviceId); + if(deviceInfo==null) { + log.info("不存在设备号为" + deviceId + "的设备"); + return; + } + deviceInfo.setUpdatetime(MyUtils.getTime()); + deviceInfo.setWebversion(version); + //查看是否存在进行中的ota信息 + OtaInfo otaInfo = otaInfoService.getJobById(deviceId); + if(otaInfo!=null){ + String targetVersion = otaInfo.getFileVersion(); + if(version.equals(targetVersion)){ + otaInfo.setStatus(1); + + }else { + otaInfo.setStatus(0); + } + otaInfoService.updateOtaInfo(otaInfo); + } + deviceInfoService.updateDeviceInfo(deviceInfo); + + + System.out.println("更新设备" + deviceId + "版本为" + version); + //执行更新内核版本操作,并更新ota状态为失败。 + } + + public void handleClientData(JSONObject msgJson) { + String clientId = msgJson.getString("clientId"); + JSONObject data =msgJson.getJSONObject("data"); + String time =msgJson.getString("time"); + System.out.println("收到设备" + clientId + "的数据" + data.toJSONString()); + //执行更新内核版本操作,并更新ota状态为失败。 + } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MyMqttClient.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MyMqttClient.java index fd05317..cbc4725 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MyMqttClient.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/MyMqttClient.java @@ -4,17 +4,20 @@ package com.aiit.xiuos.mqtt; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.io.FileOutputStream; +import java.util.ArrayList; + /** * @author wangtianyi * @description mqtt客户端 */ @Slf4j -@Component public class MyMqttClient { - - + @Autowired + PushCallback pushCallback; private static MqttClient client; @@ -88,13 +91,37 @@ public class MyMqttClient { message.setRetained(retained); message.setPayload(pushMessage.getBytes()); MqttTopic mqttTopic= MyMqttClient.getClient().getTopic(topic); + if(null== mqttTopic){ log.error("topic not exist"); } MqttDeliveryToken token; try { + //client.publish(topic,message); token=mqttTopic.publish(message); - token.waitForCompletion(); + token.waitForCompletion(100); + }catch (MqttPersistenceException e){ + log.error(e.getMessage()); + }catch (MqttException e){ + log.error(e.getMessage()); + } + } + + public void publish(int qos,boolean retained,String topic,byte[] payload){ + MqttMessage message=new MqttMessage(); + message.setQos(qos); + message.setRetained(retained); + message.setPayload(payload); + MqttTopic mqttTopic= MyMqttClient.getClient().getTopic(topic); + + if(null== mqttTopic){ + log.error("topic not exist"); + } + MqttDeliveryToken token; + try { + //client.publish(topic,message); + token=mqttTopic.publish(message); + token.waitForCompletion(100); }catch (MqttPersistenceException e){ log.error(e.getMessage()); }catch (MqttException e){ @@ -107,13 +134,14 @@ public class MyMqttClient { * @param topic */ public void subscribe(String topic){ - log.error("开始订阅主题" + topic); + log.info("开始订阅主题" + topic); subscribe(topic,0); } public void subscribe(String topic,int qos){ try { MyMqttClient.getClient().subscribe(topic,qos); + log.info("开始订阅主题" + topic); }catch (MqttException e){ log.error(e.getMessage()); } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/PushCallback.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/PushCallback.java index 43e8afa..5c12240 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/PushCallback.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/mqtt/PushCallback.java @@ -1,22 +1,30 @@ package com.aiit.xiuos.mqtt; +import cn.hutool.core.io.FileUtil; +import com.aiit.xiuos.Utils.SpringUtil; +import com.aiit.xiuos.service.OtaInfoService; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; + @Component @Slf4j public class PushCallback implements MqttCallbackExtended { - - @Autowired private MyMqttClient myMqttClient; + @Override public void connectionLost(Throwable throwable) { - log.error("mqtt lose connect:"+throwable) ; + log.error("mqtt lose connect") ; + throwable.printStackTrace(); long reconnectTimes = 1; while (true) { try { @@ -28,6 +36,7 @@ public class PushCallback implements MqttCallbackExtended { myMqttClient.getClient().reconnect(); } catch (MqttException e) { log.error("", e); + e.printStackTrace(); } try { Thread.sleep(3000); @@ -38,12 +47,34 @@ public class PushCallback implements MqttCallbackExtended { } @Override - public void messageArrived(String topic, MqttMessage message) throws Exception { + public void messageArrived(String topic, MqttMessage message) { + MsgReceiveHandle msgReceiveHandle = new MsgReceiveHandle(); System.out.println("接收消息主题 : " + topic); System.out.println("接收消息Qos : " + message.getQos()); - System.out.println("接收消息内容 : " + new String(message.getPayload())); + String msg = new String(message.getPayload()); + System.out.println("接收消息内容 : " + msg); + + try{ + JSONObject jsonObject = JSON.parseObject(msg); + if (topic.endsWith("disconnected")) { + msgReceiveHandle.handleDisConnected(jsonObject); + } else if(topic.endsWith("connected")){ + msgReceiveHandle.handleConnected(jsonObject); + } else if(topic.endsWith("ota/files")){ + msgReceiveHandle.handleOtaFile(jsonObject); + } else if(topic.endsWith("ota/version")){ + msgReceiveHandle.handleOtaVersion(jsonObject); + } else if(topic.endsWith("/data")){ + msgReceiveHandle.handleClientData(jsonObject); + } + }catch (Exception e){ + log.error("处理信息发生错误,请检查"); + e.printStackTrace(); + } + } + @Override public void deliveryComplete(IMqttDeliveryToken token) { System.out.println("deliveryComplete---------" + token.isComplete()); diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java index dbb22b8..387bc6b 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java @@ -34,7 +34,7 @@ public class GZJCTaskScheduled { } - @Scheduled(cron = "0 */1 * * * ?")//every hour + @Scheduled(cron = "0 */1 * * * ?")//every minute public void sendWebsocket() throws ParseException, IOException { GZJCData GZJCData = mockData(); JSONObject jsonObject = JSONUtil.parseObj(GZJCData,false,true); diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/OTATaskScheduled.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/OTATaskScheduled.java index 373ace9..76e827a 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/OTATaskScheduled.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/OTATaskScheduled.java @@ -1,4 +1,47 @@ package com.aiit.xiuos.scheduled; +import com.aiit.xiuos.model.FirmwareInfo; +import com.aiit.xiuos.model.OtaInfo; +import com.aiit.xiuos.mqtt.MyMqttClient; +import com.aiit.xiuos.service.FirmwareInfoService; +import com.aiit.xiuos.service.OtaInfoService; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import netscape.javascript.JSObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Slf4j +@Component public class OTATaskScheduled { + @Autowired + MyMqttClient myMqttClient; + @Autowired + FirmwareInfoService firmwareInfoService; + @Autowired + OtaInfoService otaInfoService; + @Scheduled(cron = "0 0 0 * * ?")//每日凌晨 + public void startOta(){ + List lists = otaInfoService.getNightJob(); + for(int i=0;i selectActiveDevice(String org); List selectUnActiveDevice(String org); - List selectbyNo(String no); + DeviceInfo selectbyNo(String no); List> getDeviceTypeCount(String org); - List> getDeviceRunStautsCount(String org); + List> getDeviceRunStatusCount(String org); String getTypeByName(String deviceNo); + List getDeviceList(String org); + int updateDeviceInfo(DeviceInfo deviceInfo); } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/LoraDeviceInfoService.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/LoraDeviceInfoService.java new file mode 100644 index 0000000..0f3ea26 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/LoraDeviceInfoService.java @@ -0,0 +1,16 @@ +package com.aiit.xiuos.service; + +import com.aiit.xiuos.model.LoraDeviceInfo; + +import java.util.List; +import java.util.Map; + +public interface LoraDeviceInfoService { + int addInfo (LoraDeviceInfo loraDeviceInfo); + int updateInfo(LoraDeviceInfo loraDeviceInfo); + int deleteInfo (String deveui); + List selectInfo(String org); + List selectInfoByGateway(String id,String org); + List> getDeviceRunStatusCount(String org); + int setDeviceOffline (String org); +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/LoraGatewayInfoService.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/LoraGatewayInfoService.java new file mode 100644 index 0000000..35b7611 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/LoraGatewayInfoService.java @@ -0,0 +1,15 @@ +package com.aiit.xiuos.service; + +import com.aiit.xiuos.model.LoraGatewayInfo; + +import java.util.List; +import java.util.Map; + +public interface LoraGatewayInfoService { + int addGatewayInfo(LoraGatewayInfo loraGatewayInfo); + int updateGatewayInfo(LoraGatewayInfo loraGatewayInfo); + List selectGatewayInfo(String org); + int deleteGatewayInfo(String gatewayId); + List> getGatewayRunStatusCount(String org); + int setGatewayOffline (String org); +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/OtaInfoService.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/OtaInfoService.java index fb91d19..0717226 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/OtaInfoService.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/OtaInfoService.java @@ -1,4 +1,16 @@ package com.aiit.xiuos.service; +import com.aiit.xiuos.model.OtaInfo; + +import java.util.List; + public interface OtaInfoService { + List getALL(String name,String version ,String org); + int addOtaInfo(OtaInfo info); + List getNightJob(); + int updateOtaInfo(OtaInfo info); + OtaInfo getJobById(String clientId); + int deleteOtaInfo(String name,String version); + + } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/DeviceInfoServiceImpl.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/DeviceInfoServiceImpl.java index 0777c06..7209212 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/DeviceInfoServiceImpl.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/DeviceInfoServiceImpl.java @@ -42,8 +42,8 @@ public class DeviceInfoServiceImpl implements DeviceInfoService { } @Override - public List selectbyNo(String no) { - return deviceInfoMapper.selectByNo(no); + public DeviceInfo selectbyNo(String no) { + return deviceInfoMapper.selectByPrimaryKey(no); } @Override @@ -52,7 +52,7 @@ public class DeviceInfoServiceImpl implements DeviceInfoService { } @Override - public List> getDeviceRunStautsCount(String org) { + public List> getDeviceRunStatusCount(String org) { return deviceInfoMapper.getDeviceRunStatusCount(org); } @@ -64,4 +64,14 @@ public class DeviceInfoServiceImpl implements DeviceInfoService { } return null; } + + @Override + public List getDeviceList(String org) { + return null; + } + + @Override + public int updateDeviceInfo(DeviceInfo deviceInfo) { + return deviceInfoMapper.updateByPrimaryKeySelective(deviceInfo); + } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/LoraDeviceInfoServiceImpl.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/LoraDeviceInfoServiceImpl.java new file mode 100644 index 0000000..6d9d23a --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/LoraDeviceInfoServiceImpl.java @@ -0,0 +1,51 @@ +package com.aiit.xiuos.service.impl; + +import com.aiit.xiuos.dao.mappers.LoraDeviceInfoMapper; +import com.aiit.xiuos.model.LoraDeviceInfo; +import com.aiit.xiuos.service.LoraDeviceInfoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service +public class LoraDeviceInfoServiceImpl implements LoraDeviceInfoService { + @Autowired + LoraDeviceInfoMapper loraDeviceInfoMapper; + + @Override + public int addInfo(LoraDeviceInfo loraDeviceInfo) { + return loraDeviceInfoMapper.insert(loraDeviceInfo); + } + + @Override + public int updateInfo(LoraDeviceInfo loraDeviceInfo) { + return loraDeviceInfoMapper.updateByPrimaryKeySelective(loraDeviceInfo); + } + + @Override + public int deleteInfo(String deveui) { + return loraDeviceInfoMapper.deleteByPrimaryKey(deveui); + } + + @Override + public List selectInfo(String org) { + return loraDeviceInfoMapper.selectAll(org); + } + + @Override + public List selectInfoByGateway(String id, String org) { + return loraDeviceInfoMapper.selectById(id,org); + } + + @Override + public List> getDeviceRunStatusCount(String org) { + return loraDeviceInfoMapper.getDeviceRunStatusCount(org); + } + + @Override + public int setDeviceOffline(String org) { + return loraDeviceInfoMapper.setDeviceOffLine(org); + } +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/LoraGatewayInfoServiceImpl.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/LoraGatewayInfoServiceImpl.java new file mode 100644 index 0000000..4162845 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/LoraGatewayInfoServiceImpl.java @@ -0,0 +1,45 @@ +package com.aiit.xiuos.service.impl; + +import com.aiit.xiuos.dao.mappers.LoraDeviceInfoMapper; +import com.aiit.xiuos.dao.mappers.LoraGatewayInfoMapper; +import com.aiit.xiuos.model.LoraGatewayInfo; +import com.aiit.xiuos.service.LoraGatewayInfoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service +public class LoraGatewayInfoServiceImpl implements LoraGatewayInfoService { + @Autowired + LoraGatewayInfoMapper loraGatewayInfoMapper; + @Override + public int addGatewayInfo(LoraGatewayInfo loraGatewayInfo) { + return loraGatewayInfoMapper.insert(loraGatewayInfo); + } + + @Override + public int updateGatewayInfo(LoraGatewayInfo loraGatewayInfo) { + return loraGatewayInfoMapper.updateByPrimaryKeySelective(loraGatewayInfo); + } + + @Override + public List selectGatewayInfo(String org) { + return loraGatewayInfoMapper.selectAll(org); + } + + @Override + public int deleteGatewayInfo(String gatewayId) { + return loraGatewayInfoMapper.deleteByPrimaryKey(gatewayId); + } + @Override + public List> getGatewayRunStatusCount(String org) { + return loraGatewayInfoMapper.getGatewayRunStatusCount(org); + } + + @Override + public int setGatewayOffline(String org) { + return loraGatewayInfoMapper.setGatewayOffLine(org); + } +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/OtaInfoServiceImpl.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/OtaInfoServiceImpl.java index df4b38a..7a3beb2 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/OtaInfoServiceImpl.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/OtaInfoServiceImpl.java @@ -1,4 +1,43 @@ package com.aiit.xiuos.service.impl; -public class OtaInfoServiceImpl { +import com.aiit.xiuos.dao.mappers.OtaInfoMapper; +import com.aiit.xiuos.model.OtaInfo; +import com.aiit.xiuos.service.OtaInfoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +@Service +public class OtaInfoServiceImpl implements OtaInfoService { + @Autowired + OtaInfoMapper otaInfoMapper; + @Override + public List getALL(String name,String version,String org) { + return otaInfoMapper.getAll(name,version,org); + } + + @Override + public int addOtaInfo(OtaInfo info) { + return otaInfoMapper.insertSelective(info); + } + + @Override + public List getNightJob() { + return otaInfoMapper.getJob(); + } + + @Override + public int updateOtaInfo(OtaInfo info) { + return otaInfoMapper.updateByPrimaryKeySelective(info); + } + + @Override + public OtaInfo getJobById(String clientId) { + return otaInfoMapper.getOtaInfo(clientId); + } + + @Override + public int deleteOtaInfo(String name, String version) { + return otaInfoMapper.deleteOtaInfo(name,version); + } } diff --git a/xiuosiot-backend/src/main/resources/application-local.yml b/xiuosiot-backend/src/main/resources/application-local.yml index 4e48282..60a4b0b 100644 --- a/xiuosiot-backend/src/main/resources/application-local.yml +++ b/xiuosiot-backend/src/main/resources/application-local.yml @@ -76,9 +76,11 @@ mqtt: #默认主题 default-topic: xiuosiot/# #api key - apikey: 6eea245a7f531f2f + apikey: ac527c699ec31245 #secret key - secretkey: XsyKpCXGvPfWifwOjsK2J2JP3E9AouxezXm8SqaJXcYP + secretkey: UEqxxgyY9Ba3Kebl2zlEu9Bq67n9AR9CdxDQ0iYz802XsbO + #api url + apiurl: http://8.140.53.225:18083 tdengine: url: jdbc:TAOS://taosnode1:6030/xiuosiot?user=root&password=taosdata diff --git a/xiuosiot-backend/src/main/resources/application-prod.yml b/xiuosiot-backend/src/main/resources/application-prod.yml index 0c8909d..5295285 100644 --- a/xiuosiot-backend/src/main/resources/application-prod.yml +++ b/xiuosiot-backend/src/main/resources/application-prod.yml @@ -57,7 +57,7 @@ mybatis: #MQTT Config mqtt: #MQTT-服务器连接地 - hostUrl: tcp://localhost:1883 + hostUrl: tcp://115.238.53.59:1883 #MQTT-连接服务器默认客户端ID clientId: xiuosiot-client #MQTT-用户名 @@ -74,6 +74,8 @@ mqtt: apikey: 13e1c0be6d573f86 #secret key secretkey: 9BLxyWslygS5oFTz8TX5ssJ7JMiBY9CefU9B35uWx3ltqN + #api url + apiurl: http://115.238.53.59:18083 tdengine: url: jdbc:TAOS://xiuosiot.taosnode1:6030/xiuosiot?user=root&password=taosdata diff --git a/xiuosiot-backend/src/main/resources/logback.xml b/xiuosiot-backend/src/main/resources/logback.xml index f981884..a3edb84 100644 --- a/xiuosiot-backend/src/main/resources/logback.xml +++ b/xiuosiot-backend/src/main/resources/logback.xml @@ -27,13 +27,13 @@ - + - + \ No newline at end of file diff --git a/xiuosiot-backend/src/main/resources/mappers/DeviceInfoMapper.xml b/xiuosiot-backend/src/main/resources/mappers/DeviceInfoMapper.xml index 0fc5dfb..08e7363 100644 --- a/xiuosiot-backend/src/main/resources/mappers/DeviceInfoMapper.xml +++ b/xiuosiot-backend/src/main/resources/mappers/DeviceInfoMapper.xml @@ -3,8 +3,8 @@ - + @@ -31,52 +31,50 @@ - id, no, productname, type, activestatus, updatetime, devicedesc, runstatus, statusdesc, - kernel, webversion, ipaddr, netmask, gateway, dnsserver0, dnsserver1, topic, serveraddr, - serverport, username, clientid, privateserveraddr, privateserverport, privateserverusername, + no, id, productname, type, activestatus, updatetime, devicedesc, runstatus, statusdesc, + kernel, webversion, ipaddr, netmask, gateway, dnsserver0, dnsserver1, topic, serveraddr, + serverport, username, clientid, privateserveraddr, privateserverport, privateserverusername, org - + select from device_info - where id = #{id,jdbcType=VARCHAR} - and no = #{no,jdbcType=VARCHAR} + where no = #{no,jdbcType=VARCHAR} - + delete from device_info - where id = #{id,jdbcType=VARCHAR} - and no = #{no,jdbcType=VARCHAR} + where no = #{no,jdbcType=VARCHAR} - insert into device_info (id, no, productname, - type, activestatus, updatetime, - devicedesc, runstatus, statusdesc, - kernel, webversion, ipaddr, - netmask, gateway, dnsserver0, - dnsserver1, topic, serveraddr, - serverport, username, clientid, - privateserveraddr, privateserverport, privateserverusername, - org) - values (#{id,jdbcType=VARCHAR}, #{no,jdbcType=VARCHAR}, #{productname,jdbcType=VARCHAR}, - #{type,jdbcType=VARCHAR}, #{activestatus,jdbcType=INTEGER}, #{updatetime,jdbcType=VARCHAR}, - #{devicedesc,jdbcType=VARCHAR}, #{runstatus,jdbcType=INTEGER}, #{statusdesc,jdbcType=VARCHAR}, - #{kernel,jdbcType=VARCHAR}, #{webversion,jdbcType=VARCHAR}, #{ipaddr,jdbcType=VARCHAR}, - #{netmask,jdbcType=VARCHAR}, #{gateway,jdbcType=VARCHAR}, #{dnsserver0,jdbcType=VARCHAR}, - #{dnsserver1,jdbcType=VARCHAR}, #{topic,jdbcType=VARCHAR}, #{serveraddr,jdbcType=VARCHAR}, - #{serverport,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{clientid,jdbcType=VARCHAR}, - #{privateserveraddr,jdbcType=VARCHAR}, #{privateserverport,jdbcType=VARCHAR}, #{privateserverusername,jdbcType=VARCHAR}, - #{org,jdbcType=VARCHAR}) + insert into device_info (no, id, productname, + type, activestatus, updatetime, + devicedesc, runstatus, statusdesc, + kernel, webversion, ipaddr, + netmask, gateway, dnsserver0, + dnsserver1, topic, serveraddr, + serverport, username, clientid, + privateserveraddr, privateserverport, privateserverusername, + org) + values (#{no,jdbcType=VARCHAR}, #{id,jdbcType=VARCHAR}, #{productname,jdbcType=VARCHAR}, + #{type,jdbcType=VARCHAR}, #{activestatus,jdbcType=INTEGER}, #{updatetime,jdbcType=VARCHAR}, + #{devicedesc,jdbcType=VARCHAR}, #{runstatus,jdbcType=INTEGER}, #{statusdesc,jdbcType=VARCHAR}, + #{kernel,jdbcType=VARCHAR}, #{webversion,jdbcType=VARCHAR}, #{ipaddr,jdbcType=VARCHAR}, + #{netmask,jdbcType=VARCHAR}, #{gateway,jdbcType=VARCHAR}, #{dnsserver0,jdbcType=VARCHAR}, + #{dnsserver1,jdbcType=VARCHAR}, #{topic,jdbcType=VARCHAR}, #{serveraddr,jdbcType=VARCHAR}, + #{serverport,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{clientid,jdbcType=VARCHAR}, + #{privateserveraddr,jdbcType=VARCHAR}, #{privateserverport,jdbcType=VARCHAR}, #{privateserverusername,jdbcType=VARCHAR}, + #{org,jdbcType=VARCHAR}) insert into device_info - - id, - no, + + id, + productname, @@ -148,12 +146,12 @@ - - #{id,jdbcType=VARCHAR}, - #{no,jdbcType=VARCHAR}, + + #{id,jdbcType=VARCHAR}, + #{productname,jdbcType=VARCHAR}, @@ -228,6 +226,9 @@ update device_info + + id = #{id,jdbcType=VARCHAR}, + productname = #{productname,jdbcType=VARCHAR}, @@ -298,36 +299,35 @@ org = #{org,jdbcType=VARCHAR}, - where id = #{id,jdbcType=VARCHAR} - and no = #{no,jdbcType=VARCHAR} + where no = #{no,jdbcType=VARCHAR} update device_info - set productname = #{productname,jdbcType=VARCHAR}, - type = #{type,jdbcType=VARCHAR}, - activestatus = #{activestatus,jdbcType=INTEGER}, - updatetime = #{updatetime,jdbcType=VARCHAR}, - devicedesc = #{devicedesc,jdbcType=VARCHAR}, - runstatus = #{runstatus,jdbcType=INTEGER}, - statusdesc = #{statusdesc,jdbcType=VARCHAR}, - kernel = #{kernel,jdbcType=VARCHAR}, - webversion = #{webversion,jdbcType=VARCHAR}, - ipaddr = #{ipaddr,jdbcType=VARCHAR}, - netmask = #{netmask,jdbcType=VARCHAR}, - gateway = #{gateway,jdbcType=VARCHAR}, - dnsserver0 = #{dnsserver0,jdbcType=VARCHAR}, - dnsserver1 = #{dnsserver1,jdbcType=VARCHAR}, - topic = #{topic,jdbcType=VARCHAR}, - serveraddr = #{serveraddr,jdbcType=VARCHAR}, - serverport = #{serverport,jdbcType=VARCHAR}, - username = #{username,jdbcType=VARCHAR}, - clientid = #{clientid,jdbcType=VARCHAR}, - privateserveraddr = #{privateserveraddr,jdbcType=VARCHAR}, - privateserverport = #{privateserverport,jdbcType=VARCHAR}, - privateserverusername = #{privateserverusername,jdbcType=VARCHAR}, - org = #{org,jdbcType=VARCHAR} - where id = #{id,jdbcType=VARCHAR} - and no = #{no,jdbcType=VARCHAR} + set id = #{id,jdbcType=VARCHAR}, + productname = #{productname,jdbcType=VARCHAR}, + type = #{type,jdbcType=VARCHAR}, + activestatus = #{activestatus,jdbcType=INTEGER}, + updatetime = #{updatetime,jdbcType=VARCHAR}, + devicedesc = #{devicedesc,jdbcType=VARCHAR}, + runstatus = #{runstatus,jdbcType=INTEGER}, + statusdesc = #{statusdesc,jdbcType=VARCHAR}, + kernel = #{kernel,jdbcType=VARCHAR}, + webversion = #{webversion,jdbcType=VARCHAR}, + ipaddr = #{ipaddr,jdbcType=VARCHAR}, + netmask = #{netmask,jdbcType=VARCHAR}, + gateway = #{gateway,jdbcType=VARCHAR}, + dnsserver0 = #{dnsserver0,jdbcType=VARCHAR}, + dnsserver1 = #{dnsserver1,jdbcType=VARCHAR}, + topic = #{topic,jdbcType=VARCHAR}, + serveraddr = #{serveraddr,jdbcType=VARCHAR}, + serverport = #{serverport,jdbcType=VARCHAR}, + username = #{username,jdbcType=VARCHAR}, + clientid = #{clientid,jdbcType=VARCHAR}, + privateserveraddr = #{privateserveraddr,jdbcType=VARCHAR}, + privateserverport = #{privateserverport,jdbcType=VARCHAR}, + privateserverusername = #{privateserverusername,jdbcType=VARCHAR}, + org = #{org,jdbcType=VARCHAR} + where no = #{no,jdbcType=VARCHAR} + select + + from lora_device_info + where dev_EUI = #{devEui,jdbcType=VARCHAR} + + + delete from lora_device_info + where dev_EUI = #{devEui,jdbcType=VARCHAR} + + + insert into lora_device_info (dev_EUI, join_EUI, name, + app_key, support_OTAA, support_ClassC, + gatewayId, remark1, remark2, + org, remark3, remark4, + status, remark5) + values (#{devEui,jdbcType=VARCHAR}, #{joinEui,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, + #{appKey,jdbcType=VARCHAR}, #{supportOtaa,jdbcType=BIT}, #{supportClassc,jdbcType=BIT}, + #{gatewayid,jdbcType=VARCHAR}, #{remark1,jdbcType=VARCHAR}, #{remark2,jdbcType=VARCHAR}, + #{org,jdbcType=VARCHAR}, #{remark3,jdbcType=VARCHAR}, #{remark4,jdbcType=VARCHAR}, + #{status,jdbcType=INTEGER}, #{remark5,jdbcType=VARCHAR}) + + + insert into lora_device_info + + + dev_EUI, + + + join_EUI, + + + name, + + + app_key, + + + support_OTAA, + + + support_ClassC, + + + gatewayId, + + + remark1, + + + remark2, + + + org, + + + remark3, + + + remark4, + + + status, + + + remark5, + + + + + #{devEui,jdbcType=VARCHAR}, + + + #{joinEui,jdbcType=VARCHAR}, + + + #{name,jdbcType=VARCHAR}, + + + #{appKey,jdbcType=VARCHAR}, + + + #{supportOtaa,jdbcType=BIT}, + + + #{supportClassc,jdbcType=BIT}, + + + #{gatewayid,jdbcType=VARCHAR}, + + + #{remark1,jdbcType=VARCHAR}, + + + #{remark2,jdbcType=VARCHAR}, + + + #{org,jdbcType=VARCHAR}, + + + #{remark3,jdbcType=VARCHAR}, + + + #{remark4,jdbcType=VARCHAR}, + + + #{status,jdbcType=INTEGER}, + + + #{remark5,jdbcType=VARCHAR}, + + + + + update lora_device_info + + + join_EUI = #{joinEui,jdbcType=VARCHAR}, + + + name = #{name,jdbcType=VARCHAR}, + + + app_key = #{appKey,jdbcType=VARCHAR}, + + + support_OTAA = #{supportOtaa,jdbcType=BIT}, + + + support_ClassC = #{supportClassc,jdbcType=BIT}, + + + gatewayId = #{gatewayid,jdbcType=VARCHAR}, + + + remark1 = #{remark1,jdbcType=VARCHAR}, + + + remark2 = #{remark2,jdbcType=VARCHAR}, + + + org = #{org,jdbcType=VARCHAR}, + + + remark3 = #{remark3,jdbcType=VARCHAR}, + + + remark4 = #{remark4,jdbcType=VARCHAR}, + + + status = #{status,jdbcType=INTEGER}, + + + remark5 = #{remark5,jdbcType=VARCHAR}, + + + where dev_EUI = #{devEui,jdbcType=VARCHAR} + + + update lora_device_info + set join_EUI = #{joinEui,jdbcType=VARCHAR}, + name = #{name,jdbcType=VARCHAR}, + app_key = #{appKey,jdbcType=VARCHAR}, + support_OTAA = #{supportOtaa,jdbcType=BIT}, + support_ClassC = #{supportClassc,jdbcType=BIT}, + gatewayId = #{gatewayid,jdbcType=VARCHAR}, + remark1 = #{remark1,jdbcType=VARCHAR}, + remark2 = #{remark2,jdbcType=VARCHAR}, + org = #{org,jdbcType=VARCHAR}, + remark3 = #{remark3,jdbcType=VARCHAR}, + remark4 = #{remark4,jdbcType=VARCHAR}, + status = #{status,jdbcType=INTEGER}, + remark5 = #{remark5,jdbcType=VARCHAR} + where dev_EUI = #{devEui,jdbcType=VARCHAR} + + \ No newline at end of file diff --git a/xiuosiot-backend/src/main/resources/mappers/LoraGatewayInfoMapper.xml b/xiuosiot-backend/src/main/resources/mappers/LoraGatewayInfoMapper.xml new file mode 100644 index 0000000..223fde1 --- /dev/null +++ b/xiuosiot-backend/src/main/resources/mappers/LoraGatewayInfoMapper.xml @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + gatewayId, name, device_num, last_connect, status, org, remark1, remark2, remark3, + remark4, remark5 + + + + delete from lora_gateway_info + where gatewayId = #{gatewayid,jdbcType=VARCHAR} + + + insert into lora_gateway_info (gatewayId, name, device_num, + last_connect, status, org, + remark1, remark2, remark3, + remark4, remark5) + values (#{gatewayid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{deviceNum,jdbcType=INTEGER}, + #{lastConnect,jdbcType=TIMESTAMP}, #{status,jdbcType=INTEGER}, #{org,jdbcType=VARCHAR}, + #{remark1,jdbcType=VARCHAR}, #{remark2,jdbcType=VARCHAR}, #{remark3,jdbcType=VARCHAR}, + #{remark4,jdbcType=VARCHAR}, #{remark5,jdbcType=VARCHAR}) + + + insert into lora_gateway_info + + + gatewayId, + + + name, + + + device_num, + + + last_connect, + + + status, + + + org, + + + remark1, + + + remark2, + + + remark3, + + + remark4, + + + remark5, + + + + + #{gatewayid,jdbcType=VARCHAR}, + + + #{name,jdbcType=VARCHAR}, + + + #{deviceNum,jdbcType=INTEGER}, + + + #{lastConnect,jdbcType=TIMESTAMP}, + + + #{status,jdbcType=INTEGER}, + + + #{org,jdbcType=VARCHAR}, + + + #{remark1,jdbcType=VARCHAR}, + + + #{remark2,jdbcType=VARCHAR}, + + + #{remark3,jdbcType=VARCHAR}, + + + #{remark4,jdbcType=VARCHAR}, + + + #{remark5,jdbcType=VARCHAR}, + + + + + update lora_gateway_info + + + name = #{name,jdbcType=VARCHAR}, + + + device_num = #{deviceNum,jdbcType=INTEGER}, + + + last_connect = #{lastConnect,jdbcType=TIMESTAMP}, + + + status = #{status,jdbcType=INTEGER}, + + + org = #{org,jdbcType=VARCHAR}, + + + remark1 = #{remark1,jdbcType=VARCHAR}, + + + remark2 = #{remark2,jdbcType=VARCHAR}, + + + remark3 = #{remark3,jdbcType=VARCHAR}, + + + remark4 = #{remark4,jdbcType=VARCHAR}, + + + remark5 = #{remark5,jdbcType=VARCHAR}, + + + where gatewayId = #{gatewayid,jdbcType=VARCHAR} + + + update lora_gateway_info + set name = #{name,jdbcType=VARCHAR}, + device_num = #{deviceNum,jdbcType=INTEGER}, + last_connect = #{lastConnect,jdbcType=TIMESTAMP}, + status = #{status,jdbcType=INTEGER}, + org = #{org,jdbcType=VARCHAR}, + remark1 = #{remark1,jdbcType=VARCHAR}, + remark2 = #{remark2,jdbcType=VARCHAR}, + remark3 = #{remark3,jdbcType=VARCHAR}, + remark4 = #{remark4,jdbcType=VARCHAR}, + remark5 = #{remark5,jdbcType=VARCHAR} + where gatewayId = #{gatewayid,jdbcType=VARCHAR} + + \ No newline at end of file diff --git a/xiuosiot-backend/src/main/resources/mappers/OtaInfoMapper.xml b/xiuosiot-backend/src/main/resources/mappers/OtaInfoMapper.xml new file mode 100644 index 0000000..a934657 --- /dev/null +++ b/xiuosiot-backend/src/main/resources/mappers/OtaInfoMapper.xml @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + id, file_name, device_id, current_process, status, create_time, update_type, timeout, + file_version, org + + + + delete from ota_info + where id = #{id,jdbcType=INTEGER} + + + insert into ota_info (id, file_name, device_id, + current_process, status, create_time, + update_type, timeout, file_version, + org) + values (#{id,jdbcType=INTEGER}, #{fileName,jdbcType=VARCHAR}, #{deviceId,jdbcType=VARCHAR}, + #{currentProcess,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, + #{updateType,jdbcType=INTEGER}, #{timeout,jdbcType=TIMESTAMP}, #{fileVersion,jdbcType=VARCHAR}, + #{org,jdbcType=VARCHAR}) + + + insert into ota_info + + + id, + + + file_name, + + + device_id, + + + current_process, + + + status, + + + create_time, + + + update_type, + + + timeout, + + + file_version, + + + org, + + + + + #{id,jdbcType=INTEGER}, + + + #{fileName,jdbcType=VARCHAR}, + + + #{deviceId,jdbcType=VARCHAR}, + + + #{currentProcess,jdbcType=INTEGER}, + + + #{status,jdbcType=INTEGER}, + + + #{createTime,jdbcType=TIMESTAMP}, + + + #{updateType,jdbcType=INTEGER}, + + + #{timeout,jdbcType=TIMESTAMP}, + + + #{fileVersion,jdbcType=VARCHAR}, + + + #{org,jdbcType=VARCHAR}, + + + + + update ota_info + + + file_name = #{fileName,jdbcType=VARCHAR}, + + + device_id = #{deviceId,jdbcType=VARCHAR}, + + + current_process = #{currentProcess,jdbcType=INTEGER}, + + + status = #{status,jdbcType=INTEGER}, + + + create_time = #{createTime,jdbcType=TIMESTAMP}, + + + update_type = #{updateType,jdbcType=INTEGER}, + + + timeout = #{timeout,jdbcType=TIMESTAMP}, + + + file_version = #{fileVersion,jdbcType=VARCHAR}, + + + org = #{org,jdbcType=VARCHAR}, + + + where id = #{id,jdbcType=INTEGER} + + + update ota_info + set file_name = #{fileName,jdbcType=VARCHAR}, + device_id = #{deviceId,jdbcType=VARCHAR}, + current_process = #{currentProcess,jdbcType=INTEGER}, + status = #{status,jdbcType=INTEGER}, + create_time = #{createTime,jdbcType=TIMESTAMP}, + update_type = #{updateType,jdbcType=INTEGER}, + timeout = #{timeout,jdbcType=TIMESTAMP}, + file_version = #{fileVersion,jdbcType=VARCHAR}, + org = #{org,jdbcType=VARCHAR} + where id = #{id,jdbcType=INTEGER} + + \ No newline at end of file