Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package heizung;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.lang.Float;
import java.util.Calendar;
import java.util.Date;
public class heizung implements ActionListener {
// Network
private String strServerName = "www.theosys.at";
// Head
private JComboBox cmbWDay = new JComboBox();
// Spalte 2 - Von
private JTextField tfVon_1 = new JTextField();
private JTextField tfVon_2 = new JTextField();
private JTextField tfVon_3 = new JTextField();
private JTextField tfVon_4 = new JTextField();
private JTextField tfVon_5 = new JTextField();
private JTextField tfVon_6 = new JTextField();
private JTextField tfVon_7 = new JTextField();
private JTextField tfVon_8 = new JTextField();
private JTextField tfVon_9 = new JTextField();
private JTextField tfVon_10 = new JTextField();
// Spalte 3 - Bis
private JTextField tfBis_1 = new JTextField();
private JTextField tfBis_2 = new JTextField();
private JTextField tfBis_3 = new JTextField();
private JTextField tfBis_4 = new JTextField();
private JTextField tfBis_5 = new JTextField();
private JTextField tfBis_6 = new JTextField();
private JTextField tfBis_7 = new JTextField();
private JTextField tfBis_8 = new JTextField();
private JTextField tfBis_9 = new JTextField();
private JTextField tfBis_10 = new JTextField();
// Spalte 4 - Temperatur
private JTextField tfTemp_1 = new JTextField();
private JTextField tfTemp_2 = new JTextField();
private JTextField tfTemp_3 = new JTextField();
private JTextField tfTemp_4 = new JTextField();
private JTextField tfTemp_5 = new JTextField();
private JTextField tfTemp_6 = new JTextField();
private JTextField tfTemp_7 = new JTextField();
private JTextField tfTemp_8 = new JTextField();
private JTextField tfTemp_9 = new JTextField();
private JTextField tfTemp_10 = new JTextField();
// Bottom line with buttons
private JButton btSave = new JButton();
private JButton btExit = new JButton();
private JButton btSchema = new JButton();
private int actWDay = 0;
private void createAndShowGUI() {
// The main window
JFrame frame = new JFrame("Heizung");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// The content of the window
JLabel lbl_WDay = new JLabel("Wochentag");
JLabel lbl_top1 = new JLabel("#");
JLabel lbl_top2 = new JLabel("Von");
JLabel lbl_top3 = new JLabel("Bis");
JLabel lbl_top4 = new JLabel("Temp");
// Spalte 1
JLabel lbl_1 = new JLabel("1");
JLabel lbl_2 = new JLabel("2");
JLabel lbl_3 = new JLabel("3");
JLabel lbl_4 = new JLabel("4");
JLabel lbl_5 = new JLabel("5");
JLabel lbl_6 = new JLabel("6");
JLabel lbl_7 = new JLabel("7");
JLabel lbl_8 = new JLabel("8");
JLabel lbl_9 = new JLabel("9");
JLabel lbl_10 = new JLabel("10");
// Initialize the combo box
cmbWDay.addActionListener(this);
cmbWDay.addItem("Montag");
cmbWDay.addItem("Dienstag");
cmbWDay.addItem("Mittwoch");
cmbWDay.addItem("Donnerstag");
cmbWDay.addItem("Freitag");
cmbWDay.addItem("Samstag");
cmbWDay.addItem("Sonntag");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
actWDay = cal.get(Calendar.DAY_OF_WEEK);
if (actWDay == Calendar.SUNDAY)
actWDay = 7;
else
actWDay--;
cmbWDay.setSelectedIndex(actWDay - 1);
// Initialize the buttons
btSave.setText("Speichern");
btSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
saveButtonActionPerformed(e);
}
});
btExit.setText("Exit");
btExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
exitButtonActionPerformed(e);
}
});
btSchema.setText("Schema");
btSchema.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
/* To do */
}
});
// Define and initialize the layout
GroupLayout layout = new GroupLayout(frame.getContentPane());
frame.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbl_WDay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmbWDay, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_top1, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbl_top2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbl_top3, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbl_top4, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_1, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_1, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_1, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_2, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_2, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_2, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_3, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_3, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_3, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_3, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_4, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_4, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_4, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_4, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_5, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_5, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_5, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_5, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_6, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_6, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_6, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_6, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_7, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_7, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_7, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_7, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_8, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_8, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_8, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_8, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_9, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_9, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_9, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_9, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lbl_10, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfVon_10, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfBis_10, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfTemp_10, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btSave, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btSchema, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btExit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(27, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lbl_top1, lbl_top2, lbl_top3, lbl_top4});
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lbl_WDay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cmbWDay, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_top1)
.addComponent(lbl_top2)
.addComponent(lbl_top3)
.addComponent(lbl_top4))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_1)
.addComponent(tfVon_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_2)
.addComponent(tfVon_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_3)
.addComponent(tfVon_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_4)
.addComponent(tfVon_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_5)
.addComponent(tfVon_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_6)
.addComponent(tfVon_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_7)
.addComponent(tfVon_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_8)
.addComponent(tfVon_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_9)
.addComponent(tfVon_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_10)
.addComponent(tfVon_10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfBis_10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tfTemp_10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btSave, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btSchema, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btExit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addContainerGap(21, Short.MAX_VALUE))
);
//Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* This method performs saveButtonActionPerformed
*/
private void saveButtonActionPerformed(ActionEvent evt) {
int idx = cmbWDay.getSelectedIndex();
saveDay(idx + 1);
clearLines();
readDay(idx + 1);
}
private void exitButtonActionPerformed(ActionEvent evt) {
System.exit(0);
}
private String timeToString(long tm) {
int hour, min;
hour = (int)(tm / 3600);
min = (int)((tm - (hour * 3600)) / 60);
return String.format("%02d:%02d",hour, min);
}
private boolean isDigit(int ch)
{
if (ch < '0' || ch > '9')
return false;
return true;
}
private long stringToTime(String stm)
{
int hour, min, i;
long tm;
if (stm.length() != 5 || !stm.contains(":") || stm.indexOf(':') != 2)
return 0;
for (i = 0; i < stm.length(); i++)
{
if (i != 2 && !isDigit(stm.charAt(i)))
return 0;
}
hour = Integer.parseInt(stm.substring(0, 2));
min = Integer.parseInt(stm.substring(3, 5));
if (hour < 0 || hour > 23 || min < 0 || min > 59)
return 0;
tm = (long)(hour * 3600 + min * 60);
return tm;
}
private void decode(String str) {
// Check for initial key word
if (str.startsWith("LINE:"))
{
rm_str lr = new rm_str();
String work;
int line;
int wday;
long start;
long end;
float temp;
if (str.startsWith("ERROR:"))
{
System.out.println(str);
return;
}
lr.remove_string(str, "LINE:");
str = lr.getRight();
work = lr.remove_string(str, ":");
str = lr.getRight();
line = Integer.parseInt(work.substring(0, work.length() - 1));
work = lr.remove_string(str, ":");
str = lr.getRight();
wday = Integer.parseInt(work.substring(0, work.length() - 1));
work = lr.remove_string(str, ":");
str = lr.getRight();
start = Integer.parseInt(work.substring(0, work.length() - 1));
work = lr.remove_string(str, ":");
str = lr.getRight();
end = Integer.parseInt(work.substring(0, work.length() - 1));
temp = Float.parseFloat(str);
if (line >= 1 && line <= 10 && wday >= 1 && wday <= 7 && start >= 0 && end >= 0 && start != end && temp > 0.0)
{
switch(line)
{
case 1:
tfVon_1.setText(timeToString(start));
tfBis_1.setText(timeToString(end));
tfTemp_1.setText(String.format("%.1f",temp));
break;
case 2:
tfVon_2.setText(timeToString(start));
tfBis_2.setText(timeToString(end));
tfTemp_2.setText(String.format("%.1f",temp));
break;
case 3:
tfVon_3.setText(timeToString(start));
tfBis_3.setText(timeToString(end));
tfTemp_3.setText(String.format("%.1f",temp));
break;
case 4:
tfVon_4.setText(timeToString(start));
tfBis_4.setText(timeToString(end));
tfTemp_4.setText(String.format("%.1f",temp));
break;
case 5:
tfVon_5.setText(timeToString(start));
tfBis_5.setText(timeToString(end));
tfTemp_5.setText(String.format("%.1f",temp));
break;
case 6:
tfVon_6.setText(timeToString(start));
tfBis_6.setText(timeToString(end));
tfTemp_6.setText(String.format("%.1f",temp));
break;
case 7:
tfVon_7.setText(timeToString(start));
tfBis_7.setText(timeToString(end));
tfTemp_7.setText(String.format("%.1f",temp));
break;
case 8:
tfVon_8.setText(timeToString(start));
tfBis_8.setText(timeToString(end));
tfTemp_8.setText(String.format("%.1f",temp));
break;
case 9:
tfVon_9.setText(timeToString(start));
tfBis_9.setText(timeToString(end));
tfTemp_9.setText(String.format("%.1f",temp));
break;
case 10:
tfVon_10.setText(timeToString(start));
tfBis_10.setText(timeToString(end));
tfTemp_10.setText(String.format("%.1f",temp));
break;
}
}
}
}
private void clearLines()
{
tfVon_1.setText("");
tfBis_1.setText("");
tfTemp_1.setText("");
tfVon_2.setText("");
tfBis_2.setText("");
tfTemp_2.setText("");
tfVon_3.setText("");
tfBis_3.setText("");
tfTemp_3.setText("");
tfVon_4.setText("");
tfBis_4.setText("");
tfTemp_4.setText("");
tfVon_5.setText("");
tfBis_5.setText("");
tfTemp_5.setText("");
tfVon_6.setText("");
tfBis_6.setText("");
tfTemp_6.setText("");
tfVon_7.setText("");
tfBis_7.setText("");
tfTemp_7.setText("");
tfVon_8.setText("");
tfBis_8.setText("");
tfTemp_8.setText("");
tfVon_9.setText("");
tfBis_9.setText("");
tfTemp_9.setText("");
tfVon_10.setText("");
tfBis_10.setText("");
tfTemp_10.setText("");
}
private boolean readDay(int wday) {
Socket s = null;
PrintWriter out = null;
BufferedReader in = null;
char[] ch;
String answer;
if (wday < 1 || wday > 7)
return false;
ch = new char[2];
try {
s = new Socket(strServerName, 11001);
s.setSoTimeout(3000);
out = new PrintWriter(s.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost: "+e.getMessage());
System.exit(1);
}
catch (SocketTimeoutException e)
{
System.err.println("Could't set timeout: "+e.getMessage());
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: "+strServerName+": "+e.getMessage());
System.exit(1);
}
// Just wait a little to let daemon initialize
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
System.out.println("Error: "+e1.getMessage());
}
// Request a particular day
out.print("GET WDAY:"+wday+";");
out.flush();
answer = "";
try {
while (in.read(ch, 0, 1) != -1)
{
if (ch[0] == ';')
{
System.out.println("Found answer:"+answer);
if (answer.equals("OK") || answer.equals("NAK"))
break;
decode(answer);
answer = "";
continue;
}
answer = answer+ch[0];
}
}
catch (SocketTimeoutException e)
{
System.err.println("Error: "+e.getMessage());
}
catch (IOException e) {
System.err.println("Error reading from stream: "+e.getMessage());
out.print("quit;");
out.close();
System.exit(1);
}
out.print("quit;");
out.close();
try {
in.close();
s.close();
}
catch (IOException e) {
System.err.println("Error closing a socket: "+e.getMessage());
System.exit(1);
}
return true;
}
public boolean saveDay(int wday)
{
Socket s = null;
PrintWriter out = null;
BufferedReader in = null;
char[] ch;
String answer;
int i, numLines = 0;
int err = 0;
float tp;
if (wday < 1 || wday > 7)
return false;
ch = new char[2];
try {
s = new Socket(strServerName, 11001);
s.setSoTimeout(3000);
out = new PrintWriter(s.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch (UnknownHostException e) {
System.err.println("Don't know about host: "+strServerName+": "+e.getMessage());
System.exit(1);
}
catch (SocketTimeoutException e)
{
System.err.println("Could't set timeout: "+e.getMessage());
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: "+strServerName+": "+e.getMessage());
System.exit(1);
}
// Just wait a little to let daemon initialize
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
System.out.println("Error: "+e1.getMessage());
}
// Find the number of complete lines
if (!tfVon_1.getText().isEmpty() && !tfTemp_1.getText().isEmpty())
numLines = 1;
if (!tfVon_2.getText().isEmpty() && !tfTemp_2.getText().isEmpty())
numLines = 2;
if (!tfVon_3.getText().isEmpty() && !tfTemp_3.getText().isEmpty())
numLines = 3;
if (!tfVon_4.getText().isEmpty() && !tfTemp_4.getText().isEmpty())
numLines = 4;
if (!tfVon_5.getText().isEmpty() && !tfTemp_5.getText().isEmpty())
numLines = 5;
if (!tfVon_6.getText().isEmpty() && !tfTemp_6.getText().isEmpty())
numLines = 6;
if (!tfVon_7.getText().isEmpty() && !tfTemp_7.getText().isEmpty())
numLines = 7;
if (!tfVon_8.getText().isEmpty() && !tfTemp_8.getText().isEmpty())
numLines = 8;
if (!tfVon_9.getText().isEmpty() && !tfTemp_9.getText().isEmpty())
numLines = 9;
if (!tfVon_10.getText().isEmpty() && !tfTemp_10.getText().isEmpty())
numLines = 10;
// Write out the actual day
answer = "SET DAY:"+numLines+":"+wday;
for (i = 0; i < numLines; i++)
{
String st;
switch(i)
{
case 0:
st = tfTemp_1.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_1.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_1.getText())+":"+tp;
else
err = 1;
break;
case 1:
st = tfTemp_2.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_2.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_2.getText())+":"+tp;
else
err = 1;
break;
case 2:
st = tfTemp_3.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_3.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_3.getText())+":"+tp;
else
err = 1;
break;
case 3:
st = tfTemp_4.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_4.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_4.getText())+":"+tp;
else
err = 1;
break;
case 4:
st = tfTemp_5.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_5.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_5.getText())+":"+tp;
else
err = 1;
break;
case 5:
st = tfTemp_6.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_6.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_6.getText())+":"+tp;
else
err = 1;
break;
case 6:
st = tfTemp_7.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_7.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_7.getText())+":"+tp;
else
err = 1;
break;
case 7:
st = tfTemp_8.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_8.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_8.getText())+":"+tp;
else
err = 1;
break;
case 8:
st = tfTemp_9.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_9.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_9.getText())+":"+tp;
else
err = 1;
break;
case 9:
st = tfTemp_10.getText().replace(",", ".");
tp = Float.parseFloat(st);
if (stringToTime(tfBis_10.getText()) > 0 && tp > 5.0 && tp <= 30.0)
answer += ":"+stringToTime(tfBis_10.getText())+":"+tp;
else
err = 1;
break;
}
}
answer += ";";
if (err == 0)
{
out.print(answer);
out.flush();
System.out.println("Send:"+answer);
answer = "";
try {
while (in.read(ch, 0, 1) != -1)
{
if (ch[0] == ';')
{
System.out.println("Found answer:"+answer);
break;
}
answer = answer+ch[0];
}
}
catch (SocketTimeoutException e)
{
System.err.println("Error: "+e.getMessage());
}
catch (IOException e) {
System.err.println("Error reading from stream: "+e.getMessage());
out.print("quit;");
out.close();
System.exit(1);
}
}
out.print("quit;");
out.close();
try {
in.close();
s.close();
}
catch (IOException e) {
System.err.println("Error closing a socket: "+e.getMessage());
System.exit(1);
}
if (err == 0)
return true;
else
return false;
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
heizung thisClass = new heizung();
thisClass.createAndShowGUI();
thisClass.readDay(thisClass.actWDay);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
int wday = cb.getSelectedIndex();
if (wday >= 0 && wday < 7)
{
actWDay = wday + 1;
clearLines();
readDay(actWDay);
}
}
}