Search in sources :

Example 61 with PointValueTime

use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.

the class PointValueEmporter method importRow.

/*
     * (non-Javadoc)
     * @see com.serotonin.m2m2.vo.emport.AbstractSheetEmporter#importRow(org.apache.poi.ss.usermodel.Row)
     */
@Override
protected void importRow(Row rowData) throws SpreadsheetException {
    int cellNum = 0;
    // Data Point XID
    Cell xidCell = rowData.getCell(cellNum++);
    if (xidCell == null)
        throw new SpreadsheetException(rowData.getRowNum(), "emport.error.xidRequired");
    if ((xidCell.getStringCellValue() == null) || (xidCell.getStringCellValue().isEmpty()))
        throw new SpreadsheetException("emport.error.xidRequired");
    // First Check to see if we already have a point
    String xid = xidCell.getStringCellValue();
    DataPointVO dp = voMap.get(xid);
    DataPointRT dpRt = 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 (dp == null) {
        dp = dataPointDao.getDataPoint(xid);
        if (dp == null)
            throw new SpreadsheetException(rowData.getRowNum(), "emport.error.missingPoint", xid);
        dpRt = Common.runtimeManager.getDataPoint(dp.getId());
        rtMap.put(xid, dpRt);
        voMap.put(xid, dp);
    }
    PointValueTime pvt;
    // Cell Device name (Not using Here)
    cellNum++;
    // Cell Point name (Not using Here)
    cellNum++;
    // Cell Time
    Date time = rowData.getCell(cellNum++).getDateCellValue();
    // delete/add column
    Cell modifyCell = rowData.getCell(7);
    boolean add = false;
    boolean delete = false;
    if (modifyCell != null) {
        String modification = (String) modifyCell.getStringCellValue();
        if (modification.equalsIgnoreCase("delete")) {
            delete = true;
        } else if (modification.equalsIgnoreCase("add")) {
            add = true;
        } else {
            throw new SpreadsheetException(rowData.getRowNum(), "emport.spreadsheet.modifyCellUnknown");
        }
    }
    // What do we do with the row
    if (delete) {
        if (time == null) {
            throw new SpreadsheetException(rowData.getRowNum(), "emport.error.deleteNew", "no timestamp, unable to delete");
        } else {
            try {
                this.rowsDeleted += Common.runtimeManager.purgeDataPointValue(dp.getId(), time.getTime());
            } catch (Exception e) {
                if (e instanceof DataIntegrityViolationException)
                    throw new SpreadsheetException(rowData.getRowNum(), "emport.error.unableToDeleteDueToConstraints");
                else
                    throw new SpreadsheetException(rowData.getRowNum(), "emport.error.unableToDelete", e.getMessage());
            }
        }
        // Done now
        return;
    } else if (add) {
        // Cell Value
        Cell cell;
        cell = rowData.getCell(cellNum++);
        // Create a data value
        DataValue dataValue;
        switch(dp.getPointLocator().getDataTypeId()) {
            case DataTypes.ALPHANUMERIC:
                dataValue = new AlphanumericValue(cell.getStringCellValue());
                break;
            case DataTypes.BINARY:
                switch(cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        dataValue = new BinaryValue(new Boolean(cell.getBooleanCellValue()));
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (cell.getNumericCellValue() == 0)
                            dataValue = new BinaryValue(new Boolean(false));
                        else
                            dataValue = new BinaryValue(new Boolean(true));
                        break;
                    case Cell.CELL_TYPE_STRING:
                        if (cell.getStringCellValue().equalsIgnoreCase("false"))
                            dataValue = new BinaryValue(new Boolean(false));
                        else
                            dataValue = new BinaryValue(new Boolean(true));
                        break;
                    default:
                        throw new SpreadsheetException(rowData.getRowNum(), "common.default", "Invalid cell type for extracting boolean");
                }
                break;
            case DataTypes.MULTISTATE:
                dataValue = new MultistateValue((int) cell.getNumericCellValue());
                break;
            case DataTypes.NUMERIC:
                dataValue = new NumericValue(cell.getNumericCellValue());
                break;
            default:
                throw new SpreadsheetException(rowData.getRowNum(), "emport.spreadsheet.unsupportedDataType", dp.getPointLocator().getDataTypeId());
        }
        // Cell Rendered Value (Not using yet)
        cellNum++;
        // Cell Annotation
        Cell annotationRow = rowData.getCell(cellNum++);
        if (annotationRow != null) {
            String annotation = annotationRow.getStringCellValue();
            // TODO These methods here do not allow updating the Annotation. We need to be a set point source for that to work
            TranslatableMessage sourceMessage = new TranslatableMessage("common.default", annotation);
            pvt = new AnnotatedPointValueTime(dataValue, time.getTime(), sourceMessage);
        } else {
            pvt = new PointValueTime(dataValue, time.getTime());
        }
        // Save to cache if running
        if (dpRt != null)
            dpRt.savePointValueDirectToCache(pvt, null, true, true);
        else {
            if (pointValueDao instanceof EnhancedPointValueDao) {
                DataSourceVO<?> ds = getDataSource(dp.getDataSourceId());
                ((EnhancedPointValueDao) pointValueDao).savePointValueAsync(dp, ds, pvt, null);
            } else {
                pointValueDao.savePointValueAsync(dp.getId(), pvt, null);
            }
        }
        // Increment our counter
        this.rowsAdded++;
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) SpreadsheetException(com.serotonin.m2m2.vo.emport.SpreadsheetException) Date(java.util.Date) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) SpreadsheetException(com.serotonin.m2m2.vo.emport.SpreadsheetException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EnhancedPointValueDao(com.serotonin.m2m2.db.dao.EnhancedPointValueDao) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) Cell(org.apache.poi.ss.usermodel.Cell)

Example 62 with PointValueTime

use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.

the class StateChangeCountDetectorRT method scheduleTimeoutImpl.

@Override
public void scheduleTimeoutImpl(long fireTime) {
    synchronized (pointValues) {
        // This call was scheduled to occur at the eventInactiveTime.
        // Strictly speaking, the fact this method was called implies that the detector is going from active to
        // inactive. However, it really doesn't hurt to do a bit of cleanup and checking, so what the heck...
        removeOldPointValues(fireTime);
        if (pointValues.size() >= vo.getChangeCount()) {
            // Something has gone wrong.
            StringBuilder sb = new StringBuilder();
            sb.append("I was supposed to go inactive, but there are still too many state changes in my list: ");
            sb.append("fireTime=").append(fireTime);
            sb.append(", list=[");
            for (PointValueTime pvt : pointValues) sb.append(pvt.getTime()).append(", ");
            sb.append("], durationMS=").append(getDurationMS());
            sb.append(", changeCount=").append(vo.getChangeCount());
            log.error(sb.toString(), new Exception());
        }
    }
    // Deactive the event.
    eventActive = false;
    returnToNormal(fireTime);
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 63 with PointValueTime

use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-core-public by infiniteautomation.

the class AnalogChangeDetectorRT method pruneValueList.

private void pruneValueList(long time) {
    long cutoff = time - durationMillis;
    boolean recomputeMinimum = false, recomputeMaximum = false;
    if (dirty) {
        Collections.sort(periodValues);
        latestTime = periodValues.get(periodValues.size() - 1).getTime();
        dirty = false;
    }
    while (periodValues.size() > 1) {
        PointValueTime pvt1 = periodValues.get(0);
        PointValueTime pvt2 = periodValues.get(1);
        if (pvt2.getTime() <= cutoff) {
            if (pvt1.getDoubleValue() >= max)
                recomputeMaximum = true;
            if (pvt1.getDoubleValue() <= min)
                recomputeMinimum = true;
            periodValues.remove(0);
        } else {
            break;
        }
    }
    recomputeMaximum |= periodValues.size() <= 1;
    recomputeMinimum |= periodValues.size() <= 1;
    if (recomputeMaximum || recomputeMinimum) {
        double newMax = Double.NEGATIVE_INFINITY;
        double newMin = Double.POSITIVE_INFINITY;
        Iterator<PointValueTime> iter = periodValues.iterator();
        while (iter.hasNext()) {
            PointValueTime pvt = iter.next();
            if (pvt.getDoubleValue() > newMax)
                newMax = pvt.getDoubleValue();
            if (pvt.getDoubleValue() < newMin)
                newMin = pvt.getDoubleValue();
        }
        if (recomputeMaximum)
            max = newMax;
        if (recomputeMinimum)
            min = newMin;
    }
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 64 with PointValueTime

use of com.serotonin.m2m2.rt.dataImage.PointValueTime 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 65 with PointValueTime

use of com.serotonin.m2m2.rt.dataImage.PointValueTime in project ma-modules-public by infiniteautomation.

the class EnvCanDataSourceRT method doPoll.

@Override
protected void doPoll(long time) {
    if (nextValueTime == -1) {
        // Determine when we should start from
        nextValueTime = System.currentTimeMillis();
        for (DataPointRT dp : dataPoints) {
            PointValueTime pvt = dp.getPointValue();
            if (pvt == null) {
                nextValueTime = 0;
                break;
            }
            if (nextValueTime > pvt.getTime())
                nextValueTime = pvt.getTime();
        }
        if (nextValueTime == 0)
            nextValueTime = vo.getDataStartTime();
        else
            nextValueTime += 1000 * 60 * 60;
    }
    long previousValueTime = nextValueTime;
    doPollImpl(time);
    if (nextValueTime == previousValueTime)
        raiseEvent(NO_DATA_RETRIEVED_EVENT, time, true, new TranslatableMessage("envcands.event.noTemperatureData"));
    else
        returnToNormal(NO_DATA_RETRIEVED_EVENT, time);
    while (nextValueTime != previousValueTime) {
        // Something was changed.
        DateTime prev = new DateTime(previousValueTime);
        DateTime now = new DateTime(System.currentTimeMillis());
        if (prev.getYear() < now.getYear() || prev.getMonthOfYear() < now.getMonthOfYear()) {
            previousValueTime = nextValueTime;
            doPollImpl(time);
        } else
            break;
    }
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DateTime(org.joda.time.DateTime)

Aggregations

PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)104 ArrayList (java.util.ArrayList)33 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)31 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)29 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)24 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)23 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)23 IOException (java.io.IOException)18 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)17 HashMap (java.util.HashMap)17 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)15 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)12 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)11 ScriptException (javax.script.ScriptException)10 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)9 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)9 LogStopWatch (com.serotonin.log.LogStopWatch)8 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)8 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8