/******************************************************************************* * This file is part of the Appliance Energy Detector, a free household appliance energy disaggregation intelligence engine and webapp. * * Copyright (C) 2011,2012 Taylor Raack <traack@raack.info> * * The Appliance Energy Detector is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * * The Appliance Energy Detector 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License along with the Appliance Energy Detector. If not, see <http://www.gnu.org/licenses/>. * * According to sec. 7 of the GNU Affero General Public License, version 3, the terms of the AGPL are supplemented with the following terms: * * If you modify this Program, or any covered work, by linking or combining it with any of the following programs (or modified versions of those libraries), containing parts covered by the terms of those libraries licenses, the licensors of this Program grant you additional permission to convey the resulting work: * * Javabeans(TM) Activation Framework 1.1 (activation) - Common Development and Distribution License Version 1.0 * AspectJ 1.6.9 (aspectjrt and aspectjweaver) - Eclipse Public License 1.0 * EMMA 2.0.5312 (emma and emma_ant) - Common Public License Version 1.0 * JAXB Project Libraries 2.2.2 (jaxb-api, jaxb-impl, jaxb-xjc) - Common Development and Distribution License Version 1.0 * Java Standard Template Library 1.2 (jstl) - Common Development and Distribution License Version 1.0 * Java Servlet Pages API 2.1 (jsp-api) - Common Development and Distribution License Version 1.0 * Java Transaction API 1.1 (jta) - Common Development and Distribution License Version 1.0 * JavaMail(TM) 1.4.1 (mail) - Common Development and Distribution License Version 1.0 * XML Pull Parser 3 (xpp3) - Indiana University Extreme! Lab Software License Version 1.1.1 * * The interactive user interface of the software display an attribution notice containing the phrase "Appliance Energy Detector". Interactive user interfaces of unmodified and modified versions must display Appropriate Legal Notices according to sec. 5 of the GNU Affero General Public License, version 3, when you propagate an unmodified or modified version of the Program. In accordance with sec. 7 b) of the GNU Affero General Public License, version 3, these Appropriate Legal Notices must prominently display either a) "Initial Development by <a href='http://www.linkedin.com/in/taylorraack'>Taylor Raack</a>" if displayed in a web browser or b) "Initial Development by Taylor Raack (http://www.linkedin.com/in/taylorraack)" if displayed otherwise. ******************************************************************************/ package info.raack.appliancelabeler.model.energymonitor; import info.raack.appliancelabeler.security.OAuthData; import info.raack.appliancelabeler.service.DataService.DataFrequency; import info.raack.appliancelabeler.service.OAuthRequestProcessor; import info.raack.appliancelabeler.util.ResponseHandler; import java.io.ByteArrayInputStream; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.oauth.consumer.OAuthSecurityContext; import edu.cmu.hcii.stepgreen.data.base.GatewayTime; import edu.cmu.hcii.stepgreen.data.base.RawTedDataType; import edu.cmu.hcii.stepgreen.data.base.Ted5000LiveData; import edu.cmu.hcii.stepgreen.data.base.Ted5000MtuSecondData; import edu.cmu.hcii.stepgreen.data.ted.SecondDataParser; import edu.cmu.hcii.stepgreen.data.ted.data.SecondData; /** * All data from aggregated teds feed is pulled, with the ted data collector's stepgreeen system client oauth credentials * * @author Taylor Raack * */ public class Ted5000Monitor extends EnergyMonitor { private Logger logger = LoggerFactory.getLogger(Ted5000Monitor.class); private String tedId; private String mtuId; private String monitorId = null; private String stepgreenBasehost; private SecondDataParser secondDataParser = new SecondDataParser(); private String tedDataURLPattern = "http://%s/api/v1/users/%s/ted5000s/%s/mtus/%s/%s?offset=%d&count=%d&direction=asc"; private String tedLiveDataURLPattern = "http://%s/api/v1/users/%s/ted5000s/%s/live_data.xml"; public Ted5000Monitor(int id, String userId, String tedId, String mtuId, String stepgreenBasehost) { super(id, userId); if(tedId == null || mtuId == null || stepgreenBasehost == null) { throw new IllegalArgumentException("Userid, ted, stepgreenBasehost nor mtu may be null"); } this.tedId = tedId; this.mtuId = mtuId; this.stepgreenBasehost = stepgreenBasehost; } public String getMonitorId() { if(monitorId == null) { monitorId = tedId + "_" + mtuId; } return monitorId; } public String getTedId() { return tedId; } public String getMtuId() { return mtuId; } public String getType() { return "ted5000"; } public List<SecondData> getSecondData(OAuthRequestProcessor oAuthRequestProcessor, OAuthSecurityContext oAuthContext, long offset, long datapointsBack) { String requestURI = String.format(tedDataURLPattern, stepgreenBasehost, getUserId(), tedId, mtuId, DataFrequency.SECONDS.toString().toLowerCase(), offset, datapointsBack); List<SecondData> data = null; data = oAuthRequestProcessor.processRequest(requestURI, oAuthContext, new ResponseHandler<Ted5000MtuSecondData,List<SecondData>>() { public List<SecondData> extractValue(Ted5000MtuSecondData teddata) { StringBuilder builder = new StringBuilder(); logger.debug("Got seconds object from HTTP parser"); Ted5000MtuSecondData secondData = (Ted5000MtuSecondData)teddata; List<RawTedDataType> seconds = secondData.getSecond(); // TODO - use sax parsing rather than DOM in case this data is big logger.debug("Got " + seconds.size() + " + datapoints for " + toString()); for(RawTedDataType rawData : seconds) { builder.append(rawData.getRaw()).append("\n"); } List<SecondData> data = secondDataParser.parse(new ByteArrayInputStream(builder.toString().getBytes())); return data; } }); return data; } public String toString() { return "TED5000 (" + getMonitorId() + ")"; } /** * Get the current time as perceived by the TED5000 by querying it's live data feed. This is important as the device may not have it's clock synchronized with any other time source. * * @param oAuthData a valid OAuthData object holding a request processor and security context * * @return the current perceived date of the TED5000 */ public Date getCurrentMonitorTime(OAuthData oAuthData) { String requestURI = String.format(tedLiveDataURLPattern, stepgreenBasehost, getUserId(), tedId); return oAuthData.getRequestProcessor().processRequest(requestURI, oAuthData.getSecurityContext(), new ResponseHandler<Ted5000LiveData,Date>() { public Date extractValue(Ted5000LiveData teddata) { GatewayTime gatewayTime = teddata.getGatewayTime(); Calendar cal = new GregorianCalendar(2000 + gatewayTime.getYear(), gatewayTime.getMonth() - 1, gatewayTime.getDay(), gatewayTime.getHour(), gatewayTime.getMinute(), gatewayTime.getSecond()); return cal.getTime(); } }); } }