Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import sun.org.mozilla.javascript.internal.json.JsonParser;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Random;
- public class A {
- private Connection connection;
- public void init() {
- String address = "localhost";
- String dbName = "vpp";
- String username = "root";
- String password = "";
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String path = String.format("jdbc:mysql://%s/%s?user=%s&password=%s", address, dbName, username, password);
- this.connection = DriverManager.getConnection(path);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- File file = new File(System.getProperty("user.dir"), "data.json");
- JSONParser parser = new JSONParser();
- JSONObject obj = null;
- try {
- obj = (JSONObject) parser.parse(new FileReader(file));
- } catch (IOException | ParseException e) {
- e.printStackTrace();
- }
- for (Object object : (JSONArray) obj.get("list")) {
- JSONObject oo = (JSONObject) object;
- JSONObject coll = new JSONObject();
- JSONObject weather_station = new JSONObject();
- double humidty = (double) ((JSONObject) oo.get("main")).get("humidity");
- humidty = new Random().nextBoolean() ? (humidty + (humidty / 100)) : (humidty - (humidty / 100));
- double temperature = (double)((JSONObject) oo.get("main")).get("temp");
- temperature = new Random().nextBoolean() ? (temperature + (temperature / 100)) : (temperature - (temperature / 100));
- double pressure = (double)((JSONObject) oo.get("main")).get("grnd_level");
- pressure = new Random().nextBoolean() ? (pressure + (pressure / 100)) : (pressure - (pressure / 100));
- double windSpeed = (double) ((JSONObject) oo.get("main")).get("grnd_level");
- windSpeed = new Random().nextBoolean() ? (windSpeed + (windSpeed / 100)) : (windSpeed - (windSpeed / 100));
- weather_station.put("humidity", humidty);
- weather_station.put("temperature", humidty);
- weather_station.put("pressure", pressure);
- weather_station.put("wind_speed", windSpeed);
- weather_station.put("ldrA", 405.5);
- weather_station.put("ldrB", 405.5);
- weather_station.put("ldrC", 405.5);
- weather_station.put("ldrD", 405.5);
- coll.put("openweathermap", oo);
- coll.put("weather_station", weather_station);
- try {
- PreparedStatement statement = this.connection.prepareStatement("INSERT INTO `vpp_sensor_data` (node_id, data, received) VALUES (?, ?, ?)");
- statement.setObject(1, "0013A2004071CF15");
- statement.setObject(2, coll.toJSONString());
- statement.setObject(3, oo.get("dt_txt"));
- statement.executeUpdate();
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement