diff --git a/src/AprsService.scala b/src/AprsService.scala index 0f87b1c..76bd3d7 100644 --- a/src/AprsService.scala +++ b/src/AprsService.scala @@ -8,6 +8,8 @@ import _root_.android.preference.PreferenceManager import _root_.android.util.Log import _root_.android.widget.Toast +import _root_.net.ab0oo.aprs.parser._ + object AprsService { val PACKAGE = "org.aprsdroid.app" // intent actions @@ -257,8 +259,53 @@ class AprsService extends Service with LocationListener { Log.d(TAG, "onStatusChanged: " + provider) } + def handleMessage(ts : Long, ap : APRSPacket, msg : MessagePacket) { + if (msg.isAck() || msg.isRej()) { + // TODO: implement ack parsing + return + } + val callssid = prefs.getCallSsid() + if (msg.getTargetCallsign() == callssid) { + db.addMessage(ts, ap, msg) + if (msg.getMessageNumber() != "") { + // we need to send an ack + val ack = AprsPacket.formatMessage(callssid, appVersion(), ap.getSourceCall(), "ack", msg.getMessageNumber()) + val status = poster.update(ack) + addPost(StorageDatabase.Post.TYPE_POST, status, ack.toString) + } + } + } + + def parsePacket(ts : Long, message : String) { + try { + val fap = new Parser().parse(message) + if (fap.getAprsInformation() == null) { + Log.d(TAG, "parsePacket() misses payload: " + message) + return + } + if (fap.hasFault()) + throw new Exception("FAP fault") + fap.getAprsInformation() match { + case pp : PositionPacket => db.addPosition(ts, fap, pp.getPosition(), null) + case op : ObjectPacket => db.addPosition(ts, fap, op.getPosition(), op.getObjectName()) + case msg : MessagePacket => handleMessage(ts, fap, msg) + } + } catch { + case e : Exception => + Log.d(TAG, "parsePacket() unsupported packet: " + message) + e.printStackTrace() + } + } + def addPost(t : Int, status : String, message : String) { - db.addPost(System.currentTimeMillis(), t, status, message) + val ts = System.currentTimeMillis() + db.addPost(ts, t, status, message) + if (t == StorageDatabase.Post.TYPE_POST || t == StorageDatabase.Post.TYPE_INCMG) { + parsePacket(ts, message) + } else { + // only log status messages + Log.d(TAG, "addPost: " + status + " - " + message) + } sendBroadcast(new Intent(UPDATE).putExtra(STATUS, message)) } diff --git a/src/StorageDatabase.scala b/src/StorageDatabase.scala index 97db56d..803d932 100644 --- a/src/StorageDatabase.scala +++ b/src/StorageDatabase.scala @@ -213,12 +213,6 @@ class StorageDatabase(context : Context) extends def addMessage(ts : Long, ap : APRSPacket, msg : MessagePacket) { import Message._ - if (msg.isAck() || msg.isRej()) { - // TODO: implement ack parsing - return - } - if (msg.getTargetCallsign() == "ASDF") - return val cv = new ContentValues() cv.put(TS, ts.asInstanceOf[java.lang.Long]) cv.put(RETRYCNT, 0.asInstanceOf[java.lang.Integer]) @@ -229,27 +223,6 @@ class StorageDatabase(context : Context) extends addMessage(cv) } - def parsePacket(ts : Long, message : String) { - try { - val fap = new Parser().parse(message) - if (fap.getAprsInformation() == null) { - Log.d(TAG, "parsePacket() misses payload: " + message) - return - } - if (fap.hasFault()) - throw new Exception("FAP fault") - fap.getAprsInformation() match { - case pp : PositionPacket => addPosition(ts, fap, pp.getPosition(), null) - case op : ObjectPacket => addPosition(ts, fap, op.getPosition(), op.getObjectName()) - case msg : MessagePacket => addMessage(ts, fap, msg) - } - } catch { - case e : Exception => - Log.d(TAG, "parsePacket() unsupported packet: " + message) - e.printStackTrace() - } - } - def getPositions(sel : String, selArgs : Array[String], limit : String) : Cursor = Benchmark("getPositions") { getReadableDatabase().query(Position.TABLE, Position.COLUMNS_MAP, sel, selArgs, @@ -307,12 +280,6 @@ class StorageDatabase(context : Context) extends cv.put(Post.STATUS, status) cv.put(Post.MESSAGE, message) getWritableDatabase().insertOrThrow(Post.TABLE, Post.MESSAGE, cv) - if (posttype == Post.TYPE_POST || posttype == Post.TYPE_INCMG) { - parsePacket(ts, message) - } else { - // only log status messages - Log.d(TAG, "StorageDatabase.addPost: " + status + " - " + message) - } if (Post.trimCounter == 0) { trimPosts() Post.trimCounter = 100