46_SmartPi.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 17119 2018-08-10 14:56:05Z 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.4.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. }
  133. elsif( $cmd eq "del" ) {
  134. Log3 $name, 3, "SmartPi ($name) - enabled";
  135. readingsSingleUpdate ( $hash, "state", "active", 1 );
  136. }
  137. } elsif( $attrName eq "interval" ) {
  138. if( $cmd eq "set" ) {
  139. $hash->{INTERVAL} = $attrVal;
  140. Log3 $name, 3, "SmartPi ($name) - set interval: $attrVal";
  141. } else {
  142. $hash->{INTERVAL} = 300;
  143. Log3 $name, 3, "SmartPi ($name) - delete User interval and set default: 300";
  144. }
  145. }
  146. return undef;
  147. }
  148. sub SmartPi_Notify($$) {
  149. my ($hash,$dev) = @_;
  150. my $name = $hash->{NAME};
  151. return if (IsDisabled($name));
  152. my $devname = $dev->{NAME};
  153. my $devtype = $dev->{TYPE};
  154. my $events = deviceEvents($dev,1);
  155. return if (!$events);
  156. SmartPi_Timer_GetData($hash) if( (grep /^DEFINED.$name$/,@{$events}
  157. or grep /^INITIALIZED$/,@{$events}
  158. or grep /^MODIFIED.$name$/,@{$events}
  159. or grep /^DELETEATTR.$name.interval$/,@{$events}
  160. or grep /^ATTR.$name.interval.[0-9]+/,@{$events}
  161. or grep /^DELETEATTR.$name.disable$/,@{$events}
  162. or grep /^ATTR.$name.disable.0$/,@{$events}
  163. or grep /^DELETEATTR.$name.decimalPlace$/,@{$events}
  164. or grep /^ATTR.$name.decimalPlace.[0-9]+/,@{$events}) and $init_done );
  165. return;
  166. }
  167. sub SmartPi_Get($@) {
  168. my ($hash, $name, $cmd, @args) = @_;
  169. my ($arg, @params) = @args;
  170. my @phaseId = ('phase1','phase2','phase3','all');
  171. my @valueId = ('all','current','voltage','power','cosphi','frequency');
  172. my $phaseId;
  173. my $valueId;
  174. if( $cmd eq 'phase1' ) {
  175. $phaseId = 1;
  176. $valueId = $arg;
  177. } elsif( $cmd eq 'phase2' ) {
  178. $phaseId = 2;
  179. $valueId = $arg;
  180. } elsif( $cmd eq 'phase3' ) {
  181. $phaseId = 3;
  182. $valueId = $arg;
  183. } elsif( $cmd eq 'phase4' ) {
  184. $phaseId = 4;
  185. $valueId = $arg;
  186. } elsif( $cmd eq 'all' ) {
  187. $phaseId = 'all';
  188. $valueId = $arg;
  189. } else {
  190. my $list = '';
  191. foreach(@phaseId) {
  192. $list .= $_ . ':' . join(',',@valueId) . ' ';
  193. }
  194. $list .= 'phase4:current';
  195. return "Unknown argument $cmd, choose one of $list";
  196. }
  197. if( not IsDisabled($name) ) {
  198. SmartPi_GetData($hash,$phaseId,$valueId);
  199. } else {
  200. readingsSingleUpdate($hash,'state','disabled',1);
  201. }
  202. return undef;
  203. }
  204. sub SmartPi_Timer_GetData($) {
  205. my $hash = shift;
  206. my $name = $hash->{NAME};
  207. RemoveInternalTimer($hash);
  208. if( not IsDisabled($name) ) {
  209. SmartPi_GetData($hash,'all','all');
  210. } else {
  211. readingsSingleUpdate($hash,'state','disabled',1);
  212. }
  213. InternalTimer( gettimeofday()+$hash->{INTERVAL}, 'SmartPi_Timer_GetData', $hash, 1 );
  214. Log3 $name, 4, "SmartPi ($name) - Call InternalTimer SmartPi_Timer_GetData";
  215. }
  216. sub SmartPi_GetData($@) {
  217. my ($hash,$phaseId,$valueId) = @_;
  218. my $name = $hash->{NAME};
  219. my $host = $hash->{HOST};
  220. my $port = $hash->{PORT};
  221. my $uri = $host . ':' . $port . '/api/' . $phaseId . '/' . $valueId . '/now';
  222. readingsSingleUpdate($hash,'state','fetch data',1);
  223. HttpUtils_NonblockingGet(
  224. {
  225. url => "http://" . $uri,
  226. timeout => 5,
  227. method => 'GET',
  228. hash => $hash,
  229. doTrigger => 1,
  230. callback => \&SmartPi_ErrorHandling,
  231. }
  232. );
  233. Log3 $name, 5, "SmartPi ($name) - Send with URI: $uri";
  234. }
  235. sub SmartPi_ErrorHandling($$$) {
  236. my ($param,$err,$data) = @_;
  237. my $hash = $param->{hash};
  238. my $name = $hash->{NAME};
  239. ### Begin Error Handling
  240. if( defined( $err ) ) {
  241. if( $err ne "" ) {
  242. readingsBeginUpdate( $hash );
  243. readingsBulkUpdateIfChanged ( $hash, 'state', $err, 1);
  244. readingsBulkUpdateIfChanged( $hash, 'lastRequestError', $err, 1 );
  245. readingsEndUpdate( $hash, 1 );
  246. Log3 $name, 3, "SmartPi ($name) - RequestERROR: $err";
  247. return;
  248. }
  249. }
  250. if( $data eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) {
  251. readingsBeginUpdate( $hash );
  252. readingsBulkUpdateIfChanged ( $hash, 'state', $param->{code}, 1 );
  253. readingsBulkUpdateIfChanged( $hash, 'lastRequestError', $param->{code}, 1 );
  254. Log3 $name, 3, "SmartPi ($name) - RequestERROR: ".$param->{code};
  255. readingsEndUpdate( $hash, 1 );
  256. Log3 $name, 5, "SmartPi ($name) - RequestERROR: received http code ".$param->{code}." without any data after requesting";
  257. return;
  258. }
  259. if( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
  260. readingsBeginUpdate( $hash );
  261. readingsBulkUpdateIfChanged( $hash, 'state', $param->{code}, 1 );
  262. readingsBulkUpdateIfChanged( $hash, "lastRequestError", $param->{code}, 1 );
  263. readingsEndUpdate( $hash, 1 );
  264. Log3 $name, 3, "SmartPi ($name) - statusRequestERROR: http error ".$param->{code};
  265. return;
  266. ### End Error Handling
  267. }
  268. Log3 $name, 4, "SmartPi ($name) - Recieve JSON data: $data";
  269. SmartPi_ResponseProcessing($hash,$data);
  270. }
  271. sub SmartPi_ResponseProcessing($$) {
  272. my ($hash,$json) = @_;
  273. my $name = $hash->{NAME};
  274. my $decode_json;
  275. #$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}]}]}]}';
  276. $decode_json = eval{decode_json($json)};
  277. if($@){
  278. Log3 $name, 4, "SmartPi ($name) - error while request: $@";
  279. readingsSingleUpdate($hash, "state", "error", 1);
  280. return;
  281. }
  282. SmartPi_WriteReadings($hash,$decode_json);
  283. }
  284. sub SmartPi_WriteReadings($$) {
  285. my ($hash,$decode_json) = @_;
  286. my $name = $hash->{NAME};
  287. Log3 $name, 4, "SmartPi ($name) - Write Readings";
  288. readingsBeginUpdate($hash);
  289. readingsBulkUpdateIfChanged($hash,'serialNumber',$decode_json->{serial},1);
  290. readingsBulkUpdateIfChanged($hash,'smartPiName',$decode_json->{name},1);
  291. readingsBulkUpdateIfChanged($hash,'latitude',$decode_json->{lat},1);
  292. readingsBulkUpdateIfChanged($hash,'longitude',$decode_json->{lng},1);
  293. readingsBulkUpdateIfChanged($hash,'lastfetchTime',$decode_json->{time},1);
  294. readingsBulkUpdateIfChanged($hash,'serialNumber',$decode_json->{softwareversion},1);
  295. if( ref($decode_json->{datasets}) eq "ARRAY" and scalar(@{$decode_json->{datasets}}) > 0 ) {
  296. my $dataset;
  297. my $phase;
  298. my $value;
  299. my $decimal = AttrVal($name,'decimalPlace',2);
  300. foreach $dataset (@{$decode_json->{datasets}}) {
  301. readingsBulkUpdateIfChanged($hash,'datasetsTime',$dataset->{time},1);
  302. if( ref($dataset->{phases}) eq "ARRAY" and scalar(@{$dataset->{phases}}) > 0 ) {
  303. foreach $phase (@{$dataset->{phases}}) {
  304. if( ref($phase->{values}) eq "ARRAY" and scalar(@{$phase->{values}}) > 0 ) {
  305. foreach $value (@{$phase->{values}}) {
  306. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Current", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'current' );
  307. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Voltage", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'voltage' );
  308. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Power", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'power' );
  309. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Cosphi", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'cosphi' );
  310. readingsBulkUpdateIfChanged( $hash, "phase$phase->{phase}_Frequency", sprintf("%.${decimal}f",$value->{data}), 1 ) if( $value->{type} eq 'frequency' );
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. readingsBulkUpdateIfChanged($hash,'state','done',1);
  318. readingsEndUpdate($hash,1);
  319. }
  320. 1;
  321. =pod
  322. =item device
  323. =item summary Support read data from Smart Pi expansion module
  324. =item summary_DE Liest die Daten vom Smart Pi Aufsteckmodul aus
  325. =begin html
  326. <a name="SmartPi"></a>
  327. <h3>SmartPi</h3>
  328. <a name="GardenaSmartBridgedefine"></a>
  329. <b>Define</b>
  330. <ul><br>
  331. <code>define &lt;name&gt; SmartPi &lt;HOST&gt;</code>
  332. <br><br>
  333. Beispiel:
  334. <ul><br>
  335. <code>define Gardena_Bridge SmartPi 192.168.1.34</code><br>
  336. </ul>
  337. <br><br>
  338. </ul>
  339. <ul>
  340. <a name="SmartPireadings"></a>
  341. <b>Readings</b>
  342. <ul>
  343. <li>phaseX_Current - Current [A] (available for phase 1,2,3, neutral conductor)</li>
  344. <li>phaseX_Voltage - Voltage [V] (available for phase 1,2,3)</li>
  345. <li>phaseX_Power - Power [W] (available for phase 1,2,3)</li>
  346. <li>phaseX_Cosphi - cos φ (available for phase 1,2,3 – it is important to measure the voltage)</li>
  347. <li>phaseX_Frequency - Frequency [Hz] (available for phase 1,2,3)</li>
  348. </ul>
  349. <a name="SmartPiget"></a>
  350. <b>get</b>
  351. <ul>
  352. <li>phaseX Y - get new Y (Voltage or Current or so)data about phaseX</li>
  353. </ul>
  354. <a name="SmartPiattribut"></a>
  355. <b>Attribute</b>
  356. <ul>
  357. <li>disable - disables the device</li>
  358. <li>disabledForIntervals - disable device for interval time (13:00-18:30 or 13:00-18:30 22:00-23:00)</li>
  359. <li>interval - interval in seconds for statusRequest</li>
  360. <li>decimalPlace - set decimal Place</li>
  361. </ul>
  362. </ul>
  363. =end html
  364. =begin html_DE
  365. <a name="SmartPi"></a>
  366. <h3>SmartPi</h3>
  367. =end html_DE
  368. =cut