| 1 | package Bosch::RCPPlus::Commands; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use POSIX qw(floor); | ||
| 5 | use Bosch::RCPPlus::Utils qw(bytes2int); | ||
| 6 | |||
| 7 | our %Type = ( | ||
| 8 | flag => 'F_FLAG', | ||
| 9 | unicode => 'P_UNICODE', | ||
| 10 | octect => 'P_OCTET', | ||
| 11 | word => 'T_DWORD', | ||
| 12 | ); | ||
| 13 | |||
| 14 | our %Direction = ( | ||
| 15 | read => 'READ', | ||
| 16 | write => 'WRITE', | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub ptz_available | ||
| 20 | { | ||
| 21 | my ($num) = @_; | ||
| 22 | |||
| 23 | return ( | ||
| 24 | command => '0x0a51', | ||
| 25 | type => $Type{flag}, | ||
| 26 | drection => $Direction{read}, | ||
| 27 | num => $num || 1, | ||
| 28 | ); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub name | ||
| 32 | { | ||
| 33 | my ($num) = @_; | ||
| 34 | |||
| 35 | return ( | ||
| 36 | command => '0x0019', | ||
| 37 | type => $Type{unicode}, | ||
| 38 | drection => $Direction{read}, | ||
| 39 | num => $num || 1, | ||
| 40 | ); | ||
| 41 | } | ||
| 42 | |||
| 43 | ##* | ||
| 44 | # -1 <= evt.pan <= 1 | ||
| 45 | # -1 <= evt.tilt <= 1 | ||
| 46 | # Zoom: -1 / +1 | ||
| 47 | # | ||
| 48 | # See code from Js UI: | ||
| 49 | # - function sendPTZCommand() | ||
| 50 | # - PTZControllerEventTransmitter::sendPTZ | ||
| 51 | sub ptz | ||
| 52 | { | ||
| 53 | my ($pan, $tilt, $zoom, $num) = @_; | ||
| 54 | |||
| 55 | # BicomPTZController.alignPTZ = function (evt) { | ||
| 56 | # // -1 <= evt.pan <= 1 | ||
| 57 | # // -1 <= evt.tilt <= 1 | ||
| 58 | # // Zoom: -1 / +1 | ||
| 59 | # var p = (-1 * Math.round(evt.pan * 15)); | ||
| 60 | # var t = Math.round(evt.tilt * 15); | ||
| 61 | # var z = -1 * Math.round(evt.zoom * 7); | ||
| 62 | # if (evt.zoom === -999) { | ||
| 63 | # z = -999; | ||
| 64 | # } | ||
| 65 | # return new PTZObject(p, t, z); | ||
| 66 | #}; | ||
| 67 | $pan = int(sprintf("%.0f", $pan * 15)); | ||
| 68 | $tilt = int(sprintf("%.0f", $tilt * 15)); | ||
| 69 | $zoom = -1 * int(sprintf("%.0f", $zoom * 7)); | ||
| 70 | |||
| 71 | # BicomPTZController.prototype.sendPTZ | ||
| 72 | $pan = abs($pan) + 0x80 if ($pan < 0); | ||
| 73 | $tilt = abs($tilt) + 0x80 if ($tilt < 0); | ||
| 74 | $zoom = abs($zoom) + 0x80 if ($zoom < 0); | ||
| 75 | |||
| 76 | return ( | ||
| 77 | num => $num || 1, | ||
| 78 | direction => $Direction{write}, | ||
| 79 | command => '0x09A5', | ||
| 80 | type => $Type{octect}, | ||
| 81 | # nbrArrayToString | ||
| 82 | payload => '0x80' . '0006' . '0110' . '85' . sprintf("%02X%02X%02X", $pan, $tilt, $zoom), | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub zoom_in | ||
| 87 | { | ||
| 88 | my ($num) = @_; | ||
| 89 | return ptz(0, 0, 1, $num); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub zoom_out | ||
| 93 | { | ||
| 94 | my ($num) = @_; | ||
| 95 | return ptz(0, 0, -1, $num); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub ptz_stop | ||
| 99 | { | ||
| 100 | my ($num) = @_; | ||
| 101 | return ptz(0, 0, 0, $num); | ||
| 102 | } | ||
| 103 | |||
| 104 | ##* | ||
| 105 | # 1 <= $preset <= 6 | ||
| 106 | # | ||
| 107 | # See code from Js UI: | ||
| 108 | # - PresetSupport::checkPreset2 | ||
| 109 | # - RCP.readBicom(0x6, 0x2001 + ((preset - 1) << 4), { bicomtype: 'NUMBER' }) | ||
| 110 | # | ||
| 111 | sub check_preset | ||
| 112 | { | ||
| 113 | my ($preset, $num) = @_; | ||
| 114 | # RCP.readBicom(0x6, 0x2001 + ((preset - 1) << 4), { bicomtype: 'NUMBER' }) | ||
| 115 | my $bitcom_obj_id = sprintf('%04X', 0x2001 + (($preset - 1) << 4)); | ||
| 116 | |||
| 117 | return ( | ||
| 118 | num => $num || 0, | ||
| 119 | direction => $Direction{write}, | ||
| 120 | command => '0x09A5', | ||
| 121 | type => $Type{octect}, | ||
| 122 | payload => '0x85' . '0006' . $bitcom_obj_id . '01', | ||
| 123 | format => sub { | ||
| 124 | # var processAnswer = function(res, conf) { // type: NUMBER | ||
| 125 | my @payload = @{(shift)}; | ||
| 126 | |||
| 127 | # payload.splice(1, 6) //leasetime | ||
| 128 | splice @payload, 1, 6 if (($payload[0] & 0x08) > 0); | ||
| 129 | |||
| 130 | if ($payload[5] == 0x6f) { | ||
| 131 | # payload.splice(0, 6); | ||
| 132 | # ERROR | ||
| 133 | # return error; | ||
| 134 | return undef; | ||
| 135 | } | ||
| 136 | |||
| 137 | splice @payload, 0, 6; | ||
| 138 | |||
| 139 | # val = DataHelper.nbrArrayToNbr(a, false); | ||
| 140 | my $back = 0; | ||
| 141 | for (my $i; $i <= $#payload; $i++) { | ||
| 142 | my $idx = (scalar @payload) - 1 - $i; | ||
| 143 | my $val = $payload[$idx]; | ||
| 144 | |||
| 145 | # back += val<<(i*8) | ||
| 146 | $back += $val * (256 ** $i); | ||
| 147 | } | ||
| 148 | |||
| 149 | if (($payload[0] & 0x80) > 0) { | ||
| 150 | # 1st bit is set --> negative number | ||
| 151 | my $intMax = (2 ** (scalar @payload) * 8) - 1; | ||
| 152 | $back = ($intMax - $back + 1) * (-1); | ||
| 153 | } | ||
| 154 | |||
| 155 | return $back; | ||
| 156 | }, | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | ##* | ||
| 161 | # 1 <= $preset <= 6 | ||
| 162 | # | ||
| 163 | # See code from Js UI: | ||
| 164 | # - PresetSupport::isPresetMapped | ||
| 165 | # | ||
| 166 | sub is_preset_mapped | ||
| 167 | { | ||
| 168 | my ($preset, $num) = @_; | ||
| 169 | # RCP.readBicom(0x6, 0x0212, { rcpnum: data.rcpnum, sessionid: data.sessionid, bicomtype: "BYTES" }) | ||
| 170 | |||
| 171 | return ( | ||
| 172 | num => $num || 0, | ||
| 173 | direction => $Direction{write}, | ||
| 174 | command => '0x09A5', | ||
| 175 | type => $Type{octect}, | ||
| 176 | payload => '0x85' . '0006' . '0212' . '01', | ||
| 177 | format => sub { | ||
| 178 | # -> fixByteOrderForMapping | ||
| 179 | # function fixByteOrderForMapping(b) { | ||
| 180 | # var back = [], pos = 0; | ||
| 181 | # while(b.length>=pos+4) { | ||
| 182 | # back.push(b[pos+3]); | ||
| 183 | # back.push(b[pos+2]); | ||
| 184 | # back.push(b[pos+1]); | ||
| 185 | # back.push(b[pos]); | ||
| 186 | # pos += 4; | ||
| 187 | # } | ||
| 188 | # return back; | ||
| 189 | # } | ||
| 190 | my @payload = @{(shift)}; | ||
| 191 | my $length = scalar @payload; | ||
| 192 | my @bytes; | ||
| 193 | my $pos = 0; | ||
| 194 | |||
| 195 | while ($length >= $pos + 4) { | ||
| 196 | push @bytes, $payload[$pos + 3]; | ||
| 197 | push @bytes, $payload[$pos + 2]; | ||
| 198 | push @bytes, $payload[$pos + 1]; | ||
| 199 | push @bytes, $payload[$pos]; | ||
| 200 | $pos += 4; | ||
| 201 | } | ||
| 202 | |||
| 203 | # idx - 1 | ||
| 204 | # (bytes[Math.floor(idx / 8)] & Math.pow(2, idx % 8)) > 0 | ||
| 205 | if ($preset) { | ||
| 206 | my $idx = $preset - 1; | ||
| 207 | return $bytes[floor($idx / 8)] & (2 ** ($idx % 8)); | ||
| 208 | } | ||
| 209 | return \@bytes; | ||
| 210 | }, | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | |||
| 214 | ##* | ||
| 215 | # 1 <= $preset <= 6 | ||
| 216 | # | ||
| 217 | # See code from Js UI: | ||
| 218 | # - PresetSupport::setScene | ||
| 219 | # | ||
| 220 | sub set_scene | ||
| 221 | { | ||
| 222 | my ($preset, $num) = @_; | ||
| 223 | # $.rcp.doRequest(PRESET, { // { bicomflags: 128, bicomserver: 6, noglobalfinish: true } | ||
| 224 | # bicomobjid: 0x2000 + ((num - 1) << 4), | ||
| 225 | # bicomaction: 0x80, | ||
| 226 | # idstring: 'setScene' + num, | ||
| 227 | # rcpnum: sessionid > 0 ? 0 : getNum(), | ||
| 228 | # sessionid: sessionid > 0 ? sessionid : null, | ||
| 229 | # callback: function(val, conf, error) { | ||
| 230 | # if (error) { | ||
| 231 | # printError(conf, error); | ||
| 232 | # } | ||
| 233 | # } | ||
| 234 | # }); | ||
| 235 | my $bitcom_obj_id = sprintf('%04X', 0x2000 + (($preset - 1) << 4)); | ||
| 236 | |||
| 237 | return ( | ||
| 238 | num => $num || 0, | ||
| 239 | direction => $Direction{write}, | ||
| 240 | command => '0x09A5', | ||
| 241 | type => $Type{octect}, | ||
| 242 | idstring => 'setScene' . $preset, | ||
| 243 | payload => '0x80' . '0006' . $bitcom_obj_id . '80', | ||
| 244 | ); | ||
| 245 | } | ||
| 246 | |||
| 247 | ##* | ||
| 248 | # 1 <= $preset <= 6 | ||
| 249 | # | ||
| 250 | # See code from Js UI: | ||
| 251 | # - PresetSupport::showScene | ||
| 252 | # | ||
| 253 | sub show_scene | ||
| 254 | { | ||
| 255 | my ($preset, $num) = @_; | ||
| 256 | # bicomobjid: 0x2000 + ((num - 1) << 4) | ||
| 257 | my $bitcom_obj_id = sprintf('%04X', 0x2000 + (($preset - 1) << 4)); | ||
| 258 | |||
| 259 | return ( | ||
| 260 | num => $num || 0, | ||
| 261 | direction => $Direction{write}, | ||
| 262 | command => '0x09A5', | ||
| 263 | type => $Type{octect}, | ||
| 264 | payload => '0x80' . '0006' . $bitcom_obj_id . '81', | ||
| 265 | idstring => 'setScene' . $preset, | ||
| 266 | ); | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | ##* | ||
| 271 | # This command is used on the UI to check AUTH | ||
| 272 | # (aparently there are different users levels) | ||
| 273 | # | ||
| 274 | sub one | ||
| 275 | { | ||
| 276 | my ($num) = @_; | ||
| 277 | |||
| 278 | return ( | ||
| 279 | num => $num || 1, | ||
| 280 | direction => $Direction{read}, | ||
| 281 | command => '0x0001', | ||
| 282 | type => $Type{word}, | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | |||
| 286 | ##* | ||
| 287 | # | ||
| 288 | # See code from Js UI: | ||
| 289 | # - PresetSupport::getAvailableScenes | ||
| 290 | # | ||
| 291 | sub available_scenes | ||
| 292 | { | ||
| 293 | my ($num) = @_; | ||
| 294 | |||
| 295 | return ( | ||
| 296 | num => $num || 1, | ||
| 297 | direction => $Direction{write}, | ||
| 298 | command => '0x09A5', | ||
| 299 | type => $Type{octect}, | ||
| 300 | payload => '0x85' . '0006' . '2000' . '84', | ||
| 301 | idstring => 'availableScenes', | ||
| 302 | format => sub { | ||
| 303 | my @payload = @{(shift)}; | ||
| 304 | |||
| 305 | # payload.splice(1, 6) //leasetime | ||
| 306 | splice @payload, 1, 6 if (($payload[0] & 0x08) > 0); | ||
| 307 | |||
| 308 | if ($payload[5] == 0x6f) { | ||
| 309 | # payload.splice(0, 6); | ||
| 310 | # ERROR | ||
| 311 | # return error; | ||
| 312 | return undef; | ||
| 313 | } | ||
| 314 | |||
| 315 | splice @payload, 0, 6; | ||
| 316 | |||
| 317 | # Implemented for 0x84 bitcom action (might be used for 0x82 too) | ||
| 318 | my $is82 = 0; | ||
| 319 | my @b; | ||
| 320 | my @available; | ||
| 321 | my $l = $is82 ? 16 : 32; | ||
| 322 | |||
| 323 | if ((scalar @payload) >= $l) { | ||
| 324 | my $length = $is82 ? 4 : 8; | ||
| 325 | for (my $i = 0; $i < $length; $i++) { | ||
| 326 | my @current = @payload[($i * 4) .. ($i * 4 + 3)]; | ||
| 327 | |||
| 328 | $b[$i] = bytes2int(\@current, 1); | ||
| 329 | my $offset = ($is82 && $i == 0) ? 1 : 0; # 1st bit of 1st byte is never set for bicom action 82 | ||
| 330 | |||
| 331 | for (my $j = 0; ($j + $offset) < 32; $j++) { | ||
| 332 | if ($b[$i] & (1 << ($j + $offset))) { | ||
| 333 | my $idx = ($i * 32) + $j; | ||
| 334 | $idx++ if (!$is82 || $i == 0); | ||
| 335 | push @available, $idx; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | return \@available; | ||
| 342 | }, | ||
| 343 | ); | ||
| 344 | } | ||
| 345 | |||
| 346 | 1; |