Beispiel #1
0
def media_upload(files) :
	"""Save file to SD card."""
	
	log.info('Upload file')
	result = 0
	for file in files:
		# Check if the file is one of the allowed types/extensions
		if file and allowed_file(file.filename):
			# Make the filename safe, remove unsupported chars
			filename = secure_filename(file.filename)
			# Move the file form the temporal folder to the upload
			# folder we setup
			try :
				file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
				log.debug('file saved : %s',filename)
				result = result + 1
			except UploadNotAllowed:
				log.error('file too large : %s',filename)
		else:
			log.warning('file not allowed : %s',filename)
	
	# Reload all song on playlist
	subprocess.call('mpc clear; mpc add %s; mpc update',app.config['UPLOAD_FOLDER'])
	
	log.debug('result : %s',result)
	return result
Beispiel #2
0
def media_ctr() :
	"""Control media center.
	
	@Imput    command : [Upload,Delete,List,Play,Stop,VolUp,VolDown].
	          titles.
	@Return   result : [%d,list,Success,Error].
	"""
	
	log.info('media_ctr BEGIN')
	
	# Get the JSON data sent from the form
	data = request.get_json(force=True)
	
	log.info(data['command'])
	if (data['command'] == 'Upload') :
		result = media_upload(request.files.getlist("file[]"))
	elif (data['command'] == 'Delete') :
		result = media_del(data['titles'])
	elif (data['command'] == 'List') :
		result = media_list()
	elif (data['command'] == 'Play') :
		result = media_Play()
	elif (data['command'] == 'Stop') :
		result = media_Stop()
	elif (data['command'] == 'VolUp') :
		result = media_VolUp()
	elif (data['command'] == 'VolDown') :
		result = media_VolDown()
	else :
		log.warning('invalid command')
		result = 'None'
	
	log.debug(result)
	log.info('media_ctr END')
	return jsonify(result=result)
Beispiel #3
0
def stream_ctr() :
	"""Control streamer.
	
	@Imput    command : [Start,Stop,Restart].
	@Return   result : [Success,Error,None].
	"""
	
	log.info('stream_ctr BEGIN')
	
	# Get the JSON data sent from the form
	data = request.get_json(force=True)
	
	log.info(data['command'])
	if (data['command'] == 'Start') :
		result = steam_ctr_start()
	elif (data['command'] == 'Stop') :
		result = steam_ctr_stop()
	elif (data['command'] == 'Restart') :
		result = steam_ctr_restart()
	else :
		log.warning('invalid command')
		result = 'None'
	
	log.debug(result)
	log.info('stream_ctr END')
	return jsonify(result=result)
Beispiel #4
0
def activity_ctr() :
	"""Control activity.
	
	@Imput    command : [Start,Stop,Calibrate].
			agi_normal (decimal).
	@Return   result : [In progress,Success,Stoped,Error,None].
	"""
	
	log.info('activity_ctr BEGIN')
	
	# Get the JSON data sent from the form
	data = request.get_json(force=True)
	
	log.info(data['command'])
	if (data['command'] == 'Start') :
		result = 'Error'
		if ((db.has_key('act_job_id')) and (db['act_job_id'] != '')) :
			result = 'In progress'
		else :
			# Start the agitation controller
			agi_job = agitation_ctr_exe.delay()
			result = agi_job.AsyncResult(agi_job.id).state
			if (result == states.SUCCESS) :
				# Start the activity controller
				act_job = activity_ctr_exe.delay()
				result = act_job.AsyncResult(act_job.id).state
				if (result == states.SUCCESS) :
					result = 'Success'
					db['act_job_id'] = act_job.AsyncResult(act_job.id).id
	elif (data['command'] == 'Stop') :
		result = 'Error'
		if ((not db.has_key('act_job_id')) or (db['act_job_id'] == '')) :
			result = 'Stoped'
		else :
			# Stop the activity controller
			revoke(act_job.id, terminate=True, signal='SIGTERM')
			result = act_job.AsyncResult(act_job.id).state
			if (result == states.REVOKED) :
				# Stop the agitation controller
				revoke(agi_job.id, terminate=True, signal='SIGTERM')
				result = agi_job.AsyncResult(agi_job.id).state
				if (result == states.REVOKED) :
					result = 'Success'
					db['act_job_id'] = ''
			
	elif (data['command'] == 'Calibrate') :
		# Calibrate the normale sound level
		result = normal_levels(data['agi_normal'])
		#TODO : adjust intervals
	else :
		log.warning('invalid command')
		result = 'None'
	
	log.debug(result)
	log.info('activity_ctr END')
	return jsonify(result=result)
Beispiel #5
0
def media_Play() :
	"""Play playlist songs."""
	
	log.info('Play songs')
	try :
		subprocess.call('mpc play ')
	
		log.debug('Play')
		result = 'Success'
	except subprocess.CalledProcessError :
		# file not deleted
		log.error('Play error')
		log.warning('Return code : %s',subprocess.CalledProcessError.returncode)
		result = 'Error'
		
	log.debug('result : %s',result)
	return result
Beispiel #6
0
def media_VolDown() :
	"""Volume down."""
	
	log.info('Volume Down')
	try :
		subprocess.call('mpc volume - ')
	
		log.debug('Volume decreased')
		result = 'Success'
	except subprocess.CalledProcessError :
		# file not deleted
		log.error('Volume decrease error')
		log.warning('Return code : %s',subprocess.CalledProcessError.returncode)
		result = 'Error'
		
	log.debug('result : %s',result)
	return result
Beispiel #7
0
def media_list() :
	"""List song files."""
	
	log.info('List songs')
	try :
		#TODO : list command
		titles = subprocess.check_output('')
		
		log.debug('Playlist titles : %s',titles)
		titles = titles.splitlines()
		result = titles
	except subprocess.CalledProcessError :
		# file not deleted
		log.error('list error')
		log.warning('Return code : %s',subprocess.CalledProcessError.returncode)
		result = 'Error'
	
	log.debug('result : %s',result)
	return result
Beispiel #8
0
def media_del(files) :
	"""Delete file from SD card and reload playlist."""
	
	log.info('Delete file')
	result = 0
	for file in files:
		try :
			#delete file from directory
			subprocess.call('rm -f %s',file)
			
			log.debug('file deleted : %s',file)
			result = result + 1
		except subprocess.CalledProcessError :
			# file not deleted
			log.error('file not deleted')
			log.warning('Return code : %s',subprocess.CalledProcessError.returncode)
	
	# Reload all song on playlist
	subprocess.call('mpc clear; mpc add %s; mpc update',app.config['UPLOAD_FOLDER'])
	
	log.debug('result : %s',result)
	return result