-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolarPositionCalculator.java
197 lines (168 loc) · 7.61 KB
/
SolarPositionCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.e175.klaus.solarpositioning.*;
import java.time.ZonedDateTime;
public class SolarPositionCalculator extends JFrame {
private JLabel latitudeLabel, longitudeLabel, elevationLabel, pressureLabel, temperatureLabel;
private JTextField latitudeField, longitudeField, elevationField, pressureField, temperatureField;
private JButton calculateButton;
private JTextArea resultArea;
private JComboBox<String> cityComboBox;
public static String ip;
// Default values for elevation, air pressure, and air temperature
private static final double DEFAULT_ELEVATION = 100.0; // meters
private static final double DEFAULT_PRESSURE = 1013.25; // hPa
private static final double DEFAULT_TEMPERATURE = 20.0; // °C
public SolarPositionCalculator(String ipAddress) {
this.ip = ipAddress;
setTitle("Solar Position Calculator");
setSize(500, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
// Initialize components
latitudeLabel = new JLabel("Latitude:");
longitudeLabel = new JLabel("Longitude:");
elevationLabel = new JLabel("Elevation (m):");
pressureLabel = new JLabel("Average Air Pressure (hPa):");
temperatureLabel = new JLabel("Average Air Temperature (°C):");
latitudeField = new JTextField(10);
longitudeField = new JTextField(10);
elevationField = new JTextField(String.valueOf(DEFAULT_ELEVATION), 10); // Set default elevation
pressureField = new JTextField(String.valueOf(DEFAULT_PRESSURE), 10); // Set default pressure
temperatureField = new JTextField(String.valueOf(DEFAULT_TEMPERATURE), 10); // Set default temperature
calculateButton = new JButton("Calculate");
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
// Initialize cityComboBox and populate it with city names
cityComboBox = new JComboBox<>();
populateCities();
// Layout
setLayout(new GridLayout(8, 2));
add(new JLabel("City:"));
add(cityComboBox);
add(latitudeLabel);
add(latitudeField);
add(longitudeLabel);
add(longitudeField);
add(elevationLabel);
add(elevationField);
add(pressureLabel);
add(pressureField);
add(temperatureLabel);
add(temperatureField);
add(calculateButton);
add(new JScrollPane(resultArea));
// Action listener for the Calculate button
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateAndDisplaySolarPosition(ipAddress);
}
});
// Add action listener to cityComboBox to fill latitude and longitude fields
cityComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fillCoordinates();
}
});
}
private void fillCoordinates() {
try {
// Connect to the SQLite database
Connection conn = DriverManager.getConnection("jdbc:sqlite:cities.db");
Statement stmt = conn.createStatement();
// Get selected city name
String selectedCity = (String) cityComboBox.getSelectedItem();
// Execute query to get coordinates of the selected city
ResultSet rs = stmt.executeQuery("SELECT latitude, longitude FROM cities WHERE name = '" + selectedCity + "'");
// Display coordinates in the text fields
if (rs.next()) {
double latitude = rs.getDouble("latitude");
double longitude = rs.getDouble("longitude");
latitudeField.setText(String.valueOf(latitude));
longitudeField.setText(String.valueOf(longitude));
}
// Close connections
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void calculateAndDisplaySolarPosition(String ipAddress) {
try {
double latitude = Double.parseDouble(latitudeField.getText());
double longitude = Double.parseDouble(longitudeField.getText());
double elevation = Double.parseDouble(elevationField.getText());
double pressure = Double.parseDouble(pressureField.getText());
double temperature = Double.parseDouble(temperatureField.getText());
// Create Spos object with input data
Spos spos = new Spos(latitude, longitude, pressure, elevation, temperature, "");
// Connect to the Solar Position Calculator Server
try (Socket socket = new Socket(ipAddress, 12347)) {
System.out.println("Connected to server.");
// Send Spos object to the server
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(spos);
outputStream.flush();
System.out.println("Sent Spos object to server.");
// Receive updated Spos object from the server
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
Spos updatedSpos = (Spos) inputStream.readObject();
System.out.println("Received updated Spos object from server.");
// Display solar position in the result area
resultArea.setText(updatedSpos.getSunpos());
// Close resources
inputStream.close();
outputStream.close();
} catch (ConnectException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Cannot connect to server. Please ensure the server is up and running.", "Connection Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error communicating with server.", "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid input. Please enter numbers only.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void populateCities() {
try {
// Connect to the SQLite database
Connection conn = DriverManager.getConnection("jdbc:sqlite:cities.db");
Statement stmt = conn.createStatement();
// Execute query to get cities
ResultSet rs = stmt.executeQuery("SELECT name FROM cities");
// Populate the combo box with city names
while (rs.next()) {
String cityName = rs.getString("name");
cityComboBox.addItem(cityName);
}
// Close connections
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading cities from database!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SolarPositionCalculator(ip).setVisible(true);
}
});
}
}