If you think the Android project Go2-Rennes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
* //fromwww.java2s.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/package fr.gotorennes.remote;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import fr.gotorennes.domain.Equipement;
import fr.gotorennes.domain.EquipementStatus;
publicclass EquipementStatusService extends RemoteService<EquipementStatus> {
publicstaticfinal String COMMAND = "getequipmentsstatus";
publicstaticfinal String NODE = "equipment";
privatestatic EquipementStatusService instance;
private EquipementStatusService(Context context) {
super();
}
protected String getVersion() {
return"2.0";
}
publicstaticsynchronized EquipementStatusService getInstance(Context context) {
if (instance == null) {
instance = new EquipementStatusService(context);
}
return instance;
}
privatelong lastUpdate = 0;
private Map<String, Boolean> cache = null;
public Map<String, Boolean> getEquipementStatus() {
if (cache == null || System.currentTimeMillis() - lastUpdate > 300000) {
List<EquipementStatus> result = loadList(COMMAND, NODE);
if (result != null && !result.isEmpty()) {
cache = new HashMap<String, Boolean>();
for(EquipementStatus status : result) {
cache.put(status.id, status.status);
}
lastUpdate = System.currentTimeMillis();
}
}
return cache;
}
publicboolean getEquipementStatus(Equipement equipement) {
Map<String, Boolean> status = getEquipementStatus();
if (status == null) {
return true;
}
return status.get(equipement.id);
}
@Override
protected EquipementStatus populate(JSONObject jsonObject) throws JSONException {
EquipementStatus equipementStatus = new EquipementStatus();
equipementStatus.id = jsonObject.getString("id");
equipementStatus.status = "1".equals(jsonObject.getString("state"));
return equipementStatus;
}
}