Quantcast

Perl Coding Help

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Perl Coding Help

Craig Lindholm
I want to trigger (test) for any state change of a list of items and then do a command for each of the items that changed state.

For example, I have 10 lamp items, and two of them change state on a particular pass. I then want to post a log, for example, for only those two. I could have 10 of these if statements, one for each item, but its ugly:

if(state_changed $Living_Room_Lamps)
{
if(state_now $Living_Room_Lamps eq 'on'){
print_log(" Turning Living Room Lamps ON");}
if(state_now $Living_Room_Lamps eq 'off'){
print_log(" Turning Living Room Lamps OFF");}
}
if(state_changed $Kitchen_Lights)
{
if(state_now $Kitchen_Lights eq 'on'){
print_log(" Turning Kitchen Lights ON");}
if(state_now $Kitchen_Lights eq 'off'){
print_log(" Turning Kitchen Lights OFF");}
}

How could I determine if any item in a group (Lights) changes, and then go though and handle only the items that DID change?

Thanks
Craig

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Perl Coding Help

Jeff


Craig,

I don't claim to be an expert, but I think your code below could be reduced to this:

if ($state = $Living_Room_Lamps->state_changed()) {
  print_log("Turning Living Room Lamps $state")
}

if ($state = $Kitchen_Lights->state_changed()) {
  print_log("Turning Kitchen Lights $state")
}

If you want to make it further generic, I think you need to do something similar to the code in code/examples/test_group_memeber_change.pl (pasted the relevant parts below):

$mg = new Group( $m1, $m2, $m3 );

if ( $state = state_now $mg) {
    my $member = member_changed $mg;
    print "pass=$Loop_Count: mg changed to $state by $member\n";
}

Hope this helps.

Merry Christmas!
Jeff


On 12/23/2016 12:01 PM, Craig Lindholm wrote:
I want to trigger (test) for any state change of a list of items and then do a command for each of the items that changed state.

For example, I have 10 lamp items, and two of them change state on a particular pass. I then want to post a log, for example, for only those two. I could have 10 of these if statements, one for each item, but its ugly:

if(state_changed $Living_Room_Lamps)
{
if(state_now $Living_Room_Lamps eq 'on'){
print_log(" Turning Living Room Lamps ON");}
if(state_now $Living_Room_Lamps eq 'off'){
print_log(" Turning Living Room Lamps OFF");}
}
if(state_changed $Kitchen_Lights)
{
if(state_now $Kitchen_Lights eq 'on'){
print_log(" Turning Kitchen Lights ON");}
if(state_now $Kitchen_Lights eq 'off'){
print_log(" Turning Kitchen Lights OFF");}
}

How could I determine if any item in a group (Lights) changes, and then go though and handle only the items that DID change?

Thanks
Craig


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel


________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users

Rob
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Perl Coding Help

Rob
In reply to this post by Craig Lindholm
The state_now function works on groups and returns the new state when a group member was changed.  You could code

if ($_ = state_now $Lights) {

  my $light_changed = /member (.+)$/;
  my $light_state = $1;  # New state
  if ($light_changed) {
    my $item = member_changed $Lights;
    my $name = $item->{object_name};
    print_log(" Turning $name $light_state")
  }
}
Loading...