- My massive abuse of print statements is only for debugging purposes doing development.
(My massive abuse of print statements is only for debugging purposes doing development.)
*Import of various liberaries...*
public class SMSRelay {
private Service instance;
public SMSRelay() {
instance = Service.getInstance();
instance.setInboundMessageNotification(new InboundHandling());
}
public void addGateway(String name, String port, String pin) throws GatewayException {
SerialModemGateway g = new SerialModemGateway(name, port, 115200, "Huawei", "E220");
g.setProtocol(Protocols.PDU);
g.setInbound(true);
g.setOutbound(true);
g.setSimPin(pin);
g.getATHandler().setStorageLocations("SMME"); //To prevent duplicate texts
instance.addGateway(g);
}
public void removeGateway(String name) throws GatewayException {
instance.removeGateway(instance.getGateway(name));
}
public void start() throws Exception {
instance.startService();
}
public void stop() throws Exception {
instance.stopService();
}
public class InboundHandling implements IInboundMessageNotification {
private Pattern p = Pattern.compile("lat: (?<lat>\\d+\\.\\d+) long: (?<lng>\\d+\\.\\d+).*imei:(?<imei>\\d+)");
public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg) {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = formatter.format(msg.getDate());
String originator = msg.getOriginator();
String text = msg.getText();
//Delete message from SIM
try {
instance.deleteMessage(msg);
} catch (Exception e) {
System.out.println("Failed to delete message...");
}
//Look for GPS data
Matcher m = p.matcher(text);
if (m.matches()) {
System.out.println("Parsing valid info to database...");
toDatabase(m.group("lat"), m.group("lng"), m.group("imei"), date, originator);
} else {
System.out.println("Invalid SMS");
System.out.println(text);
}
}
private void toDatabase(String lat, String lng, String imei, String date, String originator) {
Connection con = null;
PreparedStatement pst = null;
try {
//Get MySQL info from properties file
Properties props = new Properties();
props.load(new FileInputStream("properties/database.properties"));
String url = props.getProperty("db.url");
String user = props.getProperty("db.user");
String password = props.getProperty("db.passwd");
System.out.println("Connecting to database...");
//Connect to database
con = DriverManager.getConnection(url,user,password);
System.out.println("Writing...");
//Write data
pst = con.prepareStatement("INSERT INTO positions VALUES (default, ?, ?, ?, ?, ?)");
pst.setString(1, date);
pst.setString(2, lat);
pst.setString(3, lng);
pst.setString(4, originator);
pst.setString(5, imei);
pst.executeUpdate();
System.out.println("Data has been written!");
} catch (IOException e) {
System.out.println("Can't access database.properties!");
} catch (SQLException e) {
System.out.println("Something went wrong while interacting with database...");
while (e != null) {
System.out.println ("SQLState: " + e.getSQLState () + "");
System.out.println ("Message: " + e.getMessage() + "");
System.out.println ("Vendor ErrorCode: " + e.getErrorCode() + "");
e = e.getNextException();
System.out.println("");
}
} finally {
try {
if (pst != null) pst.close();
if (con != null) con.close();
} catch (SQLException e) {
System.out.println("Something went wrong closing the connections...");
}
}
}
}
}