예제 #1
0
  private void updateExistingProfilesForPastEventCondition(
      Condition eventCondition, Condition parentCondition) {
    long t = System.currentTimeMillis();
    List<Condition> l = new ArrayList<Condition>();
    Condition andCondition = new Condition();
    andCondition.setConditionType(definitionsService.getConditionType("booleanCondition"));
    andCondition.setParameter("operator", "and");
    andCondition.setParameter("subConditions", l);

    l.add(eventCondition);

    Integer numberOfDays = (Integer) parentCondition.getParameter("numberOfDays");
    if (numberOfDays != null) {
      Condition numberOfDaysCondition = new Condition();
      numberOfDaysCondition.setConditionType(
          definitionsService.getConditionType("sessionPropertyCondition"));
      numberOfDaysCondition.setParameter("propertyName", "timeStamp");
      numberOfDaysCondition.setParameter("comparisonOperator", "greaterThan");
      numberOfDaysCondition.setParameter("propertyValue", "now-" + numberOfDays + "d");
      l.add(numberOfDaysCondition);
    }
    String propertyKey = (String) parentCondition.getParameter("generatedPropertyKey");
    Map<String, Long> res =
        persistenceService.aggregateQuery(
            andCondition, new TermsAggregate("profileId"), Event.ITEM_TYPE);
    for (Map.Entry<String, Long> entry : res.entrySet()) {
      if (!entry.getKey().startsWith("_")) {
        Map<String, Object> p = new HashMap<>();
        p.put(propertyKey, entry.getValue());
        Map<String, Object> p2 = new HashMap<>();
        p2.put("pastEvents", p);
        try {
          persistenceService.update(entry.getKey(), null, Profile.class, "systemProperties", p2);
        } catch (Exception e) {
          logger.error(e.getMessage(), e);
        }
      }
    }

    logger.info("Profiles past condition updated in {}", System.currentTimeMillis() - t);
  }
  public void logController(String topic, MqttMessage mqttMessage) {
    // get deviceId
    String[] split = topic.split("log/");
    String deviceId = split[1];

    // parse log
    try {
      JsonReader reader = Json.createReader(new StringReader(mqttMessage.toString()));
      JsonObject jsonMessage = reader.readObject();
      String logType = jsonMessage.getString("type");
      switch (logType) {
        case LOG_NORMAL:
          int temperature = 0;
          int timer = jsonMessage.getInt("time");
          try {
            temperature = jsonMessage.getInt("temperature");
          } catch (Exception se1) {
            // @todo: warning
            break;
          }
          JsonString subDeviceId = jsonMessage.getJsonString("subId");
          if (subDeviceId == null) sessionHandle.drawTemperature(deviceId, timer, temperature);
          else sessionHandle.drawTemperature(deviceId, timer, temperature, subDeviceId.toString());
          break;
        case LOG_WARNING:
          timer = jsonMessage.getInt("time");
          subDeviceId = jsonMessage.getJsonString("subId");
          int subdeviceNo = jsonMessage.getInt("number");
          String deviceState = jsonMessage.getString("value");
          String msg;
          switch (subdeviceNo) {
            case 1:
              msg = ("0".equals(deviceState)) ? "Button release" : "Button clicked";
              break;
            case 2:
              msg = ("0".equals(deviceState)) ? "Cửa cuốn được cuộn lên" : "Đang thả cửa cuốn";
              break;
            default:
              msg = DEFAULT_ALERT_MSG;
              break;
          }
          if (subDeviceId == null) sessionHandle.doAlert(deviceId, timer, msg);
          else sessionHandle.doAlert(deviceId, timer, msg, subDeviceId.toString());
          break;
        case LOG_CONTROL:
          String state = jsonMessage.getString("light");
          subdeviceNo = jsonMessage.getInt("no");
          sessionHandle.updateLightState(deviceId, subdeviceNo, state);
          break;
        case LOG_ACK:
          subdeviceNo = jsonMessage.getInt("number");
          deviceState = jsonMessage.getString("value");
          state = ("0".equals(deviceState)) ? "off" : "on";
          sessionHandle.ackLightState(deviceId, subdeviceNo, state);
          break;
        case LOG_DEVICE_STATUS:
          JsonArray lstDevice = jsonMessage.getJsonArray("status");
          System.out.println("Update status:");
          for (int i = 0; i < lstDevice.size(); i++) {
            JsonObject device = (JsonObject) lstDevice.get(i);
            System.out.println(device.toString());
            subdeviceNo = device.getInt("no");
            int intState = device.getInt("value");
            state = (intState == 0) ? "off" : "on";
            sessionHandle.updateLightState(deviceId, subdeviceNo, state);
          }
          sessionHandle.finishUpdateDeviceStatus();
          break;
        default:
      }

    } catch (Exception ex) {
      System.out.println("Parse error");
      System.out.println(topic);
      System.out.println(mqttMessage);
      System.out.println(ex.getMessage());
    }
  }