Fixed some zigbee bugs

This commit is contained in:
Ziver Koc 2021-07-12 00:35:25 +02:00
parent d6fc1390fd
commit bcc4133a31
3 changed files with 12 additions and 7 deletions

View file

@ -41,12 +41,15 @@ import java.sql.SQLException;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class ZigBeeHalDataStore implements ZigBeeNetworkDataStore { public class ZigBeeHalDataStore implements ZigBeeNetworkDataStore {
private static final Logger logger = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
private static final String ZIGBEE_NODE_TABLE = "hal_zigbee_node";
private DBConnection db; private DBConnection db;
@ -60,14 +63,14 @@ public class ZigBeeHalDataStore implements ZigBeeNetworkDataStore {
Set<IeeeAddress> ieeeAddresses = new HashSet<>(); Set<IeeeAddress> ieeeAddresses = new HashSet<>();
try { try {
PreparedStatement stmt = db.getPreparedStatement( "SELECT address FROM zigbee_node" ); PreparedStatement stmt = db.getPreparedStatement( "SELECT address FROM " +ZIGBEE_NODE_TABLE);
List<String> strAddresses = DBConnection.exec(stmt, new ListSQLResult<String>()); List<String> strAddresses = DBConnection.exec(stmt, new ListSQLResult<String>());
for (String address : strAddresses) { for (String address : strAddresses) {
ieeeAddresses.add(new IeeeAddress(address)); ieeeAddresses.add(new IeeeAddress(address));
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); logger.log(Level.SEVERE, "Unable to read Zigbee Nodes from DB.", e);
} }
return ieeeAddresses; return ieeeAddresses;
@ -80,7 +83,7 @@ public class ZigBeeHalDataStore implements ZigBeeNetworkDataStore {
if (dso != null) if (dso != null)
return dso.getConfig(); return dso.getConfig();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); logger.log(Level.SEVERE, "Unable to read Zigbee Node from DB.", e);
} }
return null; return null;
} }
@ -106,7 +109,7 @@ public class ZigBeeHalDataStore implements ZigBeeNetworkDataStore {
dso.save(db); dso.save(db);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); logger.log(Level.SEVERE, "Unable to updated Zigbee Node in DB.", e);
} }
} }
@ -119,20 +122,21 @@ public class ZigBeeHalDataStore implements ZigBeeNetworkDataStore {
if (dso != null) if (dso != null)
dso.delete(db); dso.delete(db);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); logger.log(Level.SEVERE, "Unable to remove Zigbee Node from DB.", e);
} }
} }
/** /**
* A private data storage object connected to the DB schema. * A private data storage object connected to the DB schema.
*/ */
@DBBean.DBTable(ZIGBEE_NODE_TABLE)
private static class ZigbeeNodeDSO extends DBBean { private static class ZigbeeNodeDSO extends DBBean {
protected String address; protected String address;
protected String config; protected String config;
public static ZigbeeNodeDSO load(DBConnection db, String address) throws SQLException{ public static ZigbeeNodeDSO load(DBConnection db, String address) throws SQLException{
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM zigbee_node WHERE ? == zigbee_node.address" ); PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM " + ZIGBEE_NODE_TABLE + " WHERE ? == address" );
stmt.setString(1, address); stmt.setString(1, address);
return DBConnection.exec(stmt, DBBeanSQLResultHandler.create(ZigbeeNodeDSO.class, db)); return DBConnection.exec(stmt, DBBeanSQLResultHandler.create(ZigbeeNodeDSO.class, db));
} }

View file

@ -1,5 +1,6 @@
package se.hal.plugin.zigbee; package se.hal.plugin.zigbee;
import se.hal.HalContext;
import se.hal.intf.HalDatabaseUpgrade; import se.hal.intf.HalDatabaseUpgrade;
import zutil.db.DBConnection; import zutil.db.DBConnection;
import zutil.log.LogUtil; import zutil.log.LogUtil;
@ -12,7 +13,7 @@ import java.util.logging.Logger;
*/ */
public class ZigbeeHalDatabaseUpgrade extends HalDatabaseUpgrade { public class ZigbeeHalDatabaseUpgrade extends HalDatabaseUpgrade {
private static final int REFERENCE_DB_VERSION = 1; private static final int REFERENCE_DB_VERSION = 1;
private static final String REFERENCE_DB_PATH = "resource/hal-zigbee-reference.db"; private static final String REFERENCE_DB_PATH = HalContext.RESOURCE_ROOT + "/resource/hal-zigbee-reference.db";
public ZigbeeHalDatabaseUpgrade() { public ZigbeeHalDatabaseUpgrade() {