Commit e891d8b0 authored by Jan Wagemakers's avatar Jan Wagemakers

Configuration database backend : start

parent a0404f95
package be.janwagemakers.controlthermostatbluetooth.Entity;
import javax.persistence.*;
import java.util.List;
@Entity
public class Configuration {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String airport; // where to get outside information ; EBAW = Belgium Antwerp Airport
private float lowTemp; // temperature when nobody is home
protected Configuration() {}
public int getId() {
return id;
}
public String getAirport() {
return airport;
}
public float getLowTemp() {
return lowTemp;
}
}
......@@ -9,12 +9,12 @@ import java.net.URL;
@Component("Outside")
public class Outside {
private int outsideTemp;
private int outsidePressure;
private int temperature;
private int pressure;
public void update() {
public void update(String airport) {
try {
URL url = new URL("https://tgftp.nws.noaa.gov/data/observations/metar/stations/EBAW.TXT");
URL url = new URL("https://tgftp.nws.noaa.gov/data/observations/metar/stations/" + airport + ".TXT");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
int counter = 0;
......@@ -30,14 +30,14 @@ public class Outside {
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;
temperature = Integer.parseInt("" + c1 + c2);
if (c == 'M') temperature = temperature * -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);
pressure = 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);
float p = pressure * 1.3333f * 25.4f / 100; // hg -> hectopascal / inch -> mm
pressure = Math.round(p);
}
}
}
......@@ -50,11 +50,11 @@ public class Outside {
}
}
public int getOutsideTemp() {
return outsideTemp;
public int getTemperature() {
return temperature;
}
public int getOutsidePressure() {
return outsidePressure;
public int getPressure() {
return pressure;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package be.janwagemakers.controlthermostatbluetooth.Repository;
import be.janwagemakers.controlthermostatbluetooth.Entity.Configuration;
import be.janwagemakers.controlthermostatbluetooth.Entity.Rule;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
/**
*
* @author jan
*/
@RepositoryRestResource
public interface ConfigurationRepository extends PagingAndSortingRepository<Configuration, Integer>{
}
......@@ -5,8 +5,10 @@
*/
package be.janwagemakers.controlthermostatbluetooth;
import be.janwagemakers.controlthermostatbluetooth.Entity.Configuration;
import be.janwagemakers.controlthermostatbluetooth.Entity.Rule;
import be.janwagemakers.controlthermostatbluetooth.Entity.User;
import be.janwagemakers.controlthermostatbluetooth.Repository.ConfigurationRepository;
import be.janwagemakers.controlthermostatbluetooth.Repository.RuleRepository;
import be.janwagemakers.controlthermostatbluetooth.Repository.UserRepository;
......@@ -28,6 +30,7 @@ public class StartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
private static final Serial SERIAL = new Serial();
private Configuration config;
private int oldOA = -1;
private int oldOT = -1;
private int oldYear = -1;
......@@ -35,6 +38,8 @@ public class StartupRunner implements CommandLineRunner {
private int oldDay = -1;
private int oldHour = -1;
@Autowired
private ConfigurationRepository configurationRepository;
@Autowired
private UserRepository userRepository;
@Autowired
......@@ -46,17 +51,21 @@ public class StartupRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
logger.info("Control Thermostat With Bluetooth");
// ID 1 = current configuration (schema.sql)
config = configurationRepository.findById(1).get();
System.out.println("CONFIG : " + config.getAirport() + " " + config.getLowTemp());
}
@Scheduled(initialDelay = 1 * 1000, fixedDelay = 15 * 60 * 1000)
public void repeat15m() {
logger.info("RUNRUNRUN! 15M");
outside.update();
logger.info("Repeat 15M");
outside.update(config.getAirport());
}
@Scheduled(initialDelay = 17 * 1000, fixedDelay = 1 * 60 * 1000)
public void repeat1m() {
logger.info("RUNRUNRUN! 1M");
logger.info("Repeat 1M");
float sp = 0;
......@@ -69,17 +78,11 @@ public class StartupRunner implements CommandLineRunner {
int dayWeek = rightNow.get(Calendar.DAY_OF_WEEK);
int hm = 100 * hour + min;
float lowestTemperature = -1f;
float personSP = 0;
for (User user : userRepository.findAll()) {
boolean isHome = user.isHome();
for (Rule rule : ruleRepository.findAll()) {
if (rule.getUser().getId() == user.getId()) {
if (lowestTemperature == -1f || lowestTemperature > rule.getTemp()) {
lowestTemperature = rule.getTemp();
}
if (isHome) {
int rday = rule.getDay();
int rhm = 100 * rule.getHour() + rule.getMin();
......@@ -90,23 +93,22 @@ public class StartupRunner implements CommandLineRunner {
if (hm >= rhm) {
data.add("Rule : " + rhm);
personSP = rule.getTemp();
}
}
}
}
}
if (sp < lowestTemperature) {
sp = lowestTemperature;
if (sp < config.getLowTemp()) {
sp = config.getLowTemp();
}
if (personSP > sp) {
sp = personSP;
}
}
int outsideTemp = outside.getOutsideTemp();
int outsidePressure = outside.getOutsidePressure();
int outsideTemp = outside.getTemperature();
int outsidePressure = outside.getPressure();
data.add("Outside Temperature = " + outsideTemp);
data.add("Outside Atmospheric Pressure = " + outsidePressure);
if (SERIAL.open()) {
......@@ -169,9 +171,9 @@ public class StartupRunner implements CommandLineRunner {
if (outsideTemp > pv) {
sp = sp + pv - outsideTemp;
}
// Lower SP if it's warmer outside then defaultLowTemperature
if (outsideTemp > lowestTemperature) {
sp = sp + lowestTemperature - outsideTemp;
// Lower SP if it's warmer outside then config LowTemperature
if (outsideTemp > config.getLowTemp()) {
sp = sp + config.getLowTemp() - outsideTemp;
}
// No negative SP
if (sp < 0) {
......@@ -208,4 +210,3 @@ public class StartupRunner implements CommandLineRunner {
oldOA = oldOT = oldYear = oldMonth = oldDay = oldHour = -1;
}
}
......@@ -45,7 +45,7 @@ public class ShowController {
model.addAttribute("sp", data.getSp());
model.addAttribute("pv", data.getPv());
model.addAttribute("op", data.getOp());
model.addAttribute("outsideTemp", outside.getOutsideTemp());
model.addAttribute("outsideTemp", outside.getTemperature());
return "index";
}
......@@ -67,7 +67,7 @@ public class ShowController {
@GetMapping("/users")
public String users(Model model) {
model.addAttribute("data", data.getData());
// model.addAttribute("outsideTemp", outside.getOutsideTemp());
// model.addAttribute("outsideTemp", outside.getTemperature());
model.addAttribute("users", userRepository.findAll());
model.addAttribute("rules", ruleRepository.findAll());
model.addAttribute("newUser", new User("New User"));
......
drop table configuration if exists;
-- drop table rule if exists;
-- drop table user if exists;
--
create table if not exists configuration (
id integer generated by default as identity,
airport varchar(255),
-- default_high_temperature float not null,
low_temp float not null,
-- first_name varchar(255),
-- last_name varchar(255),
-- name varchar(255),
primary key (id)
);
--
create table if not exists rule (
line integer generated by default as identity,
......@@ -21,7 +34,10 @@ create table if not exists user (
-- last_name varchar(255),
name varchar(255),
primary key (id)
);
);
--
alter table rule add constraint if not exists FK55ew2b76r531nnmw97mm96yt2 foreign key (user_id) references user;
--
alter table rule add constraint if not exists FK55ew2b76r531nnmw97mm96yt2 foreign key (user_id) references user
INSERT INTO configuration (airport, low_temp) VALUES ('EBAW', '12.0');
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