pepoipdiotocsd

最初に

raspberry pi+Pifaceでwgetを使いリモートホストのDIO・Tocos無線DIO・各ポートへの読み書きや温湿度センサー情報を取得するdaemonです

  1. #!/bin/bash
  2. # pepoipdiotocsd ; For raspberry pi , and scripts to run the Remote DIO and TOCOS .
  3. # licence GPLv2 ; the scripts designed by Isamu.Yamauchi, 2017.7.23 update 2017.7.28
  4. #
  5. RETRYTIME=20
  6. RETRY=1
  7. WAIT=5000
  8. PORT=443
  9. LOCAL_DIR=/www/remote-hand/tmp
  10. REMOTE_DIR=remote-hand
  11. SERVERCONF=$LOCAL_DIR/.startup.s.tmp
  12. WORK=$LOCAL_DIR/.ip_json_work
  13. tWORK=$LOCAL_DIR/.ip_json_work_tmp
  14. ALIAS_DI=$LOCAL_DIR/.alias_di
  15. USERAGENT="59.0.3071.115"m
  16. JSON_DATA=.di_read_data.json
  17. RM_JSON_DATA=$REMOTE_DIR/$JSON_DATA
  18. LO_JSON_DATA=$LOCAL_DIR/$JSON_DATA
  19. TEMPERFILE=$LOCAL_DIR/temperature
  20. tTEMPERFILE=$LOCAL_DIR/temperature.tmp
  21. DI_READ=.di_read_data
  22. DO_READ=.do_write_data
  23. LO_DI_DATA=$LOCAL_DIR/$DI_READ
  24. tLO_DI_DATA=$LOCAL_DIR/${DI_READ}.tmp
  25. LO_DO_DATA=$LOCAL_DIR/$DO_READ
  26. tLO_DO_DATA=$LOCAL_DIR/${DO_READ}.tmp
  27. TOCOS_RD_READ=.tocos_read_data
  28. TOCOS_WD_READ=.tocos_write_data
  29. LO_TOCOS_RD_DATA=$LOCAL_DIR/$TOCOS_RD_READ
  30. tLO_TOCOS_RD_DATA=$LOCAL_DIR/${TOCOS_RD_READ}.tmp
  31. LO_TOCOS_WD_DATA=$LOCAL_DIR/$TOCOS_WD_READ
  32. tLO_TOCOS_WD_DATA=$LOCAL_DIR/${TOCOS_WD_READ}.tmp
  33. while true;do
  34. while [ -e $ALIAS_DI -a -e $SERVERCONF ];do
  35. unset tocos_ip piface_ip vWEBPASSWORD vWEBUSER
  36. . $ALIAS_DI
  37. . $SERVERCONF
  38. TOCOS_IP=$tocos_ip
  39. Piface_IP=$piface_ip
  40. PASSWORD=$vWEBPASSWORD
  41. USER=$vWEBUSER
  42. LOOPTIME=$WAIT
  43. if [ ! -z $Piface_IP ];then
  44. RM_HOST=$Piface_IP
  45. rm -f $WORK
  46. wget -q -T $RETRYTIME -t $RETRY --secure-protocol=TLSv1 --no-check-certificate --user-agent=${USERAGENT} https://$RM_HOST:$PORT/$RM_JSON_DATA --http-user=${USER} --http-password=${PASSWORD} --output-document=${WORK}
  47. if [ -e $WORK ];then
  48. LEN=`cat $WORK |wc -l`
  49. if [ $LEN -gt 2 ];then
  50. rm -f $LO_DI_DATA $LO_DO_DATA
  51. cat $WORK |awk '
  52. /"di0"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[0]="i)}
  53. /"di1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[1]="i)}
  54. /"di2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[2]="i)}
  55. /"di3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[3]="i)}
  56. /"di4"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[4]="i)}
  57. /"di5"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[5]="i)}
  58. /"di6"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[6]="i)}
  59. /"di7"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[7]="i)}
  60. /"ti1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[8]="i)}
  61. /"ti2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[9]="i)}
  62. /"ti3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[10]="i)}
  63. /"ti4"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[11]="i)}
  64. /"ai2di1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[12]="i)}
  65. /"ai2di2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[13]="i)}
  66. /"ai2di3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[14]="i)}
  67. /"ai2di4"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[15]="i)}
  68. /"ai2di5"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[16]="i)}
  69. /"ai2di6"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[17]="i)}
  70. /"ai2di7"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[18]="i)}
  71. /"ai2di8"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("di[19]="i)}
  72. ' >$tLO_DI_DATA
  73. [ -e $tLO_DI_DATA ] && mv $tLO_DI_DATA $LO_DI_DATA
  74. cat $WORK |awk '
  75. /"do0"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[0]="i)}
  76. /"do1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[1]="i)}
  77. /"do2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[2]="i)}
  78. /"do3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[3]="i)}
  79. /"do4"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[4]="i)}
  80. /"do5"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[5]="i)}
  81. /"do6"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[6]="i)}
  82. /"do7"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[7]="i)}
  83. /"to1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[8]="i)}
  84. /"to2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[9]="i)}
  85. /"to3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[10]="i)}
  86. /"to4"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("do[11]="i)}
  87. ' >$tLO_DO_DATA
  88. [ -e $tLO_DO_DATA ] && mv $tLO_DO_DATA $LO_DO_DATA
  89. fi
  90. fi
  91. fi
  92. if [ ! -z $TOCOS_IP ];then
  93. if [ ! -z $Piface_IP ];then
  94. if [ $TOCOS_IP != $Piface_IP ];then
  95. RM_HOST=$TOCOS_IP
  96. wget -q -T $RETRYTIME -t $RETRY --secure-protocol=TLSv1 --no-check-certificate --user-agent=${USERAGENT} https://$RM_HOST:$PORT/$RM_JSON_DATA --http-user=${USER} --http-password=${PASSWORD} --output-document=${WORK}
  97. fi
  98. else
  99. RM_HOST=$TOCOS_IP
  100. wget -q -T $RETRYTIME -t $RETRY --secure-protocol=TLSv1 --no-check-certificate --user-agent=${USERAGENT} https://$RM_HOST:$PORT/$RM_JSON_DATA --http-user=${USER} --http-password=${PASSWORD} --output-document=${WORK}
  101. fi
  102. rm -f $TEMPERFILE $LO_TOCOS_WD_DATA $LO_TOCOS_RD_DATA
  103. if [ -e $WORK ];then
  104. LEN=`cat $WORK |wc -l`
  105. if [ $LEN -gt 2 ];then
  106. cat $WORK |awk '
  107. /"temper"/{gsub(/\,|\"temper\":\"|\"/,"");split($0,j," ");print(j[1]" "j[2])}
  108. ' >$tTEMPERFILE
  109. [ -e $tTEMPERFILE ] && mv $tTEMPERFILE $TEMPERFILE
  110. cat $WORK |awk '
  111. /"ti1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("TI1="i)}
  112. /"ti2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("TI2="i)}
  113. /"ti3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("TI3="i)}
  114. /"vai1"/{gsub(/\,/,"",$0);split($0,j,":");print("VAI1="j[2])}
  115. /"vai2"/{gsub(/\,/,"",$0);split($0,j,":");print("VAI2="j[2])}
  116. /"vai3"/{gsub(/\,/,"",$0);split($0,j,":");print("VAI3="j[2])}
  117. /"vai4"/{gsub(/\,/,"",$0);split($0,j,":");print("VAI4="j[2])}
  118. /"i2ctemp"/{gsub(/\"i2ctemp\":|\,/,"",$0);split($0,j," ");printf("i2c=%s,%s,%s\n",j[1],j[2],j[3])}
  119. ' >$tLO_TOCOS_RD_DATA
  120. [ -e $tLO_TOCOS_RD_DATA ] && mv $tLO_TOCOS_RD_DATA $LO_TOCOS_RD_DATA
  121. cat $WORK |awk '
  122. /"to1"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("TO1="i)}
  123. /"to2"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("TO2="i)}
  124. /"to3"/{if(/high/) i=1 ;if(/low/) i=0 ;if(/none/) i=-1;print("TO3="i)}
  125. ' >$tLO_TOCOS_WD_DATA
  126. [ -e $tLO_TOCOS_WD_DATA ] && mv $tLO_TOCOS_WD_DATA $LO_TOCOS_WD_DATA
  127. fi
  128. fi
  129. fi
  130. rm -f $WORK
  131. msleep $WAIT
  132. done
  133. done