From aec8fc7bcfdf780c649a3938498d95e9a4d238ac Mon Sep 17 00:00:00 2001 From: wty <419034340@qq.com> Date: Mon, 14 Nov 2022 14:51:23 +0800 Subject: [PATCH] get device data from jiangxishida --- .../xiuos/controller/ExcludeController.java | 53 +++++- .../aiit/xiuos/service/DeviceInfoService.java | 1 + .../service/impl/DeviceInfoServiceImpl.java | 10 + .../aiit/xiuos/socket/WebSocketServer.java | 45 ++--- .../aiit/xiuos/tdengine/TDengineJDBCUtil.java | 174 ++++++++++++++++++ .../aiit/xiuos/tdengine/tdengineConfig.java | 14 ++ .../mappers/DataForwardingMapper.xml | 130 +++++++++++++ 7 files changed, 400 insertions(+), 27 deletions(-) create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/TDengineJDBCUtil.java create mode 100644 xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/tdengineConfig.java create mode 100644 xiuosiot-backend/src/main/resources/mappers/DataForwardingMapper.xml diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java index fadc9ee..3149316 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java @@ -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 selectbyNo(String no); List> getDeviceTypeCount(String org); List> getDeviceRunStautsCount(String org); + String getTypeByName(String deviceNo); } 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 b1c8a61..0777c06 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 @@ -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> getDeviceRunStautsCount(String org) { return deviceInfoMapper.getDeviceRunStatusCount(org); } + + @Override + public String getTypeByName(String deviceNo) { + List types = deviceInfoMapper.selectTypeByNo(deviceNo); + if(types!=null && types.size()>0){ + return types.get(0); + } + return null; + } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/socket/WebSocketServer.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/socket/WebSocketServer.java index 8869a88..ec80785 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/socket/WebSocketServer.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/socket/WebSocketServer.java @@ -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()); } } diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/TDengineJDBCUtil.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/TDengineJDBCUtil.java new file mode 100644 index 0000000..d84dabd --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/TDengineJDBCUtil.java @@ -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 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()); + 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(); + } + + } + } + + + +} diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/tdengineConfig.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/tdengineConfig.java new file mode 100644 index 0000000..e3b6dcc --- /dev/null +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/tdengine/tdengineConfig.java @@ -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; + } + +} diff --git a/xiuosiot-backend/src/main/resources/mappers/DataForwardingMapper.xml b/xiuosiot-backend/src/main/resources/mappers/DataForwardingMapper.xml new file mode 100644 index 0000000..8eb1b6a --- /dev/null +++ b/xiuosiot-backend/src/main/resources/mappers/DataForwardingMapper.xml @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + id, topic, content, type, time, address, org, sql + + + + delete from data_forwarding + where id = #{id,jdbcType=INTEGER} + + + 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 into data_forwarding + + + id, + + + topic, + + + content, + + + type, + + + time, + + + address, + + + org, + + + sql, + + + + + #{id,jdbcType=INTEGER}, + + + #{topic,jdbcType=VARCHAR}, + + + #{content,jdbcType=VARCHAR}, + + + #{type,jdbcType=VARCHAR}, + + + #{time,jdbcType=TIMESTAMP}, + + + #{address,jdbcType=VARCHAR}, + + + #{org,jdbcType=VARCHAR}, + + + #{sql,jdbcType=VARCHAR}, + + + + + update data_forwarding + + + 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 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} + + \ No newline at end of file