Device data object are configurable from web but is not visualized correctly
Former-commit-id: a960f1bb610cb1dbbbc76826d15bdb4c6e90c984
This commit is contained in:
parent
1e4bc587bc
commit
02e15810ec
6 changed files with 39 additions and 32 deletions
|
|
@ -7,6 +7,7 @@ import se.hal.struct.Sensor;
|
|||
import se.hal.struct.User;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.parser.json.JSONParser;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
|
@ -66,7 +67,7 @@ public class PCDataSynchronizationClient implements HalDaemon {
|
|||
sensor.setExternalId(sensorDTO.sensorId);
|
||||
sensor.setName(sensorDTO.name);
|
||||
sensor.setType(sensorDTO.type);
|
||||
sensor.setConfig(sensorDTO.config);
|
||||
sensor.getDeviceConfig().setValues(JSONParser.read(sensorDTO.config)).applyConfiguration();
|
||||
sensor.setUser(user);
|
||||
sensor.save(db);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import zutil.db.SQLResultHandler;
|
|||
import zutil.log.LogUtil;
|
||||
import zutil.net.threaded.ThreadedTCPNetworkServer;
|
||||
import zutil.net.threaded.ThreadedTCPNetworkServerThread;
|
||||
import zutil.parser.json.JSONWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
|
|
@ -85,7 +86,7 @@ public class PCDataSynchronizationDaemon extends ThreadedTCPNetworkServer implem
|
|||
dto.sensorId = sensor.getId();
|
||||
dto.name = sensor.getName();
|
||||
dto.type = sensor.getType();
|
||||
dto.config = sensor.getConfig();
|
||||
dto.config = JSONWriter.toString(sensor.getDeviceConfig().getValuesAsNode());
|
||||
rsp.sensors.add(dto);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package se.hal.page;
|
|||
|
||||
import se.hal.ControllerManager;
|
||||
import se.hal.HalContext;
|
||||
import se.hal.intf.HalEventData;
|
||||
import se.hal.intf.HalHttpPage;
|
||||
import se.hal.struct.Event;
|
||||
import se.hal.struct.User;
|
||||
|
|
@ -52,22 +53,25 @@ public class EventConfigHttpPage extends HalHttpPage {
|
|||
if(request.containsKey("action")){
|
||||
int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1);
|
||||
Event event;
|
||||
Configurator<HalEventData> configurator;
|
||||
switch(request.get("action")) {
|
||||
// Local events
|
||||
case "create_local_event":
|
||||
event = new Event();
|
||||
event.setName(request.get("name"));
|
||||
event.setType(request.get("type"));
|
||||
//event.setConfig(request.get("config"));
|
||||
event.setUser(localUser);
|
||||
configurator = event.getDeviceConfig();
|
||||
configurator.setValues(request);
|
||||
configurator.applyConfiguration();
|
||||
event.save(db);
|
||||
case "modify_local_event":
|
||||
event = Event.getEvent(db, id);
|
||||
if(event != null){
|
||||
event.setName(request.get("name"));
|
||||
event.setType(request.get("type"));
|
||||
//event.setConfig(request.get("config"));
|
||||
event.setUser(localUser);
|
||||
event.getDeviceConfig().setValues(request).applyConfiguration();
|
||||
event.save(db);
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package se.hal.page;
|
|||
import se.hal.ControllerManager;
|
||||
import se.hal.HalContext;
|
||||
import se.hal.intf.HalHttpPage;
|
||||
import se.hal.intf.HalSensorData;
|
||||
import se.hal.struct.Sensor;
|
||||
import se.hal.struct.User;
|
||||
import zutil.db.DBConnection;
|
||||
|
|
@ -53,6 +54,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
|
|||
int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1);
|
||||
Sensor sensor;
|
||||
User user;
|
||||
Configurator<HalSensorData> configurator;
|
||||
switch(request.get("action")) {
|
||||
// Local Sensors
|
||||
case "create_local_sensor":
|
||||
|
|
@ -60,8 +62,10 @@ public class SensorConfigHttpPage extends HalHttpPage {
|
|||
sensor.setName(request.get("name"));
|
||||
sensor.setType(request.get("type"));
|
||||
sensor.setSynced(Boolean.parseBoolean(request.get("sync")));
|
||||
//sensor.setConfig(request.get("config"));
|
||||
sensor.setUser(localUser);
|
||||
configurator = sensor.getDeviceConfig();
|
||||
configurator.setValues(request);
|
||||
configurator.applyConfiguration();
|
||||
sensor.save(db);
|
||||
case "modify_local_sensor":
|
||||
sensor = Sensor.getSensor(db, id);
|
||||
|
|
@ -69,7 +73,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
|
|||
sensor.setName(request.get("name"));
|
||||
sensor.setType(request.get("type"));
|
||||
sensor.setSynced(Boolean.parseBoolean(request.get("sync")));
|
||||
//sensor.setConfig(request.get("config"));
|
||||
sensor.getDeviceConfig().setValues(request).applyConfiguration();
|
||||
sensor.save(db);
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public abstract class AbstractDevice<T> extends DBBean {
|
|||
// Sensor specific data
|
||||
private String name;
|
||||
private String type;
|
||||
private String config;
|
||||
private String config; // only used to store the deviceData configuration in DB
|
||||
|
||||
// Sensor specific data
|
||||
private transient T deviceData;
|
||||
|
|
@ -30,16 +30,23 @@ public abstract class AbstractDevice<T> extends DBBean {
|
|||
private User user;
|
||||
|
||||
|
||||
|
||||
public Configurator<T> getDeviceConfig() {
|
||||
T obj = getDeviceData();
|
||||
if (obj != null)
|
||||
return new Configurator<>(obj);
|
||||
return null;
|
||||
}
|
||||
public T getDeviceData() {
|
||||
if (config != null && deviceData == null) {
|
||||
if (deviceData == null) {
|
||||
try {
|
||||
Class c = Class.forName(type);
|
||||
deviceData = (T) c.newInstance();
|
||||
|
||||
Configurator<T> configurator = new Configurator<>(deviceData);
|
||||
if (config != null && !config.isEmpty()) {
|
||||
Configurator<T> configurator = getDeviceConfig();
|
||||
configurator.setValues(JSONParser.read(config));
|
||||
configurator.applyConfiguration();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Unable to read device data", e);
|
||||
}
|
||||
|
|
@ -48,27 +55,17 @@ public abstract class AbstractDevice<T> extends DBBean {
|
|||
}
|
||||
public void setDeviceData(T data) {
|
||||
this.deviceData = data;
|
||||
updateConfig();
|
||||
}
|
||||
|
||||
public String getConfig() {
|
||||
return config;
|
||||
}
|
||||
public void setConfig(String config) {
|
||||
if (this.config == null || !this.config.equals(config)) {
|
||||
this.config = config;
|
||||
this.deviceData = null; // invalidate current sensor data object
|
||||
}
|
||||
updateConfigString();
|
||||
}
|
||||
public void save(DBConnection db) throws SQLException {
|
||||
if (deviceData != null)
|
||||
updateConfig();
|
||||
updateConfigString();
|
||||
else
|
||||
this.config = null;
|
||||
super.save(db);
|
||||
}
|
||||
protected void updateConfig() {
|
||||
Configurator<T> configurator = new Configurator<>(deviceData);
|
||||
protected void updateConfigString() {
|
||||
Configurator<T> configurator = getDeviceConfig();
|
||||
this.config = JSONWriter.toString(configurator.getValuesAsNode());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
<td>{{.getName()}}</td>
|
||||
<td>{{.getType()}}</td>
|
||||
<td>{{.isSynced()}}</td>
|
||||
<td>{{.getConfig()}}</td>
|
||||
<td>{{.getDeviceConfig()}}</td>
|
||||
<td>
|
||||
<form method="POST">
|
||||
<input type="hidden" name="action" value="remove_local_sensor">
|
||||
|
|
@ -148,24 +148,24 @@
|
|||
<tr>
|
||||
<td>{{.getName()}}</td>
|
||||
<td>{{.getType()}}</td>
|
||||
<td>{{.getConfig()}}</td>
|
||||
<td>{{.getDeviceConfig()}}</td>
|
||||
<td>
|
||||
<form method="POST">
|
||||
<div class="btn-toolbar pull-right">
|
||||
<input type="hidden" name="action" value="modify_external_sensor">
|
||||
<input type="hidden" name="id" value="{{.getId()}}">
|
||||
{{^.sync}}
|
||||
{{^.isSynced()}}
|
||||
<input type="hidden" name="sync" value="true">
|
||||
<button type="submit" class="btn btn-default btn-xs" title="Start Syncing">
|
||||
<span class="glyphicon glyphicon-save"></span>
|
||||
</button>
|
||||
{{/.sync}}
|
||||
{{#.sync}}
|
||||
{{/.isSynced()}}
|
||||
{{#.isSynced()}}
|
||||
<input type="hidden" name="sync" value="false">
|
||||
<button type="submit" class="btn btn-danger btn-xs" title="Stop Syncing and Clear Data">
|
||||
<span class="glyphicon glyphicon-remove"></span>
|
||||
</button>
|
||||
{{/.sync}}
|
||||
{{/.isSynced()}}
|
||||
</div>
|
||||
</form>
|
||||
</td>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue