Esempio n. 1
0
 public void processMessage(String msg) {
   if (msg.length() > 0 && msg.contains("/")) {
     String deviceName = msg.substring(0, msg.indexOf("/"));
     String cmd = msg.substring(msg.indexOf("/") + 1, msg.length());
     if (deviceName != null && cmd != null) {
       for (Device d : Kernel.getInstance().devices) {
         if (deviceName.equals("*") || d.getName().equals(deviceName)) {
           try {
             d.send(cmd);
           } catch (IOException ex) {
             Logger.getLogger(Manager.class.getName())
                 .log(
                     Level.SEVERE,
                     "Error sending Serial Message to {0}. {1}",
                     new Object[] {d.getName(), ex.getMessage()});
           }
         }
       }
     }
   }
 }
Esempio n. 2
0
 @Override
 public synchronized void run() {
   Collection<Device> devices = Kernel.getInstance().getDevices();
   if (devices == null) {
     return;
   }
   for (Device device : devices) {
     try {
       Logger.getLogger(Manager.class.getName())
           .log(
               Level.INFO,
               "Reading sensors from " + device.getName() + ", sending " + requestString);
       device.send("sensors");
       Kernel.delay(940);
       String s = device.receive();
       if (s != null && !s.equals("")) {
         // Logger.getLogger(SensorPublisher.class.getName()).log(Level.INFO,
         //        "Sending MQT Message " + s + " to QUEUE " + MQTTBaseService.MQTT_QUEUE + "/" +
         // device.getName());
         try {
           newSensorData(device.getName(), s);
         } catch (Exception e) {
         }
         sendMessage(s, MQTT_QUEUE_PUBLISH);
         Persistence.save(s, "sensors");
       }
     } catch (IOException ex) {
       try {
         device.close();
       } catch (IOException ex1) {
         Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex1);
       }
       Kernel.getInstance().devices.remove(device);
       Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
     } catch (MqttException ex) {
       Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
     }
   }
 }