Created
July 22, 2013 20:20
-
-
Save mhermans/6057294 to your computer and use it in GitHub Desktop.
Recorder debugging voor jekke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/var/packages/python/target/bin/python | |
import cgitb, os, time, urllib2, datetime | |
cgitb.enable() | |
TIME_WAIT = 10 # neem aan dat dit seconden zijn? | |
STAT_FILE = "RECORDING" | |
class Recorder(object): | |
# initialisatie-functie is nodig | |
# extra parameters kun je ook hier meegeven, bv. | |
# als je __init__(self, location) hebt | |
# kan je r = Recorder('thuis') meegeven | |
def __init__(self): | |
self.CAMS = [1, 2, 3] | |
# elke class-method heeft als eerste argument self | |
# dit kun je in je method gebruiken (zoals je nu al doet) | |
# het moet in de functie-definitie staan, maar je moet | |
# het niet meegeven bij het aanroepen. | |
# Echter, als je het niet opneemnt in je class method definition, | |
# dan neemt de funtie het eerste argument als "self-argument" | |
# en krijg je "<method> takes no arguments (1 given"-errors | |
def start(self): | |
touch(STAT_FILE) | |
self.record_action("start") | |
return self | |
def stop(self): | |
last_time = os.path.getmtime(STAT_FILE) # returns string | |
last_time = datetime.datetime.fromtimestamp(last_time) # convert to datetime object | |
current_time = datetime.datetime.now() # default datetime object | |
print(last_time) | |
print(current_time) | |
# twee datetime objects kan je van elkaar aftrekken, geeft timedelta object | |
td = current_time - last_time # time-delta object (heeft ook milisec, minutes property | |
# time-delta object kan je niet rechtstreeks met integer vgl. vraag bv. het in seconden | |
if td.seconds < TIME_WAIT: | |
return self | |
self.record_action("stop") | |
return self | |
def record_action(self, action): | |
for cam_nr in self.CAMS: | |
u = self.url(action, cam_nr) | |
#r = urllib2.urlopen(u) | |
def url(self, action, id): | |
print "Executing %s for cam %s" % (action, id) | |
u = ''.join([os.environ["USER"], | |
"/webapi/SurveillanceStation/extrecord.cgi?api=SYNO.SurveillanceStation.ExternalRecording&method=Record&version=2&cameraId=", | |
str(id), "&action=", action]) | |
return u | |
def touch(fname, times=None): | |
with file(fname, 'a'): | |
os.utime(fname, times) | |
print "Content-Type: text/html;" | |
print "" | |
#os.system('./login.sh') | |
recorder = Recorder() | |
#recorder.start() | |
#time.sleep(TIME_WAIT) | |
#recorder.stop() | |
recorder.start().stop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment