starwisp.scm 68.2 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 37 38 39 40 41 42 43
(define pup-focal-export
  (list
   "pup-focal-nearest"
   "pup-focal-pupfeed"
   "pup-focal-pupfind"
   "pup-focal-pupcare"
   "pup-focal-pupaggr"))


44 45
;; colours

46 47 48 49 50 51 52 53 54 55 56 57
(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))

58 59 60


(define trans-col (list 0 0 0 0))
61 62 63 64 65 66 67 68 69 70 71 72 73 74

(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")
75
   (list "Pup assoc" "gc-pup-assoc")
76 77 78 79
   (list "Oestrus" "gc-oestrus")
   (list "Babysit" "gc-babysitting")
   (list "End" "gc-end")))

80
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Dave Griffiths's avatar
Dave Griffiths committed
81
;; persistent database
82

83
(define db "/sdcard/mongoose/local-mongoose.db")
84
(define main-db "/sdcard/mongoose/mongoose.db")
85

86 87 88 89 90 91 92 93 94 95 96 97 98
(define (setup-database!)
  (msg "setting up database")
  (db-close db) ;; close just in case (sorts out db file delete while running problem)
  (db-open db)
  (msg "setting up tables")
  (setup db "local")
  (setup db "sync")
  (setup db "stream")
  (msg (db-status db))
  (insert-entity-if-not-exists
   db "local" "app-settings" "null" 1
   (list
    (ktv "user-id" "varchar" "No name yet...")))
99
  (msg (db-all-sort-normal db "local" "app-settings")))
Dave Griffiths's avatar
Dave Griffiths committed
100

Dave Griffiths's avatar
Dave Griffiths committed
101
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102
;; user interface abstraction
Dave Griffiths's avatar
Dave Griffiths committed
103

Dave Griffiths's avatar
Dave Griffiths committed
104
(define (mbutton id title fn)
Dave Griffiths's avatar
Dave Griffiths committed
105
  (button (make-id id) title 30 (layout 'fill-parent 'wrap-content 1 'centre 10) fn))
Dave Griffiths's avatar
Dave Griffiths committed
106

107
(define (mbutton2 id title fn)
Dave Griffiths's avatar
Dave Griffiths committed
108
  (button (make-id id) title 30 (layout 150 100 1 'centre 10) fn))
109

110
(define (mtoggle-button id title fn)
Dave Griffiths's avatar
Dave Griffiths committed
111
  (toggle-button (make-id id) title 30 (layout 'fill-parent 'wrap-content 1 'centre 10) "fancy" fn))
112

113 114 115 116 117 118 119 120 121
(define (mtoggle-button-yes id title fn)
  (toggle-button (make-id id) title 30 (layout 49 43 1 'centre 0) "yes" fn))

(define (mtoggle-button-maybe id title fn)
  (toggle-button (make-id id) title 30 (layout  49 43 1 'centre 0) "maybe" fn))

(define (mtoggle-button-no id title fn)
  (toggle-button (make-id id) title 30 (layout  49 43 1 'centre 0) "no" fn))

122
(define (mtoggle-button2 id title fn)
Dave Griffiths's avatar
Dave Griffiths committed
123
  (toggle-button (make-id id) title 30 (layout 150 100 1 'centre 10) "plain" fn))
124

Dave Griffiths's avatar
Dave Griffiths committed
125
(define (mtext id text)
126
  (text-view (make-id id) text 30 fillwrap))
127

128
(define (mtitle id text)
129
  (text-view (make-id id) text 50 fillwrap))
130

131 132
(define (medit-text id text type fn)
  (vert
133
   (mtext (string-append id "-title") text)
134
   (edit-text (make-id id) "" 30 type fillwrap fn)))
135

Dave Griffiths's avatar
Dave Griffiths committed
136 137 138 139 140
(define (medit-text-value id text value type fn)
  (vert
   (mtext (string-append id "-title") text)
   (edit-text (make-id id) value 30 type fillwrap fn)))

141 142 143 144 145 146
(define (mclear-toggles id-list)
  (map
   (lambda (id)
     (update-widget 'toggle-button (get-id id) 'checked 0))
   id-list))

147 148 149 150 151 152 153
(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))

154 155 156 157 158 159 160 161 162 163
(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))

164 165
;;;;

166
(define (build-grid-selector name type title)
167 168 169 170 171 172 173 174 175 176
  (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
177
      (image-view (make-id "im") "arrow_left" (layout 200 'fill-parent 1 'left 0))
178 179 180 181 182 183 184 185
      (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
186
          (button-grid (make-id name) type 3 30 (layout 100 60 1 'left 40)
187
                       (list) (lambda (v) '()))))))
188
      (image-view (make-id "im") "arrow_right" (layout 200 'fill-parent 1 'right 0)))))))
189

190
;; assumes grid selectors on mongeese only
191 192 193
(define (fast-get-name item)
  (list-ref (list-ref item 1) 2))

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
(define (build-button-items name items unknown)
  (append
   (map
    (lambda (item)
      (let ((item-name (fast-get-name item)))
        (list (make-id (string-append name item-name))
              item
              item-name)))
    items)
   (if unknown
       (list
        (list (make-id (string-append name "-unknown"))
              (list (ktv "name" "varchar" "Unknown")
                    (ktv "unique_id" "varchar" "Unknown"))
              "???"))
       '())))

(define (populate-grid-selector name type items unknown fn)
212 213
  (prof-start "popgrid")
  (prof-start "popgrid setup")
214
  (let ((id->items (build-button-items name items unknown))
215
        (selected-set '()))
216 217
    (prof-end "popgrid setup")
    (let ((r (update-widget
218 219
     'button-grid (get-id name) 'grid-buttons
     (list
220
      type 3 30 (layout 100 60 1 'left 0)
221 222
      (map
       (lambda (ii)
223
         (list (car ii) (caddr ii)))
224
       id->items)
225 226
      (lambda (v state)
        (cond
227 228
         ((equal? type "toggle")
          ;; update list of selected items
229 230 231 232 233 234 235 236 237
          (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
238
          ;;(msg (findv v id->items))
239
          (fn (cadr (findv v id->items))))))))))
240 241 242 243
      (prof-end "popgrid")
      r)))

(define (db-mongooses-by-pack)
244
  (db-all-where
245 246
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))))
247

248 249 250 251 252 253
(define (db-mongooses-by-pack-ignore-delete)
  (db-all-where-ignore-delete
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))))


254
(define (db-mongooses-by-pack-male)
255
  (db-all-where2or
256 257
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
258
   (ktv "gender" "varchar" "Male") "Unknown"))
259 260

(define (db-mongooses-by-pack-female)
261
  (db-all-where2or
262 263
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
264
   (ktv "gender" "varchar" "Female") "Unknown"))
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280


;; (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
281
  (db-all-newer
282 283 284 285
   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)))))

286 287 288 289 290 291
(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)))))

292 293 294 295 296 297 298 299 300 301 302 303 304 305


(define (tri-state id text key)
  (linear-layout
   (make-id "") 'vertical (layout 'fill-parent 'wrap-content '1 'centre 0) trans-col
   (list
    (linear-layout
     (make-id "") 'horizontal (layout 'wrap-content 'wrap-parent '1 'centre 0) trans-col
     (list
      (mtoggle-button-yes
       (string-append id "-y") ""
       (lambda (v)
         (cond
          (v
Dave Griffiths's avatar
Dave Griffiths committed
306
           (entity-set-value! key "varchar" "yes")
307 308 309 310 311 312 313 314 315 316 317 318
           (list
            (update-widget 'toggle-button (get-id (string-append id "-n")) 'checked 0)
            (update-widget 'toggle-button (get-id (string-append id "-m")) 'checked 0)))
               (else
                (list
                 (update-widget 'toggle-button (get-id (string-append id "-y")) 'checked 1))))
              ))
      (mtoggle-button-maybe
       (string-append id "-m")  ""
       (lambda (v)
         (cond
          (v
Dave Griffiths's avatar
Dave Griffiths committed
319
           (entity-set-value! key "varchar" "maybe")
320 321 322 323 324 325 326 327 328 329 330 331 332
           (list
            (update-widget 'toggle-button (get-id (string-append id "-y")) 'checked 0)
            (update-widget 'toggle-button (get-id (string-append id "-n")) 'checked 0)))
          (else
           (list
            (update-widget 'toggle-button (get-id (string-append id "-m")) 'checked 1))))
         ))

      (mtoggle-button-no
       (string-append id "-n") ""
       (lambda (v)
         (cond
          (v
Dave Griffiths's avatar
Dave Griffiths committed
333
           (entity-set-value! key "varchar" "no")
334 335 336 337 338 339 340 341 342 343
           (list
            (update-widget 'toggle-button (get-id (string-append id "-y")) 'checked 0)
            (update-widget 'toggle-button (get-id (string-append id "-m")) 'checked 0)))
          (else
           (list
            (update-widget 'toggle-button (get-id (string-append id "-n")) 'checked 1))))
         ))))

    (text-view 0 text 30 (layout 'wrap-content 'wrap-parent '1 'centre 0)))))

Dave Griffiths's avatar
Dave Griffiths committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; review

(define (review-build-contents uid entity)
  (append
   (map
    (lambda (ktv)
      (cond
       ((equal? (ktv-type ktv) "varchar")
        (medit-text-value (string-append uid (ktv-key ktv))
                          (ktv-key ktv)
                          (ktv-value ktv) "normal"
                          (lambda (v) '())))
       ((equal? (ktv-type ktv) "int")
        (medit-text-value (string-append uid (ktv-key ktv))
                          (ktv-key ktv)
                          (number->string (ktv-value ktv)) "numeric"
                          (lambda (v) '())))
       ((equal? (ktv-type ktv) "real")
        (medit-text-value (string-append uid (ktv-key ktv))
                          (ktv-key ktv)
                          (number->string (ktv-value ktv)) "numeric"
                          (lambda (v) '())))
       (else (mtext "" (string-append (ktv-type ktv) " not handled")))))
    entity)
   (list
    (mbutton (string-append uid "-save") "Save" (lambda () '())))))

(define (review-update-list)
  (list
   (update-widget
    'linear-layout (get-id "review-list") 'contents
    (map
     (lambda (dirty-entity)
       ;; consists of ((type,uid,dirty,version) (ktvlist))
       (let* ((data (car dirty-entity))
              (entity (cadr dirty-entity))
              (time (ktv-get entity "time"))
              (uid (list-ref data 1)))
         (msg dirty-entity)
         (vert
          (mtoggle-button
           (string-append "review-" uid)
           (string-append (list-ref data 0) (if time (string-append "-" time) ""))
           (lambda (v)
             (list
              (update-widget 'linear-layout
                             (get-id (string-append uid "-container"))
                             'contents
                             (if (not v) '() (review-build-contents uid entity)))
              )))
          (linear-layout
           (make-id (string-append uid "-container"))
           'vertical
           (layout 'fill-parent 'wrap-content 1 'centre 0)
           (list 0 0 0 0)
           (list)))))
     (dirty-entities-for-review db "stream")))))

403

404 405
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
Dave Griffiths's avatar
Dave Griffiths committed
406

407 408 409 410
(define (debug! txt)
  (set-current! 'debug-text (string-append txt "\n" (get-current 'debug-text ""))))

(define (update-debug)
411 412 413
  (update-widget 'debug-text-view (get-id "sync-debug") 'text
                 (get-current 'debug-text "")))

414
(define (debug-timer-cb)
Dave Griffiths's avatar
Dave Griffiths committed
415 416 417 418 419 420 421 422 423 424 425 426 427
  (append
   (cond
    ((get-current 'sync-on #f)
     (set-current! 'upload 0)
     (set-current! 'download 0)
     (connect-to-net
      (lambda ()
        (append
         (list (toast "sync-cb"))
         (upload-dirty db)
         (suck-new db "sync")))))
    (else '()))
   (list
428
    (delayed "debug-timer" (+ 10000 (random 5000)) debug-timer-cb)
Dave Griffiths's avatar
Dave Griffiths committed
429
    (update-debug))))
430

431

432 433
(define pf-length 20) ;; minutes...

434 435 436 437 438 439 440
(define (timer-cb)
  (set-current!
   'timer-seconds
   (- (get-current 'timer-seconds 59) 1))
  (append
   (cond
    ((< (get-current 'timer-seconds 59) 0)
441
     (set-current! 'timer-minutes (- (get-current 'timer-minutes pf-length) 1))
442
     (set-current! 'timer-seconds 59)
443 444 445 446 447 448 449 450 451 452 453 454 455 456
     (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")))))
457 458 459 460 461
    (else '()))
   (list
    (delayed "timer" 1000 timer-cb)
    (update-widget
     'text-view (get-id "pf-timer-time-minutes") 'text
462
     (string-append (number->string (get-current 'timer-minutes pf-length))))
463 464 465 466 467
    (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
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
(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 '()))))))))

483 484 485 486
(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
487

488 489
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; fragments
Dave Griffiths's avatar
Dave Griffiths committed
490 491 492

(define-fragment-list

493 494 495
  (fragment
   "pf-timer"
   (linear-layout
496
    (make-id "") 'vertical fillwrap trans-col
497
    (list
498
     (mtitle "pf-details" "Pack: xxx Pup: xxx")))
499 500 501
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
502 503 504 505 506 507
     (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"))
                     )))
508 509 510 511 512 513 514
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


  (fragment
515
   "events"
516
   (linear-layout
517
    0 'vertical fillwrap trans-col
518
    (list
519
     (linear-layout
Dave Griffiths's avatar
Dave Griffiths committed
520
      (make-id "ev-pf") 'vertical fill pf-col
521
      (list
522 523
       (mtitle "ev-pf-text" "Pup Focal Events")
       (horiz
Dave Griffiths's avatar
Dave Griffiths committed
524 525 526 527
        (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")))))))
528
     (linear-layout
529
      (make-id "ev-pf") 'vertical fill gp-col
530
      (list
531 532
       (mtitle "text" "Group Events")
       (horiz
Dave Griffiths's avatar
Dave Griffiths committed
533 534
        (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"))))
535 536
        (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")))))))))
537 538 539
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
540
     (if (equal? (get-current 'observation "none") obs-pf)
541 542
         (list
          (update-widget 'text-view (get-id "ev-pf-text") 'show 0)
Dave Griffiths's avatar
Dave Griffiths committed
543 544 545 546
          (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))))
547 548 549 550
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))
551

552 553 554
  (fragment
   "pf-scan1"
   (linear-layout
555
    (make-id "") 'vertical fillwrap pf-col
556
    (list
Dave Griffiths's avatar
Dave Griffiths committed
557
     (build-grid-selector "pf-scan-nearest" "single" "<b>Nearest Neighbour Scan</b>: Closest Mongoose")
558
     (build-grid-selector "pf-scan-close" "toggle" "Mongooses within 2m")
559 560
     (mbutton "pf-scan-done" "Done"
              (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
561
                (entity-set-value! "parent" "varchar" (get-current 'pup-focal-id ""))
Dave Griffiths's avatar
Dave Griffiths committed
562
                (entity-record-values!)
563
                (list (replace-fragment (get-id "pf-top") "pf-timer"))))))
564 565 566 567

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
568
     (entity-init! db "stream" "pup-focal-nearest" '())
Dave Griffiths's avatar
Dave Griffiths committed
569
     (entity-set-value! "scan-time" "varchar" (date-time->string (date-time)))
570
     (list
571 572
      (play-sound "ping")
      (vibrate 300)
573
      (populate-grid-selector
574
       "pf-scan-nearest" "single"
575
       (db-mongooses-by-pack-adults) #t
576
       (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
577
         (entity-set-value! "id-nearest" "varchar" (ktv-get individual "unique_id"))
578 579
         (list)))
      (populate-grid-selector
580
       "pf-scan-close" "toggle"
581
       (db-mongooses-by-pack-adults) #t
582
       (lambda (individuals)
Dave Griffiths's avatar
Dave Griffiths committed
583
         (entity-set-value! "id-list-close" "varchar" (assemble-array individuals))
584 585
         (list)))
      ))
586 587 588 589 590 591
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


592 593 594
  (fragment
   "ev-pupfeed"
   (linear-layout
595
    (make-id "") 'vertical fillwrap pf-col
596
    (list
597
     (mtitle "title" "Event: Pup is fed")
598
     (build-grid-selector "pf-pupfeed-who" "single" "Who fed the pup?")
Dave Griffiths's avatar
Dave Griffiths committed
599
     (spacer 20)
600
     (horiz
Dave Griffiths's avatar
Dave Griffiths committed
601
      (mtext "text" "Food size")
602 603
      (spinner (make-id "pf-pupfeed-size") (list "Small" "Medium" "Large") fillwrap
               (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
604
                 (entity-set-value! "size" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
605 606
     (spacer 20)
     (horiz
607 608
      (mbutton "pf-pupfeed-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
609
                 (entity-set-value! "parent" "varchar" (get-current 'pup-focal-id ""))
Dave Griffiths's avatar
Dave Griffiths committed
610
                 (entity-record-values!)
611 612 613
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupfeed-cancel" "Cancel"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
614
                 (list (replace-fragment (get-id "event-holder") "events")))))))
615 616 617 618

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
619
     (entity-init!  db "stream" "pup-focal-pupfeed" '())
620 621 622
     (list
      (populate-grid-selector
       "pf-pupfeed-who" "single"
623
       (db-mongooses-by-pack-adults) #t
624
       (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
625
         (entity-set-value! "id-who" "varchar" (ktv-get individual "unique_id"))
626 627 628 629 630 631 632
         (list)))
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

Dave Griffiths's avatar
Dave Griffiths committed
633 634 635
  (fragment
   "ev-pupfind"
   (linear-layout
636
    (make-id "") 'vertical fillwrap pf-col
Dave Griffiths's avatar
Dave Griffiths committed
637
    (list
638
     (mtitle "title" "Event: Pup found food")
Dave Griffiths's avatar
Dave Griffiths committed
639 640 641
     (horiz
      (mtext "text" "Food size")
      (spinner (make-id "pf-pupfind-size") (list "Small" "Medium" "Large") fillwrap
Dave Griffiths's avatar
Dave Griffiths committed
642
               (lambda (v) (entity-set-value! "size" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
643
     (spacer 20)
Dave Griffiths's avatar
Dave Griffiths committed
644
     (horiz
645 646
      (mbutton "pf-pupfind-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
647
                 (entity-set-value! "parent" "varchar" (get-current 'pup-focal-id ""))
Dave Griffiths's avatar
Dave Griffiths committed
648
                 (entity-record-values!)
649 650 651
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupfind-cancel" "Cancel"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
652
                 (list (replace-fragment (get-id "event-holder") "events")))))))
Dave Griffiths's avatar
Dave Griffiths committed
653 654 655 656

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
657
     (entity-init! db "stream" "pup-focal-pupfind" '())
Dave Griffiths's avatar
Dave Griffiths committed
658 659 660 661 662 663 664 665 666 667 668
     (list
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


  (fragment
   "ev-pupcare"
   (linear-layout
669
    (make-id "") 'vertical fillwrap pf-col
Dave Griffiths's avatar
Dave Griffiths committed
670
    (list
671
     (mtitle "title" "Event: Pup is cared for")
Dave Griffiths's avatar
Dave Griffiths committed
672
     (build-grid-selector "pf-pupcare-who" "single" "Who cared for the pup?")
Dave Griffiths's avatar
Dave Griffiths committed
673
     (spacer 20)
Dave Griffiths's avatar
Dave Griffiths committed
674
     (horiz
Dave Griffiths's avatar
Dave Griffiths committed
675
      (mtext "text" "Type of care")
676 677
      (spinner (make-id "pf-pupcare-type") (list "Carry" "Lead" "Sniff" "Play" "Ano-genital sniff") fillwrap
               (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
678
                 (entity-set-value! "type" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
679 680
     (spacer 20)
     (horiz
681 682
      (mbutton "pf-pupcare-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
683
                 (entity-set-value! "parent" "varchar" (get-current 'pup-focal-id ""))
Dave Griffiths's avatar
Dave Griffiths committed
684
                 (entity-record-values!)
685 686 687
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-pupcare-cancel" "Cancel"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
688
                 (list (replace-fragment (get-id "event-holder") "events")))))))
Dave Griffiths's avatar
Dave Griffiths committed
689 690 691 692

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
693
     (entity-init! db "stream" "pup-focal-pupcare" '())
Dave Griffiths's avatar
Dave Griffiths committed
694 695 696
     (list
      (populate-grid-selector
       "pf-pupcare-who" "single"
697
       (db-mongooses-by-pack-adults) #t
Dave Griffiths's avatar
Dave Griffiths committed
698
       (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
699
         (entity-set-value! "id-who" "varchar" (ktv-get individual "unique_id"))
Dave Griffiths's avatar
Dave Griffiths committed
700 701 702 703 704 705 706 707 708 709
         (list)))
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

  (fragment
   "ev-pupaggr"
   (linear-layout
710
    (make-id "") 'vertical fillwrap pf-col
Dave Griffiths's avatar
Dave Griffiths committed
711
    (list
712
     (mtitle "title" "Event: Pup aggression")
Dave Griffiths's avatar
Dave Griffiths committed
713 714 715
     (build-grid-selector "pf-pupaggr-partner" "single" "Aggressive mongoose")

     (linear-layout
716
      (make-id "") 'horizontal (layout 'fill-parent 100 '1 'left 0) trans-col
Dave Griffiths's avatar
Dave Griffiths committed
717 718 719
      (list
       (vert
        (mtext "" "Fighting over")
720 721
        (spinner (make-id "pf-pupaggr-over") (list "Food" "Escort" "Nothing" "Other") fillwrap
                 (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
722
                   (entity-set-value! "over" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
723 724
       (vert
        (mtext "" "Level")
725 726
        (spinner (make-id "pf-pupaggr-level") (list "Block" "Snap" "Chase" "Push" "Fight") fillwrap
                 (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
727
                   (entity-set-value! "level" "varchar" v) '())))
728 729 730 731 732

       (tri-state "pf-pupaggr-in" "Initiate?" "initiate")

       ;(mtoggle-button "pf-pupaggr-in" "Initiate?"
       ;                (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
733
       ;                  (entity-set-value! "initiate" "varchar" (if v "yes" "no")) '()))
734 735 736 737


       (tri-state "pf-pupaggr-win" "Win?" "win")))

Dave Griffiths's avatar
Dave Griffiths committed
738
     (spacer 20)
739 740 741
     (horiz
      (mbutton "pf-pupaggr-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
742
                 (entity-set-value! "parent" "varchar" (get-current 'pup-focal-id ""))
Dave Griffiths's avatar
Dave Griffiths committed
743
                 (entity-record-values!)
744 745 746 747 748
                 (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
749 750 751 752

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
753
     (entity-init! db "stream" "pup-focal-pupaggr" '())
Dave Griffiths's avatar
Dave Griffiths committed
754 755 756
     (list
      (populate-grid-selector
       "pf-pupaggr-partner" "single"
757
       (db-mongooses-by-pack) #t
Dave Griffiths's avatar
Dave Griffiths committed
758
       (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
759
         (entity-set-value! "id-with" "varchar" (ktv-get individual "unique_id"))
Dave Griffiths's avatar
Dave Griffiths committed
760 761 762 763 764 765 766
         (list)))
      ))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

767 768 769 770 771 772 773
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  (fragment
   "ev-grpint"
   (linear-layout
    (make-id "") 'vertical fillwrap gp-col
    (list
774 775 776 777 778
     (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
779
        (mtext "text" "Outcome")
780
        (spinner (make-id "gp-int-out") (list "Retreat" "Advance" "Fight retreat" "Fight win") fillwrap
781
                 (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
782
                   (entity-set-value! "outcome" "varchar" v) '()))
783
        (mtext "text" "Duration")
784
        (edit-text (make-id "gp-int-dur") "" 30 "numeric" fillwrap
Dave Griffiths's avatar
Dave Griffiths committed
785
                   (lambda (v) (entity-set-value! "duration" "int" (string->number v)) '()))))
786 787 788 789
      (build-grid-selector "gp-int-pack" "single" "Other pack"))
     (linear-layout
      (make-id "") 'horizontal (layout 'fill-parent 80 '1 'left 0) trans-col
      (list
790 791
       (mbutton "pf-grpint-done" "Done"
                (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
792
                  (entity-record-values!)
793 794 795
                  (list (replace-fragment (get-id "event-holder") "events"))))
       (mbutton "pf-grpint-cancel" "Cancel"
                (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
796
                  (list (replace-fragment (get-id "event-holder") "events"))))))))
797

798

799 800 801
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
802
     (entity-init! db "stream" "group-interaction" '())
803 804 805 806 807
     (append
      (force-pause)
      (list
       (populate-grid-selector
        "gp-int-pack" "single"
808
        (db-all-sort-normal db "sync" "pack") #f
809
        (lambda (pack)
Dave Griffiths's avatar
Dave Griffiths committed
810
          (entity-set-value! "id-other-pack" "varchar" (ktv-get pack "unique_id"))
811 812 813
          (list)))
       (populate-grid-selector
        "gp-int-leader" "single"
814
        (db-mongooses-by-pack) #t
815
        (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
816
          (entity-set-value! "id-leader" "varchar" (ktv-get individual "unique_id"))
817 818
          (list)))
       )))
819 820 821 822 823 824 825 826 827 828 829 830 831
   (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")
832 833 834 835 836 837 838 839

     (linear-layout
      (make-id "") 'horizontal fillwrap trans-col
      (list
       (vert
        (mtext "text" "Cause")
        (spinner (make-id "gp-alarm-cause") (list "Predator" "Other mongoose pack" "Humans" "Other" "Unknown") fillwrap
                 (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
840
                   (entity-set-value! "cause" "varchar" v) '())))
841 842 843

       (tri-state "gp-alarm-join" "Did the others join in?" "others-join")))

844 845 846
     (horiz
      (mbutton "pf-grpalarm-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
847
                 (entity-record-values!)
848 849 850 851
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-grpalarm-cancel" "Cancel"
               (lambda ()
                 (list (replace-fragment (get-id "event-holder") "events")))))))
852 853 854 855

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
856
     (entity-init! db "stream" "group-alarm" '())
857 858 859 860 861
     (append
      (force-pause)
      (list
       (populate-grid-selector
        "gp-alarm-caller" "single"
862
        (db-mongooses-by-pack) #t
863
        (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
864
          (entity-set-value! "id-caller" "varchar" (ktv-get individual "unique_id"))
865
          (list))))
866 867 868 869 870 871 872 873 874 875 876
      ))
   (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
877
     (build-grid-selector "gp-mov-leader" "single" "<b>Group movement</b>: Leader")
878
     (linear-layout
879
      (make-id "") 'horizontal (layout 'fill-parent 90 '1 'left 0) trans-col
880
      (list
Dave Griffiths's avatar
Dave Griffiths committed
881
       (medit-text "gp-mov-w" "Pack width" "numeric"
Dave Griffiths's avatar
Dave Griffiths committed
882
                   (lambda (v) (entity-set-value! "pack-width" "int" (string->number v)) '()))
Dave Griffiths's avatar
Dave Griffiths committed
883
       (medit-text "gp-mov-l" "Pack depth" "numeric"
Dave Griffiths's avatar
Dave Griffiths committed
884
                   (lambda (v) (entity-set-value! "pack-depth" "int" (string->number v)) '()))
Dave Griffiths's avatar
Dave Griffiths committed
885
       (medit-text "gp-mov-c" "How many?" "numeric"
Dave Griffiths's avatar
Dave Griffiths committed
886
                   (lambda (v) (entity-set-value! "pack-count" "int" (string->number v)) '()))))
887
     (linear-layout
888
      (make-id "") 'horizontal (layout 'fill-parent 'wrap-content '1 'left 0) trans-col
889
      (list
890 891
       (vert
        (mtext "" "Direction")
892
        (spinner (make-id "gp-mov-dir") (list "To" "From") fillwrap
Dave Griffiths's avatar
Dave Griffiths committed
893
                 (lambda (v) (entity-set-value! "direction" "varchar" v)  '())))
894

Dave Griffiths's avatar
Dave Griffiths committed
895 896 897
       (vert
        (mtext "" "Where to")
        (spinner (make-id "gp-mov-to") (list "Latrine" "Water" "Food" "Nothing" "Den" "Unknown") fillwrap
Dave Griffiths's avatar
Dave Griffiths committed
898
                 (lambda (v) (entity-set-value! "destination" "varchar" v)  '())))))
Dave Griffiths's avatar
Dave Griffiths committed
899 900 901 902 903

     (spacer 20)
     (horiz
      (mbutton "pf-grpmov-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
904
                 (entity-record-values!)
Dave Griffiths's avatar
Dave Griffiths committed
905 906 907 908
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "pf-grpalarm-cancel" "Cancel"
               (lambda ()
                 (list (replace-fragment (get-id "event-holder") "events")))))))
909 910 911 912

   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
913
     (entity-init! db "stream" "group-move" '())
914 915 916 917 918
     (append
      (force-pause)
      (list
       (populate-grid-selector
        "gp-mov-leader" "single"
919
        (db-mongooses-by-pack) #t
920
        (lambda (individual)
Dave Griffiths's avatar
Dave Griffiths committed
921
          (entity-set-value! "id-leader" "varchar" (ktv-get individual "unique_id"))
922 923
          (list)))
       )))
924 925 926 927 928
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))

929 930 931 932 933 934
  (fragment
   "note"
   (linear-layout
    (make-id "") 'vertical fillwrap gp-col
    (list
     (mtitle "title" "Make a note")
935
     (edit-text (make-id "note-text") "" 30 "text" fillwrap
936
                (lambda (v)
Dave Griffiths's avatar
Dave Griffiths committed
937
                  (entity-set-value! "text" "varchar" v)
938 939 940 941
                  '()))
     (horiz
      (mbutton "note-done" "Done"
               (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
942
                 (entity-record-values!)
943 944 945 946 947 948 949
                 (list (replace-fragment (get-id "event-holder") "events"))))
      (mbutton "note-cancel" "Cancel"
               (lambda ()
                 (list (replace-fragment (get-id "event-holder") "events")))))))

   (lambda (fragment arg)
     (activity-layout fragment))
950
   (lambda (fragment arg)
Dave Griffiths's avatar
Dave Griffiths committed
951
     (entity-init!  db "stream" "note" '())
952 953 954 955
     (append
      (force-pause)
      (list
       (update-widget 'edit-text (get-id "note-text") 'request-focus 1))))