package com.example.demo01.UDPServer;
import com.example.demo01.dao.Device.DeviceMapper;
import com.example.demo01.model.Device;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.sql.*;
import java.text.SimpleDateFormat;
public class JavaTCP implements Runnable {
@Autowired
@Resource
private DeviceMapper tempMapper_tcp_save;
public static JavaTCP tcpServer;
@PostConstruct
public void init() {
tcpServer = this;
tcpServer.tempMapper_tcp_save = this.tempMapper_tcp_save;
}
static String temp;
static String co2;
static String gas;
static String state;
static String time;
private static final String URI = "jdbc:mysql://localhost:3306/test?"
+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF-8";
private static final String DRIVER = "com.mysql.jdbc.Driver";
public static void addTemp(String temp, String co2, String gas, String state) {
Device device = new Device();
device.setTemp(JavaTCP.temp);
device.setCo(JavaTCP.co2);
device.setGas(JavaTCP.gas);
device.setFire(JavaTCP.state);
tcpServer.tempMapper_tcp_save.addTemp(device);
}
public static String getHtmlInfoFromUrl(String url, String encoding) {
try {
URL url1 = new URL(url);
//打开和url之间的连接
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
//添加请求headers
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("api-key", "****"); //存放api-key
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");//GET和POST必须全大写
connection.connect();
//获取URLConnection对象对应的输入流
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) { //读取字符串
str = new String(str.getBytes(), "UTF-8");
System.out.println(str);
temp = str.substring(99, 101);
co2 = str.substring(170, 171);
gas = str.substring(242, 244);
state = str.substring(302, 303);
}
Thread.sleep(100);
is.close();//关闭流
connection.disconnect(); //断开连接
}catch (Exception e){
e.printStackTrace();
}
return "温度:"+ temp + "二氧化碳"+ co2 +"氨气:" + gas + "设备状态:" + state;
}
@Override
public void run() {
while(true){
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 声明数据库view的URL
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";
// 数据库用户名
String user = "root";
// 数据库密码
String password = "123456";
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
getHtmlInfoFromUrl("http://api.heclouds.com/devices/**你自己的设备id**/datapoints","UTF-8");
System.out.println("温度:" + temp);
System.out.println("二氧化碳:" + co2);
System.out.println("氨气:" + gas);
System.out.println("设备状态:" + state);
Date date =new Date(System.currentTimeMillis());
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
time =sdf.format(date);
System.out.println(time);
String sql = "insert into device_table (temp,co2,gas,state,time) values(?,?,?,?,?)"; // 生成一条sql语句
// 创建一个Statment对象
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, temp);
ps.setString(2, co2);
ps.setString(3, gas);
ps.setString(4, state);
ps.setString(5, time);
// 执行sql语句
ps.executeUpdate();
// 关闭数据库连接对象
conn.close();
System.out.println("SQL插入完毕!");
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}