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 updateSystemPoint( final String pointName, final double value, final boolean incrementAsCounter, final PointType type) throws NimbitsException { EntityName name = CommonFactory.createName(pointName, EntityType.point); User admin = userService.getAdmin(); List<Entity> e = entityService.getEntityByName(admin, name, EntityType.point); Point p; if (e.isEmpty()) { String ownerKey = admin.getKey(); Entity ep = EntityModelFactory.createEntity( name, "", EntityType.point, ProtectionLevel.onlyMe, ownerKey, ownerKey, UUID.randomUUID().toString()); Point pm = PointModelFactory.createPointModel( ep, 0.0, EXPIRE, "", 0.0, false, false, false, 0, false, FilterType.none, 0.0, false, type, 0, false, 0.0); p = (Point) entityService.addUpdateEntity(admin, pm); } else { p = (Point) e.get(0); } Value vx; if (incrementAsCounter) { List<Value> c = valueService.getCurrentValue(p); double cd = !c.isEmpty() ? c.get(0).getDoubleValue() : 0.0; vx = ValueFactory.createValueModel(cd + value); } else { vx = ValueFactory.createValueModel(value); } valueService.recordValue(admin, p, vx); }
@Override public Value deserialize( final JsonElement jsonElement, final Type type, final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { Location location; JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement valueElement = jsonObject.get("d"); JsonElement dataElement = jsonObject.get("dx"); JsonElement latElement = jsonObject.get("lt"); JsonElement lngElement = jsonObject.get("lg"); JsonElement timestampElement = jsonObject.get("t"); String data = dataElement == null ? null : dataElement.getAsString(); Double lat = latElement == null || latElement.isJsonNull() ? null : latElement.getAsDouble(); Double lng = lngElement == null || lngElement.isJsonNull() ? null : lngElement.getAsDouble(); Double value = valueElement == null || valueElement.isJsonNull() ? null : valueElement.getAsDouble(); Long timestamp = timestampElement == null || timestampElement.isJsonNull() ? 0 : timestampElement.getAsLong(); if (lat != null && lng != null) { location = LocationFactory.createLocation(lat, lng); } else { location = LocationFactory.createEmptyLocation(); } Date time = timestamp > 0 ? new Date(timestamp) : new Date(); ValueData valueData; if (data != null && data.length() > 0) { valueData = ValueDataModel.getInstance(SimpleValue.getInstance(data)); } else { valueData = ValueDataModel.getEmptyInstance(); } return ValueFactory.createValueModel(location, value, time, valueData, AlertType.OK); }
@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"); }