Commit 897addd2 authored by Dave Griffiths's avatar Dave Griffiths
Browse files

simplified syncing and fixed debug text (hopefully)

parent cdff9bef
...@@ -216,9 +216,9 @@ ...@@ -216,9 +216,9 @@
(define (spit db table entities) (define (spit db table entities)
(foldl (foldl
(lambda (e r) (lambda (e r)
(debug! (string-append "Sending a " (car (car e)) " to Raspberry Pi"))
(append (append
(list (list
(debug (string-append "Sending a " (car (car e)) " to Raspberry Pi"))
(http-request (http-request
(string-append "req-" (list-ref (car e) 1)) (string-append "req-" (list-ref (car e) 1))
(build-url-from-entity table e) (build-url-from-entity table e)
...@@ -227,20 +227,17 @@ ...@@ -227,20 +227,17 @@
(cond (cond
((or (equal? (car v) "inserted") (equal? (car v) "match")) ((or (equal? (car v) "inserted") (equal? (car v) "match"))
(update-entity-clean db table (cadr v)) (update-entity-clean db table (cadr v))
(list (debug! (string-append "Uploaded " (car (car e)))))
(debug (string-append "Uploaded " (car (car e))))))
((equal? (car v) "no change") ((equal? (car v) "no change")
(list (debug (string-append "No change for " (car (car e)))))) (debug! (string-append "No change for " (car (car e)))))
((equal? (car v) "updated") ((equal? (car v) "updated")
(update-entity-clean db table (cadr v)) (update-entity-clean db table (cadr v))
(list (debug (string-append "Updated changed " (car (car e)))))) (debug! (string-append "Updated changed " (car (car e)))))
(else (else
(list (toast (string-append (debug! (string-append
"Problem uploading " "Problem uploading "
(car (car e)) " : " (car v))) (car (car e)) " : " (car v)))))
(debug (string-append (list))))
"Problem uploading "
(car (car e)) " : " (car v)))))))))
r)) r))
'() '()
entities)) entities))
...@@ -266,15 +263,14 @@ ...@@ -266,15 +263,14 @@
(update-to-version (update-to-version
db table (get-entity-id db table unique-id) db table (get-entity-id db table unique-id)
(list-ref entity 2) ktvlist)) (list-ref entity 2) ktvlist))
(debug! (string-append (if exists "Got new: " "Updated: ") (ktv-get ktvlist "name")))
(list (list
(update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty)) (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty)))))))
(debug (string-append (if exists "Got new: " "Updated: ") (ktv-get ktvlist "name")))
(toast (string-append "Downloaded " (ktv-get ktvlist "name"))))))))
;; repeatedly read version and request updates ;; repeatedly read version and request updates
(define (suck-new db table) (define (suck-new db table)
(debug! "Requesting new entities")
(list (list
(debug "Requesting new entities")
(http-request (http-request
"new-entities-req" "new-entities-req"
(string-append url "fn=entity-versions&table=" table) (string-append url "fn=entity-versions&table=" table)
...@@ -296,15 +292,17 @@ ...@@ -296,15 +292,17 @@
r))) r)))
'() '()
data))) data)))
(if (null? r) (cond
(append ((null? r)
(list (debug! "All files up to date")
(debug "All files up to date") (append
(toast "All files up to date")) r) (list
(append (toast "All files up to date")) r))
(list (else
(debug (string-append "Requesting " (number->string (length r)) " entities")) (debug! (string-append
(toast (string-append "Requesting " (number->string (length r)) " entities"))) r))))))) "Requesting "
(number->string (length r)) " entities"))
r)))))))
(define (build-dirty) (define (build-dirty)
(let ((sync (get-dirty-stats db "sync")) (let ((sync (get-dirty-stats db "sync"))
...@@ -314,6 +312,32 @@ ...@@ -314,6 +312,32 @@
"Pack data: " (number->string (car sync)) "/" (number->string (cadr sync)) " " "Pack data: " (number->string (car sync)) "/" (number->string (cadr sync)) " "
"Focal data: " (number->string (car stream)) "/" (number->string (cadr stream))))) "Focal data: " (number->string (car stream)) "/" (number->string (cadr stream)))))
(define (upload-dirty db)
(let ((r (append
(spit db "sync" (dirty-entities db "sync"))
(spit db "stream" (dirty-entities db "stream")))))
(append (cond
((> (length r) 0)
(debug! (string-append "Uploading " (number->string (/ (length r) 2)) " items..."))
(list
(toast "Uploading data...")))
(else
(debug! "No data changed to upload")
(list
(toast "No data changed to upload")))) r)))
(define (connect-to-net fn)
(list
(network-connect
"network"
"mongoose-web"
(lambda (state)
(debug! (string-append "Raspberry Pi connection state now: " state))
(append
(if (equal? state "Connected") (fn) '())
(list
;;(update-widget 'text-view (get-id "sync-connect") 'text state)
))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; user interface abstraction ;; user interface abstraction
...@@ -481,11 +505,18 @@ ...@@ -481,11 +505,18 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
(define (debug txt) (define (debug! txt)
(set-current! 'debug-text (string-append txt "\n" (get-current 'debug-text ""))) (set-current! 'debug-text (string-append txt "\n" (get-current 'debug-text ""))))
(define (update-debug)
(update-widget 'debug-text-view (get-id "sync-debug") 'text (update-widget 'debug-text-view (get-id "sync-debug") 'text
(get-current 'debug-text ""))) (get-current 'debug-text "")))
(define (debug-timer-cb)
(list
(delayed "debug-timer" 1000 debug-timer-cb)
(update-debug)))
(define (timer-cb) (define (timer-cb)
(set-current! (set-current!
...@@ -791,7 +822,7 @@ ...@@ -791,7 +822,7 @@
(list (list
(vert (vert
(mtext "text" "Outcome") (mtext "text" "Outcome")
(spinner (make-id "gp-int-out") (list "Retreat" "Advance" "Fight & retreat" "Fight & win") fillwrap (spinner (make-id "gp-int-out") (list "Retreat" "Advance" "Fight retreat" "Fight win") fillwrap
(lambda (v) (lambda (v)
(entity-add-value! "outcome" "varchar" v) '()))) (entity-add-value! "outcome" "varchar" v) '())))
(vert (vert
...@@ -1643,62 +1674,47 @@ ...@@ -1643,62 +1674,47 @@
(text-view (make-id "sync-title") "Sync database" 40 fillwrap) (text-view (make-id "sync-title") "Sync database" 40 fillwrap)
(mtext "sync-dirty" "...") (mtext "sync-dirty" "...")
(horiz (horiz
(mbutton2 "sync-connect" "Connect" (mbutton2 "sync-all" "Sync me"
(lambda () (lambda ()
(list (connect-to-net
(network-connect (lambda ()
"network" (append
"mongoose-web" (upload-dirty db)
(lambda (state) (suck-new db "sync"))))))
(list
(debug (string-append "Raspberry Pi connection state now: " state))
(update-widget 'text-view (get-id "sync-connect") 'text state)))))))
(mbutton2 "sync-syncall" "Push all" (mbutton2 "sync-syncall" "Push all"
(lambda () (lambda ()
(let ((r (append (let ((r (append
(spit db "sync" (dirty-and-all-entities db "sync")) (spit db "sync" (dirty-and-all-entities db "sync"))
(spit db "stream" (dirty-and-all-entities db "stream"))))) (spit db "stream" (dirty-and-all-entities db "stream")))))
(cons (toast "Uploading data...") r)))) (cons (toast "Uploading data...") r)))))
(mbutton2 "sync-sync" "Push changes" (mtitle "" "Export data")
(lambda ()
(let ((r (append
(spit db "sync" (dirty-entities db "sync"))
(spit db "stream" (dirty-entities db "stream")))))
(append (if (> (length r) 0)
(list
(debug (string-append "Uploading " (number->string (/ (length r) 2)) " items..."))
(toast "Uploading data..."))
(list
(debug "No data changed to upload")
(toast "No data changed to upload"))) r))))
(mbutton2 "sync-pull" "Pull"
(lambda ()
(cons (toast "Downloading data...") (suck-new db "sync")))))
(text-view (make-id "sync-console") "..." 15 (layout 300 'wrap-content 1 'left 0))
(horiz (horiz
(mbutton2 "sync-download" "Download" (mbutton2 "sync-download" "Download"
(lambda () (lambda ()
(debug! (string-append "Downloading whole db"))
(append
(foldl (foldl
(lambda (e r) (lambda (e r)
(append (debug! (string-append "Downloading /sdcard/mongoose/" e ".csv"))
(list (cons
(debug (string-append "Downloading /sdcard/mongoose/" e ".csv")) (http-download
(http-download (string-append "getting-" e)
(string-append "getting-" e) (string-append url "fn=entity-csv&table=stream&type=" e)
(string-append url "fn=entity-csv&table=stream&type=" e) (string-append "/sdcard/mongoose/" e ".csv"))
(string-append "/sdcard/mongoose/" e ".csv")))
r)) r))
(list (list
(debug (string-append "Downloading whole db"))
(http-download (http-download
"getting-db" "getting-db"
"http://192.168.2.1:8888/mongoose.db" "http://192.168.2.1:8888/mongoose.db"
(string-append "/sdcard/mongoose/mongoose.db"))) (string-append "/sdcard/mongoose/mongoose.db"))
entity-types))) )
(mbutton2 "sync-export" "Export" entity-types)
(list))))
(mbutton2 "sync-export" "Email"
(lambda () (lambda ()
(debug! "Sending mail")
(list (list
(debug "Sending mail")
(send-mail (send-mail
"" ""
"From Mongoose2000" "Please find attached your mongoose data" "From Mongoose2000" "Please find attached your mongoose data"
...@@ -1723,13 +1739,15 @@ ...@@ -1723,13 +1739,15 @@
(lambda (activity arg) (lambda (activity arg)
(activity-layout activity)) (activity-layout activity))
(lambda (activity arg) (lambda (activity arg)
(list (append
(update-widget 'debug-text-view (get-id "sync-debug") 'text (get-current 'debug-text "")) (debug-timer-cb)
(update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty)) (list
)) (update-widget 'debug-text-view (get-id "sync-debug") 'text (get-current 'debug-text ""))
(lambda (activity) '()) (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))
(lambda (activity) '()) )))
(lambda (activity) '()) (lambda (activity) '())
(lambda (activity) (list (delayed "debug-timer" 1000 (lambda () '()))))
(lambda (activity) (list (delayed "debug-timer" 1000 (lambda () '()))))
(lambda (activity) '()) (lambda (activity) '())
(lambda (activity requestcode resultcode) '())) (lambda (activity requestcode resultcode) '()))
......
...@@ -39,6 +39,8 @@ public class NetworkManager { ...@@ -39,6 +39,8 @@ public class NetworkManager {
BroadcastReceiver receiver; BroadcastReceiver receiver;
public State state; public State state;
String SSID; String SSID;
static class Lock extends Object {}
static public Lock mLock = new Lock();
String m_CallbackName; String m_CallbackName;
StarwispActivity m_Context; StarwispActivity m_Context;
...@@ -49,19 +51,24 @@ public class NetworkManager { ...@@ -49,19 +51,24 @@ public class NetworkManager {
} }
void Start(String ssid, StarwispActivity c, String name, StarwispBuilder b) { void Start(String ssid, StarwispActivity c, String name, StarwispBuilder b) {
m_CallbackName=name;
m_Context=c; if (state==NetworkManager.State.IDLE) {
m_Builder=b; m_CallbackName=name;
wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE); m_Context=c;
state = State.SCANNING; m_Builder=b;
SSID = ssid; wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
wifi.startScan(); state = State.SCANNING;
receiver = new WiFiScanReceiver(SSID, this); SSID = ssid;
c.registerReceiver(receiver, new IntentFilter( wifi.startScan();
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); receiver = new WiFiScanReceiver(SSID, this);
c.registerReceiver(receiver, new IntentFilter(
// todo - won't work from inside fragments WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Scanning\"");
// todo - won't work from inside fragments
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Scanning\"");
} else {
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Connected\"");
}
} }
void Connect() { void Connect() {
...@@ -80,6 +87,13 @@ public class NetworkManager { ...@@ -80,6 +87,13 @@ public class NetworkManager {
wifi.enableNetwork(i.networkId, true); wifi.enableNetwork(i.networkId, true);
wifi.reconnect(); wifi.reconnect();
Log.i("starwisp", "Connected"); Log.i("starwisp", "Connected");
try {
Thread.sleep(2000);
} catch (Exception e) {
Log.i("starwisp",e.toString());
e.printStackTrace();
}
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Connected\""); m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Connected\"");
break; break;
} }
...@@ -171,11 +185,9 @@ public class NetworkManager { ...@@ -171,11 +185,9 @@ public class NetworkManager {
while ((len = in.read(buffer)) != -1) { while ((len = in.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len); byteBuffer.write(buffer, 0, len);
} }
m_Builder.SaveData(m.m_CallbackName, byteBuffer.toByteArray()); m_Builder.SaveData(m.m_CallbackName, byteBuffer.toByteArray());
} else { } else {
// results in evaluating data read from via http - fix if used from net // results in evaluating data read from via http - fix if used from net
reader = new BufferedReader(new InputStreamReader(in)); reader = new BufferedReader(new InputStreamReader(in));
String line = ""; String line = "";
String all = ""; String all = "";
...@@ -184,7 +196,10 @@ public class NetworkManager { ...@@ -184,7 +196,10 @@ public class NetworkManager {
} }
Log.i("starwisp","got data for "+m.m_CallbackName+"["+all+"]"); Log.i("starwisp","got data for "+m.m_CallbackName+"["+all+"]");
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m.m_CallbackName,all); synchronized (mLock)
{
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m.m_CallbackName,all);
}
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -676,7 +676,7 @@ public class StarwispBuilder ...@@ -676,7 +676,7 @@ public class StarwispBuilder
final Integer id = arr.getInt(1); final Integer id = arr.getInt(1);
String token = arr.getString(2); String token = arr.getString(2);
//Log.i("starwisp", "Update: "+type+" "+id+" "+token); Log.i("starwisp", "Update: "+type+" "+id+" "+token);
// non widget commands // non widget commands
if (token.equals("toast")) { if (token.equals("toast")) {
...@@ -817,11 +817,9 @@ public class StarwispBuilder ...@@ -817,11 +817,9 @@ public class StarwispBuilder
} }
if (token.equals("network-connect")) { if (token.equals("network-connect")) {
if (m_NetworkManager.state==NetworkManager.State.IDLE) { final String name = arr.getString(3);
final String name = arr.getString(3); final String ssid = arr.getString(5);
final String ssid = arr.getString(5); m_NetworkManager.Start(ssid,(StarwispActivity)ctx,name,this);
m_NetworkManager.Start(ssid,(StarwispActivity)ctx,name,this);
}
return; return;
} }
......
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