public void doIt() throws Exception { Service srv; OutboundMessage msg; System.out.println("Example: Send message from BulkSMS HTTP Interface."); System.out.println(Library.getLibraryDescription()); System.out.println("Version: " + Library.getLibraryVersion()); srv = new Service(); BulkSmsHTTPGateway gateway = new BulkSmsHTTPGateway("bulksms.http.1", "username", "password"); gateway.setOutbound(true); srv.addGateway(gateway); srv.startService(); // Query the service to find out our credit balance. System.out.println("Remaining credit: " + gateway.queryBalance()); // Send a message synchronously. msg = new OutboundMessage("+306948494037", "Hello from SMSLib (BULKSMS handler)"); srv.sendMessage(msg); System.out.println(msg); System.out.println("Now Sleeping - Hit <enter> to terminate."); System.in.read(); srv.stopService(); }
// <editor-fold defaultstate="collapsed" desc=" Load Configuration from DHIS2 HOME "> private String loadConfiguration() throws Exception { String configFile = System.getenv("DHIS2_HOME") + File.separator + "SMSServer.conf"; if (new File(configFile).exists()) { Collection<AGateway> existingGateways = new ArrayList<AGateway>(); existingGateways.addAll(Service.getInstance().getGateways()); // Remove all existing gateways for (AGateway gateway : existingGateways) { Service.getInstance().removeGateway(gateway); } // Load properties from configuration file FileInputStream f = new FileInputStream(configFile); this.props = new Properties(); getProperties().load(f); f.close(); // Add gateway to service based on configuration file // <editor-fold defaultstate="collapsed" desc=" Get Gateway & Configuration "> for (int i = 0; i < Integer.MAX_VALUE; i++) { try { String propName = "gateway." + i; String propValue = getProperties().getProperty(propName, "").trim(); if (propValue.length() == 0) { break; } String modemName = propValue.split("\\,")[0].trim(); if (modemName.contains("bulksms")) { String username = getProperties().getProperty("bulksms.username"); String password = getProperties().getProperty("bulksms.password"); BulkSmsHTTPGateway gateway = new BulkSmsHTTPGateway("bulksms.http.1", username, password); gateway.setOutbound(true); gateway.setInbound(true); Service.getInstance().addGateway(gateway); } else if (modemName.contains("clickatell")) { String username = getProperties().getProperty("clickatell.username"); String password = getProperties().getProperty("clickatell.password"); String api_id = getProperties().getProperty("clickatell.api_id"); DhisClickatellGateway gateway = new DhisClickatellGateway("clickatell.http.1", api_id, username, password); gateway.setOutbound(true); gateway.setInbound(true); Service.getInstance().addGateway(gateway); } else { String port = getProperties().getProperty(modemName + ".port"); int baudRate = Integer.parseInt(getProperties().getProperty(modemName + ".baudrate")); String manufacturer = getProperties().getProperty(modemName + ".manufacturer"); String model = getProperties().getProperty(modemName + ".model"); String protocol = getProperties().getProperty(modemName + ".protocol"); String pin = getProperties().getProperty(modemName + ".pin"); String inbound = getProperties().getProperty(modemName + ".inbound"); String outbound = getProperties().getProperty(modemName + ".outbound"); String simMemLocation = getProperties().getProperty(modemName + ".simMemLocation"); // TODO: DETECT MODEM CLASS AND INSTANTIATE SerialModemGateway gateway = new SerialModemGateway(modemName, port, baudRate, manufacturer, model); if (simMemLocation != null && !simMemLocation.equals("-")) { gateway.getATHandler().setStorageLocations(simMemLocation); } if (protocol != null && protocol.equalsIgnoreCase("PDU")) { gateway.setProtocol(Protocols.PDU); } else { if (protocol != null && protocol.equalsIgnoreCase("TEXT")) { gateway.setProtocol(Protocols.TEXT); } else { gateway.setProtocol(Protocols.PDU); } } if (pin != null) { gateway.setSimPin(pin); } if (inbound.equalsIgnoreCase("yes")) { gateway.setInbound(true); } else { gateway.setInbound(false); } if (outbound.equalsIgnoreCase("yes")) { gateway.setOutbound(true); } else { gateway.setOutbound(false); } Service.getInstance().addGateway(gateway); } Logger.getInstance() .logInfo("Load Configuration: added gateway " + i + " / ", null, null); } catch (Exception e) { Logger.getInstance() .logError( "Load Configuration: Unknown Gateway in configuration file!, " + e.getMessage(), null, null); } } // </editor-fold> return "SUCCESSFULLY STARTED SERVICE"; } else { return "ERROR LOADING CONFIGURATION FILE"; } }