@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);
      }
    }
  }
Пример #2
0
  private Point createFeedPoint(final User user) throws NimbitsException {

    final EntityName name =
        CommonFactoryLocator.getInstance().createName(Const.TEXT_DATA_FEED, EntityType.point);

    final Entity entity =
        EntityModelFactory.createEntity(
            name,
            "",
            EntityType.feed,
            ProtectionLevel.onlyConnection,
            user.getKey(),
            user.getKey(),
            UUID.randomUUID().toString());
    // final Entity r = EntityServiceFactory.getInstance().addUpdateEntity(user, entity);

    Point point = PointModelFactory.createPointModel(entity);

    final Point result = (Point) EntityServiceFactory.getInstance().addUpdateEntity(point);

    postToFeed(
        user,
        "A new data point has been created for your data feed. Your data feed is just "
            + "a data point. Points are capable of storing numbers, text, json and xml data. Nimbits uses "
            + "a single data point to drive this feed.",
        FeedType.info);
    return result;
  }
Пример #3
0
  private Point getFeedPoint(final User user) throws NimbitsException {
    final Point point;
    final Map<String, Entity> map =
        EntityServiceFactory.getInstance().getEntityMap(user, EntityType.feed, 1);

    if (map.isEmpty()) {
      point = createFeedPoint(user);
    } else {
      return (Point) map.values().iterator().next();
    }
    return point;
  }
  @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");
  }
 private static void disableCalc(User u, Trigger c) throws NimbitsException {
   c.setEnabled(false);
   EntityServiceFactory.getInstance().addUpdateEntity(u, c);
 }