def cron(): week = datetime.today().weekday() now = datetime.now() now_time = now.time() # if is_time_between(time2(8, 00), time2(18, 20)) and week < 5: if now_time >= time2(8, 00) and now_time <= time2(18, 30): if week < 5: print('Starting browser... ' + str(now_time)) querySite(driver) else: print("Wrong weekday") # time.sleep(172800) else: print('Currently sleeping: ' + str(now_time)) time.sleep(3600)
def strptime(timestr): """ 将一个 time 字符串按照 HH:MM:SS 形式的字符串转换成 datetime.time 对象 :param timestr: 时间字符串 :return: datetime.time """ dt = datetime.strptime(timestr, '%H:%M:%S') return time2(dt.hour, dt.minute, dt.second)
def waitTill(runTime, timeout=2400): """ runTime is time in HH:MM (string) format, action is call to a function to be exceuted at specified time. Function source: http://stackoverflow.com/a/6579355/2769157 """ startTime = time2(*(map(int, runTime.split(':')))) waitTime = 0 # Timeout set to 20 minutes while startTime > datetime.today().time() and waitTime < 1200: time.sleep(1) waitTime += 1 return
def waitTill(runTime,timeout=2400): """ runTime is time in HH:MM (string) format, action is call to a function to be exceuted at specified time. Function source: http://stackoverflow.com/a/6579355/2769157 """ startTime = time2(*(map(int, runTime.split(':')))) waitTime=0 # Timeout set to 20 minutes while startTime > datetime.today().time() and waitTime < 1200: time.sleep(1) waitTime+=1 return
def get_hourlist(self, date=datetime.now()): if DEBUG: entries = [ (datetime(1, 1, 1, 19, 00, 11).time(), "OS (Start Dom)"), (datetime(1, 1, 1, 16, 46, 11).time(), "DK (Koniec Dom)"), (datetime(1, 1, 1, 8, 34, 58).time(), "DS (Start Dom)"), ] self.hourlist = entries return entries self._login() timestamp = time.mktime(date.timetuple()) self._open("action_popup.php?date=%s" % int(timestamp)) page = self.browser.response().get_data() soup = BeautifulSoup(page) rows = soup.find("table", id="userlist").tbody.findAll("tr") entries = [] for row in rows: soup = BeautifulSoup(str(row)) columns = soup.findAll("td", id=None) try: if "strokeout" in row.attrs[0][1]: continue except IndexError: pass if len(columns) > 2: entry_time = time2(*time.strptime(columns[1].text, "%H:%M:%S")[3:6]) # round to 30 sec try: entry_time = ( entry_time.replace(minute=entry_time.minute - 1, second=0) if entry_time.second >= 30 else entry_time.replace(second=0) ) except ValueError: pass entry_action = columns[2].text entries.append((entry_time, entry_action)) self.hourlist = entries # FIXME print entries[-1] return entries
camera_switch_status = 0 #----------------------Display current time and start Arduino ---------------------- while True: ewatering_sa1_sensor = {} #ensure if SCREEN_DISPLAY: print(strftime("%Y-%m-%d %H:%M:%S", localtime())) if SAVE_TO_FILE: fid.write(strftime("%Y-%m-%d %H:%M:%S", localtime())) ard = serial.Serial(SERIAL_PORT, timeout=20) time.sleep(5) #--------------------------- camera switch------------------------------- whether_time_for_camera_on = is_time_between(time2(7, 30), time2(16, 40)) #brisbane time if whether_time_for_camera_on and camera_switch_status == False: if SCREEN_DISPLAY: print("time for powering camera") if SAVE_TO_FILE: fid.write("time for powering camera") ard.write("power_switch,8,power_switch_status,1") ard.flushInput() msg = ard.readline() if SCREEN_DISPLAY: print(msg.rstrip()) if SAVE_TO_FILE: fid.write(DELIMITER + msg) camera_switch_status = 1 time.sleep(120) elif whether_time_for_camera_on and camera_switch_status: if SCREEN_DISPLAY: print("camera keeps on") if SAVE_TO_FILE: fid.write("camera keeps on ") elif whether_time_for_camera_on == False and camera_switch_status:
thread = threading.Thread(target=run) thread.daemon = True thread.start() logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s') LOCATIONS = loadlocations() SETTINGS = loadsettings() GMAPS = googlemaps.Client(key=SETTINGS['api_params']['api_key']) SETTINGS['api_params']['api_key'] = "" savesettings() ON_TIME = time2(int(SETTINGS['schedule']['onhour']), int(SETTINGS['schedule']['onmin'])) OFF_TIME = time2(int(SETTINGS['schedule']['offhour']), int(SETTINGS['schedule']['offmin'])) UPDATE_INTERVAL = int(SETTINGS['schedule']['update_interval']) logging.info('working hours is between %s -> %s', ON_TIME, OFF_TIME) logging.info('%d locations are loaded', len(LOCATIONS)) logging.info('starting server and updater') os.chdir('www') startupdate() startwebserver()