blib/lib/Win32/OLE/OPC.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
↑ Summary
1 package Win32::OLE::OPC;
2
3 # Copyright (c) 1999-2001 by Martin Tomes. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6
7 # $Id: OPC.pm,v 1.16 2002/04/30 09:14:55 martinto Exp $
8
9 =pod
10
11 =head1 NAME
12
13 Win32::OLE::OPC - OPC Server Interface
14
15 =head1 SYNOPSIS
16
17 Two ways of using the OPC interface are provided, the class methods:
18
19 use Win32::OLE::OPC;
20
21 my $opcintf = Win32::OLE::OPC->new('Someones.OPCAutomation',
22 'Someones.Server');
23 $opcintf->MoveToRoot;
24 foreach $item ($opcintf->Leafs) {
25 print $item->{name}, "\n";
26 my %result = $opcintf->ItemData($item->{itemid});
27 for $attrib (keys %result) {
28 print " [", $attrib, " = '", $result{$attrib}, "']", "\n";
29 }
30 print "\n";
31 }
32 foreach $item ($opcintf->Branches) {
33 print $item->{name}, "\n";
34 }
35
36 or a tied hash:
37
38 use Win32::OLE::OPC;
39
40 my %OPC;
41 tie %OPC, Win32::OLE::OPC, 'Someones.OPCAutomation', 'Someones.Server';
42
43 # OK, list the keys...
44 for $key (keys %OPC) {
45 my %x = %{$OPC{$key}};
46 print $key, "\n";
47 for $attrib (keys %x) {
48 print " '", $attrib, "' = '", $x{$attrib}, "'", "\n";
49 }
50 print "\n";
51 }
52
53 The tied hash method has to return a reference to a hash containing the item
54 data hence the unpleasant code 'C<%x = %{$OPC{$key}}>'. Alternatively one can
55 assign the returned value into a scalar and dereference it when using the hash
56 like this 'C<keys %$x>' and 'C<$result-E<gt>{$item}>'.
57
58 Note that both methods can be used together. First create an interface using the C<new()> method and then tie it like this:
59
60 tie %OPC, $opcintf, 'Someones.OPCAutomation', 'Someones.Server';
61
62 To connect to a remote server add the name of the server as a parameter to the
63 call to new() or to the tie:
64
65 my $opcintf = Win32::OLE::OPC->new('Someones.OPCAutomation',
66 'Someones.Server',
67 'machine.name');
68 tie %OPC, Win32::OLE::OPC, 'Someones.OPCAutomation',
69 'Someones.Server',
70 'machine.name';
71
72 =head1 DESCRIPTION
73
74 A partial implementation of the OLE for Process Control dispatch interface as
75 defined in the 'Data Access Automation Interface Standard' version 2.
76
77 An exception is raised using C<Carp::croak()> on any failure.
78
79 =head2 METHODS
80
81 =over 4
82
83 =cut
84
85 0 ▶use Win32::OLE qw(HRESULT);
Subroutine BEGIN: called CC=1 SCAR=2.6
86 use Win32::WinError;
87 use Win32::OLE::Variant;
88 use Sys::Hostname;
89 use Carp;
90 use strict;
91 use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK $OPCCache $OPCDevice
92 $dont_show_property_exceptions/;
93
94 require Exporter;
95 @ISA = qw(Exporter);
96 @EXPORT = qw($OPCCache $OPCDevice);
97 # GetOPCServers can be called without an object reference.
98 @EXPORT_OK = qw(&GetOPCServers $show_property_exceptions);
99 $VERSION = '0.92';
100
101 # Constants.
102 $OPCCache = 1;
103 $OPCDevice = 2;
104 # Turn on some more error reporting.
105 $dont_show_property_exceptions = 1;
106
107 my $quiet = 1;
108 my $DEBUG = 0;
109
110 BEGIN {
111 Win32::OLE->Option(Warn => 0); # Ignore errors and return undef.
112 }
113
114 # Here for convenience, returns the name of the function which called the
115 # current function, is used for reporting errors.
116 sub _whowasi { (caller(1))[3] . '()' }
117
118 sub _check_error {
119 # Check the HRESULT which came back from the dll.
120 my $msg = shift; # A message to print with the error.
121 my $quiet = shift;
122
123 if (Win32::OLE->LastError() != S_OK) {
124 carp "$msg:\n " . Win32::OLE->LastError() . "\n" unless ($quiet);
125 return 1; # Was bad.
126 }
127 return 0; # Was OK.
128 }
129
130 =pod
131
132 =item Win32::OLE::OPC->new(DLLPROGID, SERVERPROGID, SERVERNODE)
133
134 The C<new()> method creates an instance of an OPC server object. The
135 C<DLLPROGID> argument is the COM progid of the Dll which implements the Dispatch
136 interface to the OPC server. The C<SERVERPROGID> is the COM progid of the OPC
137 server containing the data you wish to access. The DLLPROGID and SERVERPROGID
138 arguments are required.
139
140 The SERVERNODE argument is optional and is the name of a remote machine
141 running the SERVERPROGID. When SERVERNODE is specified a connection is made
142 to the server using DCOM. WARNING: DCOM security can be a little difficult to
143 understand so perseverance is required.
144
145 As the OPC specification only allows one browser per instance of the dispatch
146 Dll the C<new()> method creates and keeps a browser object in
147 C<Win32::OLE::OPC-E<gt>{browser}>
148
149 =cut
150
151 sub new {
152 my $that = shift; # Either a class name or a reference to one.
153 my $class = ref($that) || $that; # Allow class name or reference to work.
154 my $opcdllprogid = shift; # The OPC foundation dispatch interface DLL.
155 # Each vendor will have their own ProgID etc.
156 my $serverprogid = shift; # The server to connect to.
157 my $servernode = shift; # The machine the server is running on.
158
159 croak "usage: @{[&_whowasi]} DLLPROGID SERVERPROGID" if @_;
160
161 # Save the progid's as they might be useful.
162 my $self = {
163 _opcdllprogid => $opcdllprogid,
164 _serverprogid => $serverprogid
165 };
166 # Get an instance of the OPC dispatch DLL.
167 $self->{dllintf} = Win32::OLE->new($opcdllprogid)
168 or croak "Failed to connect to dispatch DLL $opcdllprogid";
169 # Connect to the selected server.
170 if ($servernode) {
171 $self->{dllintf}->Connect($serverprogid, $servernode);
172 } else {
173 $self->{dllintf}->Connect($serverprogid);
174 }
175 &_check_error("OPC::Connect to server " . $serverprogid);
176 # Register my name on the server.
177 $self->{dllintf}->{ClientName} = $0 . " on " . hostname;
178 &_check_error("OPC::Connect to server - add client name " . $serverprogid);
179 # One can only have one browser so make it internal and create it now.
180 $self->{browser} = $self->{dllintf}->CreateBrowser
181 || croak "Failed to create browser for " . $serverprogid;
182 bless $self, $class;
183 return $self;
184 }
185
186 # -----------------------------------------------------------------------------
187 # Browser methods.
188 #
189 # These sort of mirrors the browser interface but it appears on the
190 # Win32::OLE::OPC object. The rational behind this is that an instance of the
191 # DLL can only provide one browser anyway so why not hide a level of
192 # complexity and put it up into the main object. The main difference is that
193 # the collections are turned into arrays which are both stored in the object
194 # hash and returned.
195 # -----------------------------------------------------------------------------
196
197 =pod
198
199 =item MoveToRoot
200
201 A browser method. Moves the current browse position to the root of the
202 address space.
203
204 =cut
205
206 sub MoveToRoot {
207 my $self = shift;
208 $self->{browser}->MoveToRoot();
209 &_check_error("OPC::MoveToRoot " . $self->{_serverprogid});
210 }
211
212 =pod
213
214 =item MoveDown(TO)
215
216 A browser method. Moves the current browse position one branch down the
217 address space. The C<TO> parameter is the branch name.
218
219 =cut
220
221 sub MoveDown {
222 my $self = shift;
223 my $to = shift; # Name of the branch to move to relative to
224 # the current branch.
225 $self->{browser}->MoveDown($to);
226 &_check_error("OPC::MoveDown " . $self->{_serverprogid}, $quiet);
227 }
228
229 =pod
230
231 =item MoveUp
232
233 A browser method. Moves the current browse position one node up the address
234 space.
235
236 =cut
237
238 sub MoveUp {
239 my $self = shift;
240 $self->{browser}->MoveUp();
241 &_check_error("OPC::MoveUp " . $self->{_serverprogid}, $quiet);
242 }
243
244 =pod
245
246 =item MoveTo(TO)
247
248 A browser method. Moves the current browse position to the absolute location
249 specified by C<TO>.
250
251 $opcintf->MoveTo('COM1._Diagnostics');
252
253 =cut
254
255 sub MoveTo {
256 my $self = shift;
257 my $to = shift;
258 my @to = split /\./, $to; # Turn it into an array.
259
260 $self->MoveToRoot; # Always start at the top.
261 foreach my $branch (@to) {
262 $self->MoveDown($branch);
263 &_check_error("OPC::MoveTo/MoveDown " . $self->{_serverprogid}, $quiet);
264 }
265 }
266
267 # I can't get an array of BSTR to work :-(
268 #sub MoveTo {
269 # my $self = shift;
270 # my $to = shift; # name of the branch to move to relative to
271 # # the root.
272 # my @to = split /\./, $to; # Turn it into an array.
273 # my $vto = Variant(VT_ARRAY|VT_BSTR, [0, $#to]);
274 # print '$#to = ', $#to, "\n";
275 # for (my $i=0; $i<=$#to; $i++) {
276 # $vto->Put($i, $to[$i]);
277 # }
278 # $self->{browser}->MoveTo($vto);
279 # &_check_error("OPC::MoveTo " . $self->{_serverprogid});
280 #}
281
282 # I can't get a ref to an array to work either!
283 #sub MoveTo {
284 # my $self = shift;
285 # my $to = shift;
286 ## my @to = split /\./, $to; # Turn it into an array.
287 # my $to2 = [ split /\./, $to ]; # Turn it into an array ref.
288 #
289 # $self->{browser}->MoveTo($to2);
290 #
291 # &_check_error("OPC::MoveTo " . $self->{_serverprogid});
292 #}
293
294 =pod
295
296 =item Branches
297
298 A browser method.
299
300 Returns the branch names and itemid in a hash in an array. The method has a
301 different name from the OPC ShowBranches method because it doesn't do quite
302 the same thing.
303
304 The returned array is also stored in C<Win32::OLE::OPC-E<gt>{items}>.
305
306 See synopsis for an example of how to call this method.
307
308 =cut
309
310 #' Satisfy emacs syntax colouring:-(
311
312 sub Branches {
313 my $self = shift;
314 $self->{browser}->ShowBranches(); # Fill the collection in the Dll.
315 &_check_error("OPC::Branches " . $self->{_serverprogid});
316 @{$self->{items}} = (); # Clear out the items array.
317 foreach my $item (in {$self->{browser}}) {
318 push @{$self->{items}}, { name => $item,
319 itemid => $self->{browser}->GetItemID($item),
320 };
321 }
322 # Set the count property.
323 $self->{count} = $self->{browser}->{Count};
324 return @{$self->{items}}; # Return the list of items.
325 }
326
327 =pod
328
329 =item Leafs
330
331 A browser method.
332
333 Returns the leaf names and itemid in a hash in an array. The method has a
334 different name from the OPC ShowLeafs because it doesn't do quite the same
335 thing. The hash has the members C<name> and C<itemid>. The number of items
336 in the array is saved in C<Win32::OLE::OPC-E<gt>{count}>
337
338 The returned array is also stored in C<Win32::OLE::OPC-E<gt>{items}>.
339
340 foreach $item ($opcintf->Leafs) {
341 print $item->{name}, " ", item->{itemid}, "\n";
342 }
343
344 =cut
345
346 #' Satisfy emacs syntax colouring:-(
347
348 sub Leafs {
349 my $self = shift;
350 my $flat = shift; # Whether to flatten out the namespace.
351 $flat ||= 0; # Set false if undefined.
352 my $vflat = Variant(VT_BOOL, $flat); # Need it as a variant.
353 $self->{browser}->ShowLeafs($vflat); # Fill the collection in the Dll.
354 &_check_error("OPC::Leafs " . $self->{_serverprogid});
355 @{$self->{items}} = (); # Clear out the items array.
356 foreach my $item (in {$self->{browser}}) {
357 push @{$self->{items}}, { name => $item,
358 itemid => $self->{browser}->GetItemID($item),
359 };
360 &_check_error("OPC::Leafs " . $self->{_serverprogid});
361 }
362 # Set the count property.
363 $self->{count} = $self->{browser}->{Count};
364 return @{$self->{items}}; # Return the list of items.
365 }
366
367 =pod
368
369 =item Item(N)
370
371 Returns a hash contining the name and itemid of item C<N>. Calls to
372 C<Leafs> and C<Branches> collect the item data. This is an alternative method
373 of fetching the address space.
374
375 $opcintf->Leafs;
376 for (my $i = 0; $i < $opcintf->{count}; $i++) {
377 my $item = $opcintf->Item($i);
378 print $item->{name}, " ", item->{itemid}, "\n";
379 }
380
381 =cut
382
383 sub Item {
384 my $self = shift;
385 my $num = shift; # 1 based index into last fetched browser
386 # collection.
387 return ${$self->{items}}[$num - 1];
388 }
389
390 =pod
391
392 =item ItemData(ITEMID)
393
394 Use this to extract all the data the server holds for this item of data. The
395 C<ITEMID> is the C<itemid> member of the hash returned by the C<Leafs>
396 method.
397
398 It returns a hash, the keys of which are the available attributes (found by
399 calling the OPC C<QueryAvailableProperties()> method) and the values of in the
400 hash are obtained by calling GetItemProperties.
401
402 =cut
403
404 sub ItemData {
405 my $self = shift; # Just *exactly* who am I?
406 # Use the function which gets the reference and dereference it. _ItemData()
407 # is called here and bu FETCH().
408 return %{$self->_ItemData(@_)};
409 }
410
411 sub _ItemData {
412 my $self = shift;
413 my $ItemID = shift; # The item id was returned by Leafs.
414
415 my $count = Variant(VT_I4|VT_BYREF, 0);
416 my $PropertyIds = Variant(VT_ARRAY|VT_I4|VT_BYREF, []);
417 my $Descriptions = Variant(VT_ARRAY|VT_BSTR|VT_BYREF, []);
418 my $DataTypes = Variant(VT_ARRAY|VT_I2|VT_BYREF, []);
419 $self->{dllintf}->QueryAvailableProperties($ItemID,
420 $count,
421 $PropertyIds,
422 $Descriptions,
423 $DataTypes);
424 &_check_error("OPC::QueryAvailableProperties " . $self->{_serverprogid});
425
426 my $PropertyValues = Variant(VT_ARRAY|VT_VARIANT|VT_BYREF, [1,1]);
427 my $Errors = Variant(VT_ARRAY|VT_I4|VT_BYREF, [1,1]);
428 $self->{dllintf}->GetItemProperties($ItemID,
429 $count, # Count
430 $PropertyIds,
431 $PropertyValues,
432 $Errors);
433 &_check_error("OPC::GetItemProperties " . $self->{_serverprogid});
434
435 carp("GetItemProperties error " . $Errors->Get(1)) if ($Errors->Get(1));
436
437 # Now turn this into a hash.
438 my %result; undef %result; # Clear it out.
439 for (my $i=1; $i<=$count; $i++) {
440 if ($Errors->Get($i) == HRESULT(0x80004005)) { # E_FAIL
441 $result{$Descriptions->Get($i)} = undef;
442 } elsif ($Errors->Get($i)) {
443 croak("GetItemProperties: "
444 . $self->{dllintf}->GetErrorString($Errors->Get($i))
445 . " " . $Descriptions->Get($i));
446 } else {
447 $result{$Descriptions->Get($i)} = $PropertyValues->Get($i)
448 if (defined($PropertyValues->Get($i)));
449 }
450 }
451 return \%result;
452 }
453
454 sub _AvailableProperties {
455 # This is an internal routine which returns the available properties for an
456 # ItemID as an array of hashes. The externally used version saves this in
457 # $self->{properties}.
458
459 my $self = shift;
460 my $ItemID = shift; # The item id was returned by Leafs.
461
462 my $count = Variant(VT_I4|VT_BYREF, 0);
463 my $PropertyIds = Variant(VT_ARRAY|VT_I4|VT_BYREF, []);
464 my $Descriptions = Variant(VT_ARRAY|VT_BSTR|VT_BYREF, []);
465 my $DataTypes = Variant(VT_ARRAY|VT_I2|VT_BYREF, []);
466 $self->{dllintf}->QueryAvailableProperties($ItemID,
467 $count,
468 $PropertyIds,
469 $Descriptions,
470 $DataTypes);
471 &_check_error("OPC::QueryAvailableProperties " . $self->{_serverprogid});
472 # Now turn this into an array of hashes.
473 my @result = (); # Clear it out.
474 for (my $i=1; $i<=$count; $i++) {
475 push @result, {
476 Id => $PropertyIds->Get($i),
477 Description => $Descriptions->Get($i),
478 DataType => $DataTypes->Get($i)
479 };
480 }
481 return @result;
482 }
483
484 =pod
485
486 =item AvailableProperties(ITEMID)
487
488 Returns the available properties of an item. The C<ITEMID> is the C<itemid>
489 member of the hash returned by the C<Leafs> method.
490
491 It returns an array of hashes containing the available attributes found by
492 calling the OPC C<QueryAvailableProperties()> method. The hash contains
493 C<Id>, C<Description> and C<DataType> members.
494
495 The returned array is also stored in C<Win32::OLE::OPC-E<gt>{properties}>.
496
497 print " Id Type Description\n";
498 foreach my $prop ($opcintf->AvailableProperties($item->{itemid})) {
499 printf "%3d %4d %s\n",
500 $prop->{Id},
501 $prop->{DataType},
502 $prop->{Description};
503 }
504
505 =cut
506
507 sub AvailableProperties {
508 my $self = shift;
509 my $ItemID = shift; # The item id was returned by Leafs.
510
511 @{$self->{properties}} = $self->_AvailableProperties($ItemID);
512 return @{$self->{properties}};
513 }
514
515 =pod
516
517 =item ServerProperties
518
519 Return a hash indexed by the following properties containing the property
520 value:
521
522 StartTime CurrentTime LastUpdateTime MajorVersion
523 MinorVersion BuildNumber VendorInfo ServerState LocaleID
524 Bandwidth OPCGroups PublicGroupNames ServerName
525 ServerNode ClientName
526
527 Note that the OPCGroups value is itself a reference to a hash and it contains
528 a hash member indexed 'Parent' which is a hash pointing back up.
529
530 =cut
531
532 sub ServerProperties {
533 # Collect everything available into a hash and return it.
534 my $self = shift;
535
536 my %retv; # Return this.
537 for my $property (qw/StartTime CurrentTime LastUpdateTime MajorVersion
538 MinorVersion BuildNumber VendorInfo ServerState LocaleID
539 Bandwidth OPCGroups PublicGroupNames ServerName
540 ServerNode ClientName/) {
541 $retv{$property} = $self->{dllintf}->{$property};
542 &Win32::OLE::OPC::_check_error("OPC::ServerProperties "
543 . $self->{_serverprogid},
544 $dont_show_property_exceptions);
545 }
546 return %retv;
547 }
548
549 =pod
550
551 =item GetOPCServers
552
553 Return an array containing the names of available servers. Can be called with
554 the progid of the dispatch Dll as an argument, in which case it will connect
555 to the Dll and extract the list of servers. It can also be called using the
556 object created by a call to new, in which case the name of the Dll is not
557 required. The GetOPCServers is not exported by default.
558
559 use Win32::OLE::OPC qw(GetOPCServers);
560
561 my @AvailableServers = GetOPCServers('Someones.OPCAutomation');
562
563 =cut
564
565 sub GetOPCServers {
566 my $self = shift;
567 if (ref($self) eq 'Win32::OLE::OPC') {
568 # Called via a blessed thingy so the dll is already connected.
569 my $varray = $self->{dllintf}->GetOPCServers();
570 &Win32::OLE::OPC::_check_error("OPC::GetOPCServers "
571 . $self->{_serverprogid});
572 return @$varray;
573 } else {
574 # Called directly, i.e., not via a blessed thingy. Therefore I need to
575 # create and then throw away an interface to the dispatch Dll. In this
576 # case $self isn't a blessed thingy reference, it is assumed to be a
577 # string which should be the name of the dispatch Dll.
578 my $dllintf = Win32::OLE->new($self)
579 or croak "Failed to connect to dispatch DLL $self";
580 my $varray = $dllintf->GetOPCServers();
581 return @$varray;
582 }
583 }
584
585 # Not implemented on any servers I have!
586 #sub QueryAvailableLocaleIDs {
587 # my $self = shift;
588 #
589 # ???? = $self->{dllintf}->QueryAvailableLocaleIDs;
590 #
591 #}
592
593 =pod
594
595 =item BrowserProperties
596
597 Return a hash indexed by the following properties containing the property
598 value:
599
600 Organization Filter DataType AccessRights CurrentPosition Count
601
602 =cut
603
604 sub BrowserProperties {
605 # Collect everything available into a hash and return it.
606 my $self = shift;
607
608 my %retv; # Return this.
609 for my $property (qw/Organization Filter DataType AccessRights
610 CurrentPosition Count/) {
611 $retv{$property} = $self->{browser}->{$property};
612 &Win32::OLE::OPC::_check_error("OPC::BrowserProperties "
613 . $self->{_serverprogid},
614 $dont_show_property_exceptions);
615 }
616 return %retv;
617 }
618
619 =pod
620
621 =item GetItemIdFromName
622
623 This is not a standard OPC browser method. It translates a full OPC path name
624 to an item id. It is often the case that the item id and the OPC path name is
625 one and the same thing, but you cannot assume that!
626
627 =cut
628
629 sub GetItemIdFromName {
630 my $self = shift;
631 my $opc_name = shift;
632
633 # Split it, take off the last part of the path and put it back together
634 # again. This leaves us with the branch and the item within the branch.
635 my @opc_name = split /\./, $opc_name;
636 my $itemname = pop @opc_name;
637 $opc_name = join '.', @opc_name; # All but the last item in the path.
638 # Remember where I am now.
639 my $startpos = $self->{browser}->{CurrentPosition};
640 $self->MoveTo($opc_name);
641 my $itemid;
642 foreach my $item ($self->Leafs) {
643 if ($item->{name} eq $itemname) {
644 $itemid = $item->{itemid};
645 last;
646 }
647 }
648 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
649 return $itemid;
650 }
651
652 =pod
653
654 =back
655
656 =cut
657
658
659 # -----------------------------------------------------------------------------
660 # Tied hash bit.
661 #
662 # This exposes the address space of the server as a hash whose keys are the
663 # browse space.
664 # -----------------------------------------------------------------------------
665
666 =pod
667
668 =head2 TIED HASH
669
670 See the SYNOPSIS for example code.
671
672 If you tie a hash to this module you can:
673
674 =over 4
675
676 =item Read an items attributes
677
678 A reference to a hash keyed by attribute names is returned. See the synopsis
679 fo an example.
680
681 =item Access the Keys
682
683 The keys of the hash can be enumerated so C<keys> and C<each> function will
684 work.
685
686 =back
687
688 Writing, deleting or undefining a member of the hash is not possible.
689
690 =cut
691
692 sub TIEHASH {
693 carp &_whowasi . " TIE" if ($DEBUG);
694 my $that = shift;
695 if (ref($that)) {
696 # An existing blessed thingy has been passed in as $that.
697 return $that;
698 }
699 my $class = $that; # $that is the class name if we get here.
700 my $opcdllprogid = shift; # The OPC foundation dispatch interface DLL.
701 # Each vendor will have their own ProgID etc.
702 my $serverprogid = shift; # The server to connect to.
703 my $servernode = shift; # The machine the server is running on.
704
705 # Make one and return it.
706 if ($servernode) {
707 return &new($class, $opcdllprogid, $serverprogid, $servernode);
708 } else {
709 return &new($class, $opcdllprogid, $serverprogid);
710 }
711 }
712
713 sub FETCH {
714 carp &_whowasi . " FETCH" if ($DEBUG);
715 my $self = shift;
716 my $path = shift; # This is the OPC name, i need the item id.
717
718 # Fetch the itemid for $path.
719
720 # Split it, take off the last part of the path and put it back together
721 # again. This leaves us with the branch and the item within the branch.
722 my @path = split /\./, $path;
723 my $itemname = pop(@path);
724 $path = join '.', @path;
725 my $startpos = $self->{browser}->{CurrentPosition};
726 $self->MoveTo($path);
727 my $item;
728 foreach $item ($self->Leafs) {
729 if ($item->{name} eq $itemname) {
730 # This returns a reference to a hash which works but is unpleasant to
731 # use. You need something like this...
732 #
733 # my $result = $OPC{'something.or.other'};
734 #
735 # foreach $item (keys %$result) {
736 # print "'", $item, "'", " = '", $$result{$item}, "'\n";
737 # }
738 #
739 # or this...
740 #
741 # my %result = %{$OPC{'something.or.other'}};
742 #
743 # foreach $item (keys %result) {
744 # print "'", $item, "'", " = '", $result{$item}, "'\n";
745 # }
746
747 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
748 return $self->_ItemData($item->{itemid});
749 }
750 }
751 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
752 return undef; # Failed to find it.
753 }
754
755 sub STORE {
756 carp &_whowasi . " STORE" if ($DEBUG);
757 my $self = shift;
758 my $key = shift; # This is the OPC name, i need the item id.
759 my $value = shift;
760
761 # Fetch the itemid for $key.
762
763 # Split it, take off the last part of the path and put it back together
764 # again. This leaves us with the branch and the item within the branch.
765 my @key = split /\./, $key;
766 my $itemname = pop(@key);
767 $key = join '.', @key;
768 my $startpos = $self->{browser}->{CurrentPosition};
769 $self->MoveTo($key);
770 my $itemid;
771 foreach my $item ($self->Leafs) {
772 if ($item->{name} eq $itemname) {
773 $itemid = $item->{itemid};
774 }
775 }
776
777 unless (defined($itemid)) {
778 carp &_whowasi . " STORE: $key.$itemname not found.";
779 return undef;
780 }
781
782 unless (defined $self->{tieitems}) {
783 # Create a groups object containing a group object containing an items
784 # object which can be used to add an item and write to it. Phew.
785 $self->{tiegroups} = $self->OPCGroups; # The groups collection.
786 # Add a group.
787 $self->{tiegroup} = $self->{tiegroups}->Add('tiegroup');
788 # Get the items collection.
789 $self->{tieitems} = $self->{tiegroup}->OPCItems;
790 }
791 # OK, one has the items collection. Add to it.
792 $self->{tieitems}->AddItem($itemid, 1);
793 # I only ever have one item in this group.
794 my $item = $self->{tieitems}->Item(1);
795 $item->Write($value);
796 $self->{tieitems}->Remove([$item->ServerHandle]);
797 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
798 }
799
800 sub DELETE {
801 carp &_whowasi . ": One cannot delete elements of this hash";
802 }
803
804 sub CLEAR {
805 carp &_whowasi . ": One cannot clear this hash";
806 }
807
808 sub EXISTS {
809 carp &_whowasi . " EXISTS" if ($DEBUG);
810 my $self = shift;
811 my $path = shift;
812
813 # Split it, take off the last part of the path and put it back together
814 # again. This leaves us with the branch and the item within the branch.
815 my @path = split /\./, $path;
816 my $itemname = pop(@path);
817 $path = join '.', @path;
818 my $startpos = $self->{browser}->{CurrentPosition};
819 $self->MoveTo($path);
820 my $item;
821 foreach $item ($self->Leafs) {
822 if ($item->{name} eq $itemname) {
823 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
824 return 1;
825 }
826 }
827 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
828 return 0; # Failed to find it.
829 }
830
831 sub _EnumThisLevel {
832 # Enumerates this level and recurse into each branch. Pushes all the item
833 # names into the array referenced by $self->{_keys}
834 my $self = shift;
835 my ($path) = @_; # The path to this level.
836
837 $path .= '.' if (length($path)); # Put a . separator in if not the root.
838 foreach my $item ($self->Leafs) {
839 push @{$self->{_keys}}, $path. $item->{name};
840 }
841 foreach my $item ($self->Branches) {
842 $self->MoveDown($item->{name});
843 $self->_EnumThisLevel($path . $item->{name});
844 $self->MoveUp;
845 }
846 }
847
848 sub FIRSTKEY {
849 carp &_whowasi . " FIRSTKEY" if ($DEBUG);
850 my $self = shift;
851
852 my $startpos = $self->{browser}->{CurrentPosition};
853 $self->MoveToRoot; # Reset to start.
854 # Now read all the item names into an internal hash which will then be used
855 # to return all the results. This is far simpler than doing it on the fly
856 # but does mean that it will take a while for the first value to be
857 # returned. If the array is already defined then use it unless someone sets
858 # CLOBBER in the object.
859 if (!defined($self->{_keys}) || $self->{CLOBBER}) {
860 $self->{_keys} = (); # Initialise the keys.
861 $self->_EnumThisLevel('');
862 }
863 $self->{_keyidx} = 0; # Go to the start of the array.
864 $self->MoveTo($startpos) if ($startpos); # Leave it where it was before.
865 return $self->NEXTKEY; # Simplest way out! Doesn't need lastkey arg.
866 }
867
868 sub NEXTKEY {
869 carp &_whowasi . " NEXTKEY" if ($DEBUG);
870 my $self = shift;
871
872 if ($self->{_keyidx} <= $#{$self->{_keys}}) {
873 my $result = ${$self->{_keys}}[$self->{_keyidx}];
874 $self->{_keyidx}++;
875 return $result;
876 } else {
877 return undef;
878 }
879 }
880
881 sub DESTROY {
882 carp &_whowasi . " DESTROY" if ($DEBUG);
883 # No need to do anything in here.
884 }
885
886 =pod
887
888 =head2 OPCGroups
889
890 The Win32::OLE::OPC::OPCGroups method returns an OPCGroups object which is
891 blessed into the perl Win32::OLE::OPC::Groups class.
892
893 =over 4
894
895 =cut
896
897 sub OPCGroups {
898 my $self = shift;
899 my $groups = { _parent => $self };
900 $groups->{groups} = $self->{dllintf}->{OPCGroups};
901 $groups->{_serverprogid} = $self->{_serverprogid};
902 &Win32::OLE::OPC::_check_error("OPC::OPCGroups "
903 . $self->{_serverprogid});
904 return bless $groups, 'Win32::OLE::OPC::Groups';
905 }
906
907 package Win32::OLE::OPC::Groups;
908 use Carp;
909
910 # -----------------------------------------------------------------------------
911 # OPCGroups object.
912 #
913 # Methods which can be used with the return value from OPCGroups.
914 #
915 #-----------------------------------------------------------------------------
916
917 =pod
918
919 =item Properties
920
921 Return a hash indexed by the following properties containing the property
922 value:
923
924 Parent DefaultGroupIsActive DefaultGroupUpdateRate DefaultGroupDeadband
925 DefaultGroupLocaleID DefaultGroupTimeBias Count
926
927 Note that the hash member indexed 'Parent' is a hash pointing back up to the
928 parent properties.
929
930 =cut
931
932 sub Properties {
933 # Collect everything available into a hash and return it.
934 my $self = shift;
935
936 my %retv; # Return this.
937 for my $property (qw/Parent DefaultGroupIsActive DefaultGroupUpdateRate
938 DefaultGroupDeadband DefaultGroupLocaleID
939 DefaultGroupTimeBias Count/) {
940 $retv{$property} = $self->{groups}->{$property};
941 &Win32::OLE::OPC::_check_error("OPC::Groups::Properties "
942 . $self->{_serverprogid},
943 $Win32::OLE::OPC::dont_show_property_exceptions);
944 }
945 return %retv;
946 }
947
948 =item SetProperty(PROPERTY,VALUE)
949
950 Set one of these properties to the value given.
951
952 DefaultGroupIsActive DefaultGroupUpdateRate DefaultGroupDeadband
953 DefaultGroupLocaleID DefaultGroupTimeBias
954
955 =cut
956
957 sub SetProperty {
958 # Collect everything available into a hash and return it.
959 my $self = shift;
960 my $property = shift; # Name of the property to set.
961 my $value = shift; # The value to put in there.
962
963 # Check the name is valid.
964 my $isvalid = 0;
965 for my $valid (qw/DefaultGroupIsActive DefaultGroupUpdateRate
966 DefaultGroupDeadband DefaultGroupLocaleID
967 DefaultGroupTimeBias/) {
968 if ($property eq $valid) {
969 $isvalid = 1;
970 last;
971 }
972 }
973 if ($isvalid) {
974 $self->{groups}->{$property} = $value;
975 &Win32::OLE::OPC::_check_error("OPC::Groups::SetProperty "
976 . $self->{_serverprogid});
977 } else {
978 carp "Attempt to set invalid property '$property' on OPC groups";
979 }
980 }
981
982 =pod
983
984 =item Add(NAME)
985
986 Add a group to the OPC groups collection. NAME is optional.
987
988 =cut
989
990 sub Add {
991 my $self = shift;
992 my $group_name = shift; # This is optional and therefore might be undef
993
994 my $group = { _parent => $self };
995 $group->{group} = $self->{groups}->Add($group_name);
996 $group->{_serverprogid} = $self->{_serverprogid};
997 &Win32::OLE::OPC::_check_error("OPC::Groups::Add "
998 . $self->{_serverprogid});
999 if (defined($group->{group})) {
1000 return bless $group, 'Win32::OLE::OPC::Group';
1001 } else {
1002 return undef;
1003 }
1004 }
1005
1006 =pod
1007
1008 =item Item([NUMBER | NAME])
1009
1010 Get group by 1 based index or by the name used when it was added to the groups
1011 list.
1012
1013 =cut
1014
1015 sub Item {
1016 my $self = shift;
1017 my $ident = shift;
1018
1019 my $group = { _parent => $self };
1020 if ($ident =~ /^[0-9]+/) {
1021 # It is an integer, pass it as such.
1022 $group->{group} = $self->{groups}->Item(int $ident);
1023 } else {
1024 # It is a name so pass the string.
1025 $group->{group} = $self->{groups}->Item($ident);
1026 }
1027 $group->{_serverprogid} = $self->{_serverprogid};
1028 &Win32::OLE::OPC::_check_error("OPC::Groups::Item "
1029 . $self->{_serverprogid});
1030 if (defined($group->{group})) {
1031 return bless $group, 'Win32::OLE::OPC::Group';
1032 } else {
1033 return undef;
1034 }
1035 }
1036
1037 =pod
1038
1039 =item GetOPCGroup([SERVERHANDLE|NAME])
1040
1041 Get group using the server handle or by the name used when it was added to the
1042 groups list.
1043
1044 =cut
1045
1046 sub GetOPCGroup {
1047 my $self = shift;
1048 my $ident = shift;
1049
1050 my $group = { _parent => $self };
1051 $group->{group} = $self->{groups}->GetOPCGroup($ident);
1052 $group->{_serverprogid} = $self->{_serverprogid};
1053 &Win32::OLE::OPC::_check_error("OPC::Groups::GetOPCGroup "
1054 . $self->{_serverprogid});
1055 if (defined($group->{group})) {
1056 return bless $group, 'Win32::OLE::OPC::Group';
1057 } else {
1058 return undef;
1059 }
1060 }
1061
1062 =pod
1063
1064 =item Remove([SERVERHANDLE|NAME])
1065
1066 Remove group using the server handle or by the name used when it was added to
1067 the groups list.
1068
1069 =cut
1070
1071 sub Remove {
1072 my $self = shift;
1073 my $ident = shift;
1074
1075 $self->{groups}->Remove($ident);
1076 &Win32::OLE::OPC::_check_error("OPC::Groups::Remove "
1077 . $self->{_serverprogid});
1078 }
1079
1080 =pod
1081
1082 =item RemoveAll
1083
1084 Remove all groups from the groups list.
1085
1086 =cut
1087
1088 sub RemoveAll {
1089 my $self = shift;
1090
1091 $self->{groups}->RemoveAll;
1092 &Win32::OLE::OPC::_check_error("OPC::Groups::RemoveAll "
1093 . $self->{_serverprogid});
1094 }
1095
1096 =pod
1097
1098 =item ConnectPublicGroup(NAME)
1099
1100 You connect to a public group, it cannot be added. NAME is a string which
1101 identifies the group.
1102
1103 This is untested as I have no server which implements public groups.
1104
1105 =cut
1106
1107 sub ConnectPublicGroup {
1108 my $self = shift;
1109 my $group_name = shift;
1110
1111 my $group = { _parent => $self };
1112 $group->{group} = $self->{groups}->ConnectPublicGroup($group_name);
1113 $group->{_serverprogid} = $self->{_serverprogid};
1114 &Win32::OLE::OPC::_check_error("OPC::Groups::ConnectPublicGroup "
1115 . $self->{_serverprogid});
1116 if (defined($group->{group})) {
1117 return bless $group, 'Win32::OLE::OPC::Group';
1118 } else {
1119 return undef;
1120 }
1121 }
1122
1123 =pod
1124
1125 =item RemovePublicGroup([SERVERHANDLE|NAME})
1126
1127 You remove to a public group using this method. NAME is a string which
1128 identifies the group or SERVERHANDLE is the server handle. Talk about stating
1129 the obvious!
1130
1131 This is untested as I have no server which implements public groups.
1132
1133 =cut
1134
1135 sub RemovePublicGroup {
1136 my $self = shift;
1137 my $ident = shift;
1138
1139 $self->{groups}->RemovePublicGroup($ident);
1140 &Win32::OLE::OPC::_check_error("OPC::Groups::RemovePublicGroup "
1141 . $self->{_serverprogid});
1142 }
1143
1144 =pod
1145
1146 =back
1147
1148 =cut
1149
1150 package Win32::OLE::OPC::Group;
1151 use Carp;
1152
1153 # -----------------------------------------------------------------------------
1154 # OPCGroup object.
1155 #
1156 #-----------------------------------------------------------------------------
1157
1158 =pod
1159
1160 =head2 OPCGroup
1161
1162 The Win32::OLE::OPC::Group object has methods Add, Item, GetOPCGroup and
1163 ConnectPublicGroup which all return a hash blessed into the OPCGroup class.
1164
1165 =over 4
1166
1167 =item Properties
1168
1169 Return a hash indexed by the following properties containing the property
1170 value:
1171
1172 Parent Name IsPublic IsActive IsSubscribed ClientHandle ServerHandle
1173 LocaleID TimeBias DeadBand UpdateRate OPCItems
1174
1175 Note that the hash member indexed 'Parent' is a hash pointing back up to the
1176 parent properties.
1177
1178 =cut
1179
1180 sub Properties {
1181 # Collect everything available into a hash and return it.
1182 my $self = shift;
1183
1184 my %retv; # Return this.
1185 for my $property (qw/Parent Name IsPublic IsActive IsSubscribed ClientHandle
1186 ServerHandle LocaleID TimeBias DeadBand UpdateRate
1187 OPCItems/) {
1188 $retv{$property} = $self->{group}->{$property};
1189 &Win32::OLE::OPC::_check_error("OPC::Group::Properties "
1190 . $self->{_serverprogid},
1191 $Win32::OLE::OPC::dont_show_property_exceptions);
1192 }
1193 return %retv;
1194 }
1195
1196 =pod
1197
1198 =item SetProperty(PROPERTY,VALUE)
1199
1200 Set one of these properties to the value given.
1201
1202 Name IsActive IsSubscribed ClientHandle LocaleID TimeBias DeadBand
1203 UpdateRate
1204
1205 =cut
1206
1207 sub SetProperty {
1208 # Collect everything available into a hash and return it.
1209 my $self = shift;
1210 my $property = shift; # Name of the property to set.
1211 my $value = shift; # The value to put in there.
1212
1213 # Check the name is valid.
1214 my $isvalid = 0;
1215 for my $valid (qw/Name IsActive IsSubscribed ClientHandle LocaleID TimeBias
1216 DeadBand UpdateRate/) {
1217 if ($property eq $valid) {
1218 $isvalid = 1;
1219 last;
1220 }
1221 }
1222 if ($isvalid) {
1223 $self->{group}->{$property} = $value;
1224 &Win32::OLE::OPC::_check_error("OPC::Group::SetProperty "
1225 . $self->{_serverprogid});
1226 } else {
1227 carp
1228 "Attempt to set invalid property '$property' on OPC group "
1229 . $self->{group}->{Name};
1230 }
1231 }
1232
1233 =pod
1234
1235 =item OPCItems
1236
1237 The Win32::OLE::OPC::OPCGroup::OPCItems method returns an OPCItems object
1238 which is blessed into the perl Win32::OLE::OPC::Items class.
1239
1240 =cut
1241
1242 sub OPCItems {
1243 my $self = shift;
1244 my $items = { _parent => $self };
1245 $items->{items} = $self->{group}->{OPCItems};
1246 $items->{_serverprogid} = $self->{_serverprogid};
1247 &Win32::OLE::OPC::_check_error("OPC::Group::OPCItems "
1248 . $self->{_serverprogid});
1249 return bless $items, 'Win32::OLE::OPC::Items';
1250 }
1251
1252 =pod
1253
1254 =back
1255
1256 =cut
1257
1258
1259 # -----------------------------------------------------------------------------
1260 # OPCItems object.
1261 #
1262 #-----------------------------------------------------------------------------
1263 package Win32::OLE::OPC::Items;
1264 use Win32::OLE::Variant;
1265 use Carp;
1266
1267 =pod
1268
1269 =head2 OPCItems
1270
1271 This class contains a collection of OPCItem objects.
1272
1273 =over 4
1274
1275 =item Properties
1276
1277 Return a hash indexed by the following properties containing the property
1278 value:
1279
1280 Parent DefaultRequestedDataType DefaultAccessPath DefaultIsActive Count
1281
1282 Note that the hash member indexed 'Parent' is a hash pointing back up to the
1283 parent properties.
1284
1285 =cut
1286
1287 sub Properties {
1288 # Collect everything available into a hash and return it.
1289 my $self = shift;
1290
1291 my %retv; # Return this.
1292 for my $property (qw/Parent DefaultRequestedDataType DefaultAccessPath
1293 DefaultIsActive Count/) {
1294 $retv{$property} = $self->{items}->{$property};
1295 &Win32::OLE::OPC::_check_error("OPC::Items::Properties "
1296 . $self->{_serverprogid},
1297 $Win32::OLE::OPC::dont_show_property_exceptions);
1298 }
1299 return %retv;
1300 }
1301
1302 =pod
1303
1304 =item SetProperty(PROPERTY,VALUE)
1305
1306 Set one of these properties to the value given.
1307
1308 DefaultRequestedDataType DefaultAccessPath DefaultIsActive Count
1309
1310 =cut
1311
1312 sub SetProperty {
1313 # Collect everything available into a hash and return it.
1314 my $self = shift;
1315 my $property = shift; # Name of the property to set.
1316 my $value = shift; # The value to put in there.
1317
1318 # Check the name is valid.
1319 my $isvalid = 0;
1320 for my $valid (qw/DefaultRequestedDataType DefaultAccessPath
1321 DefaultIsActive Count/) {
1322 if ($property eq $valid) {
1323 $isvalid = 1;
1324 last;
1325 }
1326 }
1327 if ($isvalid) {
1328 $self->{items}->{$property} = $value;
1329 &Win32::OLE::OPC::_check_error("OPC::Items::SetProperty "
1330 . $self->{_serverprogid});
1331 } else {
1332 carp "Attempt to set invalid property '$property' on OPC items";
1333 }
1334 }
1335
1336 =pod
1337
1338 =item Item(NUMBER])
1339
1340 Get item by 1 based index.
1341
1342 =cut
1343
1344 sub Item {
1345 my $self = shift;
1346 my $ident = shift;
1347
1348 my $item = { _parent => $self };
1349 $item->{item} = $self->{items}->Item(int $ident);
1350 $item->{_serverprogid} = $self->{_serverprogid};
1351 &Win32::OLE::OPC::_check_error("OPC::Items::GetOPCItem "
1352 . $self->{_serverprogid});
1353 if (defined($item->{item})) {
1354 return bless $item, 'Win32::OLE::OPC::Item';
1355 } else {
1356 return undef;
1357 }
1358 }
1359
1360 =pod
1361
1362 =item GetOPCItem(SERVERHANDLE)
1363
1364 Get item using the server handle.
1365
1366 =cut
1367
1368 sub GetOPCItem {
1369 my $self = shift;
1370 my $ident = shift;
1371
1372 my $item = { _parent => $self };
1373 $item->{item} = $self->{items}->GetOPCItem($ident);
1374 $item->{_serverprogid} = $self->{_serverprogid};
1375 &Win32::OLE::OPC::_check_error("OPC::Items::GetOPCItem "
1376 . $self->{_serverprogid});
1377 if (defined($item->{item})) {
1378 return bless $item, 'Win32::OLE::OPC::Item';
1379 } else {
1380 return undef;
1381 }
1382 }
1383
1384 =pod
1385
1386 =item AddItem(ITEMID, CLIENTHANDLE)
1387
1388 Add an item identified by ITEMID, CLIENTHANDLE is a value you get back later.
1389
1390 =cut
1391
1392 sub AddItem {
1393 my $self = shift;
1394 my $itemid = shift;
1395 my $client_handle = shift;
1396
1397 $self->{items}->AddItem($itemid, $client_handle);
1398 &Win32::OLE::OPC::_check_error("OPC::Items::AddItem "
1399 . $self->{_serverprogid});
1400 }
1401
1402 =pod
1403
1404 =item AddItems(NUM, ITEMIDS, CLIENTHANDLES)
1405
1406 Add a load of items.
1407
1408 NUM is how many.
1409 ITEMIDS is a reference to an array of itemids.
1410 CLIENTHANDLES is a reference to an array of client handles.
1411
1412 =cut
1413
1414 sub AddItems {
1415 my $self = shift;
1416 my $num = shift;
1417 my $itemids = shift;
1418 my $client_handles = shift;
1419
1420 for (my $i = 0; $i < $num; $i++) {
1421 $self->AddItem($itemids->[$i], $client_handles->[$i]);
1422 }
1423 }
1424
1425 #sub AddItems {
1426 # my $self = shift;
1427 # my $num = shift;
1428 # my $itemids = shift;
1429 # my $client_handles = shift;
1430 #
1431 #
1432 # $self->{items}->AddItems($num, $itemids, $client_handles);
1433 # &Win32::OLE::OPC::_check_error("OPC::Items::AddItems "
1434 # . $self->{_serverprogid});
1435 #
1436 ## # Convert the itemids and client_handles arrays into arrays of variants.
1437 ## my $vclient_handles = Variant(VT_ARRAY|VT_I4, $num);
1438 ## for (my $i = 0; $i < $num; $i++) {
1439 ## $vclient_handles->Put($i, @$client_handles[$i]);
1440 ## }
1441 ## my $vitemids = Variant(VT_ARRAY|VT_VARIANT, $num);
1442 ## for (my $i = 0; $i < $num; $i++) {
1443 ## my $bstr = Variant(VT_BSTR, @$itemids[$i]);
1444 ## $vitemids->Put($i, $bstr);
1445 ## }
1446 ##
1447 ## my $server_handles = Variant(VT_ARRAY|VT_I4|VT_BYREF, []);
1448 ## my $errors = Variant(VT_ARRAY|VT_I4|VT_BYREF, []);
1449 ##
1450 ## $self->{items}->AddItems($num, $vitemids, $vclient_handles,
1451 ## $server_handles, $errors);
1452 #}
1453
1454 =pod
1455
1456 =item Remove(SERVERHANDLES)
1457
1458 Removes the items in SERVERHANDLES.
1459
1460 =cut
1461
1462 sub Remove {
1463 my $self = shift;
1464 my $handles = shift;
1465
1466 my $num = $#$handles+1;
1467 my $Errors = Variant(VT_ARRAY|VT_I4|VT_BYREF, [1, $num+1]);
1468 my $vhandles = Variant(VT_ARRAY|VT_I4, [1, $num+1]);
1469 for (my $i = 1; $i < $num+1; $i++) {
1470 $vhandles->Put($i, @$handles[$i-1]);
1471 }
1472 $self->{items}->Remove($num, $vhandles, $Errors);
1473 &Win32::OLE::OPC::_check_error("OPC::Items::Remove "
1474 . $self->{_serverprogid});
1475 }
1476
1477 =pod
1478
1479 =back
1480
1481 =cut
1482
1483 # -----------------------------------------------------------------------------
1484 # OPCItem object.
1485 #
1486 #-----------------------------------------------------------------------------
1487 package Win32::OLE::OPC::Item;
1488 use Win32::OLE::Variant;
1489 use Carp;
1490
1491 =pod
1492
1493 =head2 OPCItem
1494
1495 This is the object used for reading and writing actual values.
1496
1497 =over 4
1498
1499 =item Properties
1500
1501 Return a hash indexed by the following properties containing the property
1502 value:
1503
1504 Parent ClientHandle ServerHandle AccessPath AccessRights ItemID IsActive
1505 RequestedDataType Value Quality TimeStamp CanonicalDataType EUType EUInfo
1506
1507 Note that the hash member indexed 'Parent' is a hash pointing back up to the
1508 parent properties.
1509
1510 =cut
1511
1512 sub Properties {
1513 # Collect everything available into a hash and return it.
1514 my $self = shift;
1515
1516 my %retv; # Return this.
1517 for my $property (qw/Parent ClientHandle ServerHandle AccessPath
1518 AccessRights ItemID IsActive RequestedDataType Value
1519 Quality TimeStamp CanonicalDataType EUType EUInfo/) {
1520 $retv{$property} = $self->{item}->{$property};
1521 &Win32::OLE::OPC::_check_error("OPC::Item::Properties "
1522 . $self->{_serverprogid},
1523 $Win32::OLE::OPC::show_property_exceptions);
1524 }
1525 return %retv;
1526 }
1527
1528 =pod
1529
1530 =item Read(SOURCE)
1531
1532 Read the value for this item. SOURCE is either $OPCCache or $OPCDevice, each
1533 of which is exported by OPC.pm by default.
1534
1535 Read returns a hash reference which contains Value, Quality and TimeStamp
1536 values.
1537
1538 =cut
1539
1540 sub Read {
1541 my $self = shift;
1542 my $source = shift;
1543
1544 my $value = Variant(VT_VARIANT|VT_BYREF, 0);
1545 my $quality = Variant(VT_VARIANT|VT_BYREF, 0);
1546 my $timestamp = Variant(VT_VARIANT|VT_BYREF, 0);
1547
1548 $self->{item}->Read($source+0, $value, $quality, $timestamp);
1549 &Win32::OLE::OPC::_check_error("OPC::Item::Read " . $self->{_serverprogid});
1550 return {Value => $value->Get(),
1551 Quality => $quality->Get(),
1552 TimeStamp => $timestamp->Get()};
1553 }
1554
1555 =pod
1556
1557 =item Write(VALUE)
1558
1559 Write VALUE to this item.
1560
1561 =cut
1562
1563 sub Write {
1564 my $self = shift;
1565 my $value = Variant(VT_VARIANT|VT_BYREF, shift);
1566
1567 $self->{item}->Write($value);
1568 &Win32::OLE::OPC::_check_error("OPC::Item::Write " . $self->{_serverprogid});
1569 }
1570
1571 =pod
1572
1573 =item ServerHandle
1574
1575 Returns the items server handle.
1576
1577 =cut
1578
1579 sub ServerHandle {
1580 my $self = shift;
1581 return $self->{item}->{ServerHandle};
1582 }
1583
1584 =pod
1585
1586 =back
1587
1588 =cut
1589
1590 1;
1591 __END__
↑ Summary