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