Example #1
0
  /** Obsługa zapytań */
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    long start = System.currentTimeMillis();
    Document response = createDocument();
    Element root = response.createElement("response");
    response.appendChild(root);

    String action = req.getParameter("action");
    if (action == null) {

      root.appendChild(createTextNode(response, "status", "err"));
      root.appendChild(createTextNode(response, "message", "No action parameter defined"));

    } else {

      EventManager em = EventManager.getInstance();
      Event e = em.getByName(action);

      if (e == null) {
        root.appendChild(createTextNode(response, "status", "err"));
        root.appendChild(createTextNode(response, "message", "No such event (" + action + ")"));

      } else {
        root.appendChild(e.processEvent(response, req));
      }
    }
    root.appendChild(
        createTextNode(
            response, "Total_time", String.valueOf(System.currentTimeMillis() - start) + " ms"));
    XML2Writer(response, resp.getWriter());
    XML2Writer(response, new PrintWriter(System.out));
  }
Example #2
0
  /** Inicjalizacja servletu. Tutaj ładowane są wszystkie moduły oraz zdarzenia */
  @Override
  public void init() throws ServletException {
    Config.getConf().setWorkingDir(getServletContext().getRealPath("/"));
    EventManager evm = EventManager.getInstance();
    EventManager.autoloadEvents();
    // moduly
    ModuleGet mg = new ModuleGet();
    ModuleSet ms = new ModuleSet();
    Module m = new WodaModule(); // 0
    mg.put(m);
    ms.put(m);
    m = new RoletaModule(); // 1
    mg.put(m);
    ms.put(m);
    m = new BramaModule(); // 2
    mg.put(m);
    ms.put(m);
    m = new AlarmModule(); // 3
    mg.put(m);
    ms.put(m);
    m = new OgrodModule(); // 4
    mg.put(m);
    ms.put(m);
    m = new ModbusModule(); // 5
    mg.put(m);
    ms.put(m);

    Thread t =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                AlarmModule alarm = new AlarmModule();
                System.out.println("Załadowano.");
                boolean al = false;
                while (true) {
                  if (alarm.getValue(2) > 0) {
                    if (!al) {
                      al = true;
                      System.out.println("ALARM!!");
                      DBDevices dev = new DBDevices();
                      Vector<DatabaseObj> devices = dev.executeQuery();
                      List<String> ID = new ArrayList<String>();
                      for (DatabaseObj ob : devices) {
                        ob.read();
                        ID.add(ob.get("reg_id"));
                        System.out.println(ob.get("reg_id"));
                      }
                      Sender sender = new Sender(Config.getConf().get("GCM_DEV_KEY"));
                      Message message = new Message.Builder().addData("event", "ALARM").build();
                      String id =
                          "APA91bFw1nQZxq3DYGG1Vhwmz0ryR8UUUoR7GwttSEa9AHE_HzhARZWdgxwT9xxBM3TCusm0vpEKic0KcAb7urTYMfIpkmZ5Sx6M7L-nIuZQTDrzaRJkmhPZurXMT-aPAXfvwDdG6kiB";
                      try {
                        if (!ID.isEmpty()) {
                          MulticastResult r = sender.send(message, ID, 3000);
                          System.out.println(r.toString());
                        }

                      } catch (IOException e) {
                        e
                            .printStackTrace(); // To change body of catch statement use File |
                                                // Settings | File Templates.
                      }
                    }
                  } else {
                    al = false;
                  }
                  try {
                    Thread.sleep(1000);
                  } catch (InterruptedException e) {
                    e
                        .printStackTrace(); // To change body of catch statement use File | Settings
                                            // | File Templates.
                  }
                }
              }
            });

    t.start();

    evm.registerEvent(mg);
    evm.registerEvent(ms);

    try {
      Class.forName("com.mysql.jdbc.Driver").getClass();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }