30_TradfriGateway.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # @author Peter Kappelt
  2. # @version 1.16.dev-cf.9
  3. package main;
  4. use strict;
  5. use warnings;
  6. require 'DevIo.pm';
  7. my %TradfriGateway_sets = (
  8. #'reopen' => ' ',
  9. );
  10. my %TradfriGateway_gets = (
  11. 'deviceList' => ' ',
  12. 'groupList' => ' ',
  13. );
  14. sub TradfriGateway_Initialize($) {
  15. my ($hash) = @_;
  16. $hash->{DefFn} = 'TradfriGateway_Define';
  17. $hash->{UndefFn} = 'TradfriGateway_Undef';
  18. $hash->{SetFn} = 'TradfriGateway_Set';
  19. $hash->{GetFn} = 'TradfriGateway_Get';
  20. $hash->{AttrFn} = 'TradfriGateway_Attr';
  21. $hash->{ReadFn} = 'TradfriGateway_Read';
  22. $hash->{WriteFn} = 'TradfriGateway_Write';
  23. $hash->{ReadyFn} = 'TradfriGateway_Ready';
  24. $hash->{Clients} = "TradfriDevice:TradfriGroup";
  25. $hash->{MatchList} = {
  26. "1:TradfriDevice" => '^subscribedDeviceUpdate::',
  27. "2:TradfriGroup" => '(^subscribedGroupUpdate::)|(^moodList::)' ,
  28. };
  29. }
  30. sub TradfriGateway_Define($$) {
  31. my ($hash, $def) = @_;
  32. my @param = split('[ \t]+', $def);
  33. if(int(@param) < 4) {
  34. return "too few parameters: define <name> TradfriGateway <gateway-ip> <gateway-secret>";
  35. }
  36. #close connection to socket, if open
  37. DevIo_CloseDev($hash);
  38. $hash->{name} = $param[0];
  39. $hash->{gatewayAddress} = $param[2];
  40. $hash->{gatewaySecret} = $param[3];
  41. # @todo make user settable
  42. $hash->{DeviceName} = "192.168.1.146:1505";
  43. if(int(@param) > 4){
  44. #there was a fifth parameter
  45. #it is the path to the coap client, add it to the environments path
  46. #Edit: it is obsolete now! Give advice to the user
  47. Log(0, "[TradfriGateway] The parameter \"coap-client-directory\" in the gateway's definition is obsolete now! Please remove it as soon as possible!")
  48. }
  49. #open the socket connection
  50. #@todo react to return code
  51. DevIo_OpenDev($hash, 0, "TradfriGateway_DeviceInit");
  52. return undef;
  53. }
  54. sub TradfriGateway_Undef($$) {
  55. my ($hash, $arg) = @_;
  56. # nothing to do
  57. return undef;
  58. }
  59. sub TradfriGateway_DeviceInit($){
  60. my $hash = shift;
  61. #subscribe to all devices and groups, update the moodlist of the group
  62. #@todo check, whether we this instance is the IODev of the device/ group
  63. foreach my $deviceID ( keys %{$modules{'TradfriDevice'}{defptr}}){
  64. TradfriGateway_Write($hash, 0, 'subscribe', $deviceID);
  65. }
  66. foreach my $groupID ( keys %{$modules{'TradfriGroup'}{defptr}}){
  67. TradfriGateway_Write($hash, 1, 'subscribe', $groupID);
  68. TradfriGateway_Write($hash, 1, 'moodlist', $groupID);
  69. }
  70. }
  71. # a write command, that is dispatch from the logical module to here via IOWrite requires at least two arguments:
  72. # - 1. Scope: Group (1) or Device (0)
  73. # - 2. Action : A command:
  74. # * list -> sets the readings groups/ devices
  75. # * moodlist (groups only) -> get all moods that are defined for this group
  76. # * subscribe -> subscribe to updated of that specific device
  77. # * set -> write a specific value to the group/ device
  78. # - 3. ID: ID of the group or device
  79. # - 4. attribute::value only for command set, attribute can be onoff, dimvalue, mood (groups only), color (devices only) or name
  80. sub TradfriGateway_Write ($@){
  81. my ( $hash, $groupOrDevice, $action, $id, $attrValue) = @_;
  82. if(!defined($groupOrDevice) && !defined($action)){
  83. Log(1, "[TradfriGateway] Not enough arguments for IOWrite!");
  84. return "Not enough arguments for IOWrite!";
  85. }
  86. my $command = '';
  87. #for cmd-buildup: decide on group/ device
  88. if($groupOrDevice){
  89. $command .= 'group::';
  90. }else{
  91. $command .= 'device::';
  92. }
  93. if($action eq 'list'){
  94. $command .= 'list';
  95. }elsif($action eq 'moodlist'){
  96. $command .= "moodlist::${id}";
  97. }elsif($action eq 'subscribe'){
  98. #silently return if connection is open.
  99. #at startup, every device/ group runs subscribe. If the connection isn't open, we do it later.
  100. return if($hash->{STATE} ne 'opened');
  101. $command .= "subscribe::${id}";
  102. }elsif($action eq 'set'){
  103. $command .= "set::${id}::${attrValue}";
  104. }else{
  105. return "Unknown command: " . $command;
  106. }
  107. #@todo better check, if opened
  108. if($hash->{STATE} ne 'opened'){
  109. Log(1, "[TradfriGateway] Can't write, connection is not opened!");
  110. return "Can't write, connection is not opened!";
  111. }
  112. DevIo_SimpleWrite($hash, $command . "\n", 2, 0);
  113. return undef;
  114. }
  115. #data was received on the socket
  116. sub TradfriGateway_Read ($){
  117. my ( $hash ) = @_;
  118. my $msg = DevIo_SimpleRead($hash);
  119. if(!defined($msg)){
  120. return undef;
  121. }
  122. my $msgReadableWhitespace = $msg;
  123. $msgReadableWhitespace =~ s/\r/\\r/g;
  124. $msgReadableWhitespace =~ s/\n/\\n/g;
  125. Log(4, "[TradfriGateway] Received message on socket: \"" . $msgReadableWhitespace . "\"");
  126. #there might be multiple messages at once, they are split by newline. Iterate through each of them
  127. my @messagesSingle = split(/\n/, $msg);
  128. foreach my $message(@messagesSingle){
  129. #if there is whitespace left, remove it.
  130. $message =~ s/\r//g;
  131. $message =~ s/\n//g;
  132. #devices and groups
  133. #@todo not as JSON array
  134. if(($message ne '') && ((split(/::/, $message))[0] =~ /(?:group|device)List/)){
  135. if((split(/::/, $message))[0] eq 'deviceList'){
  136. readingsSingleUpdate($hash, 'devices', (split(/::/, $message))[1], 1);
  137. }
  138. if((split(/::/, $message))[0] eq 'groupList'){
  139. readingsSingleUpdate($hash, 'groups', (split(/::/, $message))[1], 1);
  140. }
  141. }
  142. #dispatch the message if it isn't empty, only dispatch messages that come from an observe
  143. if(($message ne '') && ((split(/::/, $message))[0] =~ /(?:subscribed(?:Group|Device)Update)|(?:moodList)/)){
  144. Dispatch($hash, $message, undef);
  145. }
  146. }
  147. }
  148. sub TradfriGateway_Ready($){
  149. my ($hash) = @_;
  150. return DevIo_OpenDev($hash, 1, "TradfriGateway_DeviceInit") if($hash->{STATE} eq "disconnected");
  151. }
  152. sub TradfriGateway_Get($@) {
  153. my ($hash, @param) = @_;
  154. return '"get TradfriGateway" needs at least one argument' if (int(@param) < 2);
  155. my $name = shift @param;
  156. my $opt = shift @param;
  157. if(!$TradfriGateway_gets{$opt}) {
  158. my @cList = keys %TradfriGateway_gets;
  159. return "Unknown argument $opt, choose one of " . join(" ", @cList);
  160. }
  161. if($opt eq 'deviceList'){
  162. TradfriGateway_Write($hash, 0, 'list');
  163. }elsif($opt eq 'groupList'){
  164. TradfriGateway_Write($hash, 1, 'list');
  165. }
  166. return $TradfriGateway_gets{$opt};
  167. }
  168. sub TradfriGateway_Set($@) {
  169. my ($hash, @param) = @_;
  170. return '"set TradfriGateway" needs at least one argument' if (int(@param) < 2);
  171. my $name = shift @param;
  172. my $opt = shift @param;
  173. my $value = join("", @param);
  174. if(!defined($TradfriGateway_sets{$opt})) {
  175. my @cList = keys %TradfriGateway_sets;
  176. return "Unknown argument $opt, choose one of " . join(" ", @cList);
  177. }
  178. if($opt eq "reopen"){
  179. #close connection to socket, if open
  180. DevIo_CloseDev($hash);
  181. #@todo react to return code
  182. my $ret = DevIo_OpenDev($hash, 0, "TradfriGateway_DeviceInit");
  183. }
  184. return undef;
  185. }
  186. sub TradfriGateway_Attr(@) {
  187. my ($cmd,$name,$attr_name,$attr_value) = @_;
  188. if($cmd eq "set") {
  189. #if($attr_name eq "formal") {
  190. # if($attr_value !~ /^yes|no$/) {
  191. # my $err = "Invalid argument $attr_value to $attr_name. Must be yes or no.";
  192. # Log 3, "TradfriGateway: ".$err;
  193. # return $err;
  194. # }
  195. #} else {
  196. # return "Unknown attr $attr_name";
  197. #}
  198. }
  199. return undef;
  200. }
  201. 1;
  202. =pod
  203. =item device
  204. =item summary connects with an IKEA Trådfri gateway
  205. =item summary_DE stellt die Verbindung mit einem IKEA Trådfri Gateway her
  206. =begin html
  207. <a name="TradfriGateway"></a>
  208. <h3>TradfriGateway</h3>
  209. <ul>
  210. <i>TradfriGateway</i> stores the connection data for an IKEA Trådfri gateway. It is necessary for TradfriDevice and TradfriGroup
  211. <br><br>
  212. <a name="TradfriGatewaydefine"></a>
  213. <b>Define</b>
  214. <ul>
  215. <code>define &lt;name&gt; TradfriGateway &lt;gateway-ip&gt; &lt;gateway-secret&gt;</code>
  216. <br><br>
  217. Example: <code>define trGateway TradfriGateway TradfriGW.int.kappelt.net vBkxxxxxxxxxx7hz</code>
  218. <br><br>
  219. The IP can either be a "normal" IP-Address, like 192.168.2.60, or a DNS name (like shown above).<br>
  220. You can find the secret on a label on the bottom side of the gateway.
  221. The parameter "coap-client-path" is isn't used anymore and thus not shown here anymore. Please remove it as soon as possible, if you are still using it.<br>
  222. You need to run kCoAPSocket running in background, that acts like a translator between FHEM and the Trådfri Gateway.
  223. </ul>
  224. <br>
  225. <a name="TradfriGatewayset"></a>
  226. <b>Set</b><br>
  227. <ul>
  228. <code>set &lt;name&gt; &lt;option&gt; [&lt;value&gt;]</code>
  229. <br><br>
  230. You can set the following options. See <a href="http://fhem.de/commandref.html#set">commandref#set</a>
  231. for more info about the set command.
  232. <br><br>
  233. Options:
  234. <ul>
  235. <li><i>reopen</i><br>
  236. Re-open the connection to the Java TCP socket, that acts like a "translator" between FHEM and the Tradfri-CoAP-Infrastructure.<br>
  237. If the connection is already opened, it'll be closed and opened.<br>
  238. If the connection isn't open yet, a try to open it will be executed.<br>
  239. <b>Caution: </b>Running this command seems to trigger some issues! Do <i>not</i> run it before a update is available!</li>
  240. </ul>
  241. </ul>
  242. <br>
  243. <a name="TradfriGatewayget"></a>
  244. <b>Get</b><br>
  245. <ul>
  246. <code>get &lt;name&gt; &lt;option&gt;</code>
  247. <br><br>
  248. You can get the following information about the device. See
  249. <a href="http://fhem.de/commandref.html#get">commandref#get</a> for more info about
  250. the get command.
  251. <br><br>
  252. Options:
  253. <ul>
  254. <li><i>deviceList</i><br>
  255. Sets the reading "devices" to a JSON-formatted string of all device IDs and their names.</li>
  256. <li><i>groupList</i><br>
  257. Sets the reading "devices" to a JSON-formatted string of all group IDs and their names.</li>
  258. </ul>
  259. </ul>
  260. <br>
  261. <a name="TradfriGatewayattr"></a>
  262. <b>Attributes</b>
  263. <ul>
  264. <code>attr &lt;name&gt; &lt;attribute&gt; &lt;value&gt;</code>
  265. <br><br>
  266. See <a href="http://fhem.de/commandref.html#attr">commandref#attr</a> for more info about
  267. the attr command.
  268. <br><br>
  269. Attributes:
  270. <ul>
  271. <li><i></i><br>
  272. There are no custom attributes implemented
  273. </li>
  274. </ul>
  275. </ul>
  276. </ul>
  277. =end html
  278. =cut