Programmatically create Generic_Item named by a variable?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Programmatically create Generic_Item named by a variable?

rickerdo
This post was updated on .
I know this is generally frowned upon in Perl, but I [think I] have a need to create a list of alarm zones as Generic_Item(s) by using the value of a variable as the name of the Generic_Item.

Example:
for (my $i=1;$i<=208;$i++) {
    my $msg = $elk->requestStringTextDescription(type => '0', index => $i);
    my $zone_name = $msg->getRawText;
    $zone_name =~ s/^\s+//;
    $zone_name =~ s/\s+$//;
    ${$zone_name} = new Generic_Item; #<- this obviously croaks because strict mode is enabled.
}

Is there an MH function I can call to get around this since Perl is what stops me from doing the above? I know I should create something like a %zones hash and add each zone like: "$zone{$zone_name} = new Generic_Item;", but then I end up creating a hash of Generic_Items which prevents me from using something like:
if ($FrontDoor->{state} eq OPEN) {
  # Do something...
}

Any thoughts?

Rick
Reply | Threaded
Open this post in threaded view
|

Re: Programmatically create Generic_Item named by a variable?

H Plato
Just an idea; I think MH gets around this by generating perl code and then eval’ing it (like how the items.mht file turns into items.mhp). 

You could try creating a unique code file (like alarms.mhp) and then eval’ing it? something like:



On Nov 20, 2016, at 5:53 PM, rickerdo <[hidden email]> wrote:

I know this is generally frowned upon in Perl, but I [think I] have a need to
create a list of alarm zones as Generic_Item(s) by using the value of a
variable as the name of the Generic_Item.

Example:
for (my $i=1;$i<=208;$i++) {
   my $msg = $elk->requestStringTextDescription(type => '0', index => $i);
   my $zone_name = $msg->getRawText;
   $zone_name =~ s/^\s+//;
   $zone_name =~ s/\s+$//;
   ${$zone_name} = new Generic_Item; #<- this obviously croaks because
strict mode is enabled.
}

Is there an MH function I can call to get around this since Perl is what
stops me from doing the above? I know I should create something like a
%zones hash and add each zone like: "$zone{$zone_name} = new Generic_Item;",
but then I end up creating a hash of Generic_Items which prevents me from
using something like:
if ($FrontDoor eq OPEN) {
 # Do something...
}

Any thoughts?

Rick



--
View this message in context: http://misterhouse.10964.n7.nabble.com/Programmatically-create-Generic-Item-named-by-a-variable-tp21513.html
Sent from the Misterhouse - User mailing list archive at Nabble.com.

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



------------------------------------------------------------------------------

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

Reply | Threaded
Open this post in threaded view
|

Re: Programmatically create Generic_Item named by a variable?

rickerdo
I've tried the eval method. However, as confirmed by this thread - http://misterhouse.10964.n7.nabble.com/Eval-ed-Code-wont-work-td10878.html the objects are created, but without names. So ever though they exist, they are pretty useless, I think.

I've looked through some of the MH code code, but it's a bit over my head. How does MH read an mht file and programmatically create Generic_Items? Something tells me that's the bit of code I'm looking for, but can't quite figure out.

Reply | Threaded
Open this post in threaded view
|

Re: Programmatically create Generic_Item named by a variable?

Jon Whitear
It’s lib/read_table_A.pl that parses the mht file. It took me a while to get my head around this, and it’s a while since I rewrote the Clipsal CBus code, but you could look at that as an example. There are entries in read_table_A.pl for the various CBus object types, and that results in those objects being created per e.g. lib/Clipsal_CBus.pm, and can inherit from Generic_Item e.g. @Clipsal_CBus::CGate::ISA = ('Generic_Item', 'Clipsal_CBus’).

Hope that helps.


> On 22 Nov. 2016, at 12:48 pm, rickerdo <[hidden email]> wrote:
>
> I've tried the eval method. However, as confirmed by this thread -
> http://misterhouse.10964.n7.nabble.com/Eval-ed-Code-wont-work-td10878.html
> the objects are created, but without names. So ever though they exist, they
> are pretty useless, I think.
>
> I've looked through some of the MH code code, but it's a bit over my head.
> How does MH read an mht file and programmatically create Generic_Items?
> Something tells me that's the bit of code I'm looking for, but can't quite
> figure out.
>
>
>
>
>
> --
> View this message in context: http://misterhouse.10964.n7.nabble.com/Programmatically-create-Generic-Item-named-by-a-variable-tp21513p21516.html
> Sent from the Misterhouse - User mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> ________________________________________________________
> To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>



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

Reply | Threaded
Open this post in threaded view
|

Re: Programmatically create Generic_Item named by a variable?

H Plato
Another idea, rather than ‘eval’ing’ the code, just write a file to your code directory and then reload the code. This is how organizer.pl parses icals and then generates all the notifications.


> On Nov 23, 2016, at 4:07 AM, Jon Whitear <[hidden email]> wrote:
>
> It’s lib/read_table_A.pl that parses the mht file. It took me a while to get my head around this, and it’s a while since I rewrote the Clipsal CBus code, but you could look at that as an example. There are entries in read_table_A.pl for the various CBus object types, and that results in those objects being created per e.g. lib/Clipsal_CBus.pm, and can inherit from Generic_Item e.g. @Clipsal_CBus::CGate::ISA = ('Generic_Item', 'Clipsal_CBus’).
>
> Hope that helps.
>
>
>> On 22 Nov. 2016, at 12:48 pm, rickerdo <[hidden email]> wrote:
>>
>> I've tried the eval method. However, as confirmed by this thread -
>> http://misterhouse.10964.n7.nabble.com/Eval-ed-Code-wont-work-td10878.html
>> the objects are created, but without names. So ever though they exist, they
>> are pretty useless, I think.
>>
>> I've looked through some of the MH code code, but it's a bit over my head.
>> How does MH read an mht file and programmatically create Generic_Items?
>> Something tells me that's the bit of code I'm looking for, but can't quite
>> figure out.
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://misterhouse.10964.n7.nabble.com/Programmatically-create-Generic-Item-named-by-a-variable-tp21513p21516.html
>> Sent from the Misterhouse - User mailing list archive at Nabble.com.
>>
>> ------------------------------------------------------------------------------
>> ________________________________________________________
>> To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>>
>
>
>
> ------------------------------------------------------------------------------
> ________________________________________________________
> To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
>


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