Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

138 lignes
5.4KB

  1. ;;;; A program to print out the current real-time status of the computer parking terminal equipped
  2. ;;;; parking garages in the city of Linköping using the municipal open data API.
  3. (import medea)
  4. (import http-client)
  5. (import srfi-1)
  6. (import (chicken io))
  7. (import srfi-13)
  8. (import srfi-14)
  9. (import (chicken condition))
  10. (: curry (procedure * --> procedure))
  11. (define (curry fn a)
  12. (lambda (b)
  13. (fn a b)))
  14. (: curry2 (procedure * * --> procedure))
  15. (define (curry2 fn a b)
  16. (lambda (c)
  17. (fn a b c)))
  18. (: perhaps (procedure * -> *))
  19. (define (perhaps fn a)
  20. (if a (fn a) a))
  21. (: upon ((* -> boolean) procedure * -> *))
  22. (define (upon pred? fn a)
  23. (if (pred? a) (fn a) a))
  24. (: member? (* list --> boolean))
  25. (define (member? a ln)
  26. (if (member a ln) #t #f))
  27. (: default ('a 'b --> (or 'a 'b)))
  28. (define (default b a)
  29. (if a a b))
  30. (define-type association-list (list-of pair))
  31. ;;; Return the value for a given key in an association list, or #f if it doesn't exist.
  32. (: value (* association-list --> *))
  33. (define value
  34. (compose (curry perhaps cdr) assoc))
  35. ;;; Return the value for a given key in an association list, or a default value if it doesn't exist.
  36. (: value-with-default (* * association-list --> *))
  37. (define (value-with-default k v ln)
  38. (default v (value k ln)))
  39. (: has-key? (* association-list --> boolean))
  40. (define (has-key? k ln)
  41. (member? k (map car ln)))
  42. (: sort (('a 'a -> boolean) (list-of 'a) --> (list-of 'a)))
  43. (define (sort less-than? ln)
  44. (if (<= (length ln) 1)
  45. ln
  46. (append (sort less-than? (filter (complement (curry less-than? (car ln))) (cdr ln)))
  47. (list (car ln))
  48. (sort less-than? (filter (curry less-than? (car ln)) (cdr ln))))))
  49. ;;; Sort using fn to extract a value for less-than? to compare for each element in ln.
  50. (: sort-via (('a 'a -> boolean) ('b -> 'a) (list-of 'b) -> (list-of 'b)))
  51. (define (sort-via less-than? fn ln)
  52. (sort (lambda (a b)
  53. (less-than? (fn a) (fn b))) ln))
  54. (: display-newline (string -> undefined))
  55. (define (display-newline str)
  56. (display str)
  57. (newline))
  58. (: ->string ((or string symbol number char) -> string))
  59. (define (->string a)
  60. (cond ((string? a) a)
  61. ((symbol? a) (symbol->string a))
  62. ((number? a) (number->string a))
  63. ((char? a) (string a))))
  64. ;;; Given a list of either symbols or strings and an association list:
  65. ;;; look up every symbol from left to right in the association list and replace it with the value found
  66. ;;; then concatenate the resulting list of strings.
  67. ;;; Inspired by Python 3's dictionary formatting string syntax.
  68. (: format-assoc ((list-of (or symbol string)) association-list --> string))
  69. (define (format-assoc statement ln)
  70. (apply string-append
  71. (map (curry2 upon symbol? (compose ->string (curry (flip value) ln))) statement)))
  72. ;;; Get data from a uri and parse it as json.
  73. (define (simple-json-request uri)
  74. (condition-case (with-input-from-request uri #f read-json)
  75. ((exn i/o net) (begin (display-newline "Connection failed.")
  76. (exit 1)))))
  77. ;;; The API key is an alphanumeric string of length 32
  78. ;;; The Linköping municipal open data API key generator portal is located at http://opendata.linkoping.se
  79. (: parking-key string)
  80. (define parking-key
  81. (string-delete char-set:whitespace (with-input-from-file "key-parking" read-line)))
  82. ;;; Get a list of parking lots with various metadata from the municipal servers.
  83. ;;; It should be noted that this API is both glitchy and bugged. Two bugs have been noted:
  84. ;;; the timestamp parameter, which is supposed to give you a list of lots changed since a given UNIX timestamp
  85. ;;; instead results in giving you every lot up until a timestamp of under one hour into the future, after which
  86. ;;; it returns nothing. This bug may be platform related; as of this writing the server system providing the API
  87. ;;; runs on Windows Server 2016 via IIS 10.0.
  88. ;;; The second bug involves returning only partial data upon request - no pattern to this bug has been observed.
  89. (define (get-parking-lots)
  90. (value 'parkingareanewlist
  91. (simple-json-request (string-append "http://parkering.linkoping.se/Parkeringsdata/ParkeringsdataV1.svc/GetParkeringsYtaList/" parking-key "/0"))))
  92. ;;; Only lots tracked by parking lot computer terminals have the parkingspacesavailable key.
  93. (define (computerized? lot)
  94. (has-key? 'parkingspacesavailable lot))
  95. ;;; The handicap lots are tracked as separate lots with duplicate names in the data set.
  96. (define (only-has-handicap-spots? lot)
  97. (equal? '(4) (value 'parkingtypes lot)))
  98. ;;; Electric car lots have a separate lot type, and the downtown parking garages have spaces with charging stations
  99. ;;; but they are not implemented in the data set.
  100. (define (electric-car-lot? lot)
  101. (member? 3 (value 'parkingtypes lot)))
  102. (define (main)
  103. ;; We want members to be parsed as lowercase symbols, and arrays to be parsed as lists.
  104. ;; This is not a legal way to parse json, which requires case-sensitive members
  105. ;; but makes for neater scheme code.
  106. (json-parsers (append `((member . ,(lambda (k v)
  107. (cons (string->symbol (string-downcase k)) v)))
  108. (array . ,identity)) (json-parsers)))
  109. (display-newline "Parking spaces available:")
  110. (map (compose display-newline (curry format-assoc '(name ": " parkingspacesavailable "/" parkingspaces)))
  111. (sort-via string-ci<? (curry value 'name)
  112. (filter (conjoin computerized? (complement only-has-handicap-spots?)) (get-parking-lots)))))
  113. (main)