Skip to content

Config refactor #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def unregister(func, *targs, **kargs):
sys.exit()

try:
with open('controller.conf', 'r') as fp:
robot_config.readfp(fp)
# Values in controller.conf will overwrite values in controller.sample.conf
robot_config.read(['controller.sample.conf', 'controller.conf'])
except IOError:
print("unable to read controller.conf, please check that you have copied controller.sample.conf to controller.conf and modified it appropriately.")
sys.exit()
Expand Down
4 changes: 4 additions & 0 deletions controller.sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ num_backup=2
[misc]
# host server to connect to
server = remo.tv
# API server to connect to. This is needed for testing on local instances.
api_server = remo.tv
# API version is the version of the API.
api_version = dev
# video server to connect to so you can use local websocket and remote video server
Expand All @@ -362,6 +364,8 @@ watchdog = True
# Enable async handling of commands, your handler will either need to be able to
# function asyncronously or it will need it's own blocking.
enable_async=False
# Local instances will not use secure HTTP or WebSockets
secure_server=True

# Periodically check internet connection status, and report failure / connect
# over tts
Expand Down
82 changes: 60 additions & 22 deletions networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
import schedule

if (sys.version_info > (3, 0)):
# import _thread as thread
# import _thread as thread
import urllib.request as urllib2
else:
# import thread
import urllib2 #pylint: disable=import-error
# import thread
import urllib2 # pylint: disable=import-error

log = logging.getLogger('RemoTV.networking')

robot_key = None
webSocket = None
server = None
api_server = None
secure_server = None
version = None
channel = None
channel_id = None
Expand All @@ -40,37 +42,48 @@

ood = None


def getChatChannels(host):
url = 'https://%s/api/%s/channels/list/%s' % (server, version, host)
if (secure_server):
url = 'https://%s/api/%s/channels/list/%s' % (api_server, version, host)
else:
url = 'http://%s/api/%s/channels/list/%s' % (api_server, version, host)
response = robot_util.getWithRetry(url)
log.debug("getChatChannels : %s", response)
return json.loads(response)


def waitForWebSocket():
while True:
try:
webSocket.run_forever()
except AttributeError:
log.warning("Warning: Web Socket not connected.")


def startListenForWebSocket():
global webSocket

watchdog.start("WebSocketListen", waitForWebSocket)


def onHandleWebSocketOpen(ws):
ws.send(json.dumps({"e": "AUTHENTICATE_ROBOT", "d": {"token": robot_key}}))
log.debug(json.dumps({"e": "AUTHENTICATE_ROBOT", "d": {"token": robot_key}}))
log.debug(json.dumps(
{"e": "AUTHENTICATE_ROBOT", "d": {"token": robot_key}}))
log.info("websocket connected")


def onHandleWebSocketClose(ws):
global authenticated
authenticated = False
log.info("websocket disconnect")


def onHandleWebSocketError(ws, error):
log.error("WebSocket ERROR: {}".format(error))


def handleConnectChatChannel(host):
global channel_id
global chat
Expand All @@ -84,31 +97,37 @@ def handleConnectChatChannel(host):
if key["name"] == channel:
channel_id = key["id"]
chat = key["chat"]
log.info("channel {} found with id : {}".format(channel, channel_id))
log.info("channel {} found with id : {}".format(
channel, channel_id))
break

if channel_id == None:
channel_id = response["channels"][0]["id"]
chat = response["channels"][0]["chat"]
log.warning("channel {} NOT found, joining : {}".format(channel, channel_id))
log.warning("channel {} NOT found, joining : {}".format(
channel, channel_id))

webSocket.send(json.dumps(
{"e": "JOIN_CHANNEL", "d": channel_id}))
webSocket.send(json.dumps(
{"e": "GET_CHAT", "d": chat}))
authenticated = True


def checkWebSocket():
if not authenticated:
log.critical("Websocket failed to connect or authenticate correctly")
robot_util.terminate_controller()


def setupWebSocket(robot_config, onHandleMessage):
global robot_key

global bootMessage
global webSocket
global server
global api_server
global secure_server
global version
global ipAddr
global ood
Expand All @@ -117,6 +136,8 @@ def setupWebSocket(robot_config, onHandleMessage):

robot_key = robot_config.get('robot', 'robot_key')
server = robot_config.get('misc', 'server')
api_server = robot_config.get('misc', 'api_server')
secure_server = robot_config.getboolean('misc', 'secure_server')

if robot_config.has_option('misc', 'api_version'):
version = robot_config.get('misc', 'api_version')
Expand All @@ -133,7 +154,8 @@ def setupWebSocket(robot_config, onHandleMessage):
if robot_config.has_option('tts', 'announce_ip'):
ipAddr = robot_config.getboolean('tts', 'announce_ip')
if ipAddr:
addr = os.popen("ip -4 addr show wlan0 | grep -oP \'(?<=inet\\s)\\d+(\\.\\d+){3}\'").read().rstrip()
addr = os.popen(
"ip -4 addr show wlan0 | grep -oP \'(?<=inet\\s)\\d+(\\.\\d+){3}\'").read().rstrip()
log.info('IPv4 Addr : {}'.format(addr))
bootMessage = bootMessage + ". My IP address is {}".format(addr)

Expand All @@ -144,47 +166,63 @@ def setupWebSocket(robot_config, onHandleMessage):
if "behind" in isOod:
commits = re.search(r'\d+(\scommits|\scommit)', isOod)
log.warning('Git repo is out of date. Run "git pull" to update.')
bootMessage = bootMessage + ". Git repo is behind by {}.".format(commits.group(0))
bootMessage = bootMessage + \
". Git repo is behind by {}.".format(commits.group(0))

# log.info("using socket io to connect to control %s", controlHostPort)
log.info("configuring web socket wss://%s/" % server)
webSocket = websocket.WebSocketApp("wss://%s/" % server,
on_message=onHandleMessage,
on_error=onHandleWebSocketError,
on_open=onHandleWebSocketOpen,
on_close=onHandleWebSocketClose)
log.info("configuring web socket wss://%s/" % api_server)
if (secure_server):
webSocket = websocket.WebSocketApp("wss://%s/" % api_server,
on_message=onHandleMessage,
on_error=onHandleWebSocketError,
on_open=onHandleWebSocketOpen,
on_close=onHandleWebSocketClose)
else:
webSocket = websocket.WebSocketApp("ws://%s/" % api_server,
on_message=onHandleMessage,
on_error=onHandleWebSocketError,
on_open=onHandleWebSocketOpen,
on_close=onHandleWebSocketClose)
log.info("staring websocket listen process")
startListenForWebSocket()

schedule.single_task(5, checkWebSocket)

if robot_config.getboolean('misc', 'check_internet'):
#schedule a task to check internet status
schedule.task(robot_config.getint('misc', 'check_freq'), internetStatus_task)
# schedule a task to check internet status
schedule.task(robot_config.getint(
'misc', 'check_freq'), internetStatus_task)


def sendChatMessage(message):
log.info("Sending Message : %s" % message)
webSocket.send(json.dumps(
{"e": "ROBOT_MESSAGE_SENT",
{"e": "ROBOT_MESSAGE_SENT",
"d": {"message": "%s" % message,
"chatId": "%s" % chat,
"server_id": "%s" % server,
"channel_id": "%s" % channel_id
}
}))
}
}))


def isInternetConnected():
try:
url = 'https://{}'.format(server)
if (secure_server):
url = 'https://{}'.format(server)
else:
url = 'http://{}'.format(server)
urllib2.urlopen(url, timeout=1)
log.debug("Server Detected")
return True
except urllib2.URLError as err:
log.debug("Server NOT Detected {}".format(url))
return False


lastInternetStatus = False


def internetStatus_task():
global bootMessage
global lastInternetStatus
Expand Down