コード例 #1
0
  @Override
  public void sendMessage(IridiumMessage msg) throws Exception {
    VehicleType vt = VehiclesHolder.getVehicleWithImc(new ImcId16(msg.getDestination()));
    if (vt == null) {
      throw new Exception("Cannot send message to an unknown destination");
    }
    IridiumArgs args = (IridiumArgs) vt.getProtocolsArgs().get("iridium");
    if (askRockBlockPassword || rockBlockPassword == null || rockBlockUsername == null) {
      Pair<String, String> credentials =
          GuiUtils.askCredentials(
              ConfigFetch.getSuperParentFrame(),
              "Enter RockBlock Credentials",
              getRockBlockUsername(),
              getRockBlockPassword());
      if (credentials == null) return;
      setRockBlockUsername(credentials.first());
      setRockBlockPassword(credentials.second());
      PluginUtils.saveProperties("conf/rockblock.props", this);
      askRockBlockPassword = false;
    }

    String result =
        sendToRockBlockHttp(
            args.getImei(), getRockBlockUsername(), getRockBlockPassword(), msg.serialize());

    if (result != null) {
      if (!result.split(",")[0].equals("OK")) {
        throw new Exception("RockBlock server failed to deliver the message: '" + result + "'");
      }
    }
  }
コード例 #2
0
  @Override
  public Collection<IridiumMessage> pollMessages(Date timeSince) throws Exception {

    if (askGmailPassword || gmailPassword == null || gmailUsername == null) {
      Pair<String, String> credentials =
          GuiUtils.askCredentials(
              ConfigFetch.getSuperParentFrame(),
              "Enter Gmail Credentials",
              getGmailUsername(),
              getGmailPassword());
      if (credentials == null) return null;
      setGmailUsername(credentials.first());
      setGmailPassword(credentials.second());
      PluginUtils.saveProperties("conf/rockblock.props", this);
      askGmailPassword = false;
    }

    Properties props = new Properties();
    props.put("mail.store.protocol", "imaps");
    ArrayList<IridiumMessage> messages = new ArrayList<>();
    try {
      Session session = Session.getDefaultInstance(props, null);
      Store store = session.getStore("imaps");
      store.connect("imap.gmail.com", getGmailUsername(), getGmailPassword());

      Folder inbox = store.getFolder("Inbox");
      inbox.open(Folder.READ_ONLY);
      int numMsgs = inbox.getMessageCount();

      for (int i = numMsgs; i > 0; i--) {
        Message m = inbox.getMessage(i);
        if (m.getReceivedDate().before(timeSince)) {
          break;
        } else {
          MimeMultipart mime = (MimeMultipart) m.getContent();
          for (int j = 0; j < mime.getCount(); j++) {
            BodyPart p = mime.getBodyPart(j);
            Matcher matcher = pattern.matcher(p.getContentType());
            if (matcher.matches()) {
              InputStream stream = (InputStream) p.getContent();
              byte[] data = IOUtils.toByteArray(stream);
              IridiumMessage msg = process(data, matcher.group(1));
              if (msg != null) messages.add(msg);
            }
          }
        }
      }
    } catch (NoSuchProviderException ex) {
      ex.printStackTrace();
      System.exit(1);
    } catch (MessagingException ex) {
      ex.printStackTrace();
      System.exit(2);
    }

    return messages;
  }