46_SmartPi.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. ###############################################################################
  2. #
  3. # Developed with Kate
  4. #
  5. # (c) 2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
  6. # All rights reserved
  7. #
  8. # This script is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # any later version.
  12. #
  13. # The GNU General Public License can be found at
  14. # http://www.gnu.org/copyleft/gpl.html.
  15. # A copy is found in the textfile GPL.txt and important notices to the license
  16. # from the author is found in LICENSE.txt distributed with these scripts.
  17. #
  18. # This script is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. #
  24. # $Id: 46_SmartPi.pm 15556 2017-12-05 04:05:00Z CoolTux $
  25. #
  26. ###############################################################################
  27. ##
  28. ##
  29. ## Das JSON Modul immer in einem eval aufrufen
  30. # $data = eval{decode_json($data)};
  31. #
  32. # if($@){
  33. # Log3($SELF, 2, "$TYPE ($SELF) - error while request: $@");
  34. #
  35. # readingsSingleUpdate($hash, "state", "error", 1);
  36. #
  37. # return;
  38. # }
  39. #
  40. #
  41. ###### Möglicher Aufbau eines JSON Strings für die SmartPi
  42. #
  43. # Recieve JSON data: {"serial":"smartpi160812345","name":"B1.1_House","lat":52.3667,"lng":9.7167,"time":"2017-06-17 10:19:04","softwareversion":"","ipaddress":"169.254.3.10","datasets":[{"time":"2017-06-17 10:19:02","phases":[{"phase":1,"name":"phase 1","values":[{"type":"current","unity":"A","info":"","data":1.0003561},{"type":"voltage","unity":"V","info":"","data":230},{"type":"power","unity":"W","info":"","data":230.0819},{"type":"cosphi","unity":"","info":"","data":-0.72846437},{"type":"frequency","unity":"Hz","info":"","data":49.306625}]},{"phase":2,"name":"phase 2","values":[{"type":"current","unity":"A","info":"","data":0.45092472},{"type":"voltage","unity":"V","info":"","data":230},{"type":"power","unity":"W","info":"","data":103.712685},{"type":"cosphi","unity":"","info":"","data":-0.82941854},{"type":"frequency","unity":"Hz","info":"","data":48.192772}]},{"phase":3,"name":"phase 3","values":[{"type":"current","unity":"A","info":"","data":0.4813663},{"type":"voltage","unity":"V","info":"","data":230},{"type":"power","unity":"W","info":"","data":110.71425},{"type":"cosphi","unity":"","info":"","data":-0.2584238},{"type":"frequency","unity":"Hz","info":"","data":50.354053}]},{"phase":4,"name":"phase 4","values":[{"type":"current","unity":"A","info":"","data":0.7937981}]}]}]}
  44. #
  45. #
  46. ##
  47. ##
  48. package main;
  49. my $missingModul = "";
  50. use strict;
  51. use warnings;
  52. use HttpUtils;
  53. eval "use JSON;1" or $missingModul .= "JSON ";
  54. my $version = "1.2.0";
  55. # Declare functions
  56. sub SmartPi_Attr(@);
  57. sub SmartPi_Define($$);
  58. sub SmartPi_Initialize($);
  59. sub SmartPi_Notify($$);
  60. sub SmartPi_Get($@);
  61. sub SmartPi_GetData($@);
  62. sub SmartPi_Undef($$);
  63. sub SmartPi_ResponseProcessing($$);
  64. sub SmartPi_ErrorHandling($$$);
  65. sub SmartPi_WriteReadings($$);
  66. sub SmartPi_Timer_GetData($);
  67. sub SmartPi_Initialize($) {
  68. my ($hash) = @_;
  69. # Consumer
  70. $hash->{GetFn} = "SmartPi_Get";
  71. $hash->{DefFn} = "SmartPi_Define";
  72. $hash->{UndefFn} = "SmartPi_Undef";
  73. $hash->{NotifyFn} = "SmartPi_Notify";
  74. $hash->{AttrFn} = "SmartPi_Attr";
  75. $hash->{AttrList} = "interval ".
  76. "disable:1 ".
  77. "decimalPlace:0,1,2,3,4,5 ".
  78. "disabledForIntervals ".
  79. $readingFnAttributes;
  80. foreach my $d(sort keys %{$modules{SmartPi}{defptr}}) {
  81. my $hash = $modules{SmartPi}{defptr}{$d};
  82. $hash->{VERSION} = $version;
  83. }
  84. }
  85. sub SmartPi_Define($$) {
  86. my ( $hash, $def ) = @_;
  87. my @a = split( "[ \t][ \t]*", $def );
  88. return "too few parameters: define <name> SmartPi <HOST>" if( @a != 3);
  89. return "Cannot define a HEOS device. Perl modul $missingModul is missing." if ( $missingModul );
  90. my $name = $a[0];
  91. my $host = $a[2];
  92. $hash->{HOST} = $host;
  93. $hash->{NOTIFYDEV} = "global";
  94. $hash->{INTERVAL} = 300;
  95. $hash->{PORT} = 1080;
  96. $hash->{VERSION} = $version;
  97. $attr{$name}{room} = "SmartPi" if( !defined( $attr{$name}{room} ) );
  98. Log3 $name, 3, "SmartPi ($name) - defined SmartPi Device with Host $host, Port $hash->{PORT} and Interval $hash->{INTERVAL}";
  99. $modules{SmartPi}{defptr}{HOST} = $hash;
  100. return undef;
  101. }
  102. sub SmartPi_Undef($$) {
  103. my ( $hash, $arg ) = @_;
  104. my $name = $hash->{NAME};
  105. Log3 $name, 3, "SmartPi ($name) - Device $name deleted";
  106. delete $modules{SmartPi}{defptr}{HOST} if( defined($modules{SmartPi}{defptr}{HOST}) and $hash->{HOST} );
  107. return undef;
  108. }
  109. sub SmartPi_Attr(@) {
  110. my ( $cmd, $name, $attrName, $attrVal ) = @_;
  111. my $hash = $defs{$name};
  112. my $orig = $attrVal;
  113. if( $attrName eq "disable" ) {
  114. if( $cmd eq "set" ) {
  115. if( $attrVal eq "0" ) {
  116. readingsSingleUpdate ( $hash, "state", "enabled", 1 );
  117. Log3 $name, 3, "SmartPi ($name) - enabled";
  118. } else {
  119. RemoveInternalTimer($hash);
  120. readingsSingleUpdate ( $hash, "state", "disabled", 1 );
  121. Log3 $name, 3, "SmartPi ($name) - disabled";
  122. }
  123. } else {
  124. readingsSingleUpdate ( $hash, "state", "enabled", 1 );
  125. Log3 $name, 3, "SmartPi ($name) - enabled";
  126. }
  127. } elsif( $attrName eq "disabledForIntervals" ) {
  128. if( $cmd eq "set" ) {
  129. return "check disabledForIntervals Syntax HH:MM-HH:MM or 'HH:MM-HH:MM HH:MM-HH:MM ...'"
  130. unless($attrVal =~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/);
  131. Log3 $name, 3, "SmartPi ($name) - disabledForIntervals";
  132. readingsSingleUpdate ( $hash, "state", "disabled", 1 );
  133. }
  134. elsif( $cmd eq "del" ) {
  135. Log3 $name, 3, "SmartPi ($name) - enabled";
  136. readingsSingleUpdate ( $hash, "state", "active", 1 );
  137. }
  138. } elsif( $attrName eq "interval" ) {
  139. if( $cmd eq "set" ) {
  140. $hash->{INTERVAL} = $attrVal;
  141. } else {
  142. $hash->{INTERVAL} = 300;
  143. }
  144. }
  145. return undef;
  146. }
  147. sub SmartPi_Notify($$) {
  148. my ($hash,$dev) = @_;
  149. my $name = $hash->{NAME};
  150. return if (IsDisabled($name));
  151. my $devname = $dev->{NAME};
  152. my $devtype = $dev->{TYPE};
  153. my $events = deviceEvents($dev,1);
  154. return if (!$events);
  155. SmartPi_Timer_GetData($hash) if( (grep /^DEFINED.$name$/,@{$events}
  156. or grep /^INITIALIZED$/,@{$events}
  157. or grep /^MODIFIED.$name$/,@{$events}
  158. or grep /^DELETEATTR.$name.interval$/,@{$events}
  159. or grep /^ATTR.$name.interval.[0-9]+/,@{$events}
  160. or grep /^DELETEATTR.$name.disable$/,@{$events}
  161. or grep /^ATTR.$name.disable.0$/,@{$events}
  162. or grep /^DELETEATTR.$name.decimalPlace$/,@{$events}
  163. or grep /^ATTR.$name.decimalPlace.[0-9]+/,@{$events}) and $init_done );
  164. return;
  165. }
  166. sub SmartPi_Get($@) {
  167. my ($hash, $name, $cmd, @args) = @_;
  168. my ($arg, @params) = @args;
  169. my @phaseId = ('phase1','phase2','phase3','all');
  170. my @valueId = ('all','current','voltage','power','cosphi','frequency');
  171. my $phaseId;
  172. my $valueId;
  173. if( $cmd eq 'phase1' ) {
  174. $phaseId = 1;
  175. $valueId = $arg;
  176. } elsif( $cmd eq 'phase2' ) {
  177. $phaseId = 2;
  178. $valueId = $arg;
  179. } elsif( $cmd eq 'phase3' ) {
  180. $phaseId = 3;
  181. $valueId = $arg;
  182. } elsif( $cmd eq 'phase4' ) {
  183. $phaseId = 4;
  184. $valueId = $arg;
  185. } elsif( $cmd eq 'all' ) {
  186. $phaseId = 'all';
  187. $valueId = $arg;
  188. } else {
  189. my $list = '';
  190. foreach(@phaseId) {
  191. $list .= $_ . ':' . join(',',@valueId) . ' ';
  192. }
  193. $list .= 'phase4:current';
  194. return "Unknown argument $cmd, choose one of $list";
  195. }
  196. if( not IsDisabled($name) ) {
  197. SmartPi_GetData($hash,$phaseId,$valueId);
  198. } else {
  199. readingsSingleUpdate($hash,'state','disabled',1);
  200. }
  201. return undef;
  202. }
  203. sub SmartPi_Timer_GetData($) {
  204. my $hash = shift;
  205. my $name = $hash->{NAME};
  206. RemoveInternalTimer($hash);
  207. if( not IsDisabled($name) ) {
  208. SmartPi_GetData($hash,'all','all');
  209. } else {
  210. readingsSingleUpdate($hash,'state','disabled',1);
  211. }
  212. InternalTimer( gettimeofday()+$hash->{INTERVAL}, 'SmartPi_Timer_GetData', $hash, 1 );
  213. Log3 $name, 4, "SmartPi ($name) - Call InternalTimer SmartPi_Timer_GetData";
  214. }
  215. sub SmartPi_GetData($@) {
  216. my ($hash,$phaseId,$valueId) = @_;
  217. my $name = $hash->{NAME};
  218. my $host = $hash->{HOST};
  219. my $port = $hash->{PORT};
  220. my $uri = $host . ':' . $port . '/api/' . $phaseId . '/' . $valueId . '/now';
  221. readingsSingleUpdate($hash,'state','fetch data',1);
  222. HttpUtils_NonblockingGet(
  223. {
  224. url => "http://" . $uri,
  225. timeout => 5,
  226. method => 'GET',
  227. hash => $hash,
  228. doTrigger => 1,
  229. callback => \&SmartPi_ErrorHandling,
  230. }
  231. );
  232. Log3 $name, 5, "SmartPi ($name) - Send with URI: $uri";
  233. }
  234. sub SmartPi_ErrorHandling($$$) {
  235. my ($param,$err,$data) = @_;
  236. my $hash = $param->{hash};
  237. my $name = $hash->{NAME};
  238. ### Begin Error Handling
  239. if( defined( $err ) ) {
  240. if( $err ne "" ) {
  241. readingsBeginUpdate( $hash );
  242. readingsBulkUpdateIfChanged ( $hash, 'state', $err, 1);
  243. readingsBulkUpdateIfChanged( $hash, 'lastRequestError', $err, 1 );
  244. readingsEndUpdate( $hash, 1 );
  245. Log3 $name, 3, "SmartPi ($name) - RequestERROR: $err";
  246. return;
  247. }
  248. }
  249. if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) {
  250. readingsBeginUpdate( $hash );
  251. readingsBulkUpdateIfChanged ( $hash, 'state', $param->{code}, 1 );
  252. readingsBulkUpdateIfChanged( $hash, 'lastRequestError', $param->{code}, 1 );
  253. Log3 $name, 3, "SmartPi ($name) - RequestERROR: ".$param->{code};
  254. readingsEndUpdate( $hash, 1 );
  255. Log3 $name, 5, "SmartPi ($name) - RequestERROR: received http code ".$param->{code}." without any data after requesting";
  256. return;
  257. }
  258. if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
  259. readingsBeginUpdate( $hash );
  260. readingsBulkUpdateIfChanged( $hash, 'state', $param->{code}, 1 );
  261. readingsBulkUpdateIfChanged( $hash, "lastRequestError", $param->{code}, 1 );
  262. readingsEndUpdate( $hash, 1 );
  263. Log3 $name, 3, "SmartPi ($name) - statusRequestERROR: http error ".$param->{code};
  264. return;
  265. ### End Error Handling
  266. }
  267. Log3 $name, 4, "SmartPi ($name) - Recieve JSON data: $data";
  268. SmartPi_ResponseProcessing($hash,$data);
  269. }
  270. sub SmartPi_ResponseProcessing($$) {
  271. my ($hash,$json) = @_;
  272. my $name = $hash->{NAME};
  273. my $decode_json;
  274. #$json = '{"serial":"smartpi160812345","name":"House","lat":52.3667,"lng":9.7167,"time":"2017-05-30 19:52:11","softwareversion":"","ipaddress":"169.254.3.10","datasets":[{"time":"2017-05-30 19:52:08","phases":[{"phase":1,"name":"phase 1","values":[{"type":"current","unity":"A","info":"","data":0.24830514},{"type":"voltage","unity":"V","info":"","data":230},{"type":"power","unity":"W","info":"","data":57.110184},{"type":"cosphi","unity":"","info":"","data":0.70275474},{"type":"frequency","unity":"Hz","info":"","data":120.413925}]},{"phase":2,"name":"phase 2","values":[{"type":"current","unity":"A","info":"","data":0.86874366},{"type":"voltage","unity":"V","info":"","data":230},{"type":"power","unity":"W","info":"","data":199.81104},{"type":"cosphi","unity":"","info":"","data":0.99155134},{"type":"frequency","unity":"Hz","info":"","data":386.1237}]},{"phase":3,"name":"phase 3","values":[{"type":"current","unity":"A","info":"","data":1.3195294},{"type":"voltage","unity":"V","info":"","data":230},{"type":"power","unity":"W","info":"","data":303.49176},{"type":"cosphi","unity":"","info":"","data":-0.25960922},{"type":"frequency","unity":"Hz","info":"","data":153.38525}]},{"phase":4,"name":"phase 4","values":[{"type":"current","unity":"A","info":"","data":1.0668689}]}]}]}';
  275. $decode_json = eval{decode_json($json)};
  276. if($@){
  277. Log3 $name, 4, "SmartPi ($name) - error while request: $@";
  278. readingsSingleUpdate($hash, "state", "error", 1);
  279. return;
  280. }
  281. SmartPi_WriteReadings($hash,$decode_json);
  282. }
  283. sub SmartPi_WriteReadings($$) {
  284. my ($hash,$decode_json) = @_;
  285. my $name = $hash->{NAME};
  286. Log3 $name, 4, "SmartPi ($name) - Write Readings";
  287. readingsBeginUpdate($hash);
  288. readingsBulkUpdateIfChanged($hash,'serialNumber',$decode_json->{serial},1);
  289. readingsBulkUpdateIfChanged($hash,'smartPiName',$decode_json->{name},1);
  290. readingsBulkUpdateIfChanged($hash,'latitude',$decode_json->{lat},1);
  291. readingsBulkUpdateIfChanged($hash,'longitude',$decode_json->{lng},1);
  292. readingsBulkUpdateIfChanged($hash,'lastfetchTime',$decode_json->{time},1);
  293. readingsBulkUpdateIfChanged($hash,'serialNumber',$decode_json->{softwareversion},1);
  294. if( ref($decode_json->{datasets}) eq "ARRAY" and scalar(@{$decode_json->{datasets}}) > 0 ) {
  295. my $dataset;
  296. my $phase;
  297. my $value;
  298. my $decimal = AttrVal($name,'decimalPlace',2);
  299. foreach $dataset (@{$decode_json->{datasets}}) {
  300. readingsBulkUpdateIfChanged($hash,'datasetsTime',$dataset->{time},1);
  301. if( ref($dataset->{phases}) eq "ARRAY" and scalar(@{$dataset->{phases}}) > 0 ) {
  302. foreach $phase (@{$dataset->{phases}}) {
  303. if( ref($phase->{values}) eq "ARRAY" and scalar(@{$phase->{values}}) > 0 ) {
  304. foreach $value (@{$phase->{values}}) {
  305. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Current", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'current' );
  306. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Voltage", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'voltage' );
  307. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Power", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'power' );
  308. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Cosphi", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'cosphi' );
  309. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Frequency", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'frequency' );
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. readingsBulkUpdateIfChanged($hash,'state','done',1);
  317. readingsEndUpdate($hash,1);
  318. }
  319. 1;
  320. =pod
  321. =item device
  322. =item summary Support read data from Smart Pi expansion module
  323. =item summary_DE Liest die Daten vom Smart Pi Aufsteckmodul aus
  324. =begin html
  325. <a name="SmartPi"></a>
  326. <h3>SmartPi</h3>
  327. <ul>
  328. <a name="SmartPireadings"></a>
  329. <b>Readings</b>
  330. <ul>
  331. <li>phaseX_Current - Current [A] (available for phase 1,2,3, neutral conductor)</li>
  332. <li>phaseX_Voltage - Voltage [V] (available for phase 1,2,3)</li>
  333. <li>phaseX_Power - Power [W] (available for phase 1,2,3)</li>
  334. <li>phaseX_Cosphi - cos φ (available for phase 1,2,3 – it is important to measure the voltage)</li>
  335. <li>phaseX_Frequency - Frequency [Hz] (available for phase 1,2,3)</li>
  336. </ul>
  337. <a name="SmartPiget"></a>
  338. <b>get</b>
  339. <ul>
  340. <li>phaseX Y - get new Y (Voltage or Current or so)data about phaseX</li>
  341. </ul>
  342. <a name="SmartPiattribut"></a>
  343. <b>get</b>
  344. <ul>
  345. <li>disable - disables the device</li>
  346. <li>disabledForIntervals - disable device for interval time (13:00-18:30 or 13:00-18:30 22:00-23:00)</li>
  347. <li>interval - interval in seconds for statusRequest</li>
  348. <li></li>
  349. </ul>
  350. </ul>
  351. =end html
  352. =begin html_DE
  353. <a name="SmartPi"></a>
  354. <h3>SmartPi</h3>
  355. =end html_DE
  356. =cut