46_SmartPi.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 14748 2017-07-19 07:36:01Z 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.0.0";
  55. # Declare functions
  56. sub SmartPi_Attr(@);
  57. sub SmartPi_Define($$);
  58. sub SmartPi_Initialize($);
  59. sub SmartPi_Get($@);
  60. sub SmartPi_GetData($@);
  61. sub SmartPi_Undef($$);
  62. sub SmartPi_ResponseProcessing($$);
  63. sub SmartPi_ErrorHandling($$$);
  64. sub SmartPi_WriteReadings($$);
  65. sub SmartPi_Timer_GetData($);
  66. sub SmartPi_Initialize($) {
  67. my ($hash) = @_;
  68. # Consumer
  69. $hash->{GetFn} = "SmartPi_Get";
  70. $hash->{DefFn} = "SmartPi_Define";
  71. $hash->{UndefFn} = "SmartPi_Undef";
  72. $hash->{AttrFn} = "SmartPi_Attr";
  73. $hash->{AttrList} = "interval ".
  74. "disable:1 ".
  75. $readingFnAttributes;
  76. foreach my $d(sort keys %{$modules{SmartPi}{defptr}}) {
  77. my $hash = $modules{SmartPi}{defptr}{$d};
  78. $hash->{VERSION} = $version;
  79. }
  80. }
  81. sub SmartPi_Define($$) {
  82. my ( $hash, $def ) = @_;
  83. my @a = split( "[ \t][ \t]*", $def );
  84. return "too few parameters: define <name> SmartPi <HOST>" if( @a != 3);
  85. return "Cannot define a HEOS device. Perl modul $missingModul is missing." if ( $missingModul );
  86. my $name = $a[0];
  87. my $host = $a[2];
  88. $hash->{HOST} = $host;
  89. $hash->{INTERVAL} = 300;
  90. $hash->{PORT} = 1080;
  91. $hash->{VERSION} = $version;
  92. $attr{$name}{room} = "SmartPi" if( !defined( $attr{$name}{room} ) );
  93. Log3 $name, 3, "SmartPi ($name) - defined SmartPi Device with Host $host, Port $hash->{PORT} and Interval $hash->{INTERVAL}";
  94. if( $init_done ) {
  95. SmartPi_Timer_GetData($hash);
  96. } else {
  97. InternalTimer( gettimeofday()+15, "SmartPi_Timer_GetData", $hash, 0 );
  98. }
  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. readingsSingleUpdate ( $hash, "state", "disabled", 1 );
  120. Log3 $name, 3, "SmartPi ($name) - disabled";
  121. }
  122. } else {
  123. readingsSingleUpdate ( $hash, "state", "enabled", 1 );
  124. Log3 $name, 3, "SmartPi ($name) - enabled";
  125. }
  126. } elsif( $attrName eq "interval" ) {
  127. if( $cmd eq "set" ) {
  128. $hash->{INTERVAL} = $attrVal;
  129. } else {
  130. $hash->{INTERVAL} = 300;
  131. }
  132. }
  133. return undef;
  134. }
  135. sub SmartPi_Get($@) {
  136. my ($hash, $name, $cmd, @args) = @_;
  137. my ($arg, @params) = @args;
  138. my @phaseId = ('phase1','phase2','phase3','all');
  139. my @valueId = ('all','current','voltage','power','cosphi','frequency');
  140. my $phaseId;
  141. my $valueId;
  142. if( $cmd eq 'phase1' ) {
  143. $phaseId = 1;
  144. $valueId = $arg;
  145. } elsif( $cmd eq 'phase2' ) {
  146. $phaseId = 2;
  147. $valueId = $arg;
  148. } elsif( $cmd eq 'phase3' ) {
  149. $phaseId = 3;
  150. $valueId = $arg;
  151. } elsif( $cmd eq 'phase4' ) {
  152. $phaseId = 4;
  153. $valueId = $arg;
  154. } elsif( $cmd eq 'all' ) {
  155. $phaseId = 'all';
  156. $valueId = $arg;
  157. } else {
  158. my $list = '';
  159. foreach(@phaseId) {
  160. $list .= $_ . ':' . join(',',@valueId) . ' ';
  161. }
  162. $list .= 'phase4:current';
  163. return "Unknown argument $cmd, choose one of $list";
  164. }
  165. SmartPi_GetData($hash,$phaseId,$valueId);
  166. return undef;
  167. }
  168. sub SmartPi_Timer_GetData($) {
  169. my $hash = shift;
  170. my $name = $hash->{NAME};
  171. if( not IsDisabled($name) ) {
  172. SmartPi_GetData($hash,'all','all');
  173. } else {
  174. readingsSingleUpdate($hash,'state','disabled',1);
  175. }
  176. InternalTimer( gettimeofday()+$hash->{INTERVAL}, 'SmartPi_Timer_GetData', $hash, 1 );
  177. Log3 $name, 4, "SmartPi ($name) - Call InternalTimer SmartPi_Timer_GetData";
  178. }
  179. sub SmartPi_GetData($@) {
  180. my ($hash,$phaseId,$valueId) = @_;
  181. my $name = $hash->{NAME};
  182. my $host = $hash->{HOST};
  183. my $port = $hash->{PORT};
  184. my $uri = $host . ':' . $port . '/api/' . $phaseId . '/' . $valueId . '/now';
  185. readingsSingleUpdate($hash,'state','fetch data',1);
  186. HttpUtils_NonblockingGet(
  187. {
  188. url => "http://" . $uri,
  189. timeout => 5,
  190. method => 'GET',
  191. hash => $hash,
  192. doTrigger => 1,
  193. callback => \&SmartPi_ErrorHandling,
  194. }
  195. );
  196. Log3 $name, 5, "SmartPi ($name) - Send with URI: $uri";
  197. }
  198. sub SmartPi_ErrorHandling($$$) {
  199. my ($param,$err,$data) = @_;
  200. my $hash = $param->{hash};
  201. my $name = $hash->{NAME};
  202. ### Begin Error Handling
  203. if( defined( $err ) ) {
  204. if( $err ne "" ) {
  205. readingsBeginUpdate( $hash );
  206. readingsBulkUpdateIfChanged ( $hash, 'state', $err, 1);
  207. readingsBulkUpdateIfChanged( $hash, 'lastRequestError', $err, 1 );
  208. readingsEndUpdate( $hash, 1 );
  209. Log3 $name, 3, "SmartPi ($name) - RequestERROR: $err";
  210. return;
  211. }
  212. }
  213. if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) {
  214. readingsBeginUpdate( $hash );
  215. readingsBulkUpdateIfChanged ( $hash, 'state', $param->{code}, 1 );
  216. readingsBulkUpdateIfChanged( $hash, 'lastRequestError', $param->{code}, 1 );
  217. Log3 $name, 3, "SmartPi ($name) - RequestERROR: ".$param->{code};
  218. readingsEndUpdate( $hash, 1 );
  219. Log3 $name, 5, "SmartPi ($name) - RequestERROR: received http code ".$param->{code}." without any data after requesting";
  220. return;
  221. }
  222. if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
  223. readingsBeginUpdate( $hash );
  224. readingsBulkUpdateIfChanged( $hash, 'state', $param->{code}, 1 );
  225. readingsBulkUpdateIfChanged( $hash, "lastRequestError", $param->{code}, 1 );
  226. readingsEndUpdate( $hash, 1 );
  227. Log3 $name, 3, "SmartPi ($name) - statusRequestERROR: http error ".$param->{code};
  228. return;
  229. ### End Error Handling
  230. }
  231. Log3 $name, 4, "SmartPi ($name) - Recieve JSON data: $data";
  232. SmartPi_ResponseProcessing($hash,$data);
  233. }
  234. sub SmartPi_ResponseProcessing($$) {
  235. my ($hash,$json) = @_;
  236. my $name = $hash->{NAME};
  237. my $decode_json;
  238. $decode_json = eval{decode_json($json)};
  239. if($@){
  240. Log3 $name, 4, "SmartPi ($name) - error while request: $@";
  241. readingsSingleUpdate($hash, "state", "error", 1);
  242. return;
  243. }
  244. SmartPi_WriteReadings($hash,$decode_json);
  245. }
  246. sub SmartPi_WriteReadings($$) {
  247. my ($hash,$decode_json) = @_;
  248. my $name = $hash->{NAME};
  249. Log3 $name, 4, "SmartPi ($name) - Write Readings";
  250. #{"serial":"smartpi160812345","name":"House","lat":52.3667,"lng":9.7167,"time":"2017-05-30 19:52:11","softwareversion":"","ipaddress":"169.254.3.10",
  251. #"datasets":[{"time":"2017-05-30 19:52:08","phases":[
  252. #{"phase":1,"name":"phase 1","values":[
  253. # {"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}]},
  254. #{"phase":2,"name":"phase 2","values":[
  255. #{"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}]},
  256. #{"phase":3,"name":"phase 3","values":[
  257. #{"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}]},
  258. #{"phase":4,"name":"phase 4","values":[
  259. #{"type":"current","unity":"A","info":"","data":1.0668689}]}]}]}
  260. readingsBeginUpdate($hash);
  261. readingsBulkUpdateIfChanged($hash,'serialNumber',$decode_json->{serial},1);
  262. readingsBulkUpdateIfChanged($hash,'smartPiName',$decode_json->{name},1);
  263. readingsBulkUpdateIfChanged($hash,'latitude',$decode_json->{lat},1);
  264. readingsBulkUpdateIfChanged($hash,'longitude',$decode_json->{lng},1);
  265. readingsBulkUpdateIfChanged($hash,'lastfetchTime',$decode_json->{time},1);
  266. readingsBulkUpdateIfChanged($hash,'serialNumber',$decode_json->{softwareversion},1);
  267. if( ref($decode_json->{datasets}) eq "ARRAY" and scalar(@{$decode_json->{datasets}}) > 0 ) {
  268. my $dataset;
  269. my $phase;
  270. my $value;
  271. foreach $dataset (@{$decode_json->{datasets}}) {
  272. readingsBulkUpdateIfChanged($hash,'datasetsTime',$dataset->{time},1);
  273. if( ref($dataset->{phases}) eq "ARRAY" and scalar(@{$dataset->{phases}}) > 0 ) {
  274. foreach $phase (@{$dataset->{phases}}) {
  275. if( ref($phase->{values}) eq "ARRAY" and scalar(@{$phase->{values}}) > 0 ) {
  276. foreach $value (@{$phase->{values}}) {
  277. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Current", $value->{data}, 1 ) if( $value->{type} eq 'current' );
  278. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Voltage", $value->{data}, 1 ) if( $value->{type} eq 'voltage' );
  279. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Power", $value->{data}, 1 ) if( $value->{type} eq 'power' );
  280. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Cosphi", $value->{data}, 1 ) if( $value->{type} eq 'cosphi' );
  281. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Frequency", $value->{data}, 1 ) if( $value->{type} eq 'frequency' );
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. readingsBulkUpdateIfChanged($hash,'state','done',1);
  289. readingsEndUpdate($hash,1);
  290. }
  291. 1;
  292. =pod
  293. =item device
  294. =item summary Support read data from Smart Pi expansion module
  295. =item summary_DE Liest die Daten vom Smart Pi Aufsteckmodul aus
  296. =begin html
  297. <a name="SmartPi"></a>
  298. <h3>SmartPi</h3>
  299. <ul>
  300. <a name="SmartPireadings"></a>
  301. <b>Readings</b>
  302. <ul>
  303. <li>phaseX_Current - Current [A] (available for phase 1,2,3, neutral conductor)</li>
  304. <li>phaseX_Voltage - Voltage [V] (available for phase 1,2,3)</li>
  305. <li>phaseX_Power - Power [W] (available for phase 1,2,3)</li>
  306. <li>phaseX_Cosphi - cos φ (available for phase 1,2,3 – it is important to measure the voltage)</li>
  307. <li>phaseX_Frequency - Frequency [Hz] (available for phase 1,2,3)</li>
  308. </ul>
  309. <a name="SmartPiget"></a>
  310. <b>get</b>
  311. <ul>
  312. <li>phaseX Y - get new Y (Voltage or Current or so)data about phaseX</li>
  313. </ul>
  314. </ul>
  315. =end html
  316. =begin html_DE
  317. <a name="SmartPi"></a>
  318. <h3>SmartPi</h3>
  319. =end html_DE
  320. =cut