5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2024-12-04 12:35:32 +00:00

send new log messages to server on each HTTP request; notify server of changes in device status (currently power/battery state)

This commit is contained in:
Jesse Young 2011-10-10 16:19:38 -07:00
parent f253f54704
commit 2889bf9b4b
11 changed files with 259 additions and 31 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.envaya.sms"
android:versionCode="13"
android:versionName="2.0">
android:versionCode="14"
android:versionName="2.0.1">
<uses-sdk android:minSdkVersion="4" />
@ -105,17 +105,26 @@
</intent-filter>
</receiver>
<receiver android:name=".receiver.ConnectivityChangeReceiver" >
<receiver android:name=".receiver.ConnectivityChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<receiver android:name=".receiver.DeviceStatusReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>
<service android:name=".CheckMmsInboxService">
</service>
<service android:name=".ForegroundService">
</service>
</application>
</application>
</manifest>

View File

@ -11,12 +11,18 @@ class EnvayaSMS
const ACTION_INCOMING = 'incoming';
const ACTION_OUTGOING = 'outgoing';
const ACTION_SEND_STATUS = 'send_status';
const ACTION_DEVICE_STATUS = 'device_status';
const ACTION_TEST = 'test';
const STATUS_QUEUED = 'queued';
const STATUS_FAILED = 'failed';
const STATUS_SENT = 'sent';
const DEVICE_STATUS_POWER_CONNECTED = "power_connected";
const DEVICE_STATUS_POWER_DISCONNECTED = "power_disconnected";
const DEVICE_STATUS_BATTERY_LOW = "battery_low";
const DEVICE_STATUS_BATTERY_OKAY = "battery_okay";
const MESSAGE_TYPE_SMS = 'sms';
const MESSAGE_TYPE_MMS = 'mms';
@ -49,11 +55,13 @@ class EnvayaSMS_Request
public $version;
public $phone_number;
public $log;
function __construct()
{
$this->version = $_POST['version'];
$this->phone_number = $_POST['phone_number'];
$this->log = @$_POST['log'];
}
function get_action()
@ -77,6 +85,8 @@ class EnvayaSMS_Request
return new EnvayaSMS_Action_SendStatus($this);
case EnvayaSMS::ACTION_TEST:
return new EnvayaSMS_Action_Test($this);
case EnvayaSMS::ACTION_DEVICE_STATUS:
return new EnvayaSMS_Action_DeviceStatus($this);
default:
return new EnvayaSMS_Action($this);
}
@ -247,4 +257,16 @@ class EnvayaSMS_Action_SendStatus extends EnvayaSMS_Action
$this->id = $_POST['id'];
$this->error = $_POST['error'];
}
}
}
class EnvayaSMS_Action_DeviceStatus extends EnvayaSMS_Action
{
public $status; // EnvayaSMS::DEVICE_STATUS_* values
function __construct($request)
{
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_DEVICE_STATUS;
$this->status = $_POST['status'];
}
}

1
server/php/example/log/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.log

View File

@ -22,6 +22,16 @@ if (!isset($password) || !$request->is_validated($password))
return;
}
// append to EnvayaSMS app log
$app_log = $request->log;
if ($app_log)
{
$log_file = dirname(__DIR__)."/log/sms_".preg_replace('#[^\w]#', '', $request->phone_number).".log";
$f = fopen($log_file, "a");
fwrite($f, $app_log);
fclose($f);
}
$action = $request->get_action();
switch ($action->type)
@ -79,6 +89,10 @@ switch ($action->type)
echo "invalid id";
}
return;
case EnvayaSMS::ACTION_DEVICE_STATUS:
error_log("device_status = {$action->status}");
echo "OK";
return;
case EnvayaSMS::ACTION_TEST:
echo "OK";
return;

View File

@ -1,6 +1,6 @@
<html>
<head>
<title>EnvayaSMS Request Simulator</title>
<style type='text/css'>
body
{
@ -30,10 +30,12 @@ body
<tr><th>Server URL</th><td><input id='server_url' type='text' size='40' /></td></tr>
<tr><th>Phone Number</th><td><input id='phone_number' type='text' /></td></tr>
<tr><th>Password</th><td><input id='password' type='password' /></td></tr>
<tr><th>Log Messages</th><td><textarea id='log' style='width:250px'></textarea></td></tr>
<tr><th>Action</th><td><select id='action' onchange='actionChanged()' onkeypress='actionChanged()'>
<option value='incoming'>incoming</option>
<option value='outgoing'>outgoing</option>
<option value='send_status'>send_status</option>
<option value='device_status'>device_status</option>
<option value='test'>test</option>
</select></td></tr>
</table>
@ -46,7 +48,7 @@ body
<option value='sms'>sms</option>
<option value='mms'>mms</option>
</select></td></tr>
<tr><th>Message</th><td><textarea id='message'></textarea></td></tr>
<tr><th>Message</th><td><textarea id='message' style='width:250px'></textarea></td></tr>
<tr><th>Timestamp</th><td><input id='timestamp' type='text' /></td></tr>
</table>
</div>
@ -70,6 +72,17 @@ body
<h4>Parameters for action=test:</h4>
(None)
</div>
<div id='action_device_status' style='display:none'>
<h4>Parameters for action=device_status:</h4>
<table class='smsTable'>
<tr><th>Status</th><td><select id='device_status'>
<option value='power_connected'>power_connected</option>
<option value='power_disconnected'>power_disconnected</option>
<option value='battery_low'>battery_low</option>
<option value='battery_okay'>battery_okay</option>
</select></td></tr>
</table>
</div>
<script type='text/javascript'>
@ -100,6 +113,7 @@ function performAction() {
version: '13',
phone_number: $('phone_number').value,
action: action,
log: $('log').value
};
if (action == 'incoming')
@ -115,6 +129,10 @@ function performAction() {
params.status = $('status').value;
params.error = $('error').value;
}
else if (action == 'device_status')
{
params.status = $('device_status').value;
}
var xhr = (window.ActiveXObject && !window.XMLHttpRequest) ? new ActiveXObject("Msxml2.XMLHTTP") : new XMLHttpRequest();

View File

@ -26,8 +26,10 @@ import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
@ -41,6 +43,7 @@ import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.envaya.sms.receiver.OutgoingMessagePoller;
import org.envaya.sms.receiver.ReenableWifiReceiver;
import org.envaya.sms.task.HttpTask;
import org.envaya.sms.task.PollerTask;
import org.json.JSONArray;
import org.json.JSONException;
@ -50,12 +53,18 @@ public final class App extends Application {
public static final String ACTION_OUTGOING = "outgoing";
public static final String ACTION_INCOMING = "incoming";
public static final String ACTION_SEND_STATUS = "send_status";
public static final String ACTION_DEVICE_STATUS = "device_status";
public static final String ACTION_TEST = "test";
public static final String STATUS_QUEUED = "queued";
public static final String STATUS_FAILED = "failed";
public static final String STATUS_SENT = "sent";
public static final String DEVICE_STATUS_POWER_CONNECTED = "power_connected";
public static final String DEVICE_STATUS_POWER_DISCONNECTED = "power_disconnected";
public static final String DEVICE_STATUS_BATTERY_LOW = "battery_low";
public static final String DEVICE_STATUS_BATTERY_OKAY = "battery_okay";
public static final String MESSAGE_TYPE_MMS = "mms";
public static final String MESSAGE_TYPE_SMS = "sms";
@ -114,6 +123,8 @@ public final class App extends Application {
public final Inbox inbox = new Inbox(this);
public final Outbox outbox = new Outbox(this);
public final Queue<HttpTask> queuedTasks = new LinkedList<HttpTask>();
private SharedPreferences settings;
private MmsObserver mmsObserver;
private SpannableStringBuilder displayedLog = new SpannableStringBuilder();
@ -427,10 +438,11 @@ public final class App extends Application {
public synchronized void retryStuckMessages() {
outbox.retryAll();
inbox.retryAll();
retryQueuedTasks();
}
public synchronized int getPendingMessageCount() {
return outbox.size() + inbox.size();
public synchronized int getPendingTaskCount() {
return outbox.size() + inbox.size() + queuedTasks.size();
}
public void debug(String msg) {
@ -439,8 +451,24 @@ public final class App extends Application {
private int logEpoch = 0;
public synchronized void log(CharSequence msg)
private StringBuilder newLogBuffer = new StringBuilder();
public synchronized String getNewLogEntries()
{
String res = newLogBuffer.toString();
newLogBuffer.setLength(0);
return res;
}
// clients may sometimes unget log entries out of order,
// but most of the time this will be the right order
public synchronized void ungetNewLogEntries(String logEntries)
{
newLogBuffer.insert(0, logEntries);
}
public synchronized void log(CharSequence msg)
{
Log.d(LOG_NAME, msg.toString());
// prevent displayed log from growing too big
@ -462,6 +490,8 @@ public final class App extends Application {
logEpoch++;
}
int prevLength = displayedLog.length();
// display a timestamp in the log occasionally
long logTime = SystemClock.elapsedRealtime();
if (logTime - lastLogTime > LOG_TIMESTAMP_INTERVAL)
@ -472,8 +502,10 @@ public final class App extends Application {
}
displayedLog.append(msg);
displayedLog.append("\n");
displayedLog.append("\n");
newLogBuffer.append(displayedLog, prevLength, displayedLog.length());
sendBroadcast(new Intent(App.LOG_CHANGED_INTENT));
}
@ -670,12 +702,23 @@ public final class App extends Application {
if (activeNetwork == null || !activeNetwork.isConnected())
{
WifiManager wmgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager wmgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (activeNetwork != null)
{
log(activeNetwork.getTypeName() + "=" + activeNetwork.getState());
}
else
{
log("Not connected to any network.");
}
if (!wmgr.isWifiEnabled() && isNetworkFailoverEnabled())
{
log("Enabling WIFI...");
wmgr.setWifiEnabled(true);
}
return;
}
@ -799,7 +842,28 @@ public final class App extends Application {
if (getOutgoingPollSeconds() > 0)
{
checkOutgoingMessages();
}
// failed outgoing message status notifications are dropped...
}
retryQueuedTasks();
}
public synchronized void retryQueuedTasks()
{
while (true)
{
HttpTask task = queuedTasks.poll();
if (task == null)
{
break;
}
task.execute();
}
}
public synchronized void addQueuedTask(HttpTask task)
{
queuedTasks.add(task);
}
}

View File

@ -57,8 +57,8 @@ public class Outbox {
this.app = app;
}
private void notifyMessageStatus(OutgoingMessage sms, String status, String errorMessage) {
String serverId = sms.getServerId();
private void notifyMessageStatus(OutgoingMessage sms, final String status, final String errorMessage) {
final String serverId = sms.getServerId();
String logMessage;
if (status.equals(App.STATUS_SENT)) {
@ -73,12 +73,15 @@ public class Outbox {
if (serverId != null) {
app.log("Notifying server " + smsDesc + " " + logMessage);
new HttpTask(app,
HttpTask task = new HttpTask(app,
new BasicNameValuePair("id", serverId),
new BasicNameValuePair("status", status),
new BasicNameValuePair("error", errorMessage),
new BasicNameValuePair("action", App.ACTION_SEND_STATUS)
).execute();
);
task.setRetryOnConnectivityError(true);
task.execute();
} else {
app.log(smsDesc + " " + logMessage);
}

View File

@ -0,0 +1,53 @@
package org.envaya.sms.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.apache.http.message.BasicNameValuePair;
import org.envaya.sms.App;
import org.envaya.sms.task.HttpTask;
public class DeviceStatusReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
App app = (App) context.getApplicationContext();
if (!app.isEnabled())
{
return;
}
String action = intent.getAction();
String status = "";
if (Intent.ACTION_POWER_CONNECTED.equals(action))
{
status = App.DEVICE_STATUS_POWER_CONNECTED;
app.log("Power connected");
}
else if (Intent.ACTION_POWER_DISCONNECTED.equals(action))
{
status = App.DEVICE_STATUS_POWER_DISCONNECTED;
app.log("Power disconnected");
}
else if (Intent.ACTION_BATTERY_LOW.equals(action))
{
status = App.DEVICE_STATUS_BATTERY_LOW;
app.log("Battery low");
}
else if (Intent.ACTION_BATTERY_OKAY.equals(action))
{
status = App.DEVICE_STATUS_BATTERY_OKAY;
app.log("Battery okay");
}
HttpTask task = new HttpTask(app,
new BasicNameValuePair("action", App.ACTION_DEVICE_STATUS),
new BasicNameValuePair("status", status)
);
task.setRetryOnConnectivityError(true);
task.execute();
}
}

View File

@ -43,27 +43,40 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
protected String url;
protected List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
protected BasicNameValuePair[] paramsArr;
private List<FormBodyPart> formParts;
private boolean useMultipartPost = false;
private HttpPost post;
private String logEntries;
private boolean retryOnConnectivityError;
public HttpTask(App app, BasicNameValuePair... paramsArr)
{
super();
this.app = app;
this.url = app.getServerUrl();
params = new ArrayList<BasicNameValuePair>(Arrays.asList(paramsArr));
params.add(new BasicNameValuePair("version", "" + app.getPackageInfo().versionCode));
params.add(new BasicNameValuePair("phone_number", app.getPhoneNumber()));
this.app = app;
this.paramsArr = paramsArr;
params = new ArrayList<BasicNameValuePair>(Arrays.asList(paramsArr));
}
public void setRetryOnConnectivityError(boolean retry)
{
this.retryOnConnectivityError = retry;
}
protected HttpTask getCopy()
{
return new HttpTask(app, paramsArr);
}
public void setFormParts(List<FormBodyPart> formParts)
{
useMultipartPost = true;
this.formParts = formParts;
}
}
private String getSignature()
throws NoSuchAlgorithmException, UnsupportedEncodingException
@ -97,12 +110,20 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
return new String(Base64Coder.encode(digest));
}
protected HttpResponse doInBackground(String... ignored) {
protected HttpResponse doInBackground(String... ignored) {
url = app.getServerUrl();
if (url.length() == 0) {
app.log("Can't contact server; Server URL not set");
return null;
}
logEntries = app.getNewLogEntries();
params.add(new BasicNameValuePair("version", "" + app.getPackageInfo().versionCode));
params.add(new BasicNameValuePair("phone_number", app.getPhoneNumber()));
params.add(new BasicNameValuePair("log", logEntries));
post = new HttpPost(url);
try
@ -145,6 +166,7 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
else if (statusCode == 403)
{
response.getEntity().consumeContent();
app.ungetNewLogEntries(logEntries);
app.log("Failed to authenticate to server");
app.log("(Phone number or password may be incorrect)");
return null;
@ -152,17 +174,24 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
else
{
response.getEntity().consumeContent();
app.ungetNewLogEntries(logEntries);
app.log("Received HTTP " + statusCode + " from server");
return null;
}
}
}
catch (IOException ex)
{
post.abort();
app.ungetNewLogEntries(logEntries);
app.logError("Error while contacting server", ex);
if (ex instanceof UnknownHostException || ex instanceof SocketTimeoutException)
{
{
if (retryOnConnectivityError)
{
app.addQueuedTask(getCopy());
}
app.asyncCheckConnectivity();
}
return null;
@ -170,6 +199,7 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
catch (Throwable ex)
{
post.abort();
app.ungetNewLogEntries(logEntries);
app.logError("Unexpected error while contacting server", ex, true);
return null;
}

View File

@ -100,6 +100,13 @@ public class Main extends Activity {
registerReceiver(logReceiver, logReceiverFilter);
}
@Override
public void onDestroy()
{
this.unregisterReceiver(logReceiver);
super.onDestroy();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
@ -140,9 +147,9 @@ public class Main extends Activity {
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem retryItem = menu.findItem(R.id.retry_now);
int pendingMessages = app.getPendingMessageCount();
retryItem.setEnabled(pendingMessages > 0);
retryItem.setTitle("Retry All (" + pendingMessages + ")");
int pendingTasks = app.getPendingTaskCount();
retryItem.setEnabled(pendingTasks > 0);
retryItem.setTitle("Retry All (" + pendingTasks + ")");
return true;
}

View File

@ -93,6 +93,13 @@ public class PendingMessages extends ListActivity {
refreshMessages();
}
@Override
public void onDestroy()
{
this.unregisterReceiver(refreshReceiver);
super.onDestroy();
}
public void refreshMessages()
{