public static PointDetailsState getPointData() { // Get the point from the user's session. It should have been set by the controller. HttpServletRequest request = WebContextFactory.get().getHttpServletRequest(); User user = Common.getUser(request); DataPointVO pointVO = user.getEditPoint(); // Create the watch list state. Map<String, Object> model = new HashMap<String, Object>(); // Get the data point status from the data image. DataPointRT pointRT = Common.runtimeManager.getDataPoint(pointVO.getId()); PointDetailsState state = new PointDetailsState(); state.setId(Integer.toString(pointVO.getId())); PointValueTime pointValue = prepareBasePointState(Integer.toString(pointVO.getId()), state, pointVO, pointRT, model); setPrettyText(request, state, pointVO, model, pointValue); setChange(pointVO, state, pointRT, request, model, user); setEvents(pointVO, user, model); setMessages(state, request, "dataPointMessages.jsp", model); return state; }
@DwrPermission(user = true) public ProcessResult getFlipbookData(int limit) { HttpServletRequest request = WebContextFactory.get().getHttpServletRequest(); DataPointVO vo = Common.getUser(request).getEditPoint(); PointValueFacade facade = new PointValueFacade(vo.getId()); List<PointValueTime> values = facade.getLatestPointValues(limit); Collections.reverse(values); List<ImageValueBean> result = new ArrayList<ImageValueBean>(); for (PointValueTime pvt : values) { ImageValue imageValue = (ImageValue) pvt.getValue(); String uri = ImageValueServlet.servletPath + ImageValueServlet.historyPrefix + pvt.getTime() + "_" + vo.getId() + "." + imageValue.getTypeExtension(); result.add(new ImageValueBean(Functions.getTime(pvt), uri)); } ProcessResult response = new ProcessResult(); response.addData("images", result); addAsof(response); return response; }
@Override public void terminate() { terminateIntervalLogging(); if (detectors != null) { for (PointEventDetectorRT pedRT : detectors) { Common.runtimeManager.removeDataPointListener(vo.getId(), pedRT); pedRT.terminate(); } } Common.eventManager.cancelEventsForDataPoint(vo.getId()); }
// // / // / Listeners // / // private void fireEvents( PointValueTime oldValue, PointValueTime newValue, boolean set, boolean backdate) { DataPointListener l = Common.runtimeManager.getDataPointListeners(vo.getId()); if (l != null) Common.backgroundProcessing.addWorkItem( new EventNotifyWorkItem(l, oldValue, newValue, set, backdate)); }
// // // Data points // public void saveDataPoint(DataPointVO point) { stopDataPoint(point.getId()); // Since the point's data type may have changed, we must ensure that the other attrtibutes are // still ok with // it. int dataType = point.getPointLocator().getDataTypeId(); // Chart renderer if (point.getChartRenderer() != null && !point.getChartRenderer().getDef().supports(dataType)) // Return to a default renderer point.setChartRenderer(null); // Text renderer if (point.getTextRenderer() != null && !point.getTextRenderer().getDef().supports(dataType)) // Return to a default renderer point.defaultTextRenderer(); // Event detectors Iterator<PointEventDetectorVO> peds = point.getEventDetectors().iterator(); while (peds.hasNext()) { PointEventDetectorVO ped = peds.next(); if (!ped.getDef().supports(dataType)) // Remove the detector. peds.remove(); } new DataPointDao().saveDataPoint(point); if (point.isEnabled()) startDataPoint(point); }
@DwrPermission(user = true) public ProcessResult getHistoryTableData(int limit) { DataPointVO pointVO = Common.getUser().getEditPoint(); PointValueFacade facade = new PointValueFacade(pointVO.getId()); List<PointValueTime> rawData = facade.getLatestPointValues(limit); List<RenderedPointValueTime> renderedData = new ArrayList<RenderedPointValueTime>(rawData.size()); for (PointValueTime pvt : rawData) { RenderedPointValueTime rpvt = new RenderedPointValueTime(); rpvt.setValue(Functions.getHtmlText(pointVO, pvt)); rpvt.setTime(Functions.getTime(pvt)); if (pvt.isAnnotated()) { AnnotatedPointValueTime apvt = (AnnotatedPointValueTime) pvt; rpvt.setAnnotation(apvt.getAnnotation(getTranslations())); } renderedData.add(rpvt); } ProcessResult response = new ProcessResult(); response.addData("history", renderedData); addAsof(response); return response; }
// // // Single value // @Override public PointValueTime getPointValueBefore(long time) { for (PointValueTime pvt : valueCache.getCacheContents()) { if (pvt.getTime() < time) return pvt; } return Common.databaseProxy.newPointValueDao().getPointValueBefore(vo.getId(), time); }
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException { DataPointDao dataPointDao = new DataPointDao(); String xid = jsonObject.getString("dataPointId"); if (xid == null) throw new TranslatableJsonException("emport.error.publishedPoint.missing", "dataPointId"); DataPointVO vo = dataPointDao.getDataPoint(xid); if (vo == null) throw new TranslatableJsonException("emport.error.missingPoint", xid); dataPointId = vo.getId(); }
@Override public PointValueTime getPointValueAfter(long time) { List<PointValueTime> pvts = valueCache.getCacheContents(); for (int i = pvts.size() - 1; i >= 0; i--) { PointValueTime pvt = pvts.get(i); if (pvt.getTime() >= time) return pvt; } return Common.databaseProxy.newPointValueDao().getPointValueAfter(vo.getId(), time); }
private void updatePoints() { // Get the points List<DataPointVO> dps = query( "select dp.id, dp.xid, dp.dataSourceId, dp.data, ds.name, " // + "ds.xid, ds.dataSourceType " // + "from dataPoints dp join dataSources ds on ds.id = dp.dataSourceId ", new DataPointRowMapper()); // Resave for (DataPointVO dp : dps) ejt.update( "update dataPoints set xid=?, name=?, deviceName=?, enabled=?, loggingType=?, " // + "intervalLoggingPeriodType=?, intervalLoggingPeriod=?, intervalLoggingType=?, " // + "tolerance=?, purgeType=?, purgePeriod=?, defaultCacheSize=?, discardExtremeValues=?, " // + "engineeringUnits=?, data=? where id=?", // new Object[] { dp.getXid(), dp.getName(), dp.getDeviceName(), boolToChar(dp.isEnabled()), dp.getLoggingType(), dp.getIntervalLoggingPeriodType(), dp.getIntervalLoggingPeriod(), dp.getIntervalLoggingType(), dp.getTolerance(), dp.getPurgeType(), dp.getPurgePeriod(), dp.getDefaultCacheSize(), boolToChar(dp.isDiscardExtremeValues()), dp.getEngineeringUnits(), SerializationHelper.writeObject(dp), dp.getId() }, // new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.CHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.CHAR, Types.INTEGER, Types.BLOB, Types.INTEGER }); }
@Override public List<PointValueTime> getPointValuesBetween(long from, long to) { List<PointValueTime> result = Common.databaseProxy.newPointValueDao().getPointValuesBetween(vo.getId(), from, to); for (PointValueTime pvt : valueCache.getCacheContents()) { if (pvt.getTime() >= from && pvt.getTime() < to) { int index = Collections.binarySearch(result, pvt, pvtTimeComparator); if (index < 0) result.add(-index - 1, pvt); } } return result; }
// // // Lifecycle // @Override public void initialize() { // Get the latest value for the point from the database. pointValue = valueCache.getLatestPointValue(); // Set the tolerance origin if this is a numeric if (pointValue != null && pointValue.getValue() instanceof NumericValue) toleranceOrigin = pointValue.getDoubleValue(); // Add point event listeners for (PointEventDetectorVO ped : vo.getEventDetectors()) { if (detectors == null) detectors = new ArrayList<PointEventDetectorRT>(); PointEventDetectorRT pedRT = ped.createRuntime(); detectors.add(pedRT); pedRT.initialize(); Common.runtimeManager.addDataPointListener(vo.getId(), pedRT); } initializeIntervalLogging(); }
private void startDataPoint(DataPointVO vo) { synchronized (dataPoints) { Assert.isTrue(vo.isEnabled()); // Only add the data point if its data source is enabled. DataSourceRT ds = getRunningDataSource(vo.getDataSourceId()); if (ds != null) { // Change the VO into a data point implementation. DataPointRT dataPoint = new DataPointRT(vo, vo.getPointLocator().createRuntime()); // Add/update it in the data image. dataPoints.put(dataPoint.getId(), dataPoint); // Initialize it. dataPoint.initialize(); DataPointListener l = getDataPointListeners(vo.getId()); if (l != null) l.pointInitialized(); // Add/update it in the data source. ds.addDataPoint(dataPoint); } } }
@Override protected void createStateImpl( RuntimeManager rtm, HttpServletRequest request, JspComponentState state) { String value; DataPointRT dataPointRT = rtm.getDataPoint(dataPointVO.getId()); if (dataPointRT == null) value = disabledValue; else { PointValueTime pvt = dataPointRT.getPointValue(); if (pvt != null && pvt.getValue() instanceof ImageValue) { // Text renderers don't help here. Create a thumbnail. Map<String, Object> model = new HashMap<String, Object>(); model.put("point", dataPointVO); model.put("pointValue", pvt); value = BaseDwr.generateContent(request, "imageValueThumbnail.jsp", model); } else { int hint = raw ? TextRenderer.HINT_RAW : TextRenderer.HINT_FULL; value = dataPointVO.getTextRenderer().getText(pvt, hint); if (pvt != null && time) state.setTime(pvt.getTime()); } } state.setValue(value); }
public static void jsonReadVarContext(JsonObject json, List<IntStringPair> context) throws JsonException { JsonArray jsonContext = json.getJsonArray("context"); if (jsonContext != null) { context.clear(); DataPointDao dataPointDao = new DataPointDao(); for (JsonValue jv : jsonContext) { JsonObject jo = jv.toJsonObject(); String xid = jo.getString("dataPointXid"); if (xid == null) throw new TranslatableJsonException("emport.error.meta.missing", "dataPointXid"); DataPointVO dp = dataPointDao.getDataPoint(xid); if (dp == null) throw new TranslatableJsonException("emport.error.missingPoint", xid); String var = jo.getString("varName"); if (var == null) throw new TranslatableJsonException("emport.error.meta.missing", "varName"); context.add(new IntStringPair(dp.getId(), var)); } } }
/** * To allow simulation of points using a timer implementation * * @param vo * @param pointLocator * @param timer */ public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator, AbstractTimer timer) { this.vo = vo; this.pointLocator = pointLocator; valueCache = new PointValueCache(vo.getId(), vo.getDefaultCacheSize()); this.timer = timer; }
public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator) { this.vo = vo; this.pointLocator = pointLocator; valueCache = new PointValueCache(vo.getId(), vo.getDefaultCacheSize()); }
public void deleteDataPoint(DataPointVO point) { if (point.isEnabled()) stopDataPoint(point.getId()); new DataPointDao().deleteDataPoint(point.getId()); Common.eventManager.cancelEventsForDataPoint(point.getId()); }
// // / // / Properties // / // public int getId() { return vo.getId(); }
private void savePointValue( PointValueTime newValue, SetPointSource source, boolean async, boolean saveToDatabase) { // Null values are not very nice, and since they don't have a specific meaning they are hereby // ignored. if (newValue == null) return; // Check the data type of the value against that of the locator, just for fun. int valueDataType = DataTypes.getDataType(newValue.getValue()); if (valueDataType != DataTypes.UNKNOWN && valueDataType != vo.getPointLocator().getDataTypeId()) // This should never happen, but if it does it can have serious downstream consequences. Also, // we need // to know how it happened, and the stack trace here provides the best information. throw new ShouldNeverHappenException( "Data type mismatch between new value and point locator: newValue=" + DataTypes.getDataType(newValue.getValue()) + ", locator=" + vo.getPointLocator().getDataTypeId()); // Check if this value qualifies for discardation. if (vo.isDiscardExtremeValues() && DataTypes.getDataType(newValue.getValue()) == DataTypes.NUMERIC) { double newd = newValue.getDoubleValue(); if (newd < vo.getDiscardLowLimit() || newd > vo.getDiscardHighLimit()) // Discard the value return; } if (newValue.getTime() > System.currentTimeMillis() + SystemSettingsDao.getFutureDateLimit()) { // Too far future dated. Toss it. But log a message first. LOG.warn( "Future dated value detected: pointId=" + vo.getId() + ", value=" + newValue.getStringValue() + ", type=" + vo.getPointLocator().getDataTypeId() + ", ts=" + newValue.getTime(), new Exception()); return; } boolean backdated = pointValue != null && newValue.getTime() < pointValue.getTime(); // Determine whether the new value qualifies for logging. boolean logValue; // ... or even saving in the cache. boolean saveValue = true; switch (vo.getLoggingType()) { case DataPointVO.LoggingTypes.ON_CHANGE: if (pointValue == null) logValue = true; else if (backdated) // Backdated. Ignore it logValue = false; else { if (newValue.getValue() instanceof NumericValue) { // Get the new double double newd = newValue.getDoubleValue(); // See if the new value is outside of the tolerance. double diff = toleranceOrigin - newd; if (diff < 0) diff = -diff; if (diff > vo.getTolerance()) { toleranceOrigin = newd; logValue = true; } else logValue = false; } else logValue = !ObjectUtils.equals(newValue.getValue(), pointValue.getValue()); } saveValue = logValue; break; case DataPointVO.LoggingTypes.ALL: logValue = true; break; case DataPointVO.LoggingTypes.ON_TS_CHANGE: if (pointValue == null) logValue = true; else if (backdated) // Backdated. Ignore it logValue = false; else logValue = newValue.getTime() != pointValue.getTime(); saveValue = logValue; break; case DataPointVO.LoggingTypes.INTERVAL: if (!backdated) intervalSave(newValue); default: logValue = false; } if (!saveToDatabase) logValue = false; if (saveValue) valueCache.savePointValue(newValue, source, logValue, async); // add annotation to newValue before firing events so event detectors can // fetch the annotation if (source != null) { newValue = new AnnotatedPointValueTime( newValue.getValue(), newValue.getTime(), source.getSetPointSourceMessage()); } // Ignore historical values. if (pointValue == null || newValue.getTime() >= pointValue.getTime()) { PointValueTime oldValue = pointValue; pointValue = newValue; fireEvents(oldValue, newValue, source != null, false); } else fireEvents(null, newValue, false, true); }