From eca507ee768738bf6aa252f89d69b846245eb1ab Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Sat, 8 Nov 2008 23:52:07 +0100 Subject: [PATCH] api: convert to use MoteDatabase and fix MoteControlService usage --- api/src/main/java/remote/control/MoteDatabase.java | 75 +++++++++------------- .../axis/spi/motedata/AxisMoteDataResponse.java | 5 ++ .../remote/core/spi/motedata/MoteDataResponse.java | 2 + 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/api/src/main/java/remote/control/MoteDatabase.java b/api/src/main/java/remote/control/MoteDatabase.java index be1b28c..f28a93f 100644 --- a/api/src/main/java/remote/control/MoteDatabase.java +++ b/api/src/main/java/remote/control/MoteDatabase.java @@ -14,7 +14,7 @@ import remote.core.spi.ServiceResultException; import remote.core.spi.motecontrol.MoteControlCommand; import remote.core.spi.motecontrol.MoteControlResponse; import remote.core.spi.motedata.MoteDataProperty; -import remote.core.spi.motedata.MoteDataTable; +import remote.core.spi.motedata.MoteDataResponse; import remote.core.util.Cancellable; import remote.core.util.IteratorAdapter; @@ -130,10 +130,10 @@ final class MoteDatabase implements MoteManager { return update.send(); } - private boolean updateMoteHost(MoteModel mote, MoteDataTable table, int row) + private boolean updateMoteHost(MoteModel mote, MoteDataResponse data, int row) { - String dns = table.getProperty(row, MoteDataProperty.hostip, "localhost"); - String ip = table.getProperty(row, MoteDataProperty.hostdns, "127.0.0.1"); + String dns = data.getMoteValue(row, MoteDataProperty.hostip, "localhost"); + String ip = data.getMoteValue(row, MoteDataProperty.hostdns, "127.0.0.1"); HostModel host = hosts.get(ip); if (host == null) @@ -144,10 +144,10 @@ final class MoteDatabase implements MoteManager { return host.add(mote); } - private boolean updateMoteSite(MoteModel mote, MoteDataTable table, int row) + private boolean updateMoteSite(MoteModel mote, MoteDataResponse data, int row) { - long id = table.getProperty(row, MoteDataProperty.site_id, (long) -1); - String name = table.getProperty(row, MoteDataProperty.sitename, "unknown"); + long id = data.getMoteValue(row, MoteDataProperty.site_id, (long) -1); + String name = data.getMoteValue(row, MoteDataProperty.site, "unknown"); SiteModel site = sites.get(id); if (site == null) @@ -158,9 +158,9 @@ final class MoteDatabase implements MoteManager { return site.add(mote); } - private boolean updateMoteUsage(MoteModel mote, MoteDataTable table, int row) + private boolean updateMoteUsage(MoteModel mote, MoteDataResponse table, int row) { - String status = table.getProperty(row, MoteDataProperty.mote_usage, "unknown"); + String status = table.getMoteValue(row, MoteDataProperty.mote_usage, "unknown"); switch (MoteUsage.match(status)) { case available: @@ -190,72 +190,57 @@ final class MoteDatabase implements MoteManager { return false; } - private boolean updateMote(MoteModel mote, MoteDataTable table, int row) + private boolean updateMote(MoteModel mote, MoteDataResponse data, int row) { boolean changed = false; - for (int col = 0; col < table.getColumns(); col++) - switch (table.getColumnProperty(col)) { - case mote_usage: - if (updateMoteUsage(mote, table, row)) - changed = true; - break; + if (updateMoteUsage(mote, data, row)) + changed = true; - case site_id: - if (updateMoteSite(mote, table, row)) - changed = true; - break; + if (updateMoteSite(mote, data, row)) + changed = true; - case hostip: - if (updateMoteHost(mote, table, row)) - changed = true; - break; + if (updateMoteHost(mote, data, row)) + changed = true; + /* case netaddress: case macaddress: case platform: case unknown: - String value = (String) table.getData(row, col); - String name = (String) table.getHeaderName(col); + String value = (String) data.getData(row, col); + String name = (String) data.getHeaderName(col); String orig = mote.configMap.put(name, value); if (orig == null || !orig.equals(value)) changed = true; - break; - - case hostdns: - case sitename: - default: - /* Ignore */ - break; - } - + */ return changed; } - private ServiceResultException updateDatabase(MoteDataTable table) + private ServiceResultException updateDatabase(MoteDataResponse data) { final long UNKNOWN_MOTE_ID = -1; MoteListModel newMotes = new MoteListModel(); MoteListModel oldMotes = new MoteListModel(); MoteListModel changedMotes = new MoteListModel(); - if (!table.hasProperty(MoteDataProperty.mote_id)) + if (!data.hasProperty(MoteDataProperty.mote_id)) return new ServiceResultException("No mote id column"); updateCount++; - for (int row = 0; row < table.getRows(); row++) { - Long id = table.getProperty(row, MoteDataProperty.mote_id, UNKNOWN_MOTE_ID); + for (int row = 0; row < data.getMoteSize(); row++) { + Long id = data.getMoteValue(row, MoteDataProperty.mote_id, UNKNOWN_MOTE_ID); MoteModel mote = motes.get(id); if (mote == null) { mote = new MoteModel(id); newMotes.addMote(mote); - updateMote(mote, table, row); + updateMote(mote, data, row); - } else if (updateMote(mote, table, row)) + } else if (updateMote(mote, data, row)) changedMotes.addMote(mote); mote.updateCount = updateCount; @@ -662,7 +647,7 @@ final class MoteDatabase implements MoteManager { } - private final class UpdateRequest extends ServiceRequestAdapter { + private final class UpdateRequest extends ServiceRequestAdapter { public boolean send() { @@ -672,7 +657,7 @@ final class MoteDatabase implements MoteManager { } @Override - public void onSuccess(MoteDataTable result) + public void onSuccess(MoteDataResponse result) { Throwable caught = null; try { @@ -708,7 +693,7 @@ final class MoteDatabase implements MoteManager { { this.motes = motes; this.listener = listener; - send(services.getMoteControlService().controlMotes(ids)); + send(services.getMoteControlService().requestControl(ids)); } @Override @@ -744,7 +729,7 @@ final class MoteDatabase implements MoteManager { super(true); this.motes = motes; this.listener = listener; - send(services.getMoteControlService().releaseMotes(ids)); + send(services.getMoteControlService().releaseControl(ids)); } @Override diff --git a/axis/spi/src/main/java/remote/axis/spi/motedata/AxisMoteDataResponse.java b/axis/spi/src/main/java/remote/axis/spi/motedata/AxisMoteDataResponse.java index e649eb9..cf18f2f 100644 --- a/axis/spi/src/main/java/remote/axis/spi/motedata/AxisMoteDataResponse.java +++ b/axis/spi/src/main/java/remote/axis/spi/motedata/AxisMoteDataResponse.java @@ -81,6 +81,11 @@ class AxisMoteDataResponse implements MoteDataResponse { return getHeader(index).isVisible(); } + public boolean hasProperty(MoteDataProperty property) + { + return propertyIndex[property.ordinal()] != UNKNOWN_PROPERTY_INDEX; + } + public int getMoteSize() { return data.length > 0 ? data[0].length : 0; diff --git a/core/spi/src/main/java/remote/core/spi/motedata/MoteDataResponse.java b/core/spi/src/main/java/remote/core/spi/motedata/MoteDataResponse.java index 063fcac..023ea9a 100644 --- a/core/spi/src/main/java/remote/core/spi/motedata/MoteDataResponse.java +++ b/core/spi/src/main/java/remote/core/spi/motedata/MoteDataResponse.java @@ -18,6 +18,8 @@ public interface MoteDataResponse { boolean isHeaderVisible(int index); + boolean hasProperty(MoteDataProperty property); + int getMoteSize(); T getMoteValue(int index, MoteDataProperty property, T defaultValue); -- 2.11.4.GIT