Пример #1
0
def test_Astral_Daylight():
    a = Astral()
    l = a['London']

    d = datetime.date(2016, 1, 6)
    start, end = a.daylight_utc(d, l.latitude, l.longitude)
    cstart = datetime.datetime(2016, 1, 6, 8, 5, 0, tzinfo=pytz.UTC)
    cend = datetime.datetime(2016, 1, 6, 16, 7, 0, tzinfo=pytz.UTC)
    assert datetime_almost_equal(start, cstart, 300)
    assert datetime_almost_equal(end, cend, 300)
Пример #2
0
def main():

	srtdb = sqlite3.connect(database_location)		# establish a connection and cursor into the database
	srtdb.row_factory = sqlite3.Row
	cur = srtdb.cursor()

	telescope = Scan()			# initialize a Scan object for running scans

	config = cur.execute("SELECT * FROM CONFIG").fetchone()		# fetch config data from the db

	a = Astral()					# initalize an Astral object for computer day and night times

	today = datetime.date.today()	# get today's date

	daytimes = a.daylight_utc(today, config['lat'], config['lon'])			# compute date and night times in UTC using the Astral object
	nighttimes = a.night_utc(today, config['lat'], config['lon'])

	print('Daystart: ' + daytimes[0].isoformat() + ', Dayend: ' + daytimes[1].isoformat())
	print('Nightstart:' + nighttimes[0].isoformat() + ', Nightend: ' + nighttimes[1].isoformat())

	dusk = nighttimes[0].timestamp()	# get unix times for day and night
	dawn = nighttimes[1].timestamp()
	sunrise = daytimes[0].timestamp()
	sunset = daytimes[1].timestamp()

	dayschedule = Schedule.Schedule(sunrise + 7200, sunset - 7200)		# instantiate day and night schedules
	nightschedule = Schedule.Schedule(dusk, dawn)

	currentscanid = None
	
	### attempt to reschedule scans in SCHEDULE after restart ###
	
	schedule = cur.execute("SELECT * FROM SCHEDULE").fetchall()		# get the schedule from the db
	
	cur.execute("DELETE FROM SCHEDULE")		# delete all entries in SCHEDULE
	
	for scan in schedule:		# attempt to reschedule all scheduled scans

		scanparams = cur.execute("SELECT * FROM SCANPARAMS WHERE ID = ?", (scan['id'],)).fetchone()
		
		curtime = ntp.getcurrenttime()

		if scanparams['source'] == 'sun':		# if source is the sun, schedule during the day

			status = dayschedule.schedulescan(scanparams['id'], curtime)

		else:		# if source is not the sun, schedule at night

			status = nightschedule.schedulescan(scanparams['id'], curtime)
				
		if status != 'scheduled':
				
			today = datetime.date.today()
			cur.execute("INSERT INTO SCANHISTORY VALUES (?,?,?,?,?,?)", (newscan['id'], scanparams['name'], scanparams['type'], today.day, today.month, today.year))

		cur.execute("UPDATE SCANIDS SET STATUS = ? WHERE ID = ?", (status, newscan['id']))
		srtdb.commit()

	### MAIN EXECUTION LOOP FOR TELESCOPE-SIDE CODE ###

	while True:

		time.sleep(0.5)			# sleep for half a second to reduce looping speed


		### set whether it is day or night ###

		curtime = ntp.getcurrenttime()

		if curtime >= dusk and curtime <= dawn:

			currentschedule = nightschedule

		else:

			currentschedule = dayschedule


		### create new schedules if current schedules are completed ###

		config = cur.execute("SELECT * FROM CONFIG").fetchone()		# get config data from the db

		newday = datetime.date.today()								# get today's date

		if newday.day != today.day or newday.month != today.month or newday.year != today.year:	# if the day has changed, create new day schedule

			print('it\'s a new day! setting up new daytime schedule')

			today = newday

			daytimes = a.daylight_utc(today, config['lat'], config['lon'])

			sunrise = Time(daytimes[0], format = 'datetime', scale = 'utc').unix
			sunset = Time(daytimes[1], format = 'datetime', scale = 'utc').unix

			dayschedule = Schedule(sunrise + 7200, sunset - 7200)

		if curtime > dawn:											# if the night is over, create new night schedule

			print('good morning! setting up new nighttime schedule')

			nighttimes = a.night_utc(today, config['lat'], config['lon'])

			dusk = Time(nighttimes[0], format = 'datetime', scale = 'utc').unix
			dawn = Time(nighttimes[1], format = 'datetime', scale = 'utc').unix

			nightschedule = Schedule(dusk, dawn)


		### remove cancelled scans from schedules ###

		print('might be time to cancel some scans')

		cancelscans(dayschedule)
		cancelscans(nightschedule)


		### insert a new scan into the schedule ###

		print('is there a new scan to add?')

		newscan = cur.execute("SELECT * FROM SCANIDS WHERE STATUS = ?", ('submitted',)).fetchone()

		if newscan != None:				# check if a scan has been submitted

			print('yes there is!')

			scanparams = cur.execute("SELECT * FROM SCANPARAMS WHERE ID = ?", (newscan['id'],)).fetchone()

			if scanparams['source'] == 'sun':		# if source is the sun, schedule during the day

				status = dayschedule.schedulescan(scanparams['id'], curtime)

			else:		# if source is not the sun, schedule at night

				status = nightschedule.schedulescan(scanparams['id'], curtime)
				
			if status != 'scheduled':
				
				today = datetime.date.today()
				cur.execute("INSERT INTO SCANHISTORY VALUES (?,?,?,?,?,?)", (newscan['id'], scanparams['name'], scanparams['type'], today.day, today.month, today.year))

			cur.execute("UPDATE SCANIDS SET STATUS = ? WHERE ID = ?", (status, newscan['id']))
			srtdb.commit()


		### remove current scan from the schedule when it finishes ###

		print('did the current scan finish running?')

		if currentscanid != None:		# check to see if a scan is supposed to be running

			currentscan = cur.execute("SELECT * FROM SCANIDS WHERE ID = ?", (currentscanid,)).fetchone()

			if currentscan == None or currentscan['status'] != 'running':	# if scan is not running anymore, remove from schedule and reset currentscanid

				print('yes it did!')

				cur.execute("DELETE FROM SCHEDULE WHERE ID = ?", (currentscanid,))
				srtdb.commit()

				currentschedule.deschedulescan(currentscanid)

				currentscanid = None


		### run the next scan in the schedule ###

		print('time to run a scan?')

		currentscanid = runscan(currentschedule)