starwisp.scm 64.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
;; 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/>.

16 17 18 19 20
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; strings

(define obs-gc "Group Composition")
(define obs-pf "Pup Focal")
21 22
(define obs-gp "Group Events")

23 24 25 26 27 28 29 30 31 32 33 34
(define entity-types
  (list
   "pup-focal"
   "pup-focal-nearest"
   "pup-focal-pupfeed"
   "pup-focal-pupfind"
   "pup-focal-pupcare"
   "pup-focal-pupaggr"
   "group-interaction"
   "group-alarm"
   "group-move"))

35 36
;; colours

37 38 39 40 41 42 43 44 45 46 47 48
(define pf-col (list 255 204 51 255))
(define gp-col (list 255 102 0 255))
(define gc-col (list 164 82 9 255))

(define pf-bgcol (list 255 204 51 127))
(define gp-bgcol (list 255 102 0 127))
(define gc-bgcol (list 164 82 9 127))

;(define pf-col (list  22  19 178  127))
;(define gp-col (list 255  97   0  127))
;(define gc-col (list 255 236   0  127))

49 50 51


(define trans-col (list 0 0 0 0))
52 53 54 55 56 57 58 59 60 61 62 63 64 65

(define (get-fragment-index name frag)
  (define (_ i l)
    (cond
     ((null? l) 0)
     ((equal? name (cadr (car l))) i)
     (else (_ (+ i 1) (cdr l)))))
  (_ 0 frag))

(define gc-fragments
  (list
   (list "Start" "gc-start")
   (list "Weights" "gc-weights")
   (list "Pregnant" "gc-preg")
66
   (list "Pup assoc" "gc-pup-assoc")
67 68 69 70
   (list "Oestrus" "gc-oestrus")
   (list "Babysit" "gc-babysitting")
   (list "End" "gc-end")))

71
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Dave Griffiths's avatar
Dave Griffiths committed
72
;; persistent database
73

74
(define db "/sdcard/mongoose/local-mongoose.db")
Dave Griffiths's avatar
Dave Griffiths committed
75
(db-open db)
76 77 78 79 80 81 82 83 84 85
(setup db "local")
(setup db "sync")
(setup db "stream")

(insert-entity-if-not-exists
 db "local" "app-settings" "null" 1
 (list
  (ktv "user-id" "varchar" "No name yet...")))

(display (db-all db "local" "app-settings"))(newline)
Dave Griffiths's avatar
Dave Griffiths committed
86

Dave Griffiths's avatar
Dave Griffiths committed
87 88
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; stuff in memory
Dave Griffiths's avatar
Dave Griffiths committed
89

Dave Griffiths's avatar
Dave Griffiths committed
90 91
(define (store-set store key value)
  (cond
92 93 94 95 96
   ((null? store) (list (list key value)))
   ((eq? key (car (car store)))
    (cons (list key value) (cdr store)))
   (else
    (cons (car store) (store-set (cdr store) key value)))))
Dave Griffiths's avatar
Dave Griffiths committed
97

Dave Griffiths's avatar
Dave Griffiths committed
98
(define (store-get store key default)
Dave Griffiths's avatar
Dave Griffiths committed
99
  (cond
100 101 102 103 104 105 106 107 108 109 110 111 112
   ((null? store) default)
   ((eq? key (car (car store)))
    (cadr (car store)))
   (else
    (store-get (cdr store) key default))))

(define (store-exists? store key)
  (cond
   ((null? store) #f)
   ((eq? key (car (car store)))
    #t)
   (else
    (store-exists? (cdr store) key))))
Dave Griffiths's avatar
Dave Griffiths committed
113

Dave Griffiths's avatar
Dave Griffiths committed
114
(define store '())
Dave Griffiths's avatar
Dave Griffiths committed
115

Dave Griffiths's avatar
Dave Griffiths committed
116 117
(define (set-current! key value)
  (set! store (store-set store key value)))
Dave Griffiths's avatar
Dave Griffiths committed
118

Dave Griffiths's avatar
Dave Griffiths committed
119 120
(define (get-current key default)
  (store-get store key default))
Dave Griffiths's avatar
Dave Griffiths committed
121

122 123 124
(define (current-exists? key)
  (store-exists? store key))

125 126 127 128
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; db abstraction

;; store a ktv, replaces existing with same key
129 130 131
(define (entity-add-value! key type value)
  (set-current!
   'entity-values
132
   (ktv-set
133 134
    (get-current 'entity-values '())
    (ktv key type value))))
135

Dave Griffiths's avatar
Dave Griffiths committed
136 137 138
(define (entity-set! ktv-list)
  (set-current! 'entity-values ktv-list))

139
(define (date-time->string dt)
140 141 142
  (string-append
   (number->string (list-ref dt 0)) "-"
   (number->string (list-ref dt 1)) "-"
143
   (number->string (list-ref dt 2)) " "
144 145
   (number->string (list-ref dt 3)) ":"
   (number->string (list-ref dt 4)) ":"
146
   (substring (number->string (+ 100 (list-ref dt 5))) 1 2)))
147

148 149
;; build entity from all ktvs, insert to db, return unique_id
(define (entity-record-values db table type)
150 151
  ;; standard bits
  (entity-add-value! "user" "varchar" (get-current 'user-id "none"))
152
  (entity-add-value! "time" "varchar" (date-time->string (date-time)))
153 154
  (entity-add-value! "lat" "real" 0)
  (entity-add-value! "lon" "real" 0)
155 156 157 158 159 160 161 162 163 164 165
  (let ((values (get-current 'entity-values '())))
    (cond
     ((not (null? values))
      (let ((r (insert-entity/get-unique
                db table type (get-current 'user-id "no id")
                values)))
        (msg "inserted a " type)
        (entity-reset!) r))
     (else
      (msg "no values to add as entity!") #f))))

Dave Griffiths's avatar
Dave Griffiths committed
166 167 168 169 170 171 172 173 174 175 176 177
(define (entity-update-values db table)
  ;; standard bits
  (let ((values (get-current 'entity-values '()))
        (unique-id (ktv-get (get-current 'entity-values '()) "unique_id")))
    (cond
     ((and unique-id (not (null? values)))
      (update-entity db table (entity-id-from-unique db table unique-id) values)
      (msg "updated " unique-id)
      (entity-reset!))
     (else
      (msg "no values or no id to update as entity:" unique-id "values:" values)))))

178 179 180 181 182 183 184 185 186 187
(define (entity-reset!)
  (set-current! 'entity-values '()))

(define (assemble-array entities)
  (foldl
   (lambda (i r)
     (if (equal? r "") (ktv-get i "unique_id")
         (string-append r "," (ktv-get i "unique_id"))))
   ""
   entities))
188

Dave Griffiths's avatar
Dave Griffiths committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syncing code

(define url "http://192.168.2.1:8888/mongoose?")

(define (build-url-from-ktv ktv)
  (string-append "&" (ktv-key ktv) ":" (ktv-type ktv) "=" (stringify-value-url ktv)))

(define (build-url-from-ktvlist ktvlist)
  (foldl
   (lambda (ktv r)
     (string-append r (build-url-from-ktv ktv)))
   "" ktvlist))

(define (build-url-from-entity table e)
  (string-append
   url
   "fn=sync"
   "&table=" table
   "&entity-type=" (list-ref (car e) 0)
   "&unique-id=" (list-ref (car e) 1)
Dave Griffiths's avatar
Dave Griffiths committed
210 211
   "&dirty=" (number->string (list-ref (car e) 2))
   "&version=" (number->string (list-ref (car e) 3))
Dave Griffiths's avatar
Dave Griffiths committed
212 213 214
   (build-url-from-ktvlist (cadr e))))

;; spit all dirty entities to server
215 216 217
(define (spit db table entities)
  (foldl
   (lambda (e r)
218
     (debug! (string-append "Sending a " (car (car e)) " to Raspberry Pi"))
219 220 221 222 223 224 225 226 227
     (append
      (list
       (http-request
        (string-append "req-" (list-ref (car e) 1))
        (build-url-from-entity table e)
        (lambda (v)
          (cond
           ((or (equal? (car v) "inserted") (equal? (car v) "match"))
            (update-entity-clean db table (cadr v))
228
            (debug! (string-append "Uploaded " (car (car e)))))
229
           ((equal? (car v) "no change")
230
            (debug! (string-append "No change for " (car (car e)))))
231 232
           ((equal? (car v) "updated")
            (update-entity-clean db table (cadr v))
233
            (debug! (string-append "Updated changed " (car (car e)))))
234
           (else
235 236 237
            (debug! (string-append
                     "Problem uploading "
                     (car (car e)) " : " (car v)))))
Dave Griffiths's avatar
Dave Griffiths committed
238 239
          (list
           (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))))))
240 241 242
      r))
   '()
   entities))
243

244
(define (suck-entity-from-server db table unique-id exists)
Dave Griffiths's avatar
Dave Griffiths committed
245 246 247 248 249 250
  ;; ask for the current version
  (http-request
   (string-append unique-id "-update-new")
   (string-append url "fn=entity&table=" table "&unique-id=" unique-id)
   (lambda (data)
     ;; check "sync-insert" in sync.ss raspberry pi-side for the contents of 'entity'
251 252
     (let ((entity (list-ref data 0))
           (ktvlist (list-ref data 1)))
Dave Griffiths's avatar
Dave Griffiths committed
253
       (if (not exists)
254 255 256 257 258 259 260
           (insert-entity-wholesale
            db table
            (list-ref entity 0) ;; entity-type
            (list-ref entity 1) ;; unique-id
            0 ;; dirty
            (list-ref entity 2) ;; version
            ktvlist)
Dave Griffiths's avatar
Dave Griffiths committed
261 262
           (update-to-version
            db table (get-entity-id db table unique-id)
263
            (list-ref entity 2) ktvlist))
264
       (debug! (string-append (if exists "Got new: " "Updated: ") (ktv-get ktvlist "name")))
Dave Griffiths's avatar
Dave Griffiths committed
265
       (list
266
        (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty)))))))
Dave Griffiths's avatar
Dave Griffiths committed
267

Dave Griffiths's avatar
Dave Griffiths committed
268
;; repeatedly read version and request updates
Dave Griffiths's avatar
Dave Griffiths committed
269
(define (suck-new db table)
270
  (debug! "Requesting new entities")
Dave Griffiths's avatar
Dave Griffiths committed
271 272 273
  (list
   (http-request
    "new-entities-req"
274
    (string-append url "fn=entity-versions&table=" table)
Dave Griffiths's avatar
Dave Griffiths committed
275
    (lambda (data)
Dave Griffiths's avatar
Dave Griffiths committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
      (let ((r (foldl
                (lambda (i r)
                  (let* ((unique-id (car i))
                         (version (cadr i))
                         (exists (entity-exists? db table unique-id))
                         (old
                          (if exists
                              (> version (get-entity-version
                                          db table
                                          (get-entity-id db table unique-id)))
                              #f)))
                    ;; if we don't have this entity or the version on the server is newer
                    (if (or (not exists) old)
                        (cons (suck-entity-from-server db table unique-id exists) r)
                        r)))
                '()
                data)))
293 294
        (cond
         ((null? r)
Dave Griffiths's avatar
Dave Griffiths committed
295
          (debug! "No new data to download")
Dave Griffiths's avatar
Dave Griffiths committed
296
          (set-current! 'download 1)
297
          (append
Dave Griffiths's avatar
Dave Griffiths committed
298 299
           (if (eqv? (get-current 'upload 0) 1)
               (list (play-sound "ping")) '())
300
           (list
Dave Griffiths's avatar
Dave Griffiths committed
301
            (toast "No new data to download")) r))
302 303 304 305 306
         (else
          (debug! (string-append
                   "Requesting "
                   (number->string (length r)) " entities"))
          r)))))))
Dave Griffiths's avatar
Dave Griffiths committed
307

308 309 310 311 312 313 314
(define (build-dirty)
  (let ((sync (get-dirty-stats db "sync"))
        (stream (get-dirty-stats db "stream")))
    (string-append
     "Pack data: " (number->string (car sync)) "/" (number->string (cadr sync)) " "
     "Focal data: " (number->string (car stream)) "/" (number->string (cadr stream)))))

315 316 317 318 319 320
(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)
Dave Griffiths's avatar
Dave Griffiths committed
321
              (debug! (string-append "Uploading " (number->string (length r)) " items..."))
322 323 324 325
              (list
               (toast "Uploading data...")))
             (else
              (debug! "No data changed to upload")
Dave Griffiths's avatar
Dave Griffiths committed
326 327 328 329 330
              (set-current! 'upload 1)
              (append
               (if (eqv? (get-current 'download 0) 1)
                   (list (play-sound "ping")) '())
               (list
Dave Griffiths's avatar
Dave Griffiths committed
331
                (toast "No data changed to upload"))))) r)))
332 333 334 335 336 337 338 339 340 341 342 343 344

(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)
        ))))))
345

Dave Griffiths's avatar
Dave Griffiths committed
346
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347
;; user interface abstraction
Dave Griffiths's avatar
Dave Griffiths committed
348

Dave Griffiths's avatar
Dave Griffiths committed
349
(define (mbutton id title fn)
350
  (button (make-id id) title 30 fillwrap fn))
Dave Griffiths's avatar
Dave Griffiths committed
351

352
(define (mbutton2 id title fn)
353
  (button (make-id id) title 30 (layout 150 100 1 'centre 0) fn))
354

355
(define (mtoggle-button id title fn)
356
  (toggle-button (make-id id) title 30 (layout 'fill-parent 'wrap-content 1 'centre 0) "fancy" fn))
357

358
(define (mtoggle-button2 id title fn)
359
  (toggle-button (make-id id) title 30 (layout 150 100 1 'centre 0) "plain" fn))
360

Dave Griffiths's avatar
Dave Griffiths committed
361
(define (mtext id text)
362
  (text-view (make-id id) text 30 fillwrap))
363

364
(define (mtitle id text)
365
  (text-view (make-id id) text 50 fillwrap))
366

367 368
(define (medit-text id text type fn)
  (vert
369
   (mtext (string-append id "-title") text)
370
   (edit-text (make-id id) "" 30 type fillwrap fn)))
371 372 373 374 375 376 377

(define (mclear-toggles id-list)
  (map
   (lambda (id)
     (update-widget 'toggle-button (get-id id) 'checked 0))
   id-list))

378 379 380 381 382 383 384
(define (mclear-toggles-not-me me id-list)
  (foldl
   (lambda (id r)
     (if (equal? me id)
         r (cons (update-widget 'toggle-button (get-id id) 'checked 0) r)))
   '() id-list))

385 386 387 388 389 390 391 392 393 394
(define (xwise n l)
  (define (_ c l)
    (cond
      ((null? l) (if (null? c) '() (list c)))
      ((eqv? (length c) (- n 1))
       (cons (append c (list (car l))) (_ '() (cdr l))))
      (else
       (_ (append c (list (car l))) (cdr l)))))
  (_ '() l))

395 396
;;;;

397
(define (build-grid-selector name type title)
398 399 400 401 402 403 404 405 406 407
  (linear-layout
   0 'vertical
   (layout 'fill-parent 'wrap-content 1 'left 0)
   (list 0 0 0 0)
   (list
    (mtext "title" title)
    (linear-layout
     0 'horizontal
     (layout 'fill-parent 'wrap-content 1 'left 2) trans-col
     (list
408
      (image-view (make-id "im") "arrow_left" (layout 200 'fill-parent 1 'left 0))
409 410 411 412 413 414 415 416
      (scroll-view
       (make-id "scroller")
       (layout 'wrap-content 'wrap-content 1 'left 20)
       (list
        (linear-layout
         (make-id name) 'horizontal
         (layout 'wrap-content 'wrap-content 1 'centre 20) trans-col
         (list
417
          (button-grid (make-id name) type 3 30 (layout 100 60 1 'left 40)
418
                       (list) (lambda (v) '()))))))
419
      (image-view (make-id "im") "arrow_right" (layout 200 'fill-parent 1 'right 0)))))))
420

421
;; assumes grid selectors on mongeese only
422 423 424
(define (fast-get-name item)
  (list-ref (list-ref item 1) 2))

425 426 427 428 429 430 431 432 433
(define (build-button-items name items)
  (map
   (lambda (item)
     (let ((item-name (fast-get-name item)))
       (list (make-id (string-append name item-name))
             item
             item-name)))
   items))

434
(define (populate-grid-selector name type items fn)
435 436
  (prof-start "popgrid")
  (prof-start "popgrid setup")
437 438
  (let ((id->items (build-button-items name items))
        (selected-set '()))
439 440
    (prof-end "popgrid setup")
    (let ((r (update-widget
441 442
     'button-grid (get-id name) 'grid-buttons
     (list
443
      type 3 30 (layout 100 60 1 'left 0)
444 445 446 447
      (map
       (lambda (ii)
         (list (car ii) (caddr ii)))
       id->items)
448 449
      (lambda (v state)
        (cond
450 451
         ((equal? type "toggle")
          ;; update list of selected items
452 453 454 455 456 457 458 459 460
          (if state
              (set! selected-set (set-add v selected-set))
              (set! selected-set (set-remove v selected-set)))
          ;; find all items currently selected
          (fn (map
               (lambda (v)
                 (cadr (findv v id->items)))
               selected-set)))
         (else
Dave Griffiths's avatar
Dave Griffiths committed
461
          ;;(msg (findv v id->items))
462
          (fn (cadr (findv v id->items))))))))))
463 464 465 466
      (prof-end "popgrid")
      r)))

(define (db-mongooses-by-pack)
467
  (db-all-where
468 469
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))))
470

471
(define (db-mongooses-by-pack-male)
472
  (db-all-where2or
473 474
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
475
   (ktv "gender" "varchar" "Male") "Unknown"))
476 477

(define (db-mongooses-by-pack-female)
478
  (db-all-where2or
479 480
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
481
   (ktv "gender" "varchar" "Female") "Unknown"))
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497


;; (y m d h m s)
(define (date-minus-months d ms)
  (let ((year (list-ref d 0))
        (month (- (list-ref d 1) 1)))
    (let ((new-month (- month ms)))
      (list
       (if (< new-month 0) (- year 1) year)
       (+ (if (< new-month 0) (+ new-month 12) new-month) 1)
       (list-ref d 2)
       (list-ref d 3)
       (list-ref d 4)
       (list-ref d 5)))))

(define (db-mongooses-by-pack-pups)
Dave Griffiths's avatar
Dave Griffiths committed
498
  (db-all-newer
499 500 501 502
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
   (ktv "dob" "varchar" (date->string (date-minus-months (date-time) 6)))))

503 504 505 506 507 508
(define (db-mongooses-by-pack-adults)
  (db-all-older
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
   (ktv "dob" "varchar" (date->string (date-minus-months (date-time) 6)))))

509 510
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
Dave Griffiths's avatar
Dave Griffiths committed
511

512 513 514 515
(define (debug! txt)
  (set-current! 'debug-text (string-append txt "\n" (get-current 'debug-text ""))))

(define (update-debug)
516 517 518
  (update-widget 'debug-text-view (get-id "sync-debug") 'text
                 (get-current 'debug-text "")))

519 520 521 522 523
(define (debug-timer-cb)
  (list
   (delayed "debug-timer" 1000 debug-timer-cb)
   (update-debug)))

524

525 526
(define pf-length 20) ;; minutes...

527 528 529 530 531 532 533
(define (timer-cb)
  (set-current!
   'timer-seconds
   (- (get-current 'timer-seconds 59) 1))
  (append
   (cond
    ((< (get-current 'timer-seconds 59) 0)
534
     (set-current! 'timer-minutes (- (get-current 'timer-minutes pf-length) 1))
535
     (set-current! 'timer-seconds 59)
536 537 538 539 540 541 542 543 544 545 546 547 548 549
     (cond ((< (get-current 'timer-minutes pf-length) 1)
            (list
             (alert-dialog
              "pup-focal-end"
              "Pup focal time is up, have you finished?"
              (lambda (v)
                (cond
                 ((eqv? v 1)
                  (list (finish-activity 1)))
                 (else
                  (set-current! 'timer-minutes 1)
                  (list)))))))
           (else
            (list (replace-fragment (get-id "pf-top") "pf-scan1")))))
550 551 552 553 554
    (else '()))
   (list
    (delayed "timer" 1000 timer-cb)
    (update-widget
     'text-view (get-id "pf-timer-time-minutes") 'text
555
     (string-append (number->string (get-current 'timer-minutes pf-length))))
556 557 558 559 560
    (update-widget
     'text-view (get-id "pf-timer-time") 'text
     (string-append (number->string (get-current 'timer-seconds 59))))
    )))

Dave Griffiths's avatar
Dave Griffiths committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
(define (next-button id dialog-msg next-frag fn)
     (mbutton (string-append id "-nextb") "Next"
              (lambda ()
                (list
                 (alert-dialog
                  (string-append id "-d")
                  dialog-msg
                  (lambda (v)
                    (cond
                     ((eqv? v 1)
                      (append
                       (fn) (list (replace-fragment
                                   (get-id "gc-top") next-frag))))
                     (else '()))))))))

576 577 578 579
(define (force-pause)
  (list
   (delayed "timer" 1000 (lambda () '()))
   (update-widget 'toggle-button (get-id "pf-pause") 'checked 1)))
Dave Griffiths's avatar
Dave Griffiths committed
580

581 582
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; fragments
Dave Griffiths's avatar
Dave Griffiths committed
583 584 585

(define-fragment-list

586 587 588
  (fragment
   "pf-timer"
   (linear-layout
589
    (make-id "") 'vertical fillwrap trans-col
590
    (list
591
     (mtitle "pf-details" "Pack: xxx Pup: xxx")))
592 593 594
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
595 596 597 598 599 600
     (list
      (update-widget 'text-view (get-id "pf-details") 'text
                     (string-append
                      "Pack: " (ktv-get (get-current 'pack '()) "name") " "
                      "Pup: " (ktv-get (get-current 'individual '()) "name"))
                     )))
601 602 603 604 605 606 607
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


  (fragment
608
   "events"
609
   (linear-layout
610
    0 'vertical fillwrap trans-col
611
    (list
612
     (linear-layout
Dave Griffiths's avatar
Dave Griffiths committed
613
      (make-id "ev-pf") 'vertical fill pf-col
614
      (list
615 616
       (mtitle "ev-pf-text" "Pup Focal Events")
       (horiz
Dave Griffiths's avatar
Dave Griffiths committed
617 618 619 620
        (mbutton2 "evb-pupfeed" "Pup Feed" (lambda () (list (replace-fragment (get-id "event-holder") "ev-pupfeed"))))
        (mbutton2 "evb-pupfind" "Pup Find" (lambda () (list (replace-fragment (get-id "event-holder") "ev-pupfind"))))
        (mbutton2 "evb-pupcare" "Pup Care" (lambda () (list (replace-fragment (get-id "event-holder") "ev-pupcare"))))
        (mbutton2 "evb-pupagg" "Pup Aggression" (lambda () (list (replace-fragment (get-id "event-holder") "ev-pupaggr")))))))
621
     (linear-layout
622
      (make-id "ev-pf") 'vertical fill gp-col
623
      (list
624 625
       (mtitle "text" "Group Events")
       (horiz
Dave Griffiths's avatar
Dave Griffiths committed
626 627
        (mbutton2 "evb-grpint" "Interaction" (lambda () (list (replace-fragment (get-id "event-holder") "ev-grpint"))))
        (mbutton2 "evb-grpalarm" "Alarm" (lambda () (list (replace-fragment (get-id "event-holder") "ev-grpalarm"))))
628 629
        (mbutton2 "evb-grpmov" "Movement" (lambda () (list (replace-fragment (get-id "event-holder") "ev-grpmov"))))
        (mbutton2 "evb-grpnote" "Note" (lambda () (list (replace-fragment (get-id "event-holder") "note")))))))))
630 631 632
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
633
     (if (equal? (get-current 'observation "none") obs-pf)
634 635
         (list
          (update-widget 'text-view (get-id "ev-pf-text") 'show 0)
Dave Griffiths's avatar
Dave Griffiths committed
636 637 638 639
          (update-widget 'linear-layout (get-id "ev-pf") 'show 0))
         (list
          (update-widget 'text-view (get-id "ev-pf-text") 'hide 0)
          (update-widget 'linear-layout (get-id "ev-pf") 'hide 0))))
640 641 642 643
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))
644

645 646 647
  (fragment
   "pf-scan1"
   (linear-layout
648
    (make-id "") 'vertical fillwrap pf-col
649
    (list
Dave Griffiths's avatar
Dave Griffiths committed
650
     (build-grid-selector "pf-scan-nearest" "single" "<b>Nearest Neighbour Scan</b>: Closest Mongoose")
651
     (build-grid-selector "pf-scan-close" "toggle" "Mongooses within 2m")
652 653 654 655
     (mbutton "pf-scan-done" "Done"
              (lambda ()
                (entity-add-value! "parent" "varchar" (get-current 'pup-focal-id ""))
                (entity-record-values db "stream" "pup-focal-nearest")
656
                (list (replace-fragment (get-id "pf-top") "pf-timer"))))))
657 658 659 660

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
661 662
     (entity-reset!)
     (entity-add-value! "scan-time" "varchar" (date-time->string (date-time)))
663
     (list
664 665
      (play-sound "ping")
      (vibrate 300)
666
      (populate-grid-selector
667
       "pf-scan-nearest" "single"
668
       (db-mongooses-by-pack-adults)
669 670
       (lambda (individual)
         (entity-add-value! "id-nearest" "varchar" (ktv-get individual "unique_id"))
671 672
         (list)))
      (populate-grid-selector
673
       "pf-scan-close" "toggle"
674
       (db-mongooses-by-pack-adults)
675 676
       (lambda (individuals)
         (entity-add-value! "id-list-close" "varchar" (assemble-array individuals))
677 678
         (list)))
      ))
679 680 681 682 683 684
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


685 686 687
  (fragment
   "ev-pupfeed"
   (linear-layout
688
    (make-id "") 'vertical fillwrap pf-col
689
    (list
690
     (mtitle "title" "Event: Pup is fed")
691
     (build-grid-selector "pf-pupfeed-who" "single" "Who fed the pup?")
Dave Griffiths's avatar
Dave Griffiths committed
692
     (spacer 20)
693
     (horiz
Dave Griffiths's avatar
Dave Griffiths committed
694
      (mtext "text" "Food size")
695 696
      (spinner (make-id "pf-pupfeed-size") (list "Small" "Medium" "Large") fillwrap
               (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
697 698 699
                 (entity-add-value! "size" "varchar" v) '())))
     (spacer 20)
     (horiz
700 701 702 703
      (mbutton "pf-pupfeed-done" "Done"
               (lambda ()
                 (entity-add-value! "parent" "varchar" (get-current 'pup-focal-id ""))
                 (entity-record-values db "stream" "pup-focal-pupfeed")
704 705 706
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupfeed-cancel" "Cancel"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
707
                 (list (replace-fragment (get-id "event-holder") "events")))))))
708 709 710 711

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
712
     (entity-reset!)
713 714 715
     (list
      (populate-grid-selector
       "pf-pupfeed-who" "single"
716
       (db-mongooses-by-pack-adults)
717
       (lambda (individual)
718
         (entity-add-value! "id-who" "varchar" (ktv-get individual "unique_id"))
719 720 721 722 723 724 725
         (list)))
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

Dave Griffiths's avatar
Dave Griffiths committed
726 727 728
  (fragment
   "ev-pupfind"
   (linear-layout
729
    (make-id "") 'vertical fillwrap pf-col
Dave Griffiths's avatar
Dave Griffiths committed
730
    (list
731
     (mtitle "title" "Event: Pup found food")
Dave Griffiths's avatar
Dave Griffiths committed
732 733 734 735 736
     (horiz
      (mtext "text" "Food size")
      (spinner (make-id "pf-pupfind-size") (list "Small" "Medium" "Large") fillwrap
               (lambda (v) (entity-add-value! "size" "varchar" v) '())))
     (spacer 20)
Dave Griffiths's avatar
Dave Griffiths committed
737
     (horiz
738 739 740 741
      (mbutton "pf-pupfind-done" "Done"
               (lambda ()
                 (entity-add-value! "parent" "varchar" (get-current 'pup-focal-id ""))
                 (entity-record-values db "stream" "pup-focal-pupfind")
742 743 744
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupfind-cancel" "Cancel"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
745
                 (list (replace-fragment (get-id "event-holder") "events")))))))
Dave Griffiths's avatar
Dave Griffiths committed
746 747 748 749

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
750
     (entity-reset!)
Dave Griffiths's avatar
Dave Griffiths committed
751 752 753 754 755 756 757 758 759 760 761
     (list
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


  (fragment
   "ev-pupcare"
   (linear-layout
762
    (make-id "") 'vertical fillwrap pf-col
Dave Griffiths's avatar
Dave Griffiths committed
763
    (list
764
     (mtitle "title" "Event: Pup is cared for")
Dave Griffiths's avatar
Dave Griffiths committed
765
     (build-grid-selector "pf-pupcare-who" "single" "Who cared for the pup?")
Dave Griffiths's avatar
Dave Griffiths committed
766
     (spacer 20)
Dave Griffiths's avatar
Dave Griffiths committed
767
     (horiz
Dave Griffiths's avatar
Dave Griffiths committed
768
      (mtext "text" "Type of care")
769 770
      (spinner (make-id "pf-pupcare-type") (list "Carry" "Lead" "Sniff" "Play" "Ano-genital sniff") fillwrap
               (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
771 772 773
                 (entity-add-value! "type" "varchar" v) '())))
     (spacer 20)
     (horiz
774 775 776 777
      (mbutton "pf-pupcare-done" "Done"
               (lambda ()
                 (entity-add-value! "parent" "varchar" (get-current 'pup-focal-id ""))
                 (entity-record-values db "stream" "pup-focal-pupcare")
778 779 780
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupcare-cancel" "Cancel"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
781
                 (list (replace-fragment (get-id "event-holder") "events")))))))
Dave Griffiths's avatar
Dave Griffiths committed
782 783 784 785

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
786
     (entity-reset!)
Dave Griffiths's avatar
Dave Griffiths committed
787 788 789
     (list
      (populate-grid-selector
       "pf-pupcare-who" "single"
790
       (db-mongooses-by-pack-adults)
Dave Griffiths's avatar
Dave Griffiths committed
791
       (lambda (individual)
792
         (entity-add-value! "id-who" "varchar" (ktv-get individual "unique_id"))
Dave Griffiths's avatar
Dave Griffiths committed
793 794 795 796 797 798 799 800 801 802
         (list)))
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

  (fragment
   "ev-pupaggr"
   (linear-layout
803
    (make-id "") 'vertical fillwrap pf-col
Dave Griffiths's avatar
Dave Griffiths committed
804
    (list
805
     (mtitle "title" "Event: Pup aggression")
Dave Griffiths's avatar
Dave Griffiths committed
806 807 808
     (build-grid-selector "pf-pupaggr-partner" "single" "Aggressive mongoose")

     (linear-layout
809
      (make-id "") 'horizontal (layout 'fill-parent 100 '1 'left 0) trans-col
Dave Griffiths's avatar
Dave Griffiths committed
810 811 812
      (list
       (vert
        (mtext "" "Fighting over")
813 814 815
        (spinner (make-id "pf-pupaggr-over") (list "Food" "Escort" "Nothing" "Other") fillwrap
                 (lambda (v)
                   (entity-add-value! "over" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
816 817
       (vert
        (mtext "" "Level")
818 819 820 821 822
        (spinner (make-id "pf-pupaggr-level") (list "Block" "Snap" "Chase" "Push" "Fight") fillwrap
                 (lambda (v)
                   (entity-add-value! "level" "varchar" v) '())))
       (mtoggle-button "pf-pupaggr-in" "Initiate?"
                       (lambda (v)
823
                         (entity-add-value! "initiate" "varchar" (if v "yes" "no")) '()))
824 825
       (mtoggle-button "pf-pupaggr-win" "Win?"
                       (lambda (v)
826
                         (entity-add-value! "win" "varchar" (if v "yes" "no")) '()))))
Dave Griffiths's avatar
Dave Griffiths committed
827
     (spacer 20)
828 829 830 831 832 833 834 835 836 837
     (horiz
      (mbutton "pf-pupaggr-done" "Done"
               (lambda ()
                 (entity-add-value! "parent" "varchar" (get-current 'pup-focal-id ""))
                 (entity-record-values db "stream" "pup-focal-pupaggr")
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupaggr-cancel" "Cancel"
               (lambda ()
                 (list (replace-fragment (get-id "event-holder") "events")))))))

Dave Griffiths's avatar
Dave Griffiths committed
838 839 840 841

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
842
     (entity-reset!)
Dave Griffiths's avatar
Dave Griffiths committed
843 844 845
     (list
      (populate-grid-selector
       "pf-pupaggr-partner" "single"
846
       (db-mongooses-by-pack)
Dave Griffiths's avatar
Dave Griffiths committed
847
       (lambda (individual)
848
         (entity-add-value! "id-with" "varchar" (ktv-get individual "unique_id"))
Dave Griffiths's avatar
Dave Griffiths committed
849 850 851 852 853 854 855
         (list)))
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

856 857 858 859 860 861 862
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  (fragment
   "ev-grpint"
   (linear-layout
    (make-id "") 'vertical fillwrap gp-col
    (list
863 864 865 866 867
     (build-grid-selector "gp-int-leader" "single" "<b>Inter-group interaction</b> Leader mongoose")
     (horiz
      (linear-layout
       (make-id "") 'vertical (layout 400 'fill-parent '1 'left 0) trans-col
       (list
868
        (mtext "text" "Outcome")
869
        (spinner (make-id "gp-int-out") (list "Retreat" "Advance" "Fight retreat" "Fight win") fillwrap
870
                 (lambda (v)
871
                   (entity-add-value! "outcome" "varchar" v) '()))
872
        (mtext "text" "Duration")
873 874 875 876 877 878
        (edit-text (make-id "gp-int-dur") "" 30 "numeric" fillwrap
                   (lambda (v) (entity-add-value! "duration" "int" (string->number v)) '()))))
      (build-grid-selector "gp-int-pack" "single" "Other pack"))
     (linear-layout
      (make-id "") 'horizontal (layout 'fill-parent 80 '1 'left 0) trans-col
      (list
879 880 881
       (mbutton "pf-grpint-done" "Done"
                (lambda ()
                  (entity-record-values db "stream" "group-interaction")
882 883 884
                  (list (replace-fragment (get-id "event-holder") "events"))))
       (mbutton "pf-grpint-cancel" "Cancel"
                (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
885
                  (list (replace-fragment (get-id "event-holder") "events"))))))))
886

887

888 889 890
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
891
     (entity-reset!)
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
     (append
      (force-pause)
      (list
       (populate-grid-selector
        "gp-int-pack" "single"
        (db-all db "sync" "pack")
        (lambda (pack)
          (entity-add-value! "id-other-pack" "varchar" (ktv-get pack "unique_id"))
          (list)))
       (populate-grid-selector
        "gp-int-leader" "single"
        (db-mongooses-by-pack)
        (lambda (individual)
          (entity-add-value! "id-leader" "varchar" (ktv-get individual "unique_id"))
          (list)))
       )))
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


  (fragment
   "ev-grpalarm"
   (linear-layout
    (make-id "") 'vertical fillwrap gp-col
    (list
     (mtitle "title" "Event: Group alarm")
     (build-grid-selector "gp-alarm-caller" "single" "Alarm caller")
     (mtext "text" "Cause")
     (horiz
923 924 925 926 927 928 929
      (spinner (make-id "gp-alarm-cause") (list "Predator" "Other mongoose pack" "Humans" "Other" "Unknown") fillwrap
               (lambda (v)
                 (entity-add-value! "cause" "varchar" v) '()))
      (mtoggle-button "gp-alarm-join" "Did the others join in?"
                      (lambda (v)
                        (entity-add-value! "others-join" "varchar"
                                           (if v "yes" "no")) '())))
930 931 932 933 934 935 936 937
     (horiz
      (mbutton "pf-grpalarm-done" "Done"
               (lambda ()
                 (entity-record-values db "stream" "group-alarm")
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-grpalarm-cancel" "Cancel"
               (lambda ()
                 (list (replace-fragment (get-id "event-holder") "events")))))))
938 939 940 941

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
942
     (entity-reset!)
943 944 945 946 947 948 949 950 951
     (append
      (force-pause)
      (list
       (populate-grid-selector
        "gp-alarm-caller" "single"
        (db-mongooses-by-pack)
        (lambda (individual)
          (entity-add-value! "id-caller" "varchar" (ktv-get individual "unique_id"))
          (list))))
952 953 954 955 956 957 958 959 960 961 962
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

  (fragment
   "ev-grpmov"
   (linear-layout
    (make-id "") 'vertical fillwrap gp-col
    (list
Dave Griffiths's avatar
Dave Griffiths committed
963
     (build-grid-selector "gp-mov-leader" "single" "<b>Group movement</b>: Leader")
964
     (linear-layout
965
      (make-id "") 'horizontal (layout 'fill-parent 90 '1 'left 0) trans-col
966
      (list
Dave Griffiths's avatar
Dave Griffiths committed
967
       (medit-text "gp-mov-w" "Pack width" "numeric"
968
                   (lambda (v) (entity-add-value! "pack-width" "int" (string->number v)) '()))
Dave Griffiths's avatar
Dave Griffiths committed
969 970
       (medit-text "gp-mov-l" "Pack depth" "numeric"
                   (lambda (v) (entity-add-value! "pack-depth" "int" (string->number v)) '()))
Dave Griffiths's avatar
Dave Griffiths committed