protected DCServerFactory.ResultCode sendEmail( String frEmail, String toEmail, String subj, String body) { if (StringTools.isBlank(frEmail)) { Print.logError("'From' Email address not specified"); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } else if (StringTools.isBlank(toEmail) || !CommandPacketHandler.validateAddress(toEmail)) { Print.logError("'To' SMS Email address invalid, or not specified"); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } else if (StringTools.isBlank(subj) && StringTools.isBlank(body)) { Print.logError("Command string not specified"); return DCServerFactory.ResultCode.INVALID_ARG; } else { try { Print.logInfo("SMS email: to <" + toEmail + ">"); Print.logDebug(" From : " + frEmail); Print.logDebug(" To : " + toEmail); Print.logDebug(" Subject: " + subj); Print.logDebug(" Message: " + body); SendMail.send(frEmail, toEmail, null, null, subj, body, null); return DCServerFactory.ResultCode.SUCCESS; } catch (Throwable t) { // NoClassDefFoundException, ClassNotFoundException // this will fail if JavaMail support for SendMail is not available. Print.logWarn("SendMail error: " + t); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } } }
/** ** Scrambles String */ public static String _ens64(String d) { if (!StringTools.isBlank(d)) { return Base64.encode(StringTools.getBytes(d), getBase64Alphabet(), Base64Pad); } else { return ""; } }
/** ** Descrambles String */ public static String _des64(String e) { if (!StringTools.isBlank(e)) { byte b[] = Base64.decode(e, getBase64Alphabet(), Base64Pad); return StringTools.toStringValue(b, ' '); } else { return ""; } }
/** ** Decodes an RTP encoded argument */ public static RTProperties parseRTP(String rtpArg) { if (!StringTools.isBlank(rtpArg)) { String s = _des64(rtpArg); if (!StringTools.isBlank(s)) { return new RTProperties(s); } } return null; }
/** ** Descrambles String */ public static String _des64(String e) { if (!StringTools.isBlank(e)) { try { byte b[] = Base64.decode(e, getBase64Alphabet(), Base64Pad); return StringTools.toStringValue(b, ' '); } catch (Base64.Base64DecodeException bde) { Print.logError("Invalid Base64 characters", bde); return ""; } } else { return ""; } }
public String toString() { StringBuffer sb = new StringBuffer(); /* standard event fields */ int sc = this.getStatusCode(); sb.append("Event values:\n"); sb.append(" DeviceID : " + this.getAccountID() + "/" + this.getDeviceID() + "\n"); sb.append(" UniqueID : " + this.getUniqueID() + "\n"); sb.append( " Fixtime : " + this.getTimestamp() + " [" + new DateTime(this.getTimestamp()) + "]\n"); sb.append( " StatusCode: [" + StatusCodes.GetHex(sc) + "] " + StatusCodes.GetDescription(sc, null)); sb.append(" GPS : " + this.getGeoPoint() + " [age " + this.getGpsAge() + " sec]\n"); sb.append( " SpeedKPH : " + StringTools.format(this.getSpeedKPH(), "0.0") + " [" + this.getHeading() + "]\n"); /* remaining event fields (not already displayed) */ OrderedSet<?> fldn = new OrderedSet<Object>(this.fieldValues.getPropertyKeys()); fldn.remove(EventData.FLD_timestamp); fldn.remove(EventData.FLD_statusCode); fldn.remove(EventData.FLD_latitude); fldn.remove(EventData.FLD_longitude); fldn.remove(EventData.FLD_gpsAge); fldn.remove(EventData.FLD_speedKPH); fldn.remove(EventData.FLD_heading); for (Object k : fldn) { Object v = this.fieldValues.getProperty(k, "?"); sb.append(" "); sb.append(StringTools.leftAlign(k.toString(), 10)).append(": "); sb.append(v.toString()).append("\n"); } /* alternate fields */ if (this.otherValues != null) { for (String k : this.otherValues.keySet()) { String v = StringTools.trim(this.otherValues.get(k)); sb.append(" "); sb.append(StringTools.leftAlign(k, 10)).append(": "); sb.append(v).append("\n"); } } /* return string */ return sb.toString(); }
/** * ** Writes the specified line to the specified socket's output stream ** @param socket The * socket which's output stream to write to ** @param val The line to write to the socket output * stream. A newline is ** appended if it does not already end with one. ** @throws IOException if * an error occurs */ protected static void socketWriteLine(Socket socket, String val) throws IOException { if (val != null) { String lineTerm = ClientSocketThread.getLineTerminator(); String v = val.endsWith(lineTerm) ? val : (val + lineTerm); ClientSocketThread.socketWriteBytes(socket, StringTools.getBytes(v), 0, -1); } }
public void setGeozoneID(String gzid) { if (!StringTools.isBlank(gzid)) { this.fieldValues.setString(EventData.FLD_geozoneID, gzid); } else { this.fieldValues.removeProperty(EventData.FLD_geozoneID); } }
/** * ** Adds an argument to the URI ** @param key The key name of the argument to add ** @param * value The value of the new key ** @param encode True if <code>value</code> shoudl be hex * encoded ** @param obfuscate True if <code>value</code> should be obfuscated ** @return This * URIArg, with the argument added */ protected URIArg _addArg(String key, String value, boolean encode, boolean obfuscate) { if (!StringTools.isBlank(key)) { String val = encode ? this.encodeArg(value, obfuscate) : value; this.getKeyValList().add(new KeyVal(key, val)); } return this; }
/** ** Main entry point for testing/debugging ** @param argv Comand-line arguments */ public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); String host = RTConfig.getString(ARG_HOST, null); int port = RTConfig.getInt(ARG_PORT, 0); /* send data */ if (RTConfig.hasProperty(ARG_SEND)) { if (StringTools.isBlank(host)) { Print.logError("Target host not specified"); usage(); } if (port <= 0) { Print.logError("Target port not specified"); usage(); } String dataStr = RTConfig.getString(ARG_SEND, "hello"); byte data[] = dataStr.startsWith("0x") ? StringTools.parseHex(dataStr, null) : dataStr.getBytes(); ClientSocketThread cst = new ClientSocketThread(host, port); try { cst.openSocket(); cst.socketWriteBytes(data); } catch (Throwable t) { Print.logException("Error", t); } finally { cst.closeSocket(); } System.exit(0); } /* receive data */ if (RTConfig.hasProperty(ARG_RECEIVE)) { if (port <= 0) { Print.logError("Target port not specified"); usage(); } if (!StringTools.isBlank(host)) { Print.logWarn("Specified 'host' will be ignored"); } Print.logError("Receive not yet implemented ..."); System.exit(99); } /* show usage */ usage(); }
/** * ** Adds an argument to the URI ** @param key The key name of the argument to add ** @param rtp * The RTP encoded values of the new key ** @return This URIArg, with the argument added */ public URIArg addArg(String key, RTProperties rtp) { String r = (rtp != null) ? rtp.toString() : null; if (!StringTools.isBlank(r)) { return this._addArg(key, URIArg.encodeRTP(rtp), false /*encode*/, false /*obfuscate*/); } else { return this.addArg(key, ""); } }
public void setFieldValue(String fldName, Object fldVal) { if (!StringTools.isBlank(fldName) && (fldVal != null)) { if (USE_ALTERNATE_FIELD_MAP) { this.getAlternateFieldMap().put(fldName, fldVal); } else { this.fieldValues.setProperty(fldName, fldVal); } } }
/** ** Gets the SMSoutboubdGateway for the specified name */ public static SMSOutboundGateway GetSMSGateway(String name) { /* get handler */ if (StringTools.isBlank(name)) { return null; } else { return SmsGatewayHandlerMap.get(name.toLowerCase()); } }
/** * ** Decodes the specified hex-encoded argument (not yet fully tested) ** @param sb The * StringBuffer where the decoded String argument will be placed ** @param s The String to decode * ** @return The StringBuffer where the decoded String will be placed */ public static StringBuffer decodeArg(StringBuffer sb, String s) { if (sb == null) { sb = new StringBuffer(); } if (s != null) { char ch[] = new char[s.length()]; s.getChars(0, s.length(), ch, 0); for (int i = 0; i < ch.length; i++) { if (ch[i] == '%') { if ((i + 2) < ch.length) { int ch1 = StringTools.hexIndex(ch[i + 1]); int ch2 = StringTools.hexIndex(ch[i + 2]); sb.append((char) (((ch1 << 4) | ch2) & 0xFF)); i += 2; } else { i = ch.length - 1; } } else { sb.append(ch[i]); } } } return sb; }
protected String getStringProperty(Device device, String key, String dft) { DCServerConfig dcs = (device != null) ? DCServerFactory.getServerConfig(device.getDeviceCode()) : null; String prop = null; if (dcs != null) { prop = dcs.getStringProperty(key, dft); Print.logInfo("DCServerConfig property '" + key + "' ==> " + prop); if (StringTools.isBlank(prop) && RTConfig.hasProperty(key)) { Print.logInfo("(RTConfig property '" + key + "' ==> " + RTConfig.getString(key, "") + ")"); } } else { prop = RTConfig.getString(key, dft); Print.logInfo("RTConfig property '" + key + "' ==> " + prop); } return prop; }
/** ** Sets the 'host' */ public boolean setHost(String _host) { String uri = this.getURI(); if (!StringTools.isBlank(_host) && URIArg.isAbsoluteURL(uri)) { try { URL oldURI = new URL(uri); String proto = oldURI.getProtocol(); String host = _host; int port = oldURI.getPort(); String file = oldURI.getFile(); URL newURI = new URL(proto, host, port, file); this._setURI(newURI.toString()); return true; } catch (MalformedURLException mue) { // error } } return false; }
protected Device loadDevice(String acctID, String devID) { if (StringTools.isBlank(acctID)) { return this.loadDevice(devID); // load ad ModemID } else { try { Account account = Account.getAccount(acctID); if (account == null) { Print.logError("Account-ID not found: " + acctID); return null; } else { Device dev = Transport.loadDeviceByTransportID(account, devID); return dev; } } catch (DBException dbe) { Print.logError("Error getting Device: " + acctID + "/" + devID + " [" + dbe + "]"); return null; } } }
/** ** Add SMS Gateway support provider */ public static void AddSMSGateway(String name, SMSOutboundGateway smsGW) { /* validate name */ if (StringTools.isBlank(name)) { Print.logWarn("SMS Gateway name is blank"); return; } else if (smsGW == null) { Print.logWarn("SMS Gateway handler is null"); return; } /* initialize map? */ if (SmsGatewayHandlerMap == null) { SmsGatewayHandlerMap = new HashMap<String, SMSOutboundGateway>(); } /* save handler */ SmsGatewayHandlerMap.put(name.toLowerCase(), smsGW); Print.logDebug("Added SMS Gateway Handler: " + name); }
/** ** Sets the URI from a string */ public void setURI(String uri) { int p = (uri != null) ? uri.indexOf("?") : -1; if (p >= 0) { this._setURI(uri.substring(0, p)); String a[] = StringTools.parseString(uri.substring(p + 1), "&"); for (int i = 0; i < a.length; i++) { String key = "", val = ""; int e = a[i].indexOf("="); if (e >= 0) { key = a[i].substring(0, e); val = a[i].substring(e + 1); } else { key = a[i]; val = ""; } this._addArg(key, val, false /*encode*/, false /*obfuscate*/); // assume already encoded } } else { this._setURI(uri); } }
/** * ** Writes the specified String to the specified socket's output stream ** @param socket The * socket which's output stream to write to ** @param val The String to write to the socket output * stream. ** @throws IOException if an error occurs */ protected static void socketWriteString(Socket socket, String val) throws IOException { if (val != null) { ClientSocketThread.socketWriteBytes(socket, StringTools.getBytes(val), 0, -1); } }
/** * ** Adds a file extension to the end of this URI, ".xml" etc. The ** extension will be added to * the URI if doesn't already end with it ** @param ext The extension to add */ public void addExtension(String ext) { if (!StringTools.isBlank(ext) && !this.uri.endsWith(ext)) { this.uri += ext; } }
public boolean insertEventData() { /* valid device? */ if (this.device == null) { return false; } /* debug message */ if (RTConfig.isDebugMode()) { Print.logDebug("Inserting EventData ...\n" + this.toString()); } /* EventData key */ String acctID = this.device.getAccountID(); String devID = this.device.getDeviceID(); long fixtime = this.getTimestamp(); int statusCode = this.getStatusCode(); EventData.Key evKey = new EventData.Key(acctID, devID, fixtime, statusCode); EventData evdb = evKey.getDBRecord(); /* set EventData field values */ if (USE_EVENTDATA_SETVALUE) { for (Object fldn : this.fieldValues.getPropertyKeys()) { if (fldn.equals(EventData.FLD_timestamp)) { continue; // already set above } else if (fldn.equals(EventData.FLD_statusCode)) { continue; // already set above } Object fldv = this.fieldValues.getProperty(fldn, null); if (fldv != null) { evdb.setValue((String) fldn, fldv); // attempts to use "setter" methods } } } else { if (this.hasLatitude()) { evdb.setLatitude(this.getLatitude()); } if (this.hasLongitude()) { evdb.setLongitude(this.getLongitude()); } if (this.hasGpsAge()) { evdb.setGpsAge(this.getGpsAge()); } if (this.hasHDOP()) { evdb.setHDOP(this.getHDOP()); } if (this.hasSatelliteCount()) { evdb.setSatelliteCount(this.getSatelliteCount()); } if (this.hasSpeedKPH()) { evdb.setSpeedKPH(this.getSpeedKPH()); } if (this.hasHeading()) { evdb.setHeading(this.getHeading()); } if (this.hasAltitude()) { evdb.setAltitude(this.getAltitude()); } if (this.hasInputMask()) { evdb.setInputMask(this.getInputMask()); } if (this.hasBatteryLevel()) { evdb.setBatteryLevel(this.getBatteryLevel()); } if (this.hasSignalStrength()) { evdb.setSignalStrength(this.getSignalStrength()); } if (this.hasOdometerKM()) { evdb.setOdometerKM(this.getOdometerKM()); } if (this.hasEngineHours()) { evdb.setEngineHours(this.getEngineHours()); } if (this.hasPtoHours()) { evdb.setPtoHours(this.getPtoHours()); } if (this.hasFuelTotal()) { evdb.setFuelTotal(this.getFuelTotal()); } if (this.hasGeozoneID()) { evdb.setGeozoneID(this.getGeozoneID()); } } /* other fields (if available) */ if (this.otherValues != null) { for (String fldn : this.otherValues.keySet()) { if (fldn.equals(EventData.FLD_timestamp)) { continue; } else if (fldn.equals(EventData.FLD_statusCode)) { continue; } Object fldv = this.otherValues.get(fldn); if (fldv != null) { evdb.setValue(fldn, fldv); // attempts to use "setter" methods } } } /* insert event */ // this will display an error if it was unable to store the event Print.logInfo( "Event : [0x" + StringTools.toHexString(statusCode, 16) + "] " + StatusCodes.GetDescription(statusCode, null)); this.device.insertEventData( evdb); // FLD_lastValidLatitude,FLD_lastValidLongitude,FLD_lastGPSTimestamp,FLD_lastOdometerKM this.eventTotalCount++; return true; }