weather.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #! /bin/sh
  2. day=$(date '+%A')
  3. month=
  4. TOPIC="/card"
  5. NEW_CARD='{
  6. "cmd":"new_card",
  7. "bg_color": 13,
  8. "responseTopic": "/card_new1",
  9. "elements": [
  10. {
  11. "id": 0,
  12. "type": "text",
  13. "position": {
  14. "x": 165,
  15. "y": 30
  16. },
  17. "size": 20,
  18. "value": "#ff0000 deg# C"
  19. },
  20. {
  21. "id": 1,
  22. "type": "text",
  23. "position": {
  24. "x": 135,
  25. "y": 45,
  26. "align": "right"
  27. },
  28. "size": 80,
  29. "value": "-3"
  30. },
  31. {
  32. "id": 2,
  33. "type": "text",
  34. "position": {
  35. "x": 100,
  36. "y": 130,
  37. "align": "right"
  38. },
  39. "size": 30,
  40. "value": "'`date '+%A'`'"
  41. },
  42. {
  43. "id": 3,
  44. "type": "text",
  45. "position": {
  46. "x": 100,
  47. "y": 160,
  48. "align": "right"
  49. },
  50. "size": 30,
  51. "value": "'`date '+%b %d'`'"
  52. },
  53. {
  54. "id": 4,
  55. "type": "text",
  56. "position": {
  57. "x": 100,
  58. "y": 220,
  59. "align": "right"
  60. },
  61. "size": 20,
  62. "value": "Precipitation"
  63. },
  64. {
  65. "id": 6,
  66. "type": "image",
  67. "position": {
  68. "x": 10,
  69. "y": 30
  70. },
  71. "value": "sunny"
  72. },
  73. {
  74. "id": 5,
  75. "type": "text",
  76. "position": {
  77. "x": 115,
  78. "y": 235,
  79. "align": "right"
  80. },
  81. "size": 60,
  82. "value": "10%"
  83. }
  84. ]
  85. }'
  86. toSingleLine () {
  87. echo $(echo $1 | tr -s '\t' ' ' | tr -d '\n')
  88. }
  89. setup () {
  90. json=$(toSingleLine "$NEW_CARD")
  91. mosquitto_pub -t $TOPIC -m "$json"
  92. }
  93. # Generate a random number
  94. # arg1 - min value
  95. # arg2 - max value
  96. random () {
  97. echo $(awk -v min=$1 -v max=$2 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')
  98. }
  99. # Issue a message to update a specific element
  100. # arg1 - card ID
  101. # arg2 - element ID
  102. # arg3 - new value
  103. update () {
  104. json='{
  105. "cmd":"update_card",
  106. "id": '"$1"',
  107. "elements": [
  108. {
  109. "id": '"$2"',
  110. "value": "'"$3"'"
  111. }
  112. ]
  113. }'
  114. echo "$json"
  115. json=$(toSingleLine "$json")
  116. mosquitto_pub -t $TOPIC -m "$json"
  117. }
  118. # parse arguments
  119. if [ "$1" == "setup" ]; then
  120. setup
  121. elif [ "$1" == "update" ]; then
  122. # expect additional argument specifying the card ID
  123. # temperature
  124. update "$2" "1" $(random -15 30)
  125. # date
  126. update "$2" "3" "Feb $(random 1 28)"
  127. # precipitation
  128. update "$2" "5" "$(random 3 90)%"
  129. # image
  130. update "$2" "6" "windy"
  131. fi