2016-05-26 16:16:13 +00:00
|
|
|
# Copyright 2016 Mycroft AI, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Mycroft Core.
|
|
|
|
#
|
|
|
|
# Mycroft Core is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Mycroft Core is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-08-10 17:12:45 +00:00
|
|
|
import socket
|
2016-09-17 02:08:53 +00:00
|
|
|
import subprocess
|
2016-09-30 15:23:24 +00:00
|
|
|
import tempfile
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-17 02:08:53 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2016-05-20 14:16:01 +00:00
|
|
|
import psutil
|
2016-09-17 02:08:53 +00:00
|
|
|
from os.path import dirname
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
__author__ = 'jdorleans'
|
|
|
|
|
|
|
|
|
|
|
|
def play_wav(file_path):
|
2016-12-13 03:01:22 +00:00
|
|
|
return subprocess.Popen(["aplay", remove_https(file_path)])
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def play_mp3(file_path):
|
2016-12-13 03:01:22 +00:00
|
|
|
return subprocess.Popen(["mpg123", remove_https(file_path)])
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def record(file_path, duration, rate, channels):
|
|
|
|
if duration > 0:
|
2016-05-20 22:15:53 +00:00
|
|
|
return subprocess.Popen(
|
|
|
|
["arecord", "-r", str(rate), "-c", str(channels), "-d",
|
|
|
|
str(duration), file_path])
|
2016-05-20 14:16:01 +00:00
|
|
|
else:
|
2016-05-20 22:15:53 +00:00
|
|
|
return subprocess.Popen(
|
|
|
|
["arecord", "-r", str(rate), "-c", str(channels), file_path])
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
2016-12-13 03:01:22 +00:00
|
|
|
def remove_https(url):
|
|
|
|
return url.replace("https://", "http://")
|
|
|
|
|
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
def remove_last_slash(url):
|
|
|
|
if url and url.endswith('/'):
|
|
|
|
url = url[:-1]
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
def read_stripped_lines(filename):
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
return [line.strip() for line in f]
|
|
|
|
|
|
|
|
|
|
|
|
def read_dict(filename, div='='):
|
|
|
|
d = {}
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
(key, val) = line.split(div)
|
|
|
|
d[key.strip()] = val.strip()
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
def create_file(filename):
|
|
|
|
try:
|
|
|
|
os.makedirs(dirname(filename))
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write('')
|
|
|
|
|
|
|
|
|
|
|
|
def kill(names):
|
|
|
|
print psutil.pids()
|
|
|
|
for name in names:
|
|
|
|
for p in psutil.process_iter():
|
|
|
|
try:
|
|
|
|
if p.name() == name:
|
|
|
|
p.kill()
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2016-05-20 22:15:53 +00:00
|
|
|
|
2016-08-10 17:12:45 +00:00
|
|
|
def connected(host="8.8.8.8", port=53, timeout=3):
|
|
|
|
"""
|
|
|
|
Thanks to 7h3rAm on
|
|
|
|
Host: 8.8.8.8 (google-public-dns-a.google.com)
|
|
|
|
OpenPort: 53/tcp
|
|
|
|
Service: domain (DNS/TCP)
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
socket.setdefaulttimeout(timeout)
|
|
|
|
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
|
|
|
|
return True
|
2016-08-10 18:40:49 +00:00
|
|
|
except IOError:
|
2016-08-16 20:12:49 +00:00
|
|
|
try:
|
2016-08-16 20:15:07 +00:00
|
|
|
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(
|
|
|
|
("8.8.4.4", port))
|
2016-08-16 20:12:49 +00:00
|
|
|
return True
|
|
|
|
except IOError:
|
|
|
|
return False
|
2016-08-10 17:12:45 +00:00
|
|
|
|
|
|
|
|
2016-09-30 15:23:24 +00:00
|
|
|
def create_signal(signal_name):
|
2016-09-22 18:16:11 +00:00
|
|
|
try:
|
2016-09-30 15:23:24 +00:00
|
|
|
with open(tempfile.gettempdir() + '/' + signal_name, 'w'):
|
|
|
|
return True
|
2016-09-22 18:16:11 +00:00
|
|
|
except IOError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-09-30 15:23:24 +00:00
|
|
|
def check_for_signal(signal_name):
|
|
|
|
filename = tempfile.gettempdir() + '/' + signal_name
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
os.remove(filename)
|
2016-09-22 18:16:11 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-09-03 22:00:32 +00:00
|
|
|
def validate_param(value, name):
|
|
|
|
if not value:
|
|
|
|
raise ValueError("Missing or empty %s in mycroft.conf " % name)
|