def fetch_rsv_from_cal(room, curr_motion_time, occupied_pct, callback=None): rc = None for r in ReaperConfig.objects.all(): rc = r break #initialize google calendar api gcal = GCalendar(rc.g_consumer_key, rc.g_consumer_secret, rc.g_admin_user_email, rc.g_developer_key) if not gcal: print("Google Calendar API not initialized") return None #Get reservation if found at current time. #event = gcal.getCurrentEventFromCalendar(room['calendar_id']) (event, next_event) = gcal.getCurrentOrNextEventFromCalendar(room['calendar_id']) if event and event.has_key('id'): rsv_status=True else: rsv_status=False print ("fetch_rsv_from_cal --> room_id: %d curr_motion_time: %r rsv_status: %d" %(room['room_id'], curr_motion_time, rsv_status)) if callback: subtask(callback).delay(room, rsv_status, event, curr_motion_time, occupied_pct, next_event) return rsv_status
def reap(room, rsv_status, event, curr_motion_time, occupied_pct, next_event): print ("reap --> room_id: %d curr_motion_time: %r rsv_status: %d occ_pct %f" %(room['room_id'], curr_motion_time, rsv_status, occupied_pct)) current_time = timezone.now() print "current time (UTC): %r" %(current_time) #get timeout from reaperconfig reaper_config = None for r in ReaperConfig.objects.all(): reaper_config = r break #if api did not return motion time for some reason, then set it to current time #to prevent greedy reaps if not curr_motion_time: curr_motion_time = current_time #calculate vacant_secs if curr_motion_time: vacant_secs = get_pos_time_diff_secs(current_time, curr_motion_time) else: vacant_secs = None #Calculate occ_status #vacant_secs > 120 secs, occ_status = False #if vacant_secs > 10: if vacant_secs > OCC_THRESHOLD: occ_status = False else: occ_status = True #Reaper Logic time_to_reap = None reaped = False avail_until = None if rsv_status: occ_timeout_secs = reaper_config.occ_motion_timeout * 60 event_start_secs = get_pos_time_diff_secs(current_time,event['start_time']) print "occ_timeout_secs: %f vacant_secs: %f sec_fr_event_start: %f" %(occ_timeout_secs, vacant_secs, event_start_secs) time_to_reap = None reaped = False #use the smaller number as the comparator if vacant_secs < event_start_secs: cmp_secs = vacant_secs else: cmp_secs = event_start_secs # wait till occ_timeout_secs from vacant time or from start of meeting which ever is less if (cmp_secs >= occ_timeout_secs): print "OK to reap" time_to_reap = 0 #initialize google calendar api gcal = GCalendar(reaper_config.g_consumer_key, reaper_config.g_consumer_secret, reaper_config.g_admin_user_email, reaper_config.g_developer_key) if not gcal: print("Google Calendar API not initialized") return None reaped = gcal.deleteEventFromCalendar(room['calendar_id'], event['id'], send_notification=True) occ_status=False #just in case rsv_status=False else: time_to_reap = occ_timeout_secs - cmp_secs if time_to_reap: print "time_to_reap: %f" %(time_to_reap) else: print "time_to_reap: None" #room is available till next event if next_event and 'start_time' in next_event: avail_until = next_event['start_time'] #Update room status if rsv_status and not reaped: booked_until = None if event.has_key('booked_until'): booked_until = event['booked_until'] else: booked_until = event['end_time'] update_room_status(room, occupied = occ_status, reserved = rsv_status, last_motion_ts = curr_motion_time, last_checked_ts = current_time, time_to_reap = time_to_reap, vacant_secs = vacant_secs, occupancy_pct = occupied_pct, booked_until = booked_until, avail_until = None, rsv_begin_time = event['start_time'], rsv_end_time = event['end_time'], rsv_owner_email = event['owner_email'], rsv_owner_name = event['owner_name'], event_timezone = event['timezone'], allow_res=False ) else: update_room_status(room, occupied = occ_status, reserved = rsv_status, last_motion_ts = curr_motion_time, last_checked_ts = current_time, time_to_reap = None, vacant_secs = None, occupancy_pct = occupied_pct, booked_until = None, avail_until = avail_until, rsv_begin_time = None, rsv_end_time = None, rsv_owner_email = None, rsv_owner_name = None, event_timezone = None, allow_res=True ) #Notify on cancellation and write to log if reaped: notify_on_event.subtask().delay(room,event,action='CANCEL_RSV')