AbstractDevice was not loading latest DeviceData at startup

This commit is contained in:
Ziver Koc 2016-08-25 22:15:45 +02:00
parent 3cb72a1d17
commit 889f9fc85f
4 changed files with 15 additions and 15 deletions

View file

@ -51,7 +51,7 @@ based on Christopher Laws, March, 2013 code.
void SensorBH1750::setup() { void SensorBH1750::setup() {
Wire.begin(); Wire.begin();
configure(BH1750_ONE_TIME_HIGH_RES_MODE); //configure(BH1750_ONE_TIME_HIGH_RES_MODE);
} }
@ -78,15 +78,15 @@ void SensorBH1750::configure(uint8_t mode) {
void SensorBH1750::read(LightData& data) { void SensorBH1750::read(LightData& data) {
uint16_t level; configure(BH1750_ONE_TIME_HIGH_RES_MODE);
Wire.beginTransmission(BH1750_I2CADDR); Wire.beginTransmission(BH1750_I2CADDR);
Wire.requestFrom(BH1750_I2CADDR, 2); Wire.requestFrom(BH1750_I2CADDR, 2);
level = Wire.read(); uint16_t level = Wire.read();
level <<= 8; level <<= 8;
level |= Wire.read(); level |= Wire.read();
Wire.endTransmission(); Wire.endTransmission();
data.lumen = level/1.2; // convert to lux data.lumen = level/1.2; // convert to lux
} }

View file

@ -16,7 +16,7 @@
{{#events}} {{#events}}
<tr> <tr>
<td><a href="?id={{.getId()}}">{{.getName()}}</a></td> <td><a href="?id={{.getId()}}">{{.getName()}}</a></td>
<td>{{.getDeviceData().getClass().getSimpleName()}}</td> <td>{{.getDeviceConfig().getClass().getSimpleName()}}</td>
<td>{{.getDeviceData()}}</td> <td>{{.getDeviceData()}}</td>
<td><span class="timestamp">{{.getDeviceData().getTimestamp()}}</span></td> <td><span class="timestamp">{{.getDeviceData().getTimestamp()}}</span></td>
<td> <td>

View file

@ -15,7 +15,7 @@
{{#sensors}} {{#sensors}}
<tr> <tr>
<td><a href="?id={{.getId()}}">{{.getName()}}</a></td> <td><a href="?id={{.getId()}}">{{.getName()}}</a></td>
<td>{{.getDeviceData().getClass().getSimpleName()}}</td> <td>{{.getDeviceConfig().getClass().getSimpleName()}}</td>
<td>{{.getDeviceData()}}</td> <td>{{.getDeviceData()}}</td>
<td><span class="timestamp">{{.getDeviceData().getTimestamp()}}</span></td> <td><span class="timestamp">{{.getDeviceData().getTimestamp()}}</span></td>
</tr> </tr>

View file

@ -59,6 +59,7 @@ public abstract class AbstractDevice<T,D> extends DBBean {
deviceConfig = (T) c.newInstance(); deviceConfig = (T) c.newInstance();
applyConfig(); applyConfig();
deviceData = getLatestDeviceData(HalContext.getDB());
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "Unable instantiate DeviceConfig: "+type, e); logger.log(Level.SEVERE, "Unable instantiate DeviceConfig: "+type, e);
} }
@ -95,7 +96,7 @@ public abstract class AbstractDevice<T,D> extends DBBean {
/** /**
* Will update the config String that will be stored in DB. * Will update the config String that will be stored in DB.
*/ */
protected void updateConfigString() { private void updateConfigString() {
Configurator<T> configurator = getDeviceConfigurator(); Configurator<T> configurator = getDeviceConfigurator();
this.config = JSONWriter.toString(configurator.getValuesAsNode()); this.config = JSONWriter.toString(configurator.getValuesAsNode());
} }
@ -103,7 +104,7 @@ public abstract class AbstractDevice<T,D> extends DBBean {
* This method will configure the current DeviceData with the * This method will configure the current DeviceData with the
* configuration from the config String. * configuration from the config String.
*/ */
protected void applyConfig(){ private void applyConfig(){
if (config != null && !config.isEmpty()) { if (config != null && !config.isEmpty()) {
Configurator<T> configurator = getDeviceConfigurator(); Configurator<T> configurator = getDeviceConfigurator();
configurator.setValues(JSONParser.read(config)); configurator.setValues(JSONParser.read(config));
@ -153,9 +154,8 @@ public abstract class AbstractDevice<T,D> extends DBBean {
*/ */
public void setType(String type) { public void setType(String type) {
if (this.type == null || !this.type.equals(type)) { if (this.type == null || !this.type.equals(type)) {
setDeviceConfig(null); // reset
this.type = type; this.type = type;
this.config = null;
this.deviceConfig = null; // invalidate current sensor data object
} }
} }