99_UtilsHourCounter.pm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # $Id: 99_UtilsHourCounter.pm 2014-12-16 20:15:33 john $
  2. #
  3. # This ist a reference implementation for enhanced features for modul hourCounter
  4. #
  5. # This module is written by john.
  6. #
  7. # This file is part of fhem.
  8. #
  9. # Fhem is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # Fhem is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. # Changelog
  23. #
  24. # 04.02.14 - 1.00 modul created
  25. # 06.02.14 - 1.01 fixed: wrong timing in assignment appUtilization
  26. # 17.03.14 - 1.01 added: appHC_OnYear
  27. # 10.12.14 - 1.0.1.0 fixed: with integration of interval and support of cyclically updates
  28. # we need some changes:
  29. # instead of value and countsOverall , now tickChanged is used
  30. package main;
  31. use strict;
  32. use warnings;
  33. use POSIX;
  34. use vars qw(%defs);
  35. use vars qw(%modules);
  36. #require "98_HourCounter.pm";
  37. my $UtilsHourCounter_Version="1.0.1.0 - 10.12.2014 (john)";
  38. sub Log3($$$);
  39. # --------------------------------------------------
  40. sub UtilsHourCounter_Initialize($$)
  41. {
  42. my ($hash) = @_;
  43. Log3 '', 3, "[UtilsHourCounter] Init Done with Version $UtilsHourCounter_Version";
  44. }
  45. # --------------------------------------------------
  46. # yearly tasks
  47. #
  48. sub appHC_OnYear($$$)
  49. {
  50. my ($name,$part0,$part1) = @_; # name objects, name des parameters, wert des parameters
  51. $part0='' if (!defined($part0));
  52. my $hash = $defs{$name};
  53. return undef if (!defined ($hash));
  54. my $appCountsPerYear = ReadingsVal ($name, 'appCountsPerYearTemp' ,0);
  55. my $appOpHoursPerYear = ReadingsVal ($name, 'appOpHoursPerYearTemp' ,0);
  56. #---------------
  57. readingsBeginUpdate($hash);
  58. readingsBulkUpdate ($hash, 'appCountsPerYearTemp' , 0 );
  59. readingsBulkUpdate ($hash, 'appCountsPerYear' , $appCountsPerYear );
  60. readingsBulkUpdate ($hash, 'appOpHoursPerYearTemp' , 0 );
  61. readingsBulkUpdate ($hash, 'appOpHoursPerYear' , $appOpHoursPerYear );
  62. readingsEndUpdate($hash, 1)
  63. }
  64. # --------------------------------------------------
  65. # monthly tasks
  66. #
  67. sub appHC_OnMonth($$$)
  68. {
  69. my ($name,$part0,$part1) = @_; # name objects, name des parameters, wert des parameters
  70. $part0='' if (!defined($part0));
  71. my $hash = $defs{$name};
  72. return undef if (!defined ($hash));
  73. my $appCountsPerMonth = ReadingsVal ($name, 'appCountsPerMonthTemp' ,0);
  74. my $appOpHoursPerMonth = ReadingsVal ($name, 'appOpHoursPerMonthTemp' ,0);
  75. #---------------
  76. readingsBeginUpdate($hash);
  77. readingsBulkUpdate ($hash, 'appCountsPerMonthTemp' , 0 );
  78. readingsBulkUpdate ($hash, 'appCountsPerMonth' , $appCountsPerMonth );
  79. readingsBulkUpdate ($hash, 'appOpHoursPerMonthTemp' , 0 );
  80. readingsBulkUpdate ($hash, 'appOpHoursPerMonth' , $appOpHoursPerMonth );
  81. readingsEndUpdate($hash, 1)
  82. }
  83. # --------------------------------------------------
  84. # weekly tasks
  85. sub appHC_OnWeek($$$)
  86. {
  87. my ($name,$part0,$part1) = @_; # name objects, name des parameters, wert des parameters
  88. $part0='' if (!defined($part0));
  89. my $hash = $defs{$name};
  90. return undef if (!defined ($hash));
  91. my $appCountsPerWeek = ReadingsVal ($name, 'appCountsPerWeekTemp' ,0);
  92. my $appOpHoursPerWeek = ReadingsVal ($name, 'appOpHoursPerWeekTemp' ,0);
  93. readingsBeginUpdate($hash);
  94. readingsBulkUpdate ($hash, 'appCountsPerWeekTemp' , 0);
  95. readingsBulkUpdate ($hash, 'appCountsPerWeek' , $appCountsPerWeek);
  96. readingsBulkUpdate ($hash, 'appOpHoursPerWeekTemp' , 0);
  97. readingsBulkUpdate ($hash, 'appOpHoursPerWeek' , $appOpHoursPerWeek);
  98. readingsEndUpdate( $hash, 1 );
  99. }
  100. # --------------------------------------------------
  101. # dayly tasks
  102. sub appHC_OnDay($$$)
  103. {
  104. my ($name,$part0,$part1) = @_; # name objects, name des parameters, wert des parameters
  105. $part0='' if (!defined($part0));
  106. my $hash = $defs{$name};
  107. return undef if (!defined ($hash));
  108. my $appCountsPerDay = ReadingsVal($name, 'countsPerDay',0);
  109. my $pulseTimePerDay = ReadingsVal($name, 'pulseTimePerDay',0);
  110. #HourCounter_Log $hash, 2, "pulseTimePerDay:$pulseTimePerDay";
  111. my $appOpHoursPerDay = $pulseTimePerDay/3600;
  112. my $appOpHoursPerWeekTemp = ReadingsVal ($name,'appOpHoursPerWeekTemp',0 )+$appOpHoursPerDay;
  113. my $appOpHoursPerMonthTemp =ReadingsVal ($name,'appOpHoursPerMonthTemp',0 )+$appOpHoursPerDay;
  114. my $appOpHoursPerYearTemp =ReadingsVal ($name,'appOpHoursPerYearTemp',0 )+$appOpHoursPerDay;
  115. my $appUtilizationTempOld = ReadingsVal ($name,'appUtilizationTempOld',0 );
  116. readingsBeginUpdate($hash);
  117. readingsBulkUpdate ($hash, 'appCountsPerDay' , $appCountsPerDay);
  118. readingsBulkUpdate ($hash, 'appOpHoursPerDay' , $appOpHoursPerDay);
  119. readingsBulkUpdate ($hash, 'appOpHoursPerDayTemp' , 0);
  120. readingsBulkUpdate ($hash, 'appOpHoursPerWeekTemp' , $appOpHoursPerWeekTemp);
  121. readingsBulkUpdate ($hash, 'appOpHoursPerMonthTemp', $appOpHoursPerMonthTemp);
  122. readingsBulkUpdate ($hash, 'appOpHoursPerYearTemp' , $appOpHoursPerYearTemp);
  123. readingsBulkUpdate ($hash, 'appUtilization', $appUtilizationTempOld);
  124. readingsEndUpdate( $hash, 1 );
  125. }
  126. # --------------------------------------------------
  127. # hourly tasks
  128. sub appHC_OnHour($$$)
  129. {
  130. my ($name,$part0,$part1) = @_; # name objects, name des parameters, wert des parameters
  131. $part0='' if (!defined($part0));
  132. my $hash = $defs{$name};
  133. return undef if (!defined ($hash));
  134. my $appCountsPerHourTemp = ReadingsVal($name, 'appCountsPerHourTemp',0);
  135. readingsBeginUpdate($hash);
  136. readingsBulkUpdate ($hash, 'appCountsPerHourTemp', 0 );
  137. readingsBulkUpdate ($hash, 'appCountsPerHour', $appCountsPerHourTemp);
  138. readingsEndUpdate( $hash, 1 );
  139. }
  140. # --------------------------------------------------
  141. # task on count change
  142. sub appHC_OnCount($$$)
  143. {
  144. my ($name,$part0,$part1) = @_; # name objects, name des parameters, wert des parameters
  145. $part0='' if (!defined($part0));
  146. my $hash = $defs{$name};
  147. return undef if (!defined ($hash));
  148. my $appCountsPerHourTemp = ReadingsVal($name,'appCountsPerHourTemp',0) + 1;
  149. my $appCountsPerWeekTemp = ReadingsVal($name,'appCountsPerWeekTemp',0) + 1;
  150. my $appCountsPerMonthTemp = ReadingsVal($name,'appCountsPerMonthTemp',0)+ 1;
  151. my $appCountsPerYearTemp = ReadingsVal($name,'appCountsPerYearTemp',0) + 1;
  152. readingsBeginUpdate($hash);
  153. readingsBulkUpdate ($hash, 'appCountsPerHourTemp', $appCountsPerHourTemp );
  154. readingsBulkUpdate ($hash, 'appCountsPerWeekTemp', $appCountsPerWeekTemp );
  155. readingsBulkUpdate ($hash, 'appCountsPerMonthTemp',$appCountsPerMonthTemp );
  156. readingsBulkUpdate ($hash, 'appCountsPerYearTemp',$appCountsPerYearTemp );
  157. readingsEndUpdate( $hash, 1 );
  158. }
  159. # --------------------------------------------------
  160. # task on value change
  161. sub appHC_OnUpdate($$$)
  162. {
  163. my ($name,$part0,$part1) = @_; # object name, parameter name, parameter value
  164. $part0='' if (!defined($part0));
  165. my $hash = $defs{$name};
  166. return undef if (!defined ($hash));
  167. # acquire needed values
  168. my $secs= HourCounter_SecondsOfDay();
  169. my $pulseTimePerDay = ReadingsVal($name,'pulseTimePerDay',0);
  170. # calc utilization
  171. $secs= 1 if ($secs==0); # no zero division
  172. my $appUtilizationTempOld = ReadingsVal($name,'appUtilizationTemp',0);
  173. my $appUtilizationTemp = $pulseTimePerDay/$secs*100;
  174. # calc operating hours
  175. my $appOpHoursPerDayTemp =$pulseTimePerDay/3600; # operating time per Day temporary
  176. readingsBeginUpdate($hash);
  177. readingsBulkUpdate ($hash, 'appOpHoursPerDayTemp' , $appOpHoursPerDayTemp);
  178. readingsBulkUpdate ($hash, 'appUtilizationTemp' , $appUtilizationTemp );
  179. readingsBulkUpdate ($hash, 'appUtilizationTempOld' , $appUtilizationTempOld );
  180. readingsEndUpdate( $hash, 1 );
  181. }
  182. # --------------------------------------------------
  183. # central event dispatcher
  184. sub appHCNotify($$$)
  185. {
  186. my ($name,$part0,$part1) = @_; # object name, parameter name, parameter value
  187. $name = "?" if (!defined($name));
  188. $part0='' if (!defined($part0));
  189. my $hash = $defs{$name};
  190. return undef if (!defined ($hash));
  191. my $value = ReadingsVal($name,'value',0);
  192. #HourCounter_Log ($hash, 2, "Name:$name part0:$part0 part1:$part1 value:$value");
  193. if ($part0 eq "tickUpdated:")
  194. {
  195. HourCounter_cmdQueueAdd($hash,"appHC_OnUpdate q($name),q($part0),q($part1)");
  196. }
  197. elsif ($part0 eq "tickChanged:")
  198. {
  199. # count only if rising edge
  200. if ( $value == 1)
  201. {
  202. HourCounter_cmdQueueAdd($hash,"appHC_OnCount q($name),q($part0),q($part1)");
  203. }
  204. }
  205. elsif ($part0 eq "tickHour:") # trigger CN.Test tickHour: 1
  206. {
  207. HourCounter_cmdQueueAdd($hash,"appHC_OnHour q($name),q($part0),q($part1)");
  208. }
  209. elsif ($part0 eq "tickDay:") # trigger CN.Test tickDay: 1
  210. {
  211. HourCounter_cmdQueueAdd($hash,"appHC_OnDay q($name),q($part0),q($part1)");
  212. }
  213. elsif ($part0 eq "tickWeek:")
  214. {
  215. HourCounter_cmdQueueAdd($hash,"appHC_OnWeek q($name),q($part0),q($part1)");
  216. }
  217. elsif ($part0 eq "tickMonth:")
  218. {
  219. HourCounter_cmdQueueAdd($hash,"appHC_OnMonth q($name),q($part0),q($part1)");
  220. }
  221. elsif ($part0 eq "tickYear:")
  222. {
  223. HourCounter_cmdQueueAdd($hash,"appHC_OnYear q($name),q($part0),q($part1)");
  224. }
  225. return '';
  226. }
  227. 1;