Search in sources :

Example 1 with TranslatableException

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);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) HashMap(java.util.HashMap) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 2 with TranslatableException

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;
}
Also used : TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 3 with TranslatableException

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));
    }
}
Also used : TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) DateTime(org.joda.time.DateTime) TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 4 with TranslatableException

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());
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) IOException(java.io.IOException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Aggregations

TranslatableException (com.serotonin.m2m2.i18n.TranslatableException)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 IOException (java.io.IOException)3 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 SAXException (org.xml.sax.SAXException)2 CSVReader (au.com.bytecode.opencsv.CSVReader)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 DateTime (org.joda.time.DateTime)1