Last active
April 18, 2017 23:08
-
-
Save DrWateryCat/cc80c3e0070d13214fcd92cb92889260 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import socket | |
import logging | |
from components import drive | |
class Minimap: | |
''' | |
classdocs | |
''' | |
DRIVER_STATION = "10.21.86.5" | |
DRIVER_STATION_PORT = 5800 | |
drive = drive.Drive | |
def setup(self): | |
self.logger = logging.getLogger("minimap") | |
self.usable = False | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
i = 5 | |
while not self.usable and i > 0: | |
self._try_connect(i) | |
i -= 1 | |
self.logger.critical("Could not connect, please start the dashboard!") | |
def _try_connect(self, i): | |
address = (self.DRIVER_STATION, self.DRIVER_STATION_PORT) | |
self.logger.info("Creating client socket for minimap") | |
try: | |
self.socket.connect(address) | |
self.usable = True | |
return True | |
except Exception as e: | |
self.usable = False | |
self.logger.warning("Could not connect to minimap, are you running the dashboard?") | |
self.logger.warning("Will attempt to reconnect " + i + " more times.") | |
return False | |
def _try_connect_no_retry(self): | |
address = (self.DRIVER_STATION, self.DRIVER_STATION_PORT) | |
self.logger.info("Creating client socket for minimap") | |
try: | |
self.socket.connect(address) | |
self.usable = True | |
return False | |
except Exception as e: | |
self.usable = False | |
self.logger.warning("Could not connect to minimap, are you running the dashboard?") | |
return False | |
def _format_data(self, data): | |
data_dict = { | |
"posX": data[0], | |
"posY": data[1], | |
"posZ": data[2] | |
} | |
return json.dumps(data_dict) | |
def execute(self): | |
if not self.usable: | |
self._try_connect_no_retry() | |
else: | |
formatted_data = self._format_data(self.drive.get_position()) | |
try: | |
self.socket.sendall(formatted_data) | |
except Exception as e: | |
self.logger.warning("Did the socket disconnect?") | |
self.logger.warning("error: " + e) | |
self.usable = False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment