get device data from jiangxishida

This commit is contained in:
wty 2022-11-14 14:51:23 +08:00
parent f3ebfe025f
commit aec8fc7bcf
7 changed files with 400 additions and 27 deletions

View File

@ -1,4 +1,55 @@
package com.aiit.xiuos.controller;
import com.aiit.xiuos.Utils.MyUtils;
import com.aiit.xiuos.scheduled.TaskScheduled;
import com.aiit.xiuos.service.impl.ProtocolServiceImpl;
import com.aiit.xiuos.socket.WebSocketServer;
import com.aiit.xiuos.tdengine.TDengineJDBCUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
public class EecludeController {
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
@RestController
@RequestMapping("/exclude")
@Slf4j
public class ExcludeController {
@Autowired
ProtocolServiceImpl protocolService;
@Autowired
TaskScheduled taskScheduled;
@Autowired
WebSocketServer webSocketServer;
@PostMapping("/addjxsd")
public void addJXSD(@RequestBody String jsonString, HttpServletRequest request) throws ParseException {
System.out.println("收到的jsonString"+jsonString);
JSONObject jsonObject = MyUtils.StringToJson(jsonString);
int valueNum = jsonObject.getInteger("valueNum");
String client =jsonObject.getString("org");
webSocketServer.sendToMessageById(client,jsonString);
String deviceId= jsonObject.getString("deviceId");
if(valueNum==72){
JSONArray jsonArray =jsonObject.getJSONArray("readItemList");
StringBuilder sql =new StringBuilder("insert into jxsd_1500 values(now");
for(int i=0;i<jsonArray.size();i++){
sql.append(",");
sql.append(jsonArray.getJSONObject(i).getInteger("value"));
}
sql.append(")");
TDengineJDBCUtil.executeInsertSql(sql.toString());
}
}
@GetMapping("/executeAlarmTask")
public void executeTask(){
try {
taskScheduled.ExecuteRule();
} catch (Exception e) {
log.error(e.getMessage());
}
}
}

View File

@ -14,5 +14,6 @@ public interface DeviceInfoService {
List<DeviceInfo> selectbyNo(String no);
List<Map<String,String>> getDeviceTypeCount(String org);
List<Map<String,String>> getDeviceRunStautsCount(String org);
String getTypeByName(String deviceNo);
}

View File

@ -14,6 +14,7 @@ import java.util.Map;
public class DeviceInfoServiceImpl implements DeviceInfoService {
@Autowired
private DeviceInfoMapper deviceInfoMapper;
@Override
public int addDevice(DeviceInfo deviceInfo) {
return deviceInfoMapper.insert(deviceInfo);
@ -54,4 +55,13 @@ public class DeviceInfoServiceImpl implements DeviceInfoService {
public List<Map<String, String>> getDeviceRunStautsCount(String org) {
return deviceInfoMapper.getDeviceRunStatusCount(org);
}
@Override
public String getTypeByName(String deviceNo) {
List<String> types = deviceInfoMapper.selectTypeByNo(deviceNo);
if(types!=null && types.size()>0){
return types.get(0);
}
return null;
}
}

View File

@ -1,8 +1,6 @@
package com.aiit.xiuos.socket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
@ -11,7 +9,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ServerEndpoint(value = "/{paramName}")
@ServerEndpoint(value = "/websocket/{clientId}")
@Component
@Slf4j
public class WebSocketServer {
@ -42,29 +40,22 @@ public class WebSocketServer {
/**
* 连接建立成功调用的方法*/
@OnOpen
public void onOpen(@PathParam(value = "paramName") String param, Session session) {
if(numofAccount.containsKey(param)){
Integer num=numofAccount.get(param);
String paramtmp=param;
param=param+num;
System.out.println(param);
numofAccount.put(paramtmp,num+1);
}else{
numofAccount.put(param,1);
}
public void onOpen(@PathParam(value = "clientId") String clientId, Session session) {
//接收到发送消息的人员编号
name = param;
name = clientId;
this.session = session;
/**加入set中*/
webSocketSet.put(param,this);
webSocketSet.put(clientId,this);
/**在线数加1*/
addOnlineCount();
System.out.println("有新连接"+param+"加入!当前在线人数为" + getOnlineCount());
//try {
//sendMessage("-连接已建立-");
//} catch (IOException e) {
// System.out.println("IO异常");
// }
System.out.println("有新连接"+clientId+"加入!当前在线人数为" + getOnlineCount());
log.info("有新连接"+clientId+"加入!当前在线人数为" + getOnlineCount());
try {
sendMessage(clientId+"-连接已建立-");
} catch (IOException e) {
System.out.println("IO异常");
}
}
/**
@ -78,6 +69,7 @@ public class WebSocketServer {
/** 在线数减1 */
subOnlineCount();
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
log.info("有一连接"+name+"关闭!当前在线人数为" + getOnlineCount());
}
}
@ -88,11 +80,11 @@ public class WebSocketServer {
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);
log.info("来自客户端的消息:" + message);
try {
this.sendMessage(message);
this.sendMessage("收到!");
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
@ -122,9 +114,10 @@ public class WebSocketServer {
webSocketSet.get(id).sendMessage(message);
} else {
System.out.println("webSocketSet中没有此key不推送消息");
log.info("webSocketSet中没有此key不推送消息");
}
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
@ -140,7 +133,7 @@ public class WebSocketServer {
value.sendMessage(message);
}
}catch(IOException e){
e.printStackTrace();
log.error(e.getMessage());
}
}

View File

@ -0,0 +1,174 @@
package com.aiit.xiuos.tdengine;
import com.taosdata.jdbc.TSDBDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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");
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");
log.info(tdengineConfig.jdbcurl);
Connection conn = DriverManager.getConnection(tdengineConfig.jdbcurl, connProps);
return conn;
}
public static Connection getRestConn() throws Exception{
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
String jdbcUrl = "jdbc:TAOS-RS://taosnode1: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) {
log.error(e.getMessage());
}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 "M168_LoRa_FM100";
if(type.equals("RV400-NPU16T-5G-AR100")) return "RV400_NPU16T_5G_AR100";
if(type.equals("RV400-NPU4T-5G-SR100")) return "RV400_NPU4T_5G_SR100";
if(type.equals("M528-A800-5G-HM100")) return "M528_A800_5G_HM100";
if(type.equals("RV400-4G-FR100")) return "RV400_4G_FR100";
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());
log.error(e.getMessage());
}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 =0;
try {
connection = TDengineJDBCUtil.getConn();
Statement stmt = connection.createStatement();
log.info("tdengine executeQuery:"+sql);
ResultSet resultSet =stmt.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());
log.error(e.getMessage());
}finally {
try {
if(connection!=null){
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return count;
}
public static void executeInsertSql(String sql) {
Connection connection = null;
try {
connection = TDengineJDBCUtil.getConn();
Statement stmt = connection.createStatement();
log.info("tdengine executeQuery:"+sql);
stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("ERROR Message: " + e.getMessage());
System.out.println("ERROR Code: " + e.getErrorCode());
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
} finally {
try {
if(connection!=null){
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,14 @@
package com.aiit.xiuos.tdengine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class tdengineConfig {
public static String jdbcurl;
@Value("${tdengine.url}")
public void setJdbcurl(String jdbcurl){
tdengineConfig.jdbcurl =jdbcurl;
}
}

View File

@ -0,0 +1,130 @@
<?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.DataForwardingMapper">
<resultMap id="BaseResultMap" type="com.aiit.xiuos.model.DataForwarding">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="topic" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="content" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="type" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
<arg column="address" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="org" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="sql" javaType="java.lang.String" jdbcType="VARCHAR" />
</constructor>
</resultMap>
<sql id="Base_Column_List">
id, topic, content, type, time, address, org, sql
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from data_forwarding
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from data_forwarding
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.aiit.xiuos.model.DataForwarding">
insert into data_forwarding (id, topic, content,
type, time, address,
org, sql)
values (#{id,jdbcType=INTEGER}, #{topic,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR},
#{type,jdbcType=VARCHAR}, #{time,jdbcType=TIMESTAMP}, #{address,jdbcType=VARCHAR},
#{org,jdbcType=VARCHAR}, #{sql,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.aiit.xiuos.model.DataForwarding">
insert into data_forwarding
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="topic != null">
topic,
</if>
<if test="content != null">
content,
</if>
<if test="type != null">
type,
</if>
<if test="time != null">
time,
</if>
<if test="address != null">
address,
</if>
<if test="org != null">
org,
</if>
<if test="sql != null">
sql,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="topic != null">
#{topic,jdbcType=VARCHAR},
</if>
<if test="content != null">
#{content,jdbcType=VARCHAR},
</if>
<if test="type != null">
#{type,jdbcType=VARCHAR},
</if>
<if test="time != null">
#{time,jdbcType=TIMESTAMP},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
<if test="org != null">
#{org,jdbcType=VARCHAR},
</if>
<if test="sql != null">
#{sql,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.aiit.xiuos.model.DataForwarding">
update data_forwarding
<set>
<if test="topic != null">
topic = #{topic,jdbcType=VARCHAR},
</if>
<if test="content != null">
content = #{content,jdbcType=VARCHAR},
</if>
<if test="type != null">
type = #{type,jdbcType=VARCHAR},
</if>
<if test="time != null">
time = #{time,jdbcType=TIMESTAMP},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
<if test="org != null">
org = #{org,jdbcType=VARCHAR},
</if>
<if test="sql != null">
sql = #{sql,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.aiit.xiuos.model.DataForwarding">
update data_forwarding
set topic = #{topic,jdbcType=VARCHAR},
content = #{content,jdbcType=VARCHAR},
type = #{type,jdbcType=VARCHAR},
time = #{time,jdbcType=TIMESTAMP},
address = #{address,jdbcType=VARCHAR},
org = #{org,jdbcType=VARCHAR},
sql = #{sql,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>