init.sh 3.0 KB

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