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