pepojsond

pepojsondとはRemote-Hand rasberry_piのjsonデータを作成するdaemonです

  1. #!/bin/bash
  2. # licence GPLv3 ; this scripts designed by Isamu.Yamauchi 2013.7.15 update 2017.7.28
  3. # pepojsond is for daemon make json data
  4. USR_BIN=/usr/bin/dio
  5. EXEC_CMD=/usr/local/bin/pepodiodexec
  6. PATH=$PATH:/usr/local/bin:/usr/local/sbin
  7. DIR=/www/remote-hand/tmp
  8. ALIAS_DI=$DIR/.alias_di
  9. DIORD=$DIR/.di_read_data
  10. DIOWD=$DIR/.do_write_data
  11. JSONRD=/www/remote-hand/.di_read_data.json
  12. tJSONRD=/www/remote-hand/.di_read_data.json.tmp
  13. IRKIT_IP=$DIR/.IRKit_IP
  14. TEMPERFILE=$DIR/temperature
  15. TCOSWD=$DIR/.tocos_write_data
  16. TCOSRD=$DIR/.tocos_read_data
  17. SOUND_FILE_NAME=$DIR/.sound_file_name
  18. prog=pepojsond
  19. pidfile=/var/run/$prog.pid
  20. echo -en $$ >$pidfile
  21. WAIT=4000
  22. LWAIT=8000
  23. error(){
  24. [ -e "$JSONRD" ] && rm -f $JSONRD
  25. [ -e "$tJSONRD" ] && rm -f $tJSONRD
  26. [ -e $pidfile ] && rm -f $pidfile
  27. exit 0
  28. }
  29. trap error SIGTERM SIGHUP SIGKILL SIGINT
  30. [ -e "$JSONRD" ] && rm -f $JSONRD
  31. [ -e "$tJSONRD" ] && rm -f $tJSONRD
  32. [ -e $pidfile ] && rm -f $pidfile
  33. # loop for gpio polling read
  34. while true
  35. do
  36. unset di do sound_file piface_ip tocos_ip
  37. [ -e $ALIAS_DI ] && . $ALIAS_DI
  38. if [ ! -z $piface_ip ];then
  39. POLLTIME=$LWAIT
  40. elif [ ! -z $tocos_ip ];then
  41. POLLTIME=$LWAIT
  42. else
  43. POLLTIME=$WAIT
  44. fi
  45. for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
  46. [ ! -z "${di[$i]}" ] && old[$i]=${di[$i]} || old[$i]="-1"
  47. done
  48. [ -e $DIORD ] && . $DIORD
  49. [ -e $DIOWD ] && . $DIOWD
  50. for n in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
  51. case "$n" in
  52. 0) [ "${di[$n]}" = "1" ] && di0="high" ; [ "${di[$n]}" = "0" ] && di0="low" ; [ "${di[$n]}" = "-1" ] && di0="none"
  53. [ "${do[$n]}" = "1" ] && do0="high" ; [ "${do[$n]}" = "0" ] && do0="low" ; [ "${do[$n]}" = "-1" ] && do0="none" ;;
  54. 1) [ "${di[$n]}" = "1" ] && di1="high" ; [ "${di[$n]}" = "0" ] && di1="low" ; [ "${di[$n]}" = "-1" ] && di1="none"
  55. [ "${do[$n]}" = "1" ] && do1="high" ; [ "${do[$n]}" = "0" ] && do1="low" ; [ "${do[$n]}" = "-1" ] && do1="none" ;;
  56. 2) [ "${di[$n]}" = "1" ] && di2="high" ; [ "${di[$n]}" = "0" ] && di2="low" ; [ "${di[$n]}" = "-1" ] && di2="none"
  57. [ "${do[$n]}" = "1" ] && do2="high" ; [ "${do[$n]}" = "0" ] && do2="low" ; [ "${do[$n]}" = "-1" ] && do2="none" ;;
  58. 3) [ "${di[$n]}" = "1" ] && di3="high" ; [ "${di[$n]}" = "0" ] && di3="low" ; [ "${di[$n]}" = "-1" ] && di3="none"
  59. [ "${do[$n]}" = "1" ] && do3="high" ; [ "${do[$n]}" = "0" ] && do3="low" ; [ "${do[$n]}" = "-1" ] && do3="none" ;;
  60. 4) [ "${di[$n]}" = "1" ] && di4="high" ; [ "${di[$n]}" = "0" ] && di4="low" ; [ "${di[$n]}" = "-1" ] && di4="none"
  61. [ "${do[$n]}" = "1" ] && do4="high" ; [ "${do[$n]}" = "0" ] && do4="low" ; [ "${do[$n]}" = "-1" ] && do4="none" ;;
  62. 5) [ "${di[$n]}" = "1" ] && di5="high" ; [ "${di[$n]}" = "0" ] && di5="low" ; [ "${di[$n]}" = "-1" ] && di5="none"
  63. [ "${do[$n]}" = "1" ] && do5="high" ; [ "${do[$n]}" = "0" ] && do5="low" ; [ "${do[$n]}" = "-1" ] && do5="none" ;;
  64. 6) [ "${di[$n]}" = "1" ] && di6="high" ; [ "${di[$n]}" = "0" ] && di6="low" ; [ "${di[$n]}" = "-1" ] && di6="none"
  65. [ "${do[$n]}" = "1" ] && do6="high" ; [ "${do[$n]}" = "0" ] && do6="low" ; [ "${do[$n]}" = "-1" ] && do6="none" ;;
  66. 7) [ "${di[$n]}" = "1" ] && di7="high" ; [ "${di[$n]}" = "0" ] && di7="low" ; [ "${di[$n]}" = "-1" ] && di7="none"
  67. [ "${do[$n]}" = "1" ] && do7="high" ; [ "${do[$n]}" = "0" ] && do7="low" ; [ "${do[$n]}" = "-1" ] && do7="none" ;;
  68. 8) [ "${di[$n]}" = "1" ] && di8="high" ; [ "${di[$n]}" = "0" ] && di8="low" ; [ "${di[$n]}" = "-1" ] && di8="none"
  69. [ "${do[$n]}" = "1" ] && do8="high" ; [ "${do[$n]}" = "0" ] && do8="low" ; [ "${do[$n]}" = "-1" ] && do8="none" ;;
  70. 9) [ "${di[$n]}" = "1" ] && di9="high" ; [ "${di[$n]}" = "0" ] && di9="low" ; [ "${di[$n]}" = "-1" ] && di9="none"
  71. [ "${do[$n]}" = "1" ] && do9="high" ; [ "${do[$n]}" = "0" ] && do9="low" ; [ "${do[$n]}" = "-1" ] && do9="none" ;;
  72. 10) [ "${di[$n]}" = "1" ] && di10="high" ; [ "${di[$n]}" = "0" ] && di10="low" ; [ "${di[$n]}" = "-1" ] && di10="none"
  73. [ "${do[$n]}" = "1" ] && do10="high" ; [ "${do[$n]}" = "0" ] && do10="low" ; [ "${do[$n]}" = "-1" ] && do10="none" ;;
  74. 11) [ "${di[$n]}" = "1" ] && di11="high" ; [ "${di[$n]}" = "0" ] && di11="low" ; [ "${di[$n]}" = "-1" ] && di11="none"
  75. [ "${do[$n]}" = "1" ] && do11="high" ; [ "${do[$n]}" = "0" ] && do11="low" ; [ "${do[$n]}" = "-1" ] && do11="none" ;;
  76. 12) [ "${di[$n]}" = "1" ] && di12="high" ; [ "${di[$n]}" = "0" ] && di12="low" ; [ "${di[$n]}" = "-1" ] && di12="none"
  77. [ "${do[$n]}" = "1" ] && do12="high" ; [ "${do[$n]}" = "0" ] && do12="low" ; [ "${do[$n]}" = "-1" ] && do12="none" ;;
  78. 13) [ "${di[$n]}" = "1" ] && di13="high" ; [ "${di[$n]}" = "0" ] && di13="low" ; [ "${di[$n]}" = "-1" ] && di13="none"
  79. [ "${do[$n]}" = "1" ] && do13="high" ; [ "${do[$n]}" = "0" ] && do13="low" ; [ "${do[$n]}" = "-1" ] && do13="none" ;;
  80. 14) [ "${di[$n]}" = "1" ] && di14="high" ; [ "${di[$n]}" = "0" ] && di14="low" ; [ "${di[$n]}" = "-1" ] && di14="none"
  81. [ "${do[$n]}" = "1" ] && do14="high" ; [ "${do[$n]}" = "0" ] && do14="low" ; [ "${do[$n]}" = "-1" ] && do14="none" ;;
  82. 15) [ "${di[$n]}" = "1" ] && di15="high" ; [ "${di[$n]}" = "0" ] && di15="low" ; [ "${di[$n]}" = "-1" ] && di15="none"
  83. [ "${do[$n]}" = "1" ] && do15="high" ; [ "${do[$n]}" = "0" ] && do15="low" ; [ "${do[$n]}" = "-1" ] && do15="none" ;;
  84. 16) [ "${di[$n]}" = "1" ] && di16="high" ; [ "${di[$n]}" = "0" ] && di16="low" ; [ "${di[$n]}" = "-1" ] && di16="none"
  85. [ "${do[$n]}" = "1" ] && do16="high" ; [ "${do[$n]}" = "0" ] && do16="low" ; [ "${do[$n]}" = "-1" ] && do16="none" ;;
  86. 17) [ "${di[$n]}" = "1" ] && di17="high" ; [ "${di[$n]}" = "0" ] && di17="low" ; [ "${di[$n]}" = "-1" ] && di17="none"
  87. [ "${do[$n]}" = "1" ] && do17="high" ; [ "${do[$n]}" = "0" ] && do17="low" ; [ "${do[$n]}" = "-1" ] && do17="none" ;;
  88. 18) [ "${di[$n]}" = "1" ] && di18="high" ; [ "${di[$n]}" = "0" ] && di18="low" ; [ "${di[$n]}" = "-1" ] && di18="none"
  89. [ "${do[$n]}" = "1" ] && do18="high" ; [ "${do[$n]}" = "0" ] && do18="low" ; [ "${do[$n]}" = "-1" ] && do18="none" ;;
  90. 19) [ "${di[$n]}" = "1" ] && di19="high" ; [ "${di[$n]}" = "0" ] && di19="low" ; [ "${di[$n]}" = "-1" ] && di19="none"
  91. [ "${do[$n]}" = "1" ] && do19="high" ; [ "${do[$n]}" = "0" ] && do19="low" ; [ "${do[$n]}" = "-1" ] && do19="none" ;;
  92. esac
  93. done
  94. NOWDATE=`date +"%Y/%m/%d %T"`
  95. cat>$tJSONRD<<END
  96. {
  97. "di0":"$di0",
  98. "di1":"$di1",
  99. "di2":"$di2",
  100. "di3":"$di3",
  101. "di4":"$di4",
  102. "di5":"$di5",
  103. "di6":"$di6",
  104. "di7":"$di7",
  105. "ti1":"$di8",
  106. "ti2":"$di9",
  107. "ti3":"$di10",
  108. "ai2di1":"$di12",
  109. "ai2di2":"$di13",
  110. "ai2di3":"$di14",
  111. "ai2di4":"$di15",
  112. "ai2di5":"$di16",
  113. "ai2di6":"$di17",
  114. "ai2di7":"$di18",
  115. "ai2di8":"$di19",
  116. "do0":"$do0",
  117. "do1":"$do1",
  118. "do2":"$do2",
  119. "do3":"$do3",
  120. "do4":"$do4",
  121. "do5":"$do5",
  122. "do6":"$do6",
  123. "do7":"$do7",
  124. "to1":"$do8",
  125. "to2":"$do9",
  126. "to3":"$do10",
  127. END
  128. CT=`ls -a $DIR/|egrep '.dio(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39)[low|high]+.count+$'`
  129. if [ -n `echo $CT | wc -w` ];then
  130. for DIO in $CT ; do
  131. DIOCONT=$DIR/$DIO
  132. tDIO=`echo -en $DIO |awk '{gsub("\\\.","");sub("count","");print $0}'`
  133. echo -en \"$tDIO\":{ >>$tJSONRD
  134. cat $DIOCONT |awk '
  135. /^Reset/{printf("%creset%c:%c%s %s%c,",34,34,34,$2,$3,34)}
  136. /^Update/{printf("%cupdate%c:%c%s %s%c,",34,34,34,$2,$3,34)}
  137. /^#/{sub("#","");printf("%ccount%c:%c%s%c},\n",34,34,34,$1,34)}
  138. ' >>$tJSONRD
  139. done
  140. fi
  141. echo -e \"date\":\"$NOWDATE\", >>$tJSONRD
  142. if [ -e ${IRKIT_IP} ];then
  143. IP=`cat ${IRKIT_IP}`
  144. echo -e \"irkit_ip\":\"$IP\", >>$tJSONRD
  145. fi
  146. for J in 0 1 2 3 4 5 ; do
  147. IRFILE=$DIR/.irdata_${J}
  148. if [ -e $IRFILE ];then
  149. IRDATA=`cat $IRFILE`
  150. if [ `echo -n $IRDATA|wc -c` != 0 ];then
  151. echo -e \"irdata_${J}\":\"Ready\", >>$tJSONRD
  152. else
  153. echo -e \"irdata_${J}\":\"none\", >>$tJSONRD
  154. fi
  155. else
  156. echo -e \"irdata_${J}\":\"none\", >>$tJSONRD
  157. fi
  158. done
  159. if [ -e $TEMPERFILE ];then
  160. temper=`cat $TEMPERFILE`
  161. echo -e \"temper\":\"$temper\", >>$tJSONRD
  162. else
  163. echo -e \"temper\":\"none\", >>$tJSONRD
  164. fi
  165. if [ -e $TCOSRD ];then
  166. . $TCOSRD
  167. if [ ! -z "$i2c" ];then
  168. i2ctemp=`echo -en $i2c |awk '{gsub(","," ",$0);printf $0}'`
  169. echo -e \"i2ctemp\":\"$i2ctemp\", >>$tJSONRD
  170. else
  171. echo -e \"i2ctemp\":\"none\", >>$tJSONRD
  172. fi
  173. [ ! -z "$VAI1" ] && echo -e \"vai1\":\"$VAI1\", >>$tJSONRD || echo -e \"vai1\":\"none\", >>$tJSONRD
  174. [ ! -z "$VAI2" ] && echo -e \"vai2\":\"$VAI2\", >>$tJSONRD || echo -e \"vai2\":\"none\", >>$tJSONRD
  175. [ ! -z "$VAI3" ] && echo -e \"vai3\":\"$VAI3\", >>$tJSONRD || echo -e \"vai3\":\"none\", >>$tJSONRD
  176. [ ! -z "$VAI4" ] && echo -e \"vai4\":\"$VAI4\", >>$tJSONRD || echo -e \"vai4\":\"none\", >>$tJSONRD
  177. else
  178. echo -e \"i2ctemp\":\"none\", >>$tJSONRD
  179. fi
  180. [ -e $SOUND_FILE_NAME ] && . $SOUND_FILE_NAME
  181. [ ! -z "${sound_file[0]}" ] && echo -e \"disp_sound_0\":\"${sound_file[0]}\", >>$tJSONRD || echo -e \"disp_sound_0\":\"none\", >>$tJSONRD
  182. [ ! -z "${sound_file[1]}" ] && echo -e \"disp_sound_1\":\"${sound_file[1]}\", >>$tJSONRD || echo -e \"disp_sound_1\":\"none\", >>$tJSONRD
  183. [ ! -z "${sound_file[2]}" ] && echo -e \"disp_sound_2\":\"${sound_file[2]}\", >>$tJSONRD || echo -e \"disp_sound_2\":\"none\", >>$tJSONRD
  184. [ ! -z "${sound_file[3]}" ] && echo -e \"disp_sound_3\":\"${sound_file[3]}\", >>$tJSONRD || echo -e \"disp_sound_3\":\"none\", >>$tJSONRD
  185. [ ! -z "${sound_file[4]}" ] && echo -e \"disp_sound_4\":\"${sound_file[4]}\", >>$tJSONRD || echo -e \"disp_sound_4\":\"none\", >>$tJSONRD
  186. CPU_TEMP=`cat /sys/class/thermal/thermal_zone0/temp|awk '{printf(substr($1,1,2)"."substr($1,3,1)"℃")}'`
  187. echo -e \"cpu_temp\":\"$CPU_TEMP\" >>$tJSONRD
  188. echo "}" >>$tJSONRD
  189. mv $tJSONRD $JSONRD
  190. chown www-data.www-data $JSONRD
  191. msleep ${POLLTIME}
  192. done