Commit 45fd16ec authored by Dave Griffiths's avatar Dave Griffiths
Browse files

connection to raspberry pi working

parent 8f9f1217
......@@ -31,15 +31,16 @@
<activity android:name="NewIndividualActivity"></activity>
<activity android:name="UpdateIndividualActivity"></activity>
<activity android:name="TagLocationActivity"></activity>
<activity android:name="SyncActivity"></activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk android:minSdkVersion="8" />
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
......
......@@ -40,8 +40,12 @@
;; stringify based on type
(define (stringify-value ktv)
(cond
((equal? (ktv-type ktv) "varchar") (string-append "'" (ktv-value ktv) "'"))
(else (number->string (ktv-value ktv)))))
((null? (ktv-value ktv)) "NULL")
((equal? (ktv-type ktv) "varchar") (string-append "'" (ktv-value ktv) "'"))
(else
(if (not (string? (ktv-value ktv)))
(number->string (ktv-value ktv))
(ktv-value ktv)))))
;; helper to return first instance from a select
(define (select-first db str)
......@@ -174,6 +178,13 @@
(get-attribute-ids/types db table entity-type)))))))
(define (get-dirty-stats db table)
(list
(select-first
db (string-append "select count(entity_id) from " table "_entity where dirty=1;"))
(select-first
db (string-append "select count(entity_id) from " table "_entity;"))))
(define (all-entities db table type)
(let ((s (db-select
db (string-append "select entity_id from " table "_entity where entity_type = '"
......@@ -246,7 +257,39 @@
(insert-entity db table entity-type user ktvlist)
#f)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syncing
(define (stringify-list l)
(foldl
(lambda (i r)
(string-append r " " i))
"" l))
(define (stringify-ktvlist ktvlist)
(foldl
(lambda (i r)
(string-append r " " (ktv-key i) ":" (stringify-value i)))
""
ktvlist))
;; todo
;; update (with partial values)
(define (build-sync-debug db table)
(foldl
(lambda (i r)
(string-append
r "\n" (vector-ref i 0) " " (vector-ref i 1) " "
(stringify-ktvlist (get-entity db table (string->number (vector-ref i 0))))))
""
(cdr (db-select
db (string-append "select * from " table "_entity where dirty=1;")))))
(define (build-sync-json db table)
(dbg (scheme->json
(map
(lambda (i)
(dbg (list
(vector->list i)
(get-entity db table (string->number (vector-ref i 0))))))
(dbg (cdr (db-select
db (string-append "select * from " table "_entity where dirty=1;"))))))))
......@@ -104,7 +104,7 @@
(mtext "foo" "Database")
(horiz
(mbutton "main-send" "Email" (lambda () (list)))
(mbutton "main-sync" "Sync" (lambda () (list)))))
(mbutton "main-sync" "Sync" (lambda () (list (start-activity "sync" 0 ""))))))
(lambda (activity arg)
(activity-layout activity))
(lambda (activity arg)
......@@ -549,6 +549,38 @@
(lambda (activity requestcode resultcode) '()))
(let ((build-dirty
(lambda ()
(let ((sync (get-dirty-stats db "sync"))
(stream (get-dirty-stats db "stream")))
(msg sync stream)
(dbg (string-append
"Pack data: " (car sync) "/" (cadr sync) " "
"Focal data: " (car stream) "/" (cadr stream)))))))
(activity
"sync"
(vert
(text-view (make-id "sync-title") "Sync database" 40 fillwrap)
(mtext "sync-dirty" "...")
(horiz
(mtext "sync-range" "Out of range")
(mbutton "sync-sync" "Sync" (lambda () (list))))
(text-view (make-id "sync-console") "..." 15 (layout 300 'wrap-content 1 'left))
(mbutton "main-send" "Done" (lambda () (list (finish-activity 2)))))
(lambda (activity arg)
(activity-layout activity))
(lambda (activity arg)
(list
(update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))
(update-widget 'text-view (get-id "sync-console") 'text (build-sync-debug db "sync"))))
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity requestcode resultcode) '())))
)
......@@ -16,6 +16,16 @@ import android.content.IntentFilter;
import android.net.wifi.WifiConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import android.os.SystemClock;
public class NetworkManager {
public enum State {
......@@ -52,6 +62,7 @@ public class NetworkManager {
wifi.disconnect();
wifi.enableNetwork(i.networkId, true);
wifi.reconnect();
Log.i("starwisp", "Connected");
break;
}
}
......@@ -69,6 +80,69 @@ public class NetworkManager {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifi.addNetwork(conf);
}
else
{
StartRequestThread();
}
}
public void StartRequestThread() {
Runnable runnable = new Runnable() {
public void run() {
Request();
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
private void Request() {
try {
SystemClock.sleep(7000);
Log.i("starwisp", "Pinging URL");
String u="http://192.168.2.1:8888/mongoose?function_name=ping";
Log.i("starwisp",u);
URL url = new URL(u);
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
con.setReadTimeout(10000 /* milliseconds */);
con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("GET");
con.setDoInput(true);
// Starts the query
con.connect();
Log.i("starwisp", "Connection open");
readStream(con.getInputStream());
Log.i("starwisp", "read stream, ending");
} catch (Exception e) {
Log.i("starwisp",e.toString());
e.printStackTrace();
}
}
private void readStream(InputStream in) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
Log.i("starwisp",line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class WiFiScanReceiver extends BroadcastReceiver {
......@@ -99,11 +173,10 @@ public class NetworkManager {
}
}
/* if (nm.state==State.SCANNING) {
if (nm.state==State.SCANNING) {
Log.i("starwisp", "REScanning "+nm.state);
nm.wifi.startScan();
}*/
}
}
}
}
......
// Starwisp Copyright (C) 2013 Dave Griffiths
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package foam.mongoose;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
public class SyncActivity extends StarwispActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
m_Name = "sync";
super.onCreate(savedInstanceState);
}
}
......@@ -74,53 +74,11 @@ public class starwisp extends StarwispActivity
ActivityManager.Register("update-individual",UpdateIndividualActivity.class);
ActivityManager.Register("tag-location",TagLocationActivity.class);
ActivityManager.Register("sync",SyncActivity.class);
};
NetworkManager nm;
/* void RequestThread() {
Runnable runnable = new Runnable() {
public void run() {
Request();
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
void Request() {
try {
URL url = new URL("http://192.168.2.1:8888/mongoose?function_name=ping");
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
private void readStream(InputStream in) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
*/
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment