98_CustomReadings.pm 8.8 KB

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