~alanbell/+junk/camcontrol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/env python
import urllib
import time
from logindetails import username,password,host,port
#bunch of constants for commands we can set to decoder_control
PTZ_STOP=1;
TILT_UP=0;
TILT_UP_STOP=1;
TILT_DOWN=2;
TILT_DOWN_STOP=3;
PAN_LEFT=4;
PAN_LEFT_STOP=5;
PAN_RIGHT=6;
PAN_RIGHT_STOP=7;
PTZ_LEFT_UP=90;
PTZ_RIGHT_UP=91;
PTZ_LEFT_DOWN=92;
PTZ_RIGHT_DOWN=93;
PTZ_CENTER=25;
PTZ_VPATROL=26;
PTZ_VPATROL_STOP=27;
PTZ_HPATROL=28;
PTZ_HPATROL_STOP=29;
PTZ_PELCO_D_HPATROL=20;
TZ_PELCO_D_HPATROL_STOP=21;
IO_ON=94;
IO_OFF=95;

MEM_SET1=30
MEM_GO1=31
MEM_SET2=32
MEM_GO2=33
MEM_SET3=34
MEM_GO3=35
MEM_SET4=36
MEM_GO4=37
MEM_SET5=38
MEM_GO5=39
MEM_SET6=40
MEM_GO6=41
MEM_SET7=42
MEM_GO7=43
MEM_SET8=44
MEM_GO8=45

def camcommand(command,distance=0):
    urllib.urlopen('http://%s:%s@%s:%s/decoder_control.cgi?command=%s' % (username,password,host, port,command))
    #move the specified distance, less a fraction to allow for stopping time, so 10 short moves don't end up further than 1 long move
    #the camera is based on relative movement so this is only ever going to be approximate
    if distance:
      time.sleep((0.0024*distance)-0.02)#should swap this for a callback timer
      #time.sleep((0.006*distance)-0.02)#should swap this for a callback timer
      urllib.urlopen('http://%s:%s@%s:%s/decoder_control.cgi?command=%s' % (username,password,host, port,PTZ_STOP))

def index(req,gox=0,goy=0,preset=0):
  #we get called with a position to go to normally
  #in pixels
  if preset:
    camcommand(preset)

  gox=int(gox)
  goy=int(goy)
  if gox>0:
    camcommand(PAN_RIGHT,gox)
  elif gox<0:
    camcommand(PAN_LEFT,-gox)

  if goy>0:
    camcommand(TILT_UP,goy)
  elif goy<0:
    camcommand(TILT_DOWN,-goy)

  return "OK"