| 1 | # SNC::Manipulator | ||
| 2 | # | ||
| 3 | # Copyright (c) 2004-2005 Charles Morris | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # This program is free software; you can redistribute it and/or | ||
| 7 | # modify it under the same terms as Perl itself. | ||
| 8 | # | ||
| 9 | |||
| 10 | package SNC::Manipulator; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | use HTTP::Request::Common; | ||
| 14 | use LWP::UserAgent; | ||
| 15 | $VERSION = '0.30'; | ||
| 16 | } | ||
| 17 | |||
| 18 | #---------- Constructor ----------# | ||
| 19 | sub new { | ||
| 20 | my ($pkg, $SNCip, $SNCport) = @_; | ||
| 21 | my ($ip, $port); | ||
| 22 | |||
| 23 | my $instance = bless( {}, $pkg ); | ||
| 24 | |||
| 25 | if ($SNCport) { $port = $SNCport; } else { $port = 80; } | ||
| 26 | if ($SNCip) | ||
| 27 | { | ||
| 28 | $ip = $SNCip; | ||
| 29 | } | ||
| 30 | else | ||
| 31 | { | ||
| 32 | print STDERR "No IP was given."; | ||
| 33 | $ip = "127.0.0.1"; | ||
| 34 | } | ||
| 35 | |||
| 36 | $instance{ip} = $ip; | ||
| 37 | $instance{port} = $port; | ||
| 38 | $instance{url} = 'http://' . $ip . ':' . $port; | ||
| 39 | $instance{rawurl} = $ip . ':' . $port; | ||
| 40 | $instance{delay} = 500; #ms delay for things like move or zoom | ||
| 41 | $instance{authzone} = 'Sony Network Camera SNC-RZ30'; | ||
| 42 | |||
| 43 | $instance{UserAgent} = new LWP::UserAgent; | ||
| 44 | |||
| 45 | return $instance; | ||
| 46 | } | ||
| 47 | |||
| 48 | ########################## | ||
| 49 | #--- object functions ---# | ||
| 50 | |||
| 51 | # setDelay() | ||
| 52 | # sets the millisecond delay between commands. | ||
| 53 | sub setDelay { | ||
| 54 | my ($instance, $delay) = @_; | ||
| 55 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 56 | |||
| 57 | $instance{delay} = $delay; | ||
| 58 | |||
| 59 | return $instance; | ||
| 60 | } | ||
| 61 | |||
| 62 | ########################### | ||
| 63 | #--- inquiry functions ---# | ||
| 64 | |||
| 65 | sub inquiry() | ||
| 66 | { | ||
| 67 | my ($instance, $inqjs) = @_; | ||
| 68 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 69 | |||
| 70 | my (%ret); | ||
| 71 | |||
| 72 | foreach my $line ( split(/\n/, &sendRequest($instance, 'inquiry', $inqjs)) ) | ||
| 73 | { | ||
| 74 | my @fields = split(/\s+/, $line, 2); | ||
| 75 | if ( $fields[0] eq 'var' ) | ||
| 76 | { | ||
| 77 | my ($key, $encap_val) = split(/=/, $fields[1]); | ||
| 78 | |||
| 79 | $val = substr( $encap_val, 1, length($encap_val)-2 ); #get rid of the extra quotes | ||
| 80 | |||
| 81 | $ret{$key} = $val; | ||
| 82 | $instance{$inqjs}{$key} = $val; | ||
| 83 | #this is like $instance{sysinfo}{TitleBar} = "Problem Solving Lab"; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | return %ret; | ||
| 88 | } | ||
| 89 | |||
| 90 | ############################ | ||
| 91 | #--- movement functions ---# | ||
| 92 | |||
| 93 | # move() | ||
| 94 | # moves teh camrea. | ||
| 95 | sub move { | ||
| 96 | my ($instance, $moveinst) = @_; | ||
| 97 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 98 | |||
| 99 | $moveinst = substr($moveinst, 0, 1); | ||
| 100 | |||
| 101 | if ( $moveinst =~ /U/i ) | ||
| 102 | { | ||
| 103 | &sendRequest($instance, 'visca', '8101060103030302FF'); | ||
| 104 | &customDelay($instance); | ||
| 105 | &stop($instance); | ||
| 106 | } | ||
| 107 | elsif ( $moveinst =~ /D/i ) | ||
| 108 | { | ||
| 109 | &sendRequest($instance, 'visca', '8101060103030301FF'); | ||
| 110 | &customDelay($instance); | ||
| 111 | &stop($instance); | ||
| 112 | } | ||
| 113 | elsif ( $moveinst =~ /L/i ) | ||
| 114 | { | ||
| 115 | &sendRequest($instance, 'visca', '8101060103030203FF'); | ||
| 116 | &customDelay($instance); | ||
| 117 | &stop($instance); | ||
| 118 | } | ||
| 119 | elsif ( $moveinst =~ /R/i ) | ||
| 120 | { | ||
| 121 | &sendRequest($instance, 'visca', '8101060103030103FF'); | ||
| 122 | &customDelay($instance); | ||
| 123 | &stop($instance); | ||
| 124 | } | ||
| 125 | elsif ( $moveinst =~ /H/i ) | ||
| 126 | { | ||
| 127 | &sendRequest($instance, 'visca', '81010604FF'); | ||
| 128 | } | ||
| 129 | elsif ( $moveinst =~ /I/i ) | ||
| 130 | { | ||
| 131 | &sendRequest($instance, 'visca', '8101040724FF'); | ||
| 132 | &customDelay($instance); | ||
| 133 | &stop($instance); | ||
| 134 | } | ||
| 135 | elsif ( $moveinst =~ /O/i ) | ||
| 136 | { | ||
| 137 | &sendRequest($instance, 'visca', '8101040734FF'); | ||
| 138 | &customDelay($instance); | ||
| 139 | &stop($instance); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | # moveDirectPT() | ||
| 144 | # moves camera by direct Pan/Tilt data | ||
| 145 | sub moveDirectPT { | ||
| 146 | my ($instance, $x, $y) = @_; | ||
| 147 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 148 | |||
| 149 | if ( $x > 640 || $x < 0 || $y > 480 || $y < 0) | ||
| 150 | { | ||
| 151 | print "$x,$y needs to be inside 640x480 viewspace\n"; | ||
| 152 | return undef; | ||
| 153 | } | ||
| 154 | |||
| 155 | &sendRequest($instance, 'directpt', $x . ',' . $y); | ||
| 156 | } | ||
| 157 | |||
| 158 | #only in >v1.10 | ||
| 159 | sub moveRelative { | ||
| 160 | my ($instance, $moveDirection, $movePercentage) = @_; | ||
| 161 | |||
| 162 | print "$moveDirection : $movePercentage\n"; | ||
| 163 | |||
| 164 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 165 | |||
| 166 | #for all those wondering, the $AABB variable refrences the tech documents | ||
| 167 | |||
| 168 | if ( $moveDirection =~ /^S.?.?.?.?W.?.?.?$/i ) { $AABB = '01'; } | ||
| 169 | elsif ( $moveDirection =~ /^S.?.?.?.?$/i ) { $AABB = '02'; } | ||
| 170 | elsif ( $moveDirection =~ /^S.?.?.?.?E.?.?.?$/i ) { $AABB = '03'; } | ||
| 171 | elsif ( $moveDirection =~ /^W.?.?.?$/i ) { $AABB = '04'; } | ||
| 172 | elsif ( $moveDirection =~ /^E.?.?.?$/i ) { $AABB = '06'; } | ||
| 173 | elsif ( $moveDirection =~ /^N.?.?.?.?W.?.?.?$/i ) { $AABB = '07'; } | ||
| 174 | elsif ( $moveDirection =~ /^N.?.?.?.?$/i ) { $AABB = '08'; } | ||
| 175 | elsif ( $moveDirection =~ /^N.?.?.?.?E.?.?.?$/i ) { $AABB = '09'; } | ||
| 176 | else | ||
| 177 | { | ||
| 178 | print STDERR "$moveDirection is not a valid compass direction.\n"; | ||
| 179 | print STDERR "can take full or abbreviated directions.\n"; | ||
| 180 | return undef; | ||
| 181 | } | ||
| 182 | |||
| 183 | if ( $movePercentage eq '10' ) { $AABB = '01'; } | ||
| 184 | elsif ( $movePercentage eq '15' ) { $AABB = '02'; } | ||
| 185 | elsif ( $movePercentage eq '20' ) { $AABB = '03'; } | ||
| 186 | elsif ( $movePercentage eq '25' ) { $AABB = '04'; } | ||
| 187 | elsif ( $movePercentage eq '30' ) { $AABB = '05'; } | ||
| 188 | elsif ( $movePercentage eq '40' ) { $AABB = '06'; } | ||
| 189 | elsif ( $movePercentage eq '50' ) { $AABB = '07'; } | ||
| 190 | elsif ( $movePercentage eq '66.7' ) { $AABB = '08'; } | ||
| 191 | elsif ( $movePercentage eq '83.3' ) { $AABB = '08'; } | ||
| 192 | elsif ( $movePercentage eq '100' ) { $AABB = '04'; } | ||
| 193 | else | ||
| 194 | { | ||
| 195 | print STDERR "$movePercentage is not a valid relative move percentage..\n"; | ||
| 196 | print STDERR "try: 10,15,20,25,30,40,50,66.7,83.3, or 100.\n"; | ||
| 197 | return undef; | ||
| 198 | } | ||
| 199 | |||
| 200 | &sendRequest( $instance, 'relative', $AABB ); | ||
| 201 | |||
| 202 | return undef; | ||
| 203 | } | ||
| 204 | |||
| 205 | # moveToPreset() | ||
| 206 | # this function causes the camera to reset to one of its programmed presets. | ||
| 207 | sub moveToPreset { | ||
| 208 | my ($instance, $preset) = @_; | ||
| 209 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 210 | if ( length($preset) < 2 ) { $preset = '0' . $preset; } #buffer preset if needed | ||
| 211 | &sendRequest( $instance, 'visca', '8101043F02' . $preset . 'FF'); | ||
| 212 | #8101043F0201FF | ||
| 213 | return $instance; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub stop { | ||
| 217 | my ($instance) = shift; | ||
| 218 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 219 | &sendRequest( $instance, 'visca', '8101060103030303FF' ); | ||
| 220 | return $instance; | ||
| 221 | } | ||
| 222 | |||
| 223 | # rawVISCA() | ||
| 224 | # for use by people who know how to build a VISCA statement. | ||
| 225 | sub rawVISCA | ||
| 226 | { | ||
| 227 | my ($instance, $visca) = @_; | ||
| 228 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 229 | &sendRequest($instance, 'visca', $visca); | ||
| 230 | } | ||
| 231 | |||
| 232 | ########################### | ||
| 233 | #--- capture functions ---# | ||
| 234 | |||
| 235 | sub capture() | ||
| 236 | { | ||
| 237 | my ($instance) = @_; | ||
| 238 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 239 | |||
| 240 | my $resp = $instance{UserAgent}->get( $instance{url} . '/oneshotimage.jpg' ); | ||
| 241 | |||
| 242 | return $resp->content; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub captureToFile() | ||
| 246 | { | ||
| 247 | my ($instance, $file) = @_; | ||
| 248 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 249 | |||
| 250 | my $resp = $instance{UserAgent}->get( $instance{url} . '/oneshotimage.jpg' ); | ||
| 251 | |||
| 252 | open( BINOUT, ">$file" ); | ||
| 253 | binmode(BINOUT); | ||
| 254 | |||
| 255 | print BINOUT $resp->content; | ||
| 256 | |||
| 257 | close (BINOUT); | ||
| 258 | } | ||
| 259 | |||
| 260 | sub captureStream() | ||
| 261 | { | ||
| 262 | my ($instance, $number, $paramtype, $param) = @_; | ||
| 263 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 264 | |||
| 265 | if ( $number >= 1 && $number <= 1000000 ) | ||
| 266 | { | ||
| 267 | $args = "?number=$number"; | ||
| 268 | } | ||
| 269 | else | ||
| 270 | { | ||
| 271 | print STDERR "Invalid argument ($number) for 'number' parameter\n"; | ||
| 272 | print STDERR "Number of frames must be between 1 and 1000000.\n"; | ||
| 273 | return undef; | ||
| 274 | } | ||
| 275 | |||
| 276 | if ( $paramtype ) | ||
| 277 | { | ||
| 278 | if ( $paramtype eq 'speed' ) | ||
| 279 | { | ||
| 280 | #speed parameters, | ||
| 281 | # 0 - fastest | ||
| 282 | # 1 - 1 frame/sec | ||
| 283 | # 2 - 2 | ||
| 284 | # 3 - 3 | ||
| 285 | # 4 - 4 | ||
| 286 | # 5 - 5 | ||
| 287 | # 6 - 6 | ||
| 288 | # 8 - 8 | ||
| 289 | # 10 - 10 NTSC model | ||
| 290 | # 10 - 12 PAL model | ||
| 291 | # 20 - 15 NTSC model | ||
| 292 | # 20 - 16 PAL model | ||
| 293 | # 20 - 20 | ||
| 294 | # 30 - 25 NTSC modelk | ||
| 295 | |||
| 296 | if ( | ||
| 297 | $param eq '0' || $param eq '1' || | ||
| 298 | $param eq '2' || $param eq '3' || | ||
| 299 | $param eq '4' || $param eq '5' || | ||
| 300 | $param eq '6' || $param eq '8' || | ||
| 301 | $param eq '10' || $param eq '20' || | ||
| 302 | $param eq '30' | ||
| 303 | ) | ||
| 304 | { | ||
| 305 | $args .= '&speed=' . $param; | ||
| 306 | } | ||
| 307 | else | ||
| 308 | { | ||
| 309 | print STDERR "Invalid argument ($param) for 'speed' parameter\n"; | ||
| 310 | print STDERR "try 0,1,2,3,4,5,6,8,10,20, or 30\n"; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | elsif ( $paramtype eq 'interval' ) # 2.0 or higher software only | ||
| 314 | { | ||
| 315 | #check version here | ||
| 316 | if ( $param >= 40 && $param <= 3600000 ) | ||
| 317 | { | ||
| 318 | $args .= '&interval=' . $param; | ||
| 319 | } | ||
| 320 | else | ||
| 321 | { | ||
| 322 | print STDERR "Invalid argument ($param) for 'interval' parameter\n"; | ||
| 323 | print STDERR "Integers from 40 to 3,600,000 are valid.\n"; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | my $resp = $instance{UserAgent}->get( $instance{url} . '/image' . $args ); | ||
| 329 | |||
| 330 | return $resp->content; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub captureStreamToFile() | ||
| 334 | { | ||
| 335 | my ($instance, $file, $number, $paramtype, $param) = @_; | ||
| 336 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 337 | |||
| 338 | open( BINOUT, ">$file" ); | ||
| 339 | binmode(BINOUT); | ||
| 340 | print BINOUT captureStream( $number, $paramtype, $param ); | ||
| 341 | close (BINOUT); | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | ######################### | ||
| 347 | #--- other functions ---# | ||
| 348 | |||
| 349 | # setLogin() | ||
| 350 | # simply username & pass to bypass the password protection on some cameras | ||
| 351 | sub setLogin | ||
| 352 | { | ||
| 353 | my ($instance, $username, $password) = @_; | ||
| 354 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 355 | |||
| 356 | $instance{username} = $username; | ||
| 357 | $instance{password} = $password; | ||
| 358 | |||
| 359 | #this is an OO method ( called after new() ) so we know all these vars are set. | ||
| 360 | $instance{UserAgent}->credentials( | ||
| 361 | $instance{rawurl}, | ||
| 362 | $instance{authzone}, | ||
| 363 | $instance{username}, | ||
| 364 | $instance{password} | ||
| 365 | ); | ||
| 366 | } | ||
| 367 | |||
| 368 | # sendRequest() is used to make the other functions look alot simpler and make the module smaller. | ||
| 369 | # it simply formats and sends the http request to the camera. | ||
| 370 | sub sendRequest { | ||
| 371 | my ($instance, $type, $code) = @_; | ||
| 372 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 373 | |||
| 374 | # warn "$type : $code\n"; | ||
| 375 | |||
| 376 | if ( $type eq 'visca' ) | ||
| 377 | { | ||
| 378 | my $formurl = $instance{url} . '/command/visca-ptzf.cgi'; #v1.09 or lower | ||
| 379 | my $req = HTTP::Request->new(GET => $formurl . '?visca=' . $code); | ||
| 380 | return $instance{UserAgent}->request($req)->as_string(); | ||
| 381 | } | ||
| 382 | elsif( $type eq 'directpt' ) | ||
| 383 | { | ||
| 384 | my $formurl = $instance{url} . '/command/directpt.cgi'; #v1.09 or lower | ||
| 385 | my $req = HTTP::Request->new(GET => $formurl . '?visca=' . $code); | ||
| 386 | return $instance{UserAgent}->request($req)->as_string(); | ||
| 387 | } | ||
| 388 | elsif( $type eq 'relative' ) | ||
| 389 | { #or maybe just pztf.cgi | ||
| 390 | my $formurl = $instance{url} . '/command/visca-pztf.cgi'; #v1.10 or higher | ||
| 391 | my $req = HTTP::Request->new(GET => $formurl . '?relative=' . $code); | ||
| 392 | return $instance{UserAgent}->request($req)->as_string(); | ||
| 393 | } | ||
| 394 | elsif( $type eq 'inquiry' ) | ||
| 395 | { | ||
| 396 | my $formurl = $instance{url} . '/command/inquiry.cgi'; #v1.10 or higher | ||
| 397 | my $req = HTTP::Request->new(GET => $formurl . '?inqjs=' . $code); | ||
| 398 | print "$formurl" . '?inqjs=' . $code . "\n"; | ||
| 399 | return $instance{UserAgent}->request($req)->as_string(); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | sub customDelay() | ||
| 404 | { | ||
| 405 | my ($instance) = @_; | ||
| 406 | die "expecting a __PACKAGE__\n" unless $instance->isa(__PACKAGE__); | ||
| 407 | sleep 0.001 * $instance{delay}; #do my millisecond delay for (pseudo)precise movements | ||
| 408 | } | ||
| 409 | |||
| 410 | 1; | ||
| 411 | |||
| 412 | __END__ |