# # appManager.py # shotmanager # # Handles app connection state and IO. # # Created by Will Silva on 3/5/2016. # Copyright (c) 2016 3D Robotics. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import errno import os import platform import select import socket import string import sys import threading import monotonic import time sys.path.append(os.path.realpath('')) import modes import settings import shots import Queue import struct from dronekit.lib import LocationGlobalRelative from sololink import btn_msg import app_packet import GoProManager import shotLogger import GeoFenceManager logger = shotLogger.logger APP_SERVER_PORT = 5507 APP_TCP_BUFSIZE = 1024 class appManager(): def __init__(self, shotMgr): self.shotMgr = shotMgr self.connected = False self.client = None self.client_address = None self.clientQueue = None self.packetBuffer = "" self.bindServer() def bindServer(self): self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if platform.system() != 'Darwin': self.server.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # After 1 second, start KEEPALIVE self.server.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1) # TCP Idle true self.server.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 5) # 5 seconds in between keepalive pings self.server.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5) # 5 max fails # Bind the socket to the port while True: try: self.server.bind(('', APP_SERVER_PORT)) except: logger.log("[app]: Can't bind, address in use. Retrying in 1 second.") time.sleep(1.0) else: break logger.log("[app]: Ready for connections from app.") # Listen for incoming connections self.server.listen(0) # establishes a connection to a Solo app def connectClient(self): client, client_address = self.server.accept() if self.isAppConnected(): if client_address[0] != self.client_address[0]: logger.log("[app]: Already connected to client %s - rejecting %s." % (self.client_address, client_address)) #TO DO:send system INFO packet to app with rejection flag True client.close() return else: logger.log("[app]: Detected re-connection attempt for client %s - reconnecting.") self.disconnectClient() self.client = client self.client_address = client_address logger.log("[app]: Connected to App. %s" % (self.client_address,)) #TO DO:send system INFO packet to app with rejection flag False self.connected = True self.client.setblocking(0) self.shotMgr.inputs.append(self.client) self.clientQueue = Queue.Queue() self.broadcastShotToApp(self.shotMgr.currentShot) self.shotMgr.buttonManager.setButtonMappings() # called to un-grey out Artoo buttons self.shotMgr.goproManager.sendState() # send gopro state to app def disconnectClient(self): if self.isAppConnected(): logger.log("[app]: Closing client connection with %s." % (self.client_address,)) self.connected = False if self.client in self.shotMgr.outputs: self.shotMgr.outputs.remove(self.client) if self.client in self.shotMgr.inputs: self.shotMgr.inputs.remove(self.client) self.client.close() self.client = None self.clientQueue = None self.shotMgr.buttonManager.setButtonMappings() # called to grey-out Artoo buttons # if this type of shot requires a client present at all times, then kill the shot if self.shotMgr.currentShot in shots.ALWAYS_NEEDS_APP_CONNECTION: self.shotMgr.enterShot(shots.APP_SHOT_NONE) # Clear Geofence when app disconnect self.shotMgr.geoFenceManager.clearGeoFence() else: logger.log('[app]: Attempted to close app connection, but no app was connected!') def isAppConnected(self): return self.connected def sendPacket(self, pkt): if self.isAppConnected(): self.clientQueue.put(pkt) if self.client not in self.shotMgr.outputs: self.shotMgr.outputs.append(self.client) else: logger.log('[app]: Can\'t send packet - app is not connected!') def broadcastShotToApp(self, shot): packet = struct.pack('