use of com.serotonin.m2m2.i18n.TranslatableException in project ma-modules-public by infiniteautomation.
the class DataImportController method importCsv.
/**
* The file needs to be in the format:
*
* Data Point XID, Device Name, Point name, Time, Value, Rendered, Annotation, Modify(Not used yet)
*
* @param reader
* @param model
* @return
* @throws IOException
* @throws TranslatableException
*/
private void importCsv(CSVReader csvReader, Map<String, Object> model, Translations translations, List<String> errorMessages) throws IOException, TranslatableException {
DataPointDao dataPointDao = DataPointDao.instance;
PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
int rowErrors = 0;
int rowsImported = 0;
int rowsDeleted = 0;
// Basic validation of header
String[] nextLine = csvReader.readNext();
if (nextLine == null) {
errorMessages.add(new TranslatableMessage("dataImport.import.noData").translate(translations));
return;
}
if (nextLine.length != ExportCsvStreamer.columns) {
errorMessages.add(new TranslatableMessage("dataImport.import.invalidHeaders", nextLine.length, ExportCsvStreamer.columns).translate(translations));
return;
}
// Map of XIDs to non-running data points
Map<String, DataPointVO> voMap = new HashMap<String, DataPointVO>();
// Map of XIDs to running data points
Map<String, DataPointRT> rtMap = new HashMap<String, DataPointRT>();
// Read in all the rows
int row = 1;
String xid;
DataPointVO vo;
DataPointRT rt;
long time;
DataValue value;
PointValueTime pvt;
while ((nextLine = csvReader.readNext()) != null) {
if (nextLine.length != ExportCsvStreamer.columns) {
errorMessages.add(new TranslatableMessage("dataImport.import.invalidLength", row).translate(translations));
rowErrors++;
row++;
continue;
}
// Check XID
xid = nextLine[0];
if (StringUtils.isBlank(xid)) {
errorMessages.add(new TranslatableMessage("dataImport.import.badXid", xid, row).translate(translations));
rowErrors++;
row++;
continue;
}
// First Check to see if we already have a point
vo = voMap.get(xid);
rt = rtMap.get(xid);
// We will always have the vo in the map but the RT may be null if the point isn't running
if (vo == null) {
vo = dataPointDao.getDataPoint(xid);
if (vo == null) {
errorMessages.add(new TranslatableMessage("dataImport.import.xidNotFound", xid, row).translate(translations));
rowErrors++;
row++;
continue;
}
rt = Common.runtimeManager.getDataPoint(vo.getId());
rtMap.put(xid, rt);
voMap.put(xid, vo);
}
// Add or delete or nothing
String modify = nextLine[7];
if (StringUtils.equalsIgnoreCase("add", modify)) {
// Going to insert some data
time = ExportCsvStreamer.dtf.parseDateTime(nextLine[3]).getMillis();
value = DataValue.stringToValue(nextLine[4], vo.getPointLocator().getDataTypeId());
// Get Annotation
String annotation = nextLine[6];
if (annotation != null)
pvt = new AnnotatedPointValueTime(value, time, new TranslatableMessage("common.default", annotation));
else
pvt = new PointValueTime(value, time);
if (rt == null) {
// Insert Via DAO
pointValueDao.savePointValueAsync(vo.getId(), pvt, null);
} else {
// Insert Via RT
rt.savePointValueDirectToCache(pvt, null, true, true);
}
rowsImported++;
} else if (StringUtils.equalsIgnoreCase("delete", modify)) {
// Delete this entry
time = ExportCsvStreamer.dtf.parseDateTime(nextLine[3]).getMillis();
rowsDeleted += Common.runtimeManager.purgeDataPointValue(vo.getId(), time);
}
row++;
}
// Setup results
model.put("rowsImported", rowsImported);
model.put("rowsDeleted", rowsDeleted);
model.put("rowsWithErrors", rowErrors);
}
use of com.serotonin.m2m2.i18n.TranslatableException in project ma-modules-public by infiniteautomation.
the class EnvCanDataSourceRT method getData.
private String getData(String url, int timeoutSeconds, int retries) throws TranslatableException {
// Try to get the data.
String data;
while (true) {
HttpClient client = Common.getHttpClient(timeoutSeconds * 1000);
HttpGet request = null;
TranslatableMessage message;
try {
request = new HttpGet(url);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// data = HttpUtils4.readResponseBody(response);
data = readResponseBody(response);
break;
}
message = new TranslatableMessage("event.http.response", url, response.getStatusLine().getStatusCode());
} catch (Exception e) {
message = DataSourceRT.getExceptionMessage(e);
} finally {
request.reset();
}
if (retries <= 0)
throw new TranslatableException(message);
retries--;
// Take a little break instead of trying again immediately.
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// no op
}
}
return data;
}
use of com.serotonin.m2m2.i18n.TranslatableException in project ma-modules-public by infiniteautomation.
the class EnvCanDataSourceRT method doPollImpl.
private void doPollImpl(long runtime) {
DateTime dt = new DateTime(nextValueTime);
StringBuilder url = new StringBuilder();
url.append("http://climate.weather.gc.ca/climate_data/bulk_data_e.html?stationID=").append(vo.getStationId());
url.append("&Year=").append(dt.getYear());
url.append("&Month=").append(dt.getMonthOfYear() + 1);
url.append("&format=xml&timeframe=1");
String data;
try {
data = getData(url.toString(), 30, 2);
} catch (Exception e) {
TranslatableMessage lm;
if (e instanceof TranslatableException)
lm = ((TranslatableException) e).getTranslatableMessage();
else
lm = new TranslatableMessage("envcands.retrievalError", e.getMessage());
raiseEvent(DATA_RETRIEVAL_FAILURE_EVENT, runtime, true, lm);
return;
}
// If we made it this far, everything is good.
returnToNormal(DATA_RETRIEVAL_FAILURE_EVENT, runtime);
try {
Document xml = XmlUtilsTS.parse(data);
Element climateDataElement = xml.getDocumentElement();
for (Element stationDataElement : XmlUtilsTS.getChildElements(climateDataElement, "stationdata")) {
int year = XmlUtilsTS.getIntAttribute(stationDataElement, "year", -1);
int month = XmlUtilsTS.getIntAttribute(stationDataElement, "month", -1);
int day = XmlUtilsTS.getIntAttribute(stationDataElement, "day", -1);
int hour = XmlUtilsTS.getIntAttribute(stationDataElement, "hour", -1);
long time = new DateTime(year, month, day, hour, 0, 0, 0, DateTimeZone.UTC).getMillis();
time -= tzOffset;
double temp = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "temp", Double.NaN);
double dptemp = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "dptemp", Double.NaN);
double visibility = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "visibility", Double.NaN);
double relhum = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "relhum", Double.NaN);
double winddir = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "winddir", Double.NaN);
double windspd = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "windspd", Double.NaN);
double stnpress = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "stnpress", Double.NaN);
double humidex = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "humidex", Double.NaN);
double windchill = XmlUtilsTS.getChildElementTextAsDouble(stationDataElement, "windchill", Double.NaN);
String weather = XmlUtilsTS.getChildElementText(stationDataElement, "weather");
if (Double.isNaN(temp))
// If there is no temp value, ignore the record
continue;
// Update the next value time.
nextValueTime = time + 1000 * 60 * 60;
for (DataPointRT dp : dataPoints) {
PointValueTime pvt = dp.getPointValue();
if (pvt != null && pvt.getTime() >= time)
// This value is in the past. Ignore it.
continue;
EnvCanPointLocatorVO plvo = dp.getVO().getPointLocator();
if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.TEMP && !Double.isNaN(temp))
pvt = new PointValueTime(temp, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.DEW_POINT_TEMP && !Double.isNaN(dptemp))
pvt = new PointValueTime(dptemp, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.REL_HUM && !Double.isNaN(relhum))
pvt = new PointValueTime(relhum, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WIND_DIR && !Double.isNaN(winddir))
pvt = new PointValueTime(winddir, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WIND_SPEED && !Double.isNaN(windspd))
pvt = new PointValueTime(windspd, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.VISIBILITY && !Double.isNaN(visibility))
pvt = new PointValueTime(visibility, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.STN_PRESS && !Double.isNaN(stnpress))
pvt = new PointValueTime(stnpress, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.HUMIDEX && !Double.isNaN(humidex))
pvt = new PointValueTime(humidex, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WIND_CHILL && !Double.isNaN(windchill))
pvt = new PointValueTime(windchill, time);
else if (plvo.getAttributeId() == EnvCanPointLocatorVO.Attributes.WEATHER && weather != null)
pvt = new PointValueTime(weather, time);
else
continue;
dp.updatePointValue(pvt);
}
}
returnToNormal(PARSE_EXCEPTION_EVENT, runtime);
} catch (SAXException e) {
raiseEvent(PARSE_EXCEPTION_EVENT, runtime, true, DataSourceRT.getExceptionMessage(e));
}
}
use of com.serotonin.m2m2.i18n.TranslatableException in project ma-modules-public by infiniteautomation.
the class DataImportController method parseFile.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.controller.FileUploadController#parseFile(java.io.InputStream, java.util.Map, com.serotonin.m2m2.i18n.Translations, javax.servlet.http.HttpServletRequest)
*/
@Override
protected void parseFile(InputStream input, Map<String, Object> model, Translations translations, HttpServletRequest request) {
// Setup for error tracking
List<String> errorMessages = new ArrayList<String>();
model.put("errorMessages", errorMessages);
CSVReader csvReader = new CSVReader(new InputStreamReader(input));
try {
importCsv(csvReader, model, translations, errorMessages);
} catch (Exception e) {
if (e instanceof TranslatableException)
errorMessages.add(((TranslatableException) e).getTranslatableMessage().translate(translations));
else
errorMessages.add(e.getMessage());
} finally {
model.put("hasImportErrors", errorMessages.size() > 0);
try {
csvReader.close();
} catch (IOException e) {
errorMessages.add(e.getMessage());
}
}
}
Aggregations