add select/update/delete/add function for alarm rule module

This commit is contained in:
wty 2022-11-02 18:00:52 +08:00
parent a22dad62c7
commit dfcb5a15b8
10 changed files with 441 additions and 3 deletions

View File

@ -0,0 +1,4 @@
package com.aiit.xiuos.Utils;
public class EmailUtil {
}

View File

@ -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;
}
}

View File

@ -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<String> executeSql(String sql) throws Exception {
Connection connection = null;
ArrayList<String> 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;
}
}

View File

@ -0,0 +1,4 @@
package com.aiit.xiuos.controller;
public class AlarmRuleController {
}

View File

@ -0,0 +1,4 @@
package com.aiit.xiuos.controller;
public class EmailController {
}

View File

@ -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<AlarmRule> selectAll(@Param("org") String org);
@Select("select * from alarm_rule where rule_status = 1")
List<AlarmRule> selectActive();
}

View File

@ -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();
}
}

View File

@ -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<AlarmRule> selectAlarmRule(String org);
List<AlarmRule> selectActive();
int updateAlarmRule(AlarmRule alarmRule);
int deleteAlarmRule(int id);
}

View File

@ -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<AlarmRule> selectAlarmRule(String org) {
return alarmRuleMapper.selectAll(org);
}
@Override
public List<AlarmRule> selectActive() {
return alarmRuleMapper.selectActive();
}
@Override
public int updateAlarmRule(AlarmRule alarmRule) {
return alarmRuleMapper.updateByPrimaryKeySelective(alarmRule);
}
@Override
public int deleteAlarmRule(int id) {
return alarmRuleMapper.deleteByPrimaryKey(id);
}
}

View File

@ -0,0 +1,144 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aiit.xiuos.dao.mappers.AlarmRuleMapper">
<resultMap id="BaseResultMap" type="com.aiit.xiuos.model.AlarmRule">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="alarm_name" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="alarm_level" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="device_no" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="alarm_sql" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="rule_status" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="notice_type" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="notice_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="org" javaType="java.lang.String" jdbcType="VARCHAR" />
</constructor>
</resultMap>
<sql id="Base_Column_List">
id, alarm_name, alarm_level, device_no, alarm_sql, rule_status, notice_type, notice_content,
org
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from alarm_rule
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from alarm_rule
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.aiit.xiuos.model.AlarmRule">
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>
<insert id="insertSelective" parameterType="com.aiit.xiuos.model.AlarmRule">
insert into alarm_rule
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="alarmName != null">
alarm_name,
</if>
<if test="alarmLevel != null">
alarm_level,
</if>
<if test="deviceNo != null">
device_no,
</if>
<if test="alarmSql != null">
alarm_sql,
</if>
<if test="ruleStatus != null">
rule_status,
</if>
<if test="noticeType != null">
notice_type,
</if>
<if test="noticeContent != null">
notice_content,
</if>
<if test="org != null">
org,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="alarmName != null">
#{alarmName,jdbcType=VARCHAR},
</if>
<if test="alarmLevel != null">
#{alarmLevel,jdbcType=INTEGER},
</if>
<if test="deviceNo != null">
#{deviceNo,jdbcType=VARCHAR},
</if>
<if test="alarmSql != null">
#{alarmSql,jdbcType=VARCHAR},
</if>
<if test="ruleStatus != null">
#{ruleStatus,jdbcType=INTEGER},
</if>
<if test="noticeType != null">
#{noticeType,jdbcType=VARCHAR},
</if>
<if test="noticeContent != null">
#{noticeContent,jdbcType=VARCHAR},
</if>
<if test="org != null">
#{org,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.aiit.xiuos.model.AlarmRule">
update alarm_rule
<set>
<if test="alarmName != null">
alarm_name = #{alarmName,jdbcType=VARCHAR},
</if>
<if test="alarmLevel != null">
alarm_level = #{alarmLevel,jdbcType=INTEGER},
</if>
<if test="deviceNo != null">
device_no = #{deviceNo,jdbcType=VARCHAR},
</if>
<if test="alarmSql != null">
alarm_sql = #{alarmSql,jdbcType=VARCHAR},
</if>
<if test="ruleStatus != null">
rule_status = #{ruleStatus,jdbcType=INTEGER},
</if>
<if test="noticeType != null">
notice_type = #{noticeType,jdbcType=VARCHAR},
</if>
<if test="noticeContent != null">
notice_content = #{noticeContent,jdbcType=VARCHAR},
</if>
<if test="org != null">
org = #{org,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.aiit.xiuos.model.AlarmRule">
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}
</update>
</mapper>