From dfcb5a15b8aad75ac0ec79a0cc185b5b3581fcbb Mon Sep 17 00:00:00 2001 From: wty <419034340@qq.com> Date: Wed, 2 Nov 2022 18:00:52 +0800 Subject: [PATCH] add select/update/delete/add function for alarm rule module --- .../java/com/aiit/xiuos/Utils/EmailUtil.java | 4 + .../java/com/aiit/xiuos/Utils/MyUtils.java | 14 +- .../aiit/xiuos/Utils/TDengineJDBCUtil.java | 150 ++++++++++++++++++ .../xiuos/controller/AlarmRuleController.java | 4 + .../xiuos/controller/NoticeController.java | 4 + .../xiuos/dao/mappers/AlarmRuleMapper.java | 29 ++++ .../java/com/aiit/xiuos/model/AlarmRule.java | 42 +++++ .../aiit/xiuos/service/AlarmRuleService.java | 15 ++ .../service/impl/AlarmRuleServiceImpl.java | 38 +++++ .../resources/mappers/AlarmRuleMapper.xml | 144 +++++++++++++++++ 10 files changed, 441 insertions(+), 3 deletions(-) create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmailUtil.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/TDengineJDBCUtil.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/AlarmRuleController.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/NoticeController.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/AlarmRuleMapper.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/model/AlarmRule.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/service/AlarmRuleService.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/AlarmRuleServiceImpl.java create mode 100644 xiuosiot-backend/src/main/resources/mappers/AlarmRuleMapper.xml diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmailUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmailUtil.java new file mode 100644 index 0000000..c24f6bb --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/EmailUtil.java @@ -0,0 +1,4 @@ +package com.aiit.xiuos.Utils; + +public class EmailUtil { +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/MyUtils.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/MyUtils.java index 55bc01d..8d1d05b 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/MyUtils.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/MyUtils.java @@ -1,5 +1,6 @@ package com.aiit.xiuos.Utils; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import javax.servlet.http.HttpServletRequest; @@ -30,9 +31,11 @@ public class MyUtils { // System.out.println(data); // } // } - JSONObject result = MyUtils.StringToJson(""); - String jsonString = MyUtils.jsonToString(result); - System.out.println(jsonString); + String json="[{\"conditaion\":\">\",\"param\":\"temperature\",\"value\":30},{\"condition\":\"<\",\"param\":\"co2\",\"value\":19}]"; + JSONArray jsonArray =JSONArray.parseArray(json); + String res = jsonArray.toJSONString(); + + System.out.println(res); } public static String getIp(HttpServletRequest request){ String ip = request.getHeader("x-forwarded-for"); @@ -77,4 +80,9 @@ public class MyUtils { return jsonObject.toJSONString(); } + + public static JSONArray StringToJsonArray(String jsonString){ + JSONArray jsonArray =JSONArray.parseArray(jsonString); + return jsonArray; + } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/TDengineJDBCUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/TDengineJDBCUtil.java new file mode 100644 index 0000000..83adecf --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/Utils/TDengineJDBCUtil.java @@ -0,0 +1,150 @@ +package com.aiit.xiuos.Utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.taosdata.jdbc.TSDBDriver; +import lombok.extern.slf4j.Slf4j; + +import java.sql.*; +import java.util.ArrayList; +import java.util.Properties; +@Slf4j +public class TDengineJDBCUtil { + public static Connection getConn() throws Exception{ + Class.forName("com.taosdata.jdbc.TSDBDriver"); + String jdbcUrl = "jdbc:TAOS://xiuosiot.taosnode1:6030/xiuosiot?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + connProps.setProperty("debugFlag", "135"); + connProps.setProperty("maxSQLLength", "1048576"); + Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + return conn; + } + + public static Connection getRestConn() throws Exception{ + Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); + String jdbcUrl = "jdbc:TAOS-RS://10.0.30.23:6041/test?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "true"); + Connection conn = DriverManager.getConnection(jdbcUrl, connProps); + return conn; + } + + + public static boolean createTable(String deviceno, String type, String org, String productname){ + Connection connection = null; + try { + connection = TDengineJDBCUtil.getConn(); + Statement stmt = connection.createStatement(); + String devicetype=TDengineJDBCUtil.changeType(type); + if(devicetype==null){ + return false; + } + // create table + String sql ="create table if not exists " + deviceno + " using "+devicetype+" tags("+"\""+org+"\","+"\"" + productname+"\")"; + stmt.executeUpdate(sql); + return true; + } catch (Exception e) { + e.printStackTrace(); + }finally { + try { + if(connection!=null){ + connection.close(); + } + + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + + } + return false; + + } + + public static String changeType(String type){ + if(type.equals("M168-LoRa-FM100")) return "M168type"; + if(type.equals("RV400-NPU16T-5G-AR100")) return "RV400ARtype"; + if(type.equals("RV400-NPU4T-5G-SR100")) return "RV400SRtype"; + if(type.equals("M528-A800-5G-HM100")) return "M528type"; + if(type.equals("RV400-4G-FR100")) return "RV400FRtype"; + return null; + } + + public static ArrayList executeSql(String sql) throws Exception { + Connection connection = null; + ArrayList arrayList =new ArrayList<>(); + try { + connection = TDengineJDBCUtil.getConn(); + Statement stmt = connection.createStatement(); + ResultSet resultSet =stmt.executeQuery(sql); + log.info("tdengine executeQuery:"+sql); + + ResultSetMetaData metaData = resultSet.getMetaData(); + while (resultSet.next()) { + StringBuilder sb=new StringBuilder(); + for (int i = 1; i <= metaData.getColumnCount(); i++) { + String columnLabel = metaData.getColumnLabel(i); + String value = resultSet.getString(i); + sb.append(columnLabel+":"+value+" "); + } + arrayList.add(sb.toString()); + System.out.println(sb.toString()); + } + return arrayList; + } catch (SQLException e) { + System.out.println("ERROR Message: " + e.getMessage()); + System.out.println("ERROR Code: " + e.getErrorCode()); + e.printStackTrace(); + }finally { + try { + if(connection!=null){ + connection.close(); + } + + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + + } + return null; + + } + + public static int getCount(String sql) throws Exception { + Connection connection = null; + int count =-1; + try { + connection = TDengineJDBCUtil.getConn(); + Statement stmt = connection.createStatement(); + ResultSet resultSet =stmt.executeQuery(sql); + log.info("tdengine executeQuery:"+sql); + ResultSetMetaData metaData = resultSet.getMetaData(); + if (resultSet.next()) { + count = resultSet.getInt(1); + } + return count; + } catch (SQLException e) { + System.out.println("ERROR Message: " + e.getMessage()); + System.out.println("ERROR Code: " + e.getErrorCode()); + e.printStackTrace(); + }finally { + try { + if(connection!=null){ + connection.close(); + } + + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + + } + return count; + + } + + + +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/AlarmRuleController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/AlarmRuleController.java new file mode 100644 index 0000000..0a12287 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/AlarmRuleController.java @@ -0,0 +1,4 @@ +package com.aiit.xiuos.controller; + +public class AlarmRuleController { +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/NoticeController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/NoticeController.java new file mode 100644 index 0000000..3118240 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/NoticeController.java @@ -0,0 +1,4 @@ +package com.aiit.xiuos.controller; + +public class EmailController { +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/AlarmRuleMapper.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/AlarmRuleMapper.java new file mode 100644 index 0000000..4961ebd --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/dao/mappers/AlarmRuleMapper.java @@ -0,0 +1,29 @@ +package com.aiit.xiuos.dao.mappers; + +import com.aiit.xiuos.model.AlarmRule; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface AlarmRuleMapper { + int deleteByPrimaryKey(Integer id); + + int insert(AlarmRule record); + + int insertSelective(AlarmRule record); + + AlarmRule selectByPrimaryKey(Integer id); + + int updateByPrimaryKeySelective(AlarmRule record); + + int updateByPrimaryKey(AlarmRule record); + + @Select("select * from alarm_rule where org =#{org}") + List selectAll(@Param("org") String org); + + @Select("select * from alarm_rule where rule_status = 1") + List selectActive(); +} \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/AlarmRule.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/AlarmRule.java new file mode 100644 index 0000000..08cfd24 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/model/AlarmRule.java @@ -0,0 +1,42 @@ +package com.aiit.xiuos.model; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class AlarmRule { + private Integer id; + + private String alarmName; + + private Integer alarmLevel; + + private String deviceNo; + + private String alarmSql; + + private Integer ruleStatus; + + private String noticeType; + + private String noticeContent; + + private String org; + + public AlarmRule(Integer id, String alarmName, Integer alarmLevel, String deviceNo, String alarmSql, Integer ruleStatus, String noticeType, String noticeContent, String org) { + this.id = id; + this.alarmName = alarmName; + this.alarmLevel = alarmLevel; + this.deviceNo = deviceNo; + this.alarmSql = alarmSql; + this.ruleStatus = ruleStatus; + this.noticeType = noticeType; + this.noticeContent = noticeContent; + this.org = org; + } + + public AlarmRule() { + super(); + } +} \ No newline at end of file diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/AlarmRuleService.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/AlarmRuleService.java new file mode 100644 index 0000000..f004431 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/AlarmRuleService.java @@ -0,0 +1,15 @@ +package com.aiit.xiuos.service; + +import com.aiit.xiuos.model.AlarmInfo; +import com.aiit.xiuos.model.AlarmRule; + +import java.util.List; + +public interface AlarmRuleService { + int addAlarmRule(AlarmRule alarmRule); + List selectAlarmRule(String org); + List selectActive(); + int updateAlarmRule(AlarmRule alarmRule); + int deleteAlarmRule(int id); + +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/AlarmRuleServiceImpl.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/AlarmRuleServiceImpl.java new file mode 100644 index 0000000..60231e9 --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/service/impl/AlarmRuleServiceImpl.java @@ -0,0 +1,38 @@ +package com.aiit.xiuos.service.impl; + +import com.aiit.xiuos.dao.mappers.AlarmRuleMapper; +import com.aiit.xiuos.model.AlarmRule; +import com.aiit.xiuos.service.AlarmRuleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import java.util.List; + +@Service +public class AlarmRuleServiceImpl implements AlarmRuleService { + @Autowired + AlarmRuleMapper alarmRuleMapper; + @Override + public int addAlarmRule(AlarmRule alarmRule) { + return alarmRuleMapper.insertSelective(alarmRule); + } + + @Override + public List selectAlarmRule(String org) { + return alarmRuleMapper.selectAll(org); + } + + @Override + public List selectActive() { + return alarmRuleMapper.selectActive(); + } + + @Override + public int updateAlarmRule(AlarmRule alarmRule) { + return alarmRuleMapper.updateByPrimaryKeySelective(alarmRule); + } + + @Override + public int deleteAlarmRule(int id) { + return alarmRuleMapper.deleteByPrimaryKey(id); + } +} diff --git a/xiuosiot-backend/src/main/resources/mappers/AlarmRuleMapper.xml b/xiuosiot-backend/src/main/resources/mappers/AlarmRuleMapper.xml new file mode 100644 index 0000000..cf546eb --- /dev/null +++ b/xiuosiot-backend/src/main/resources/mappers/AlarmRuleMapper.xml @@ -0,0 +1,144 @@ + + + + + + + + + + + + + + + + + + id, alarm_name, alarm_level, device_no, alarm_sql, rule_status, notice_type, notice_content, + org + + + + delete from alarm_rule + where id = #{id,jdbcType=INTEGER} + + + insert into alarm_rule (id, alarm_name, alarm_level, + device_no, alarm_sql, rule_status, + notice_type, notice_content, org + ) + values (#{id,jdbcType=INTEGER}, #{alarmName,jdbcType=VARCHAR}, #{alarmLevel,jdbcType=INTEGER}, + #{deviceNo,jdbcType=VARCHAR}, #{alarmSql,jdbcType=VARCHAR}, #{ruleStatus,jdbcType=INTEGER}, + #{noticeType,jdbcType=VARCHAR}, #{noticeContent,jdbcType=VARCHAR}, #{org,jdbcType=VARCHAR} + ) + + + insert into alarm_rule + + + id, + + + alarm_name, + + + alarm_level, + + + device_no, + + + alarm_sql, + + + rule_status, + + + notice_type, + + + notice_content, + + + org, + + + + + #{id,jdbcType=INTEGER}, + + + #{alarmName,jdbcType=VARCHAR}, + + + #{alarmLevel,jdbcType=INTEGER}, + + + #{deviceNo,jdbcType=VARCHAR}, + + + #{alarmSql,jdbcType=VARCHAR}, + + + #{ruleStatus,jdbcType=INTEGER}, + + + #{noticeType,jdbcType=VARCHAR}, + + + #{noticeContent,jdbcType=VARCHAR}, + + + #{org,jdbcType=VARCHAR}, + + + + + update alarm_rule + + + alarm_name = #{alarmName,jdbcType=VARCHAR}, + + + alarm_level = #{alarmLevel,jdbcType=INTEGER}, + + + device_no = #{deviceNo,jdbcType=VARCHAR}, + + + alarm_sql = #{alarmSql,jdbcType=VARCHAR}, + + + rule_status = #{ruleStatus,jdbcType=INTEGER}, + + + notice_type = #{noticeType,jdbcType=VARCHAR}, + + + notice_content = #{noticeContent,jdbcType=VARCHAR}, + + + org = #{org,jdbcType=VARCHAR}, + + + where id = #{id,jdbcType=INTEGER} + + + update alarm_rule + set alarm_name = #{alarmName,jdbcType=VARCHAR}, + alarm_level = #{alarmLevel,jdbcType=INTEGER}, + device_no = #{deviceNo,jdbcType=VARCHAR}, + alarm_sql = #{alarmSql,jdbcType=VARCHAR}, + rule_status = #{ruleStatus,jdbcType=INTEGER}, + notice_type = #{noticeType,jdbcType=VARCHAR}, + notice_content = #{noticeContent,jdbcType=VARCHAR}, + org = #{org,jdbcType=VARCHAR} + where id = #{id,jdbcType=INTEGER} + + \ No newline at end of file