Commit a0404f95 authored by Jan Wagemakers's avatar Jan Wagemakers

Merge branch 'master' of gitlab.com:jan.wagemakers/thermostat

parents b26bc1c4 f03c87ef
package be.janwagemakers.controlthermostatbluetooth;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
@Component("Outside")
public class Outside {
private int outsideTemp;
private int outsidePressure;
public void update() {
try {
URL url = new URL("https://tgftp.nws.noaa.gov/data/observations/metar/stations/EBAW.TXT");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
int counter = 0;
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(counter + " " + inputLine);
// inputLine="EBAW 112220Z AUTO 27003KT 9999 // FEW028/// M05/02 Q1032";
if (counter == 1) {
for (int i = 0; i < inputLine.length() - 4; i++) {
char c = inputLine.charAt(i);
char c1 = inputLine.charAt(i + 1);
char c2 = inputLine.charAt(i + 2);
char c3 = inputLine.charAt(i + 3);
char c4 = inputLine.charAt(i + 4);
if ((c == ' ' || c == 'M') && Character.isDigit(c1) && Character.isDigit(c2) && c3 == '/') {
outsideTemp = Integer.parseInt("" + c1 + c2);
if (c == 'M') outsideTemp = outsideTemp * -1;
}
if ((c == 'Q' || c == 'A') && Character.isDigit(c1) && Character.isDigit(c2) && Character.isDigit(c3) && Character.isDigit(c4)) {
outsidePressure = Integer.parseInt("" + c1 + c2 + c3 + c4);
if (c == 'A') {
float p = outsidePressure * 1.3333f * 25.4f / 100; // hg -> hectopascal / inch -> mm
outsidePressure = Math.round(p);
}
}
}
}
counter++;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getOutsideTemp() {
return outsideTemp;
}
public int getOutsidePressure() {
return outsidePressure;
}
}
package be.janwagemakers.controlthermostatbluetooth;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
@Component("OutsideTemp")
public class OutsideTemp {
private float outsideTemp;
public void update() {
try {
URL url = new URL("https://tgftp.nws.noaa.gov/data/observations/metar/decoded/EBAW.TXT");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains("Temperature:")) {
try {
outsideTemp = Float.parseFloat(inputLine.substring(inputLine.indexOf('(') + 1, inputLine.indexOf(')') - 1));
} catch (Exception e) {
outsideTemp = -1000;
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public float getOutsideTemp() {
return outsideTemp;
}
}
......@@ -35,90 +35,57 @@ class Serial {
// data.add("close");
// return rfcomm.closePort();
// }
public boolean setSP(float temp) {
if (temp < 0 ) return false;
DecimalFormat dec = new DecimalFormat("00.0");
String tx = "SP=" + dec.format(temp);
float rx = get(tx);
if( temp != rx ) {
private boolean tx(String command, float value, DecimalFormat dec) {
String tx = command + dec.format(value);
float rx=get(tx);
if( value != rx ) {
data.add("ERROR");
return false;
}
return true;
}
public boolean setOT(float temp) {
public boolean setSP(float temp) {
if (temp < 0 ) return false;
DecimalFormat dec = new DecimalFormat("00.0");
String tx = "OT=" + dec.format(temp);
return tx("SP=", temp, dec);
}
public boolean setOT(int temp) {
DecimalFormat dec = new DecimalFormat("00");
return tx("OT=", temp, dec);
float rx = get(tx);
if( temp != rx ) {
data.add("ERROR");
return false;
}
return true;
}
public boolean setOA(int p) {
DecimalFormat dec = new DecimalFormat("0000");
return tx("OA=", p, dec);
}
public boolean setYY(int number) {
DecimalFormat dec = new DecimalFormat("00");
String tx = "YY=" + dec.format(number);
float rx = get(tx);
if( number != rx ) {
data.add("ERROR");
return false;
}
return true;
return tx("YY=", number, dec);
}
public boolean setMM(int number) {
DecimalFormat dec = new DecimalFormat("00");
String tx = "MM=" + dec.format(number);
float rx = get(tx);
if( number != rx ) {
data.add("ERROR");
return false;
}
return true;
return tx("MM=", number, dec);
}
public boolean setDD(int number) {
DecimalFormat dec = new DecimalFormat("00");
String tx = "DD=" + dec.format(number);
float rx = get(tx);
if( number != rx ) {
data.add("ERROR");
return false;
}
return true;
return tx("DD=", number, dec);
}
public boolean sethh(int number) {
DecimalFormat dec = new DecimalFormat("00");
String tx = "hh=" + dec.format(number);
float rx = get(tx);
if( number != rx ) {
data.add("ERROR");
return false;
}
return true;
return tx("hh=", number, dec);
}
public boolean setmm(int number) {
DecimalFormat dec = new DecimalFormat("00");
String tx = "mm=" + dec.format(number);
float rx = get(tx);
if( number != rx ) {
data.add("ERROR");
return false;
}
return true;
return tx("mm=", number, dec);
}
private float get(String tx) {
......
......@@ -28,7 +28,8 @@ public class StartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
private static final Serial SERIAL = new Serial();
private float oldOT = -1;
private int oldOA = -1;
private int oldOT = -1;
private int oldYear = -1;
private int oldMonth = -1;
private int oldDay = -1;
......@@ -39,7 +40,7 @@ public class StartupRunner implements CommandLineRunner {
@Autowired
private RuleRepository ruleRepository;
@Autowired
private OutsideTemp ot;
private Outside outside;
@Autowired private DataList data;
@Override
......@@ -50,7 +51,7 @@ public class StartupRunner implements CommandLineRunner {
@Scheduled(initialDelay = 1 * 1000, fixedDelay = 15 * 60 * 1000)
public void repeat15m() {
logger.info("RUNRUNRUN! 15M");
ot.update();
outside.update();
}
@Scheduled(initialDelay = 17 * 1000, fixedDelay = 1 * 60 * 1000)
......@@ -104,10 +105,16 @@ public class StartupRunner implements CommandLineRunner {
}
}
float outsideTemp = ot.getOutsideTemp();
data.add("Outside_temp = " + outsideTemp);
int outsideTemp = outside.getOutsideTemp();
int outsidePressure = outside.getOutsidePressure();
data.add("Outside Temperature = " + outsideTemp);
data.add("Outside Atmospheric Pressure = " + outsidePressure);
if (SERIAL.open()) {
// set current outside temp / YY / MM / DD / hh / mm
if (oldOA != outsidePressure) {
if (SERIAL.setOA(outsidePressure)) oldOA = outsidePressure;
}
if (oldOT != outsideTemp) {
if (SERIAL.setOT(outsideTemp)) oldOT = outsideTemp;
}
......@@ -198,8 +205,7 @@ public class StartupRunner implements CommandLineRunner {
private void communicationError(String string) {
data.add(string);
oldYear = oldMonth = oldDay = oldHour = -1;
oldOT = -1;
oldOA = oldOT = oldYear = oldMonth = oldDay = oldHour = -1;
}
}
......@@ -8,11 +8,10 @@ package be.janwagemakers.controlthermostatbluetooth.controller;
import be.janwagemakers.controlthermostatbluetooth.DataList;
import be.janwagemakers.controlthermostatbluetooth.Entity.Rule;
import be.janwagemakers.controlthermostatbluetooth.Entity.User;
import be.janwagemakers.controlthermostatbluetooth.OutsideTemp;
import be.janwagemakers.controlthermostatbluetooth.Outside;
import be.janwagemakers.controlthermostatbluetooth.Repository.RuleRepository;
import be.janwagemakers.controlthermostatbluetooth.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
......@@ -32,7 +31,7 @@ public class ShowController {
@Autowired private UserRepository userRepository;
@Autowired private RuleRepository ruleRepository;
@Autowired private OutsideTemp ot;
@Autowired private Outside outside;
@Autowired private DataList data;
private User user = new User("New User");//= userRepository.findById(1).get();
......@@ -46,7 +45,7 @@ public class ShowController {
model.addAttribute("sp", data.getSp());
model.addAttribute("pv", data.getPv());
model.addAttribute("op", data.getOp());
model.addAttribute("outsideTemp", ot.getOutsideTemp());
model.addAttribute("outsideTemp", outside.getOutsideTemp());
return "index";
}
......@@ -68,7 +67,7 @@ public class ShowController {
@GetMapping("/users")
public String users(Model model) {
model.addAttribute("data", data.getData());
// model.addAttribute("outsideTemp", ot.getOutsideTemp());
// model.addAttribute("outsideTemp", outside.getOutsideTemp());
model.addAttribute("users", userRepository.findAll());
model.addAttribute("rules", ruleRepository.findAll());
model.addAttribute("newUser", new User("New User"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment