protected static boolean checkIdle(final Point p) throws NimbitsException { final Calendar c = Calendar.getInstance(); c.add(Calendar.SECOND, p.getIdleSeconds() * -1); boolean retVal = false; final List<Entity> result = EntityServiceFactory.getInstance() .getEntityByKey( UserServiceFactory.getServerInstance().getAdmin(), p.getOwner(), EntityType.user); if (!result.isEmpty()) { final User u = (User) result.get(0); final List<Value> v = ValueServiceFactory.getInstance().getCurrentValue(p); if (p.getIdleSeconds() > 0 && !v.isEmpty() && v.get(0).getTimestamp().getTime() <= c.getTimeInMillis() && !p.getIdleAlarmSent()) { p.setIdleAlarmSent(true); EntityServiceFactory.getInstance().addUpdateEntity(u, p); // PointServiceFactory.getInstance().updatePoint(u, p); final Value va = ValueFactory.createValueModel(v.get(0), AlertType.IdleAlert); SubscriptionServiceFactory.getInstance().processSubscriptions(u, p, va); retVal = true; } } return retVal; }
@Override public void processCalculations(final User u, final Entity point, final Value value) throws NimbitsException { final List<Entity> calculations = EntityServiceFactory.getInstance().getEntityByTrigger(u, point, EntityType.calculation); for (final Entity entity : calculations) { Calculation c = (Calculation) entity; try { final List<Entity> target = EntityServiceFactory.getInstance().getEntityByKey(u, c.getTarget(), EntityType.point); if (target.isEmpty()) { log.severe("Point target was null " + c.getTarget()); log.severe(c.getFormula()); log.severe("trigger: " + c.getTrigger()); disableCalc(u, c); } else { log.info("Solving calc" + c.getFormula()); final Value result = solveEquation(u, c); log.info("result" + result); ValueServiceFactory.getInstance().recordValue(u, target.get(0), result); } } catch (NimbitsException e1) { LogHelper.logException(this.getClass(), e1); disableCalc(u, c); } } }
protected static int processGet() throws NimbitsException { final List<Entity> points = EntityServiceFactory.getInstance().getIdleEntities(); log.info("Processing " + points.size() + " potentially idle points"); for (final Entity p : points) { try { checkIdle((Point) p); } catch (NimbitsException e) { LogHelper.logException(IdlePointCron.class, e); } } return points.size(); }
@Override public Value solveEquation(final User user, final Calculation calculation) throws NimbitsException { final MathEvaluator m = new MathEvaluatorImpl(calculation.getFormula()); log.info(calculation.getFormula()); if (!Utils.isEmptyString(calculation.getX()) && calculation.getFormula().contains("x")) { // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getX()); final Entity p = EntityServiceFactory.getInstance() .getEntityByKey(user, calculation.getX(), EntityType.point) .get(0); if (p != null) { log.info("calc has an x car and i found " + p.getName()); final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p); final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue(); m.addVariable("x", d); } else { log.severe("calc has an x car and x not found"); } } if (!Utils.isEmptyString(calculation.getY()) && calculation.getFormula().contains("y")) { final Entity p = EntityServiceFactory.getInstance() .getEntityByKey(user, calculation.getY(), EntityType.point) .get(0); // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getY()); if (p != null) { final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p); final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue(); m.addVariable("y", d); } } if (!Utils.isEmptyString(calculation.getZ()) && calculation.getFormula().contains("z")) { final Entity p = EntityServiceFactory.getInstance() .getEntityByKey(user, calculation.getZ(), EntityType.point) .get(0); // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getZ()); if (p != null) { final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p); final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue(); m.addVariable("z", d); } } final Double retVal = m.getValue(); if (retVal == null) { throw new NimbitsException("Formula returned a null value: " + calculation.getFormula()); } return ValueFactory.createValueModel(retVal, "CV"); }