01_HMDEV.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ################################################
  2. # HMRPC Device Handler
  3. # Written by Oliver Wagner <owagner@vapor.com>
  4. #
  5. # V0.5
  6. #
  7. ################################################
  8. #
  9. # This module handles individual devices via the
  10. # HMRPC provider.
  11. #
  12. package main;
  13. use strict;
  14. use warnings;
  15. sub
  16. HMDEV_Initialize($)
  17. {
  18. my ($hash) = @_;
  19. $hash->{Match} = "^HMDEV .* .* .*";
  20. $hash->{DefFn} = "HMDEV_Define";
  21. $hash->{ParseFn} = "HMDEV_Parse";
  22. $hash->{SetFn} = "HMDEV_Set";
  23. $hash->{GetFn} = "HMDEV_Get";
  24. $hash->{AttrList} = "IODev do_not_notify:0,1";
  25. }
  26. #############################
  27. sub
  28. HMDEV_Define($$)
  29. {
  30. my ($hash, $def) = @_;
  31. my @a = split("[ \t][ \t]*", $def);
  32. my $name = $hash->{NAME};
  33. return "wrong syntax: define <name> HMDEV deviceaddress" if int(@a)!=3;
  34. my $addr=$a[2];
  35. $hash->{hmaddr}=$addr;
  36. $modules{HMDEV}{defptr}{$addr} = $hash;
  37. AssignIoPort($hash);
  38. if($hash->{IODev}->{NAME})
  39. {
  40. Log 5,"Assigned $name to $hash->{IODev}->{NAME}";
  41. }
  42. return undef;
  43. }
  44. #############################
  45. sub
  46. HMDEV_Parse($$)
  47. {
  48. my ($hash, $msg) = @_;
  49. my @mp=split(" ",$msg);
  50. my $addr=$mp[1];
  51. my $attrid=$mp[2];
  52. $hash=$modules{HMDEV}{defptr}{$addr};
  53. if(!$hash)
  54. {
  55. # If not explicitely defined, reroute this event to the main device
  56. # with a suffixed attribute name
  57. $addr=~s/:([0-9]{1,2})//;
  58. my $subdev=$1;
  59. if($subdev>0)
  60. {
  61. $attrid.="_$subdev";
  62. }
  63. $hash=$modules{HMDEV}{defptr}{$addr};
  64. }
  65. if(!$hash)
  66. {
  67. Log(2,"Received callback for unknown device $msg");
  68. return "UNDEFINED HMDEV_$addr HMDEV $addr";
  69. }
  70. # Let's see whether we can update our devinfo now
  71. if(!defined $hash->{devinfo})
  72. {
  73. $hash->{hmdevinfo}=$hash->{IODev}{devicespecs}{$addr};
  74. $hash->{hmdevtype}=$hash->{hmdevinfo}{TYPE};
  75. }
  76. #
  77. # Ok update the relevant reading
  78. #
  79. my @changed;
  80. my $currentval=$hash->{READINGS}{$attrid}{VAL};
  81. $hash->{READINGS}{$attrid}{TIME}=TimeNow();
  82. # Note that we always trigger a change on PRESS_LONG/PRESS_SHORT events
  83. # (they are sent whenever a button is pressed, and there is no change back)
  84. # We also never trigger a change on the RSSI readings, for efficiency purposes
  85. if(!defined $currentval || ($currentval ne $mp[3]) || ($attrid =~ /^PRESS_/))
  86. {
  87. if(defined $currentval && !($currentval =~ m/^RSSI_/))
  88. {
  89. push @changed, "$attrid: $mp[3]";
  90. }
  91. $hash->{READINGS}{$attrid}{VAL}=$mp[3];
  92. # Also update the STATE
  93. my $state="";
  94. foreach my $key (sort(keys(%{$hash->{READINGS}})))
  95. {
  96. if(length($state))
  97. {
  98. $state.=" ";
  99. }
  100. $state.=$key.": ".$hash->{READINGS}{$key}{VAL};
  101. }
  102. $hash->{STATE}=$state;
  103. }
  104. $hash->{CHANGED}=\@changed;
  105. return $hash->{NAME};
  106. }
  107. ################################
  108. sub
  109. HMDEV_Set($@)
  110. {
  111. my ($hash, @a) = @_;
  112. return "invalid set call @a" if(@a != 3 && @a != 4);
  113. # We delegate this call to the HMRPC IODev, after having added the device address
  114. if(@a==4)
  115. {
  116. return HMRPC_Set($hash->{IODev},$hash->{IODev}->{NAME},$hash->{hmaddr},$a[1],$a[2],$a[3]);
  117. }
  118. else
  119. {
  120. return HMRPC_Set($hash->{IODev},$hash->{IODev}->{NAME},$hash->{hmaddr},$a[1],$a[2]);
  121. }
  122. }
  123. ################################
  124. sub
  125. HMDEV_Get($@)
  126. {
  127. my ($hash, @a) = @_;
  128. return "argument missing, usage is <attribute> @a" if(@a!=2);
  129. # Like set, we simply delegate to the HMPRC IODev here
  130. return HMRPC_Get($hash->{IODev},$hash->{IODev}->{NAME},$hash->{hmaddr},$a[1]);
  131. }
  132. 1;