Beispiel #1
0
 def upload_exception(self, error):
     send_json = [{"workerId": self.workerId, "status": CRASH, "errorReason": error}]
     try:
         self.output_queue.put(send_json)
     except Exception as err:
         # the QueueServer already exit.
         Debug.error("Worker upload exception failed: {}. {}".format(err, traceback.format_exc()))
Beispiel #2
0
 def upload_status(self, status):
     send_json = [{"workerId": self.workerId, "status": status}]
     try:
         self.output_queue.put(send_json)
     except Exception as err:
         # the QueueServer already exit.
         Debug.error("Worker upload status failed: {}. {}".format(err, traceback.format_exc()))
Beispiel #3
0
def wait_until(*template_ids):
    if len(template_ids) == 0:
        return
    elif len(template_ids) > 1:
        command = """
        echo -e "set head off;\\n  select 'result'||count(*) from task_info INNER JOIN plan_info ON task_info.plan_id = plan_info.plan_id   where task_info.status in (1,2)  and task_info.template_id in {};"| sqlplus -s omc/omc
        """.format(tuple(template_ids))
    else:
        # one tempalte
        command = """
        echo -e "set head off;\\n  select 'result'||count(*) from task_info INNER JOIN plan_info ON task_info.plan_id = plan_info.plan_id   where task_info.status in (1,2)  and task_info.template_id = '{}';"| sqlplus -s omc/omc
        """.format(template_ids[0])

    while 1:
        stdout, stderr = execute_command(command)
        response = stdout.replace('result', '')
        Debug.info('waituntil {}: {}'.format(tuple(template_ids), response))
        if response.isdigit():
            num = int(response)
            if num > 0:
                time.sleep(10)
            else:
                break
        else:
            time.sleep(2)
Beispiel #4
0
	def update(self):
		checks = UpdateChecks()
		config = Settings.Config()
		if checks.GetState('error') is True:
			checks.SetFalse('error')
		#self.Source.update()
		try:
			#pass
			self.Source.update()
		except Exception as e:
			Warn("Source update failed: " + str(e))
			color = (148, 14, 14)
			static = RowStatic()
			self.ErrorScreen = static.HeaderFont.render("Error: Failed to retrieve update!", True, color).convert_alpha()
			checks.SetTrue('error')
			exit()
			## This makes debugging easier
			#raise e
			
		if checks.GetState('dirty_source') is True:
			checks.SetFalse('dirty_source')
			self.List.clear()
			
			lst = self.Source.Arrivals
			if config.DisplayScreen == "departures":
				lst = self.Source.Departures
				
			for d in lst:
				self.List.append(BusRow(d))
				
			if len(self.List) <= self.RowsPerPage:
				checks.SetTrue('dirty_screen')
				self.CachedList = self.List
				Debug("Source updated with one screen of buses, scheduling screen update. (" + str(len(self.CachedList)) + ":" + str(self.RowsPerPage) + ")")
				
		if len(self.List) > self.RowsPerPage:
			if checks.TimeToUpdate('page_delay') is True:
				# It's time to paginate
				checks.Update('page_delay')
				checks.SetTrue('dirty_screen')
				self.FirstRow = self.CurrentPage * self.RowsPerPage
				self.CurrentPage += 1
				
			if len(self.CachedList) / self.RowsPerPage < self.CurrentPage:
				Debug("Updating CachedList")
				self.CurrentPage = 0
				#self.FirstRow = 0
				self.CachedList = self.List
				Debug("CurrentPage: " + str(self.CurrentPage) + "; FirstRow: " + str(self.FirstRow) + "; len(CachedList): " + str(len(self.CachedList)))
			
		if len(self.CachedList) == 0:
			Debug("CachedList is empty.  Updating.")
			self.CachedList = self.List
			self.CurrentPage = 0
			checks.SetTrue('dirty_screen')
		
		if (self.FirstRow / self.RowsPerPage > len(self.CachedList) / self.RowsPerPage):
			Debug("Past the last page.  Resetting.")
			self.FirstRow = 0
			self.CurrentPage = 0
Beispiel #5
0
	def ParseSource(self):
		Debug('Entering JSONSource.ParseSource()')
		jfile = open(self.LocalSource, 'r')
		try:
			Debug('Attempting to load jfile...')
			json_dict = json.load(jfile)
		except Exception as ex:
			Error("Unable to load jfile.\n" + str(jfile))
			exit()
		jfile.close()
		
		self.Arrivals.clear()
		self.Departures.clear()
		
		for arrival in json_dict['arrivals']:
			self.Arrivals.append(
				Data.BusArrival( arrival['company'],
								 arrival['city'],
								 arrival['time'],
								 arrival['status']
								 )
			)
		
		for departure in json_dict['departures']:
			self.Departures.append(
				Data.BusDeparture( departure['company'],
								   departure['city'],
								   departure['time'],
								   departure['status'],
								   departure['gate'],
								   departure['busnum']
								   )
			)
Beispiel #6
0
 def AddTimer(self, id, interval=None):
     if interval is None:
         self.TimerDict[id] = TimerEvent(id)
         Debug("Adding timer '" + str(id) + "' with default interval")
     else:
         self.TimerDict[id] = TimerEvent(id, interval)
         Debug("Adding timer '" + str(id) + "' with interval " +
               str(interval))
Beispiel #7
0
 def AddState(self, id, initial=None):
     if initial is None:
         self.StateDict[id] = False
         Debug("Adding state '" + str(id) +
               "' with default value of False")
     else:
         self.StateDict[id] = initial
         Debug("Adding stat '" + str(id) + "' with value " +
               str(initial))
Beispiel #8
0
 def upload_task_status(self, error=None):
     status = {"workerId": self.workerId, "PassTask": self.PassTask, "FailTask": self.FailTask}
     if error:
         status.update({"Exception": error})
     send_json = [status]
     try:
         self.output_queue.put(send_json)
     except Exception as err:
         # the QueueServer already exit.
         Debug.error("Worker upload task status failed: {}. {}".format(err, traceback.format_exc()))
Beispiel #9
0
 def constant(filename):
     root_dir = dirname(dirname(abspath(__file__)))
     template_dir = join(root_dir, 'templates')
     template_path = join(template_dir, filename)
     if isfile(template_path):
         with open(template_path, 'rb') as f:
             content = f.read().decode('utf-8')
         return content
     else:
         error_message = 'Template file: {} is not exist.'.format(
             template_path)
         Debug.error(error_message)
         raise Exception(error_message)
Beispiel #10
0
 def template(filename):
     if not filename.endswith(".json"):
         filename = "{}.json".format(filename)
     root_dir = dirname(dirname(abspath(__file__)))
     template_dir = join(root_dir, 'templates')
     template_path = join(template_dir, filename)
     if isfile(template_path):
         with open(template_path, 'rb') as f:
             content = f.read().decode('utf-8')
         return Template(content)
     else:
         error_message = 'Template file: {} is not exist.'.format(
             template_path)
         Debug.error(error_message)
         raise Exception(error_message)
Beispiel #11
0
 def SetFalse(self, id):
     if id in self.StateDict.keys():
         self.StateDict[id] = False
         Debug("Setting state with ID '" + str(id) + "' to False")
     else:
         raise KeyError("State with ID '" + str(id) +
                        "' does not exist")
Beispiel #12
0
	def __init__(self, company, city, time, status):
		if company == "Company":
			Debug("Setting data for header")
		self.Company = company
		self.City = city		## Destination/Origin
		self.Time = time		## Departure/Arrival time
		self.Status = status
Beispiel #13
0
	def __init__(self, width = 1280, height = 720):
		config = Settings.Config()
		if config.DisplayFullscreen is False:
			os.environ['SDL_VIDEO_CENTERED'] = '1'
			self.screen = pygame.display.set_mode((width, height))
		else:
			self.screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF)
			
		pygame.init()
		
		self.Background = TileImage("img/bg-dark.gif", self.screen)	
		self.screen.blit(self.Background, (0,0))
		pygame.mouse.set_visible(False)
		pygame.display.flip()
		
		checks = UpdateChecks()
		checks.AddTimer('page_delay', config.PageInterval)
		checks.AddTimer('source_update', config.UpdateInterval)
		checks.Update('page_delay')
		
		checks.AddState('dirty_source', True)
		checks.AddState('dirty_screen', True)
		checks.AddState('dirty_page', False)
		checks.AddState('error', False)
		
		self.DrawArrivals = True
		if config.DisplayScreen == 'departures':
			self.DrawArrivals = False
			
		self.CurrentPage = 1
		self.RowsPerPage = 10	# 12 rows minus the header
		self.FirstRow = 0		# First row to draw
		
		# FIXME: proper error handling and checking needed here.
		if config.SourceURI[-4:] == '.xml':
			self.Source = ds.JSONSource()
			Debug('XML source found.')
		else:
			self.Source = ds.XMLSource()
			Debug('JSON source found.')
			
		self.CachedList = Data.BusList()
		self.List = Data.BusList()
		self.ArrivalHeader = HeaderRow(Data.BusArrival("Company", "City", "Time", "Status"))
		self.DepartureHeader = HeaderRow(Data.BusDeparture("Company", "City", "Time", "Status", "Gate", "Bus #"))
		
		self.ErrorScreen = None
Beispiel #14
0
 def SetUpdateInterval(self, id, interval):
     if id in self.TimerDict.keys():
         self.TimerDict[id].UpdateInterval = interval
         Debug("Updating update interval of TimerEvent '" + str(id) +
               "' to " + str(interval))
     else:
         raise KeyError("TimeEvent with ID '" + str(id) +
                        "' does not exist")
Beispiel #15
0
def LoadImage(path):
	""" Return a surface containing the image at the given path. """
	surf = None
	Debug("Loading image at path '" + str(path) + "'")
	#path = FixPath(path)
	try:
		surf = pygame.Surface.convert_alpha(pygame.image.load(path))
	except Exception as exc:
		Warn("Unable to load image at path '" + path + "': " + str(exc))
	return surf
Beispiel #16
0
 def issue_token(self, user, cache_file, password=None):
     if not password:
         password = self._get_password(user)
     command = """
     curl -v -k -X "POST"  "https://{fqdn}:9527/restda/oauth2/token"  -H "Content-Type:application/x-www-form-urlencoded"  -d "grant_type=client_credentials"  -u  '{user_name}:{password}'
     """.format(fqdn=self.fqdn, user_name=user, password=password)
     stdout, stderr = execute_command(command)
     try:
         response = eval(stdout)
         Debug.info(response)
     except Exception as err:
         Debug.error("Command: {}, stdout: {}, stderr: {}".format(
             command, stdout, stderr))
         Debug.error("Exception occur: {}".format(err))
         raise Exception(
             "format token failed, error: {}: request: {},response: {}".
             format(err, command, stdout))
     if "token_key" in response:
         token = response["token_key"]
         with open(cache_file, "ab") as f:
             f.write("{}\n".format(
                 json.dumps(
                     collections.OrderedDict(
                         Token=token,
                         TimeStamp=int(time.time())))).encode('utf-8'))
         return token
     elif "errorMessage" in response:
         raise Exception(response["errorMessage"])
     else:
         raise Exception("token response unexpected: {}".format(stdout))
Beispiel #17
0
def wait_for(template_id_or_desc, number=1):
    if "defined" in template_id_or_desc:
        command = """
        echo -e "set head off;\\n set linesize 2000; \\n select 'result'||count(*) from task_info where status in (1,2) and template_id='{}';"| sqlplus -s omc/omc|grep result
        """.format(template_id_or_desc)
    else:
        command = """
        echo -e "set head off;\\n  select 'result'||count(*) from task_info INNER JOIN plan_info ON task_info.plan_id = plan_info.plan_id   where task_info.status in (1,2)  and plan_info.DESCRIPTION like '{}';"| sqlplus -s omc/omc
        """.format(template_id_or_desc)
    while 1:
        stdout, stderr = execute_command(command)
        response = stdout.replace('result', '')
        Debug.info('waitFor {}: {} < {}'.format(template_id_or_desc, response,
                                                number))
        if response.isdigit():
            num = int(response)
            if num >= number:
                Debug.info("Wait for {} 10s...".format(template_id_or_desc))
                time.sleep(10)
            else:
                break
        else:
            Debug.error(
                "Wait for function failed: response is not digital: {}".format(
                    response))
            time.sleep(2)
Beispiel #18
0
 def read_token(self, cache_file):
     start_time = time.time()
     while 1:
         if time.time() - start_time > self.read_token_timeout:
             raise Exception("read token timeout in {}".format(
                 self.read_token_timeout))
         if exists(cache_file):
             with open(cache_file, "rb") as f:
                 lines = f.readlines()
                 lines = list(map(lambda line: line.decode('utf-8'), lines))
             if lines:
                 last_line = lines[-1]
                 try:
                     token_map = eval(last_line)
                     token = token_map["Token"]
                     time_stamp = token_map["TimeStamp"]
                     if time.time() - time_stamp < self.token_expiration:
                         return token
                 except Exception as err:
                     Debug.error("Read Token failed, err: {}. {}".format(
                         err, traceback.format_exc()))
         time.sleep(10)
         Debug.info('read token failed, next in 10 s')
Beispiel #19
0
	def update(self):
		checks = UpdateChecks()
		if checks.TimeToUpdate('source_update'):
			Debug('Updating source')
			
			dlfile = urllib.URLopener()
			try:
				dlfile.retrieve(self.RemoteSource, self.LocalSource)
			except Exception as e:
				Warn("DL of '" + self.RemoteSource + "' failed: " + str(e))
				
			self.ParseSource()
			
			## This is just because I'm lazy...
			#json_string = json.dumps( {"arrivals": self.Arrivals.get_dict(), "departures": self.Departures.get_dict()} )
			#print "json > " + json_string + " <"
			#exit(1)
			
			checks.Update('source_update')
			checks.SetTrue('dirty_source')
Beispiel #20
0
	def draw(self, screen, current_page, total_pages):
		#print "page " + str(current_page + 1) + " of " + str(total_pages + 1)
		titletext = "Arrivals"
		if self.Data.is_departure() is True:
			titletext = "Departures"
		
		timestruct = time.localtime()
		months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
		days = "Sun Mon Tues Wed Thurs Fri Sat".split(" ")
		ampm = 'am'
		hour_12 = timestruct.tm_hour
		if hour_12 > 12:
			ampm = 'pm'
			hour_12 -= 12
		elif hour_12 == 0:
			hour_12 = 12
		
		minute = timestruct.tm_min
		if minute < 10:
			minute = '0' + str(minute)
		
		Debug("{Header} current_page: " + str(current_page) + "; total_pages: " + str(total_pages))
		pagestring = "Page " + str(current_page + 1) + " of " + str(total_pages + 1)
		
		timestring = days[timestruct.tm_wday] + ", " + months[timestruct.tm_mon-1] + " " + str(timestruct.tm_mday) + ", " + str(hour_12) + ':' + str(minute) + ampm
		
		static = RowStatic()
		
		BusRow.draw(self, self.image, -1)		
		midoffset = self.image.get_height() / 4
		
		rendered_title = self.font.render(titletext, True, self.fontcolor).convert_alpha()
		rendered_time = self.timefont.render(timestring, True, self.fontcolor).convert_alpha()
		rendered_page = self.timefont.render(pagestring, True, self.fontcolor).convert_alpha()
		screen.blit(self.image, (screen.get_width()/2 - self.image.get_width()/2, 0))
		screen.blit(rendered_title, ((self.image.get_width()/2) - (rendered_title.get_width()/2), (midoffset - rendered_title.get_height()/2)))
		screen.blit(rendered_time, (20, (midoffset - rendered_time.get_height()/2)))
		screen.blit(rendered_page, (screen.get_width() - rendered_page.get_width() - 20, (midoffset - rendered_time.get_height()/2)))
Beispiel #21
0
	def draw(self):
		checks = UpdateChecks()
		config = Settings.Config()
		if checks.GetState('error') is True:
			self.screen.blit(self.Background, (0,0))
			self.screen.blit(self.ErrorScreen, ((self.screen.get_width() / 2) - (self.ErrorScreen.get_width()/2), (self.screen.get_height() / 2) - (self.ErrorScreen.get_height()/2)))
			return
		
		if checks.GetState('dirty_screen') is True:
			self.screen.blit(self.Background, (0,0))
			
			total_pages = len(self.CachedList) / self.RowsPerPage
			if len(self.CachedList) == self.RowsPerPage:
				total_pages = 0
			
			if config.DisplayScreen == "arrivals":
				self.ArrivalHeader.draw(self.screen, (self.FirstRow / self.RowsPerPage), total_pages)
			else:
				self.DepartureHeader.draw(self.screen, (self.FirstRow / self.RowsPerPage), total_pages)
			
			if len(self.CachedList) == 0:
				Debug("Empty Cached List! Drawing error message.")
				static = RowStatic()
				emptymsg = static.HeaderFont.render("No Buses!", True, static.HeaderFontColor)
				self.screen.blit(emptymsg, ((self.screen.get_width()/2)-(emptymsg.get_width()/2),(self.screen.get_height()/2)-(emptymsg.get_height()/2)))
			else:
				row = 2
				x = 0
				for b in self.CachedList[self.FirstRow:(self.FirstRow + self.RowsPerPage)]:
					x += 1
					b.draw(self.screen, row)
					row += 1
		
		if checks.GetState('dirty_screen') is True:
			checks.SetFalse('dirty_screen')
			pygame.display.flip()
Beispiel #22
0
 def create_plan(self, token, json):
     command = """
     curl -v -k -H "Authorization:Bearer {token}" -X "POST"  "https://{fqdn}:9527/restda/api/v1/plans"  -H "Content-Type:application/json"  --data '{json}'
     """.format(token=token, fqdn=self.fqdn, json=json)
     stdout, stderr = execute_command(command)
     response = eval(stdout)
     if "status" in response:
         Debug.info("create plan request: {} response: {}".format(
             command, response))
         assert response[
             "status"] == "success", "Create A plan failed , because Response is {}".format(
                 stdout)
     elif "errorMessage" in response:
         Debug.error("create plan request: {} response: {}".format(
             command, response))
         raise Exception(response["errorMessage"])
     else:
         Debug.error("create plan request: {} response: {}".format(
             command, response))
         raise Exception(stdout)
Beispiel #23
0
 def time_to_update(self):
     if time.time() - self.LastUpdate > self.UpdateInterval:
         Debug("It's time to update '" + str(self.ID) + "'")
         return True
     return False
Beispiel #24
0
 def update(self):
     Debug("Updated '" + str(self.ID) + "'")
     self.LastUpdate = time.time()
Beispiel #25
0
 def __init__(self):
     self.TimerDict = {}
     self.StateDict = {}
     Debug('UpdateChecks() singleton init\'d')
Beispiel #26
0
	def __init__(self):
		BaseSource.__init__(self)
		Debug('JSONSource init\'d')