Skip to content

Various fixes for issues flagged by the 'ruff check' linter. #37

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 16 commits into
base: main
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
22 changes: 22 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
target-version = "py310"
exclude = ["*_pb2.py"]

[lint]
select = [
"E", # pycodestyle
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"I", # isort
]
ignore = [
"B905", # new 'strict' argument for zip in python3.10
"E501", # ignore line length errors for now
"E701", # multiple statements on one line, will be fixed by format
"E722", # do not use bare except
"E741", # ambiguous variable name (seems to trigger on l, I, and such)
"F841", # unused local variable
"SIM105", # contextlib.suppress is 3x slower than try-except
"SIM115", # use context manager to open files (only works in some places)
]
5 changes: 3 additions & 2 deletions cnc/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#
# SPDX-License-Identifier: GPL-2.0-only

from gabriel_server.network_engine import server_runner
import logging
import argparse
import logging

from gabriel_server.network_engine import server_runner

DEFAULT_PORT = 9099
DEFAULT_NUM_TOKENS = 2
Expand Down
24 changes: 12 additions & 12 deletions cnc/server/swarm_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import argparse
import json
import logging
import os
import subprocess
import sys
import logging
from urllib.parse import urlparse
from zipfile import ZipFile
from google.protobuf.message import DecodeError
from google.protobuf import text_format

import redis
import requests
from cnc_protocol import cnc_pb2
import argparse
import zmq
import zmq.asyncio
import redis
from urllib.parse import urlparse

from cnc_protocol import cnc_pb2
from google.protobuf import text_format
from google.protobuf.message import DecodeError

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -102,7 +102,7 @@ def compile_mission(dsl_file, kml_file, drone_list, alt, compiler_file):

def send_to_drone(msg, base_url, drone_list, cmd_front_cmdr_sock, redis):
try:
logger.info(f"Sending request to drone...")
logger.info("Sending request to drone...")
# Send the command to each drone
for drone_id in drone_list:
# check if the cmd is a mission
Expand All @@ -117,7 +117,7 @@ def send_to_drone(msg, base_url, drone_list, cmd_front_cmdr_sock, redis):

# store the record in redis
key = redis.xadd(
f"commands",
"commands",
{"commander": msg.commander_id, "drone": drone_id, "value": text_format.MessageToString(msg),}
)
logger.debug(f"Updated redis under stream commands at key {key}")
Expand All @@ -136,7 +136,7 @@ def listen_cmdrs(cmdr_sock, cmd_front_cmdr_sock, redis, alt, compiler_file):
logger.info(f'Request received:\n{text_format.MessageToString(msg)}')
except DecodeError:
cmdr_sock.send(b'Error decoding protobuf. Did you send a cnc_pb2?')
logger.info(f'Error decoding protobuf. Did you send a cnc_pb2?')
logger.info('Error decoding protobuf. Did you send a cnc_pb2?')
continue

# get the drone list
Expand All @@ -146,7 +146,7 @@ def listen_cmdrs(cmdr_sock, cmd_front_cmdr_sock, redis, alt, compiler_file):
logger.info(f"drone list: {drone_list}")
except json.JSONDecodeError:
cmdr_sock.send(b'Error decoding drone list. Did you send a JSON list?')
logger.info(f'Error decoding drone list. Did you send a JSON list?')
logger.info('Error decoding drone list. Did you send a JSON list?')
continue

# Check if the command contains a mission and compile it if true
Expand Down
5 changes: 3 additions & 2 deletions cnc/server/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import argparse
import logging

from gabriel_server.network_engine import engine_runner
from telemetry_engine import TelemetryEngine
import logging
import argparse

SOURCE = 'telemetry'

Expand Down
19 changes: 10 additions & 9 deletions cnc/server/telemetry_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import time
import datetime
import logging
from gabriel_server import cognitive_engine
from gabriel_protocol import gabriel_pb2
from cnc_protocol import cnc_pb2
import redis
import os
from PIL import Image
import time

import cv2
import numpy as np
import redis
from cnc_protocol import cnc_pb2
from gabriel_protocol import gabriel_pb2
from gabriel_server import cognitive_engine
from PIL import Image

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
Expand All @@ -34,7 +35,7 @@ def __init__(self, args):
os.makedirs(self.storage_path+"/raw")
except FileExistsError:
logger.info("Images directory already exists.")
logger.info("Storing detection images at {}".format(self.storage_path))
logger.info(f"Storing detection images at {self.storage_path}")
self.current_path = None
self.publish = args.publish

Expand All @@ -56,7 +57,7 @@ def handle(self, input_frame):
result = None

if input_frame.payload_type == gabriel_pb2.PayloadType.TEXT:
if extras.drone_id is not "":
if extras.drone_id != "":
if extras.registering:
logger.info(f"Drone [{extras.drone_id}] connected.")
if not os.path.exists(f"{self.storage_path}/raw/{extras.drone_id}"):
Expand All @@ -69,7 +70,7 @@ def handle(self, input_frame):

result = gabriel_pb2.ResultWrapper.Result()
result.payload_type = gabriel_pb2.PayloadType.TEXT
result.payload = "Telemetry updated.".encode(encoding="utf-8")
result.payload = b"Telemetry updated."
self.updateDroneStatus(extras)

elif input_frame.payload_type == gabriel_pb2.PayloadType.IMAGE:
Expand Down
9 changes: 4 additions & 5 deletions cnc/server/test/streaming_test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import argparse
import socket
import time

import cv2
import numpy as np
import time
import zmq
import argparse
import sys

HOST=''
PORT=8485
Expand Down Expand Up @@ -90,8 +90,7 @@ def _main():
now = time.time()
if now - lastprint > 5:
print(
"avg fps: {0:.2f}".format(
(frames_received - lastcount) / 5)
f"avg fps: {(frames_received - lastcount) / 5:.2f}"
)
print()
lastcount = frames_received
Expand Down
29 changes: 12 additions & 17 deletions cnc/streamlit/commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import streamlit as st
import pandas as pd
from streamlit_autorefresh import st_autorefresh
import datetime
import os

import folium
from folium.plugins import Draw
from streamlit_folium import st_folium
from st_keypressed import st_keypressed
import streamlit_antd_components as sac
import redis
import os
from cnc_protocol import cnc_pb2
import streamlit as st
import zmq
import time
import numpy as np
import cv2
import datetime
from cnc_protocol import cnc_pb2
from st_keypressed import st_keypressed
from streamlit_autorefresh import st_autorefresh
from streamlit_folium import st_folium

st.set_page_config(
page_title="Commander",
Expand Down Expand Up @@ -193,7 +188,7 @@ def rth():
)

if st.session_state.manual_control:
st.subheader(f":blue[Manual Control Enabled]")
st.subheader(":blue[Manual Control Enabled]")
#st.subheader(":red[Manual Speed Controls]", divider="gray")
st.sidebar.slider(
key="pitch_speed",
Expand Down Expand Up @@ -228,9 +223,9 @@ def rth():
step=5,
)
elif st.session_state.rth_sent:
st.subheader(f":orange[Return to Home Initiated]")
st.subheader(":orange[Return to Home Initiated]")
elif st.session_state.script_file is not None:
st.subheader(f":violet[Autonomous Mode Enabled]")
st.subheader(":violet[Autonomous Mode Enabled]")


with c2:
Expand Down Expand Up @@ -347,7 +342,7 @@ def rth():

#st.write(f":keyboard: {st.session_state.key_pressed}")
st.session_state.key_pressed = st_keypressed()
if st.session_state.manual_control and st.session_state.selected_drone != None:
if st.session_state.manual_control and st.session_state.selected_drone is not None:
req = cnc_pb2.Extras()
req.commander_id = os.uname()[1]
req.cmd.for_drone_id = st.session_state.selected_drone
Expand Down
13 changes: 6 additions & 7 deletions cnc/streamlit/commander_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import numpy as np
from gabriel_protocol import gabriel_pb2
from gabriel_client.websocket_client import ProducerWrapper, WebsocketClient
import argparse
import logging
import os

import zmq
from cnc_protocol import cnc_pb2
import argparse
import os
import asyncio
from gabriel_client.websocket_client import ProducerWrapper, WebsocketClient
from gabriel_protocol import gabriel_pb2

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,7 +48,7 @@ async def producer():

extras = cnc_pb2.Extras()
extras.commander_id = self.commander_id
if command != None:
if command is not None:
extras.cmd.for_drone_id = command["drone_id"]
if "kill" in command:
extras.cmd.halt = True
Expand Down
29 changes: 17 additions & 12 deletions cnc/streamlit/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
#
# SPDX-License-Identifier: GPL-2.0-only

import json
import os
import time
import json
from zipfile import ZipFile
from cnc_protocol import cnc_pb2

import folium
import streamlit as st
from streamlit_folium import st_folium
from cnc_protocol import cnc_pb2
from folium.plugins import MiniMap
from util import stream_to_dataframe, connect_redis, connect_zmq, get_drones, menu, COLORS, authenticated
from st_keypressed import st_keypressed
from streamlit_folium import st_folium
from util import (
COLORS,
authenticated,
connect_redis,
connect_zmq,
get_drones,
menu,
stream_to_dataframe,
)

if "map_server" not in st.session_state:
st.session_state.map_server = "Google Hybrid"
Expand Down Expand Up @@ -69,7 +78,7 @@ def change_center():
f"telemetry.{st.session_state.tracking_selection}", "+", "-", 1
)
)
for index, row in df.iterrows():
for _index, row in df.iterrows():
st.session_state.center = [row["latitude"], row["longitude"]]


Expand Down Expand Up @@ -132,8 +141,7 @@ def update_imagery():
drone_list.append(hsv_header)
tabs = st.tabs(drone_list)

i = 0
for d in drone_list:
for i, d in enumerate(drone_list):
with tabs[i]:
if d == detected_header:
st.image(f"http://{st.secrets.webserver}/detected/latest.jpg?a={time.time()}", use_container_width=True)
Expand All @@ -143,7 +151,7 @@ def update_imagery():
st.image(f"http://{st.secrets.webserver}/detected/hsv.jpg?a={time.time()}", use_container_width=True)
else:
st.image(f"http://{st.secrets.webserver}/raw/{d}/latest.jpg?a={time.time()}", use_container_width=True)
i += 1

@st.fragment(run_every="1s")
def draw_map():
m = folium.Map(
Expand All @@ -164,9 +172,8 @@ def draw_map():
last_update = (int(df.index[0].split("-")[0])/1000)
if time.time() - last_update < st.session_state.inactivity_time * 60: # minutes -> seconds
coords = []
i = 0
drone_name = k.split(".")[-1]
for index, row in df.iterrows():
for i, (_index, row) in enumerate(df.iterrows()):
if i % 10 == 0:
coords.append([row["latitude"], row["longitude"]])
if i == 0:
Expand Down Expand Up @@ -203,8 +210,6 @@ def draw_map():
)
)

i += 1

ls = folium.PolyLine(locations=coords, color=COLORS[marker_color])
ls.add_to(tracks)
marker_color += 1
Expand Down
Loading