98_CustomReadings.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # $Id: 98_CustomReadings.pm 12157 2016-09-13 11:23:02Z hcs-svn $
  2. #
  3. # TODO:
  4. package main;
  5. use strict;
  6. use warnings;
  7. use SetExtensions;
  8. no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  9. sub CustomReadings_Initialize($) {
  10. my ($hash) = @_;
  11. $hash->{DefFn} = "CustomReadings_Define";
  12. $hash->{UndefFn} = "CustomReadings_Undef";
  13. $hash->{AttrList} = "readingDefinitions "
  14. . "interval "
  15. . "$readingFnAttributes";
  16. }
  17. sub
  18. CustomReadings_Define($$) {
  19. my ($hash, $def) = @_;
  20. my $name = $hash->{NAME};
  21. CustomReadings_read($hash);
  22. return undef;
  23. }
  24. sub CustomReadings_read($) {
  25. my ($hash) = @_;
  26. my $name = $hash->{NAME};
  27. RemoveInternalTimer($hash);
  28. InternalTimer(gettimeofday()+ AttrVal( $name, "interval", 5), "CustomReadings_read", $hash, 0);
  29. # Get the readingDefinitions and remove all newlines from the attribute
  30. my $readingDefinitions = AttrVal( $name, "readingDefinitions", "");
  31. $readingDefinitions =~ s/\n//g;
  32. my @used = ("state");
  33. my $isCombined = 0;
  34. my @combinedOutput = ();
  35. my $hasErrors = 0;
  36. readingsBeginUpdate($hash);
  37. my @definitionList = split(",", $readingDefinitions);
  38. while (@definitionList) {
  39. my $param = shift(@definitionList);
  40. while ($param && $param =~ /{/ && $param !~ /}/ ) {
  41. my $next = shift(@definitionList);
  42. last if( !defined($next) );
  43. $param .= ",". $next;
  44. }
  45. my @definition = split(':', $param, 2);
  46. if ($definition[0] eq "COMBINED") {
  47. $isCombined = 1;
  48. my $cmdStr = $definition[1];
  49. my $cmdTemp = eval("$cmdStr");
  50. if (ref $cmdTemp eq 'ARRAY') {
  51. @combinedOutput = @{ $cmdTemp };
  52. } else {
  53. @combinedOutput = split(/^/, $cmdTemp);
  54. }
  55. Log 5, "Using combined mode for customReadings: $cmdStr";
  56. next;
  57. }
  58. else {
  59. push(@used, $definition[0]);
  60. }
  61. if($definition[1] ne "") {
  62. $isCombined = 0;
  63. }
  64. my $value;
  65. if ($isCombined) {
  66. $value = shift @combinedOutput;
  67. if (!($value)) {
  68. $value = 0;
  69. $hasErrors = 1;
  70. Log 3, "customReadings: Warning for $name: combined command for " . $definition[0] . " returned nothing or not enough lines.";
  71. }
  72. }
  73. else {
  74. $value = eval($definition[1]);
  75. }
  76. if(defined $value) {
  77. $value =~ s/^\s+|\s+$//g;
  78. }
  79. else {
  80. $value = "ERROR";
  81. $hasErrors = 1;
  82. }
  83. readingsBulkUpdate($hash, $definition[0], $value);
  84. }
  85. readingsBulkUpdate($hash, "state", $hasErrors ? "Errors" : "OK");
  86. readingsEndUpdate($hash, 1);
  87. foreach my $r (keys %{$hash->{READINGS}}) {
  88. if (not $r ~~ @used) {
  89. delete $hash->{READINGS}{$r};
  90. }
  91. }
  92. }
  93. sub CustomReadings_Undef($$) {
  94. my ($hash, $arg) = @_;
  95. my $name = $hash->{NAME};
  96. RemoveInternalTimer($hash);
  97. return undef;
  98. }
  99. sub CustomReadings_GetHTML ($) {
  100. my ($name) = @_;
  101. my $hash = $main::defs{$name};
  102. my $result = "";
  103. $result .= "<table>";
  104. my @sortedReadings = sort keys %{$hash->{READINGS}};
  105. foreach my $reading (@sortedReadings) {
  106. $result .= "<tr>";
  107. $result .= "<td>$reading:&nbsp;</td><td>" . ReadingsVal($name, $reading, "???") . "</td>";
  108. $result .= "</tr>";
  109. }
  110. $result .= "</table>";
  111. return $result;
  112. }
  113. 1;
  114. =pod
  115. =item summary Allows to define own readings.
  116. =item summary_DE Ermöglicht eingen readings.
  117. =begin html
  118. <a name="CustomReadings"></a>
  119. <h3>CustomReadings</h3>
  120. <ul>
  121. FHEM module to define own readings.
  122. <br><br>
  123. This module allows to define own readings. The readings can be defined in an attribute so that they can get changed without changing the code of the module.<br>
  124. To use this module you should have some perl and linux knowledge<br>
  125. The examples presuppose that you run FHEM on a linux machine like a Raspberry Pi or a Cubietruck.<br>
  126. Note: the "bullshit" definition is an example to show what happens if you define bullshit :-)<br><br>
  127. <u>Example (definition in fhem.cfg)</u>
  128. <br><code>
  129. define myReadings CustomReadings<br>
  130. attr myReadings room 0-Test<br>
  131. attr myReadings group Readings<br>
  132. attr myReadings interval 2<br>
  133. attr myReadings readingDefinitions hdd_temperature:qx(hddtemp /dev/sda 2>&1),<br>
  134. ac_powersupply_voltage:qx(cat /sys/class/power_supply/ac/voltage_now 2>&1) / 1000000,<br>
  135. ac_powersupply_current:qx(cat /sys/class/power_supply/ac/current_now 2>&1) / 1000000,<br>
  136. perl_version:$],<br>
  137. timezone:qx(cat /etc/timezone 2>&1),<br>
  138. kernel:qx(uname -r 2>&1),<br>
  139. device_name:$hash->{NAME},<br>
  140. bullshit: $hash->{bullshit},<br>
  141. fhem_backup_folder_size:qx(du -ch /opt/fhem/backup | grep total | cut -d 't' -f1 2>&1)<br>
  142. <br><br>
  143. <b>Optionally, to display the readings:</b><br>
  144. define myReadingsDisplay weblink htmlCode {CustomReadings_GetHTML('myReadings')}<br>
  145. attr myReadingsDisplay group Readings<br>
  146. attr myReadingsDisplay room 0-Test<br>
  147. </code>
  148. <br>
  149. <u>Resulting readings:</u><br>
  150. <table>
  151. <colgroup width="250" span="3"></colgroup>
  152. <tr>
  153. <td>ac_powersupply_current</td>
  154. <td>0.236</td>
  155. <td>2014-08-09 15:40:21</td>
  156. </tr>
  157. <tr>
  158. <td>ac_powersupply_voltage</td>
  159. <td>5.028</td>
  160. <td>2014-08-09 15:40:21</td>
  161. </tr>
  162. <tr>
  163. <td>bullshit</td>
  164. <td>ERROR</td>
  165. <td>2014-08-09 15:40:21</td>
  166. </tr>
  167. <tr>
  168. <td>device_name</td>
  169. <td>myReadings</td>
  170. <td>2014-08-09 15:40:21</td>
  171. </tr>
  172. <tr>
  173. <td>fhem_backup_folder_size</td>
  174. <td>20M</td>
  175. <td>2014-08-09 15:40:21</td>
  176. </tr>
  177. <tr>
  178. <td>hdd_temperature</td>
  179. <td>/dev/sda: TS128GSSD320: 47°C</td>
  180. <td>2014-08-09 15:40:21</td>
  181. </tr>
  182. <tr>
  183. <td>kernel</td>
  184. <td>3.4.103-sun7i+</td>
  185. <td>2014-08-09 15:40:21</td>
  186. </tr>
  187. <tr>
  188. <td>perl_version</td>
  189. <td>5.014002</td>
  190. <td>2014-08-09 15:40:21</td>
  191. </tr>
  192. <tr>
  193. <td>timezone</td>
  194. <td>Europe/Berlin</td>
  195. <td>2014-08-09 15:40:21</td>
  196. </tr>
  197. </table>
  198. <br>
  199. <a name="CustomReadings_Define"></a>
  200. <b>Define</b><br>
  201. define &lt;name&gt; CustomReadings<br>
  202. <br>
  203. <a name="CustomReadings_Readings"></a>
  204. <b>Readings</b><br>
  205. As defined
  206. <br><br>
  207. <a name="CustomReadings_Attr"></a>
  208. <b>Attributes</b>
  209. <ul>
  210. <li>interval<br>
  211. Refresh interval in seconds</li><br>
  212. <li>readingDefinitions<br>
  213. The definitions are separated by a comma. A definition consists of two parts, separated by a colon.<br>
  214. The first part is the name of the reading and the second part the function.<br>
  215. The function gets evaluated and must return a result.<br><br>
  216. Example: <code>kernel:qx(uname -r 2>&1)</code><br>
  217. Defines a reading with the name "kernel" and evaluates the linux function uname -r<br>
  218. Multiline output from commands, systemcall, scripts etc. can be use for more than one reading with <br>
  219. the keyword <code>COMBINED</code> as reading (which wont appear itself) while its command output<br>
  220. will be put line by line in the following readings defined (so they don't need a function defined<br>
  221. after the colon (it would be ignored)).But the lines given must match the number and order of the<br>
  222. following readings.<br><br>
  223. COMBINED can be used together or lets say after or even in between normal expressions if the<br>
  224. number of lines of the output matches exactly.
  225. Example: <code>COMBINED:qx(cat /proc/sys/vm/dirty_background*),dirty_bytes:,dirty_ration:</code><br>
  226. Defines two readings (dirty_bytes and dirty_ratio) which will get set by the lines of those <br>
  227. two files the cat command will find in the kernel proc directory.<br>
  228. In some cases this can give an noticeable performance boost as the readings are filled up all at once.
  229. </li>
  230. </ul><br>
  231. </ul>
  232. =end html
  233. =cut