PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/webfrontend/pgm3/include/gnuplot.php

https://github.com/frankyH/fhem-mirror
PHP | 324 lines | 247 code | 72 blank | 5 comment | 23 complexity | 1a32b813fbf2415258f4b831ac2820fc MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. ################# Creates automatically gnuplot-graphics for pgm3
  3. # Thanks to Rudi for his inspiration to automate gnuplot
  4. # (look at his fhzweb.pl in pgm2)
  5. ################
  6. function drawgnuplot($gnudraw,$gnutyp,$gnuplot,$pictype,$logpath,$FHTyrange,$FHTy2range,$DBUse)
  7. {
  8. if ($gnutyp=="userdef")
  9. {
  10. $userdef=$FHTyrange; # workaround
  11. $userdefnr=$FHTy2range; # workaround
  12. $gnuplottype= $userdef['gnuplottype'];
  13. $logfile= $userdef['logpath'];
  14. $drawuserdef=$userdef['name'];
  15. $SemanticLong=$userdef['semlong'];
  16. $SemanticShort=$userdef['semshort'];
  17. $valuefield=$userdef['valuefield'];
  18. $type='UserDef '.$userdefnr;
  19. $IN="$gnudraw ($gnutyp $userdefnr)";
  20. if ($gnuplottype=='piri') $gnutyp='piri';
  21. if ($gnuplottype=='fs20') $gnutyp='FS20';
  22. }
  23. else
  24. {
  25. $logfile=$logpath."/".$gnudraw.".log";
  26. $IN="$gnudraw ($gnutyp)";
  27. }
  28. ### DBUse for
  29. if ($DBUse == '1' and ( $gnutyp=='FHT' or $gnutyp=='HMS100T' or $gnutyp=='HMS100TF' or $gnutyp=='CUL_WS' or $gnutyp=='KS300_t1' or $gnutyp=='KS300_t2' or $gnutyp=='WS300_t1' or $gnutyp=='WS300_t2'))
  30. {
  31. include "config.php";
  32. $logfile="tmp/".$gnudraw.".log";
  33. $f1=fopen($logfile,"w+");
  34. };
  35. $gnudraw1=$gnudraw.'1';
  36. $OUT1="set output 'tmp/$gnudraw.$pictype'";
  37. $OUT2="set output 'tmp/$gnudraw1.$pictype'";
  38. $gplothdr="
  39. set terminal $pictype crop
  40. set xdata time
  41. set timefmt '%Y-%m-%d_%H:%M:%S'
  42. set xlabel ' '
  43. set ytics nomirror
  44. set y2tics
  45. set title '$IN'
  46. set grid
  47. ";
  48. $datumtomorrow= mktime (0,0,0,date("m") ,date("d")+1,date("Y"));
  49. $xrange1= date ("Y-m-d",$datumtomorrow);
  50. $datumyesterday= mktime (0,0,0,date("m") ,date("d")-1,date("Y"));
  51. $datumweek= mktime (0,0,0,date("m") ,date("d")-6,date("Y"));
  52. $xrange2= date ("Y-m-d",$datumyesterday);
  53. $xrange3= date ("Y-m-d",$datumweek);
  54. $xrange="set xrange ['$xrange2':'$xrange1'] ";
  55. $xrangeweek="set xrange ['$xrange3':'$xrange1'] ";
  56. switch ($gnutyp):
  57. Case FS20: ############################################
  58. $gplotmain=<<<EOD
  59. set size 1,0.5
  60. set noytics
  61. set noy2tics
  62. set yrange [-0.2:1.2]
  63. set ylabel "On/Off"
  64. plot "< awk '{print $1, $3==\"on\"? 1 : $3==\"dimup\"? 1 : $3==\"dimdown\"? 0 : $3==\"off\"? 0 : 0.5;}' $logfile" using 1:2 title '' with steps
  65. EOD;
  66. break;
  67. Case WS300_t1: ############################################
  68. if ($DBUse==1)
  69. {
  70. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' and reading='data' order by timestamp desc limit ".$logrotateKS300lines."") or die (mysql_error());
  71. while ($row = mysql_fetch_object($sqlarray)) {
  72. $date=str_replace(" ","_",$row->timestamp);
  73. fputs($f1,"$date $gnudraw $row->value\n");
  74. }
  75. fclose($f1);
  76. }
  77. $gplotmain="
  78. set ylabel 'Temperature (Celsius)'
  79. set y2label 'Humidity (%)'
  80. plot '$logfile' using 1:4 axes x1y1 title 'Temperature' with lines lw 2,\
  81. '$logfile' using 1:6 axes x1y2 title 'Rel. Humidity (%)' with lines
  82. ";
  83. break;
  84. Case WS300_t2: ############################################
  85. if ($DBUse==1)
  86. {
  87. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' and reading='data' order by timestamp desc limit ".$logrotateKS300lines."") or die (mysql_error());
  88. while ($row = mysql_fetch_object($sqlarray)) {
  89. $date=str_replace(" ","_",$row->timestamp);
  90. fputs($f1,"$date $gnudraw $row->value\n");
  91. }
  92. fclose($f1);
  93. }
  94. $gplotmain=<<<EOD
  95. set ylabel "Air Pressure (hPa)"
  96. set y2label "Willi"
  97. plot "< grep -v avg $logfile" using 1:8 axes x1y1 title 'Air Pressure' with lines, \
  98. "< grep -v avg $logfile" using 1:10 axes x1y2 title 'Willi' with lines
  99. EOD;
  100. break;
  101. Case KS300_t1: ############################################
  102. if ($DBUse==1)
  103. {
  104. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' and reading='data' order by timestamp desc limit ".$logrotateKS300lines."") or die (mysql_error());
  105. while ($row = mysql_fetch_object($sqlarray)) {
  106. $date=str_replace(" ","_",$row->timestamp);
  107. fputs($f1,"$date $gnudraw $row->value\n");
  108. }
  109. fclose($f1);
  110. }
  111. $gplotmain="
  112. set ylabel 'Temperature (Celsius)'
  113. set y2label 'Humidity (%)'
  114. plot '$logfile' using 1:4 axes x1y1 title 'Temperature' with lines lw 2,\
  115. '$logfile' using 1:6 axes x1y2 title 'Rel. Humidity (%)' with lines
  116. ";
  117. break;
  118. Case KS300_t2: ############################################
  119. if ($DBUse==1)
  120. {
  121. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' and reading='data' order by timestamp desc limit ".$logrotateKS300lines."") or die (mysql_error());
  122. while ($row = mysql_fetch_object($sqlarray)) {
  123. $date=str_replace(" ","_",$row->timestamp);
  124. fputs($f1,"$date $gnudraw $row->value\n");
  125. }
  126. fclose($f1);
  127. }
  128. $gplotmain="
  129. set ylabel 'Wind (Km/h)'
  130. set y2label 'Rain (l/m2)'
  131. plot '$logfile' using 1:8 axes x1y1 title 'Wind' with lines, \
  132. '$logfile' using 1:10 axes x1y2 title 'Rain' with lines
  133. ";
  134. break;
  135. Case FHT: ############################################
  136. if ($DBUse==1)
  137. {
  138. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' order by timestamp desc limit ".$logrotateFHTlines."") or die (mysql_error());
  139. while ($row = mysql_fetch_object($sqlarray)) {
  140. $date=str_replace(" ","_",$row->timestamp);
  141. fputs($f1,"$date $gnudraw $row->reading $row->value\n");
  142. }
  143. fclose($f1);
  144. }
  145. $gplotmain="
  146. set ylabel 'Temperature (Celsius)'
  147. set yrange [$FHTyrange]
  148. set grid ytics
  149. set y2label 'Actuator (%)'
  150. set y2range [$FHTy2range]
  151. ";
  152. $gplotmain2="
  153. set ylabel 'Temperature (Celsius)'
  154. set grid ytics
  155. ";
  156. $gplotmaintmp = <<<EOD
  157. plot "< awk '/measured/{print $1, $4}' $logfile"\
  158. using 1:2 axes x1y1 title 'Measured temperature' with lines lw 2,\
  159. "< awk '/actuator/{print $1, $4+0}' $logfile"\
  160. using 1:2 axes x1y2 title 'Actuator (%)' with steps lw 1,\
  161. "< awk '/desired/{print $1, $4}' $logfile"\
  162. using 1:2 axes x1y1 title 'Desired temperature' with steps
  163. EOD;
  164. $gplotmainonlymeasured = <<<EOD
  165. plot "< awk '/measured/{print $1, $4}' $logfile"\
  166. using 1:2 axes x1y1 title 'Measured temperature' with lines lw 2
  167. EOD;
  168. $gplotmain=$gplotmain.$gplotmaintmp;
  169. $gplotmain2=$gplotmain2.$gplotmainonlymeasured;
  170. break;
  171. Case HMS100T: ############################################
  172. if ($DBUse==1)
  173. {
  174. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' and reading='data' and type='HMS' order by timestamp desc limit ".$logrotateHMSlines."") or die (mysql_error());
  175. while ($row = mysql_fetch_object($sqlarray)) {
  176. $date=str_replace(" ","_",$row->timestamp);
  177. fputs($f1,"$date $gnudraw $row->value\n");
  178. }
  179. fclose($f1);
  180. }
  181. $gplotmain="
  182. set ylabel 'Temperature (Celsius)'
  183. plot '$logfile' using 1:4 axes x1y1 title 'Temperature' with lines lw 2
  184. ";
  185. break;
  186. Case userdef: ############################################
  187. $gplotmain=<<<EOD
  188. \n set ylabel '$SemanticLong ( $SemanticShort )'
  189. set size 1,0.5
  190. set noy2tics
  191. plot "$logfile" using 1:$valuefield axes x1y1 title '$SemanticLong' with lines lw 2
  192. EOD;
  193. break;
  194. Case piri: ############################################
  195. $gplotmain=<<<EOD
  196. \n set ylabel '$drawuserdef'
  197. set noytics
  198. set noy2tics
  199. set size 1,0.5
  200. set yrange [-1.2:2.2]
  201. plot "< awk '{print $1, 1; }' $logfile "\
  202. using 1:2 title '$drawuserdef' with impulses
  203. plot "$logfile" using 1:$valuefield axes x1y1 title '$SemanticLong' with lines lw 2
  204. EOD;
  205. break;
  206. Case HMS100TF: ############################################
  207. Case CUL_WS: ############################################
  208. if ($DBUse==1)
  209. {
  210. $sqlarray=mysql_query("select timestamp,reading,value from history where device='".$gnudraw."' and reading='data' and (type='HMS' or type='CUL_WS') order by timestamp desc limit ".$logrotateHMSlines."") or die (mysql_error());
  211. while ($row = mysql_fetch_object($sqlarray)) {
  212. $date=str_replace(" ","_",$row->timestamp);
  213. fputs($f1,"$date $gnudraw $row->value\n");
  214. }
  215. fclose($f1);
  216. }
  217. $gplotmain="
  218. set ylabel 'Temperature (Celsius)'
  219. set y2label 'Humidity (%)'
  220. plot '$logfile' using 1:4 axes x1y1 title 'Temperature' with lines lw 2,\
  221. '$logfile' using 1:6 axes x1y2 title 'Rel. Humidity (%)' with lines
  222. ";
  223. break;
  224. default:
  225. endswitch;
  226. $message=$OUT1.$gplothdr.$xrangeweek.$gplotmain;
  227. $f3=fopen("tmp/gnu1","w+");
  228. fputs($f3,$message);
  229. fclose($f3);
  230. exec("$gnuplot tmp/gnu1",$output);
  231. $message=$OUT2.$gplothdr.$xrange.$gplotmain;
  232. $f2=fopen("tmp/gnu2","w");
  233. fputs($f2,$message);
  234. fclose($f2);
  235. exec("$gnuplot tmp/gnu2",$output);
  236. $FOUT='tmp/'.$gnudraw1.'.'.$pictype;
  237. $FS=filesize($FOUT);
  238. if (($FS == '0') and ($gnutyp != "userdef")) ##Grafic mistake (e.G. no actuator). Draw againg without actuator
  239. {
  240. $message=$OUT1.$gplothdr.$gplotmain2;
  241. $f1=fopen("tmp/gnu1","w");
  242. fputs($f1,$message);
  243. fclose($f1);
  244. exec("$gnuplot tmp/gnu1",$output);
  245. $message=$OUT2.$gplothdr.$xrange.$gplotmain2;
  246. $f2=fopen("tmp/gnu2","w");
  247. fputs($f2,$message);
  248. fclose($f2);
  249. exec("$gnuplot tmp/gnu2",$output);
  250. }
  251. };
  252. ?>