Skip to content

Commit

Permalink
send new log messages to server on each HTTP request; notify server o…
Browse files Browse the repository at this point in the history
…f changes in device status (currently power/battery state)
  • Loading branch information
Jesse Young committed Oct 10, 2011
1 parent f253f54 commit 2889bf9
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 31 deletions.
17 changes: 13 additions & 4 deletions AndroidManifest.xml
@@ -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" />

Expand Down Expand Up @@ -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>
24 changes: 23 additions & 1 deletion server/php/EnvayaSMS.php
Expand Up @@ -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';

Expand Down Expand Up @@ -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()
Expand All @@ -77,6 +85,8 @@ private function _get_action()
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);
}
Expand Down Expand Up @@ -247,4 +257,16 @@ function __construct($request)
$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 change: 1 addition & 0 deletions server/php/example/log/.gitignore
@@ -0,0 +1 @@
*.log
14 changes: 14 additions & 0 deletions server/php/example/www/index.php
Expand Up @@ -22,6 +22,16 @@
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)
Expand Down Expand Up @@ -79,6 +89,10 @@
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;
Expand Down
22 changes: 20 additions & 2 deletions server/php/example/www/test.html
@@ -1,6 +1,6 @@
<html>
<head>

<title>EnvayaSMS Request Simulator</title>
<style type='text/css'>
body
{
Expand Down Expand Up @@ -30,10 +30,12 @@ <h2>EnvayaSMS Request Simulator</h2>
<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>
Expand All @@ -46,7 +48,7 @@ <h4>Parameters for action=incoming:</h4>
<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>
Expand All @@ -70,6 +72,17 @@ <h4>Parameters for action=send_status:</h4>
<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'>
Expand Down Expand Up @@ -100,6 +113,7 @@ <h4>Parameters for action=test:</h4>
version: '13',
phone_number: $('phone_number').value,
action: action,
log: $('log').value
};

if (action == 'incoming')
Expand All @@ -115,6 +129,10 @@ <h4>Parameters for action=test:</h4>
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();

Expand Down
80 changes: 72 additions & 8 deletions src/org/envaya/sms/App.java
Expand Up @@ -26,8 +26,10 @@
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;
Expand All @@ -41,6 +43,7 @@
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;
Expand All @@ -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";

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -427,10 +438,11 @@ public String getPassword() {
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) {
Expand All @@ -439,8 +451,24 @@ public void debug(String msg) {

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
Expand All @@ -462,6 +490,8 @@ public synchronized void log(CharSequence msg)
logEpoch++;
}

int prevLength = displayedLog.length();

// display a timestamp in the log occasionally
long logTime = SystemClock.elapsedRealtime();
if (logTime - lastLogTime > LOG_TIMESTAMP_INTERVAL)
Expand All @@ -472,8 +502,10 @@ public synchronized void log(CharSequence msg)
}

displayedLog.append(msg);
displayedLog.append("\n");

displayedLog.append("\n");

newLogBuffer.append(displayedLog, prevLength, displayedLog.length());

sendBroadcast(new Intent(App.LOG_CHANGED_INTENT));
}

Expand Down Expand Up @@ -670,12 +702,23 @@ public synchronized void asyncCheckConnectivity()

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;
}

Expand Down Expand Up @@ -799,7 +842,28 @@ private void onConnectivityRestored()
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);
}
}
11 changes: 7 additions & 4 deletions src/org/envaya/sms/Outbox.java
Expand Up @@ -57,8 +57,8 @@ public Outbox(App app)
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)) {
Expand All @@ -73,12 +73,15 @@ private void notifyMessageStatus(OutgoingMessage sms, String status, String erro
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);
}
Expand Down

0 comments on commit 2889bf9

Please sign in to comment.