-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpi_bot.py
More file actions
146 lines (126 loc) · 5.17 KB
/
pi_bot.py
File metadata and controls
146 lines (126 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import discord
from discord.ext import commands
from datetime import datetime
import sys
import os
import cv2
from queue import Queue
import subprocess
from pathlib import Path
from plane.peripherals.camera import load_calibration_data
from plane.peripherals.sensors import Sensors
import json
intents = discord.Intents.all()
client = commands.Bot(command_prefix=".", intents=intents)
class Buttons(discord.ui.View):
def __init__(self, *, timeout=None):
super().__init__(timeout=timeout)
self.process = None
self.confirm_stop = True
@discord.ui.button(label="Start plane.py", style=discord.ButtonStyle.green)
async def start_plane_button(
self, interaction: discord.Interaction, button: discord.ui.Button
):
# on click:
button.style = discord.ButtonStyle.grey
button.label = "Plane.py started"
button.disabled = True
self.reset_stop_button()
self.test_camera_button.disabled = (
True # don't allow taking pictures while plane is running
)
plane_py_path = Path(__file__).resolve().parent / "plane" / "plane.py"
plane_path = Path(__file__).resolve().parent / "plane"
command = ["python", str(plane_py_path)]
self.process = subprocess.Popen(command, cwd=plane_path)
await interaction.response.edit_message(view=self)
def reset_start_button(self):
self.start_plane_button.disabled = False
self.start_plane_button.style = discord.ButtonStyle.green
self.start_plane_button.label = "Start plane.py"
@discord.ui.button(
label="End plane.py", style=discord.ButtonStyle.grey, disabled=True
) # initially grey and disabled
async def stop_plane_button(
self, interaction: discord.Interaction, button: discord.ui.Button
):
button.label = "Plane.py ended"
button.style = discord.ButtonStyle.grey
button.disabled = True
# re-enable start button in case we want to run plane.py again
self.reset_start_button()
self.test_camera_button.disabled = False # allow taking pictures again
if self.process is not None:
self.process.terminate()
self.process = None
await interaction.response.edit_message(view=self)
def reset_stop_button(self):
self.stop_plane_button.disabled = False
self.stop_plane_button.style = discord.ButtonStyle.red
self.stop_plane_button.label = "End plane.py"
@discord.ui.button(
label="Test camera", style=discord.ButtonStyle.green, disabled=False
) # initially enabled, don't want it running while plane is running
async def test_camera_button(
self, interaction: discord.Interaction, button: discord.ui.Button
):
await interaction.response.defer()
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
camera.release()
cv2.imwrite("frame.jpg", frame)
file = discord.File("frame.jpg")
embed = discord.Embed(title="Picture taken")
embed.set_image(url="attachment://frame.jpg")
# Edit the message to add the picture
await interaction.channel.send(file=file, embed=embed, view=self)
@discord.ui.button(
label="Test sensors", style=discord.ButtonStyle.green, disabled=False
) # initially enabled, don't want it running while plane is running
async def test_sensors_button(
self, interaction: discord.Interaction, button: discord.ui.Button
):
await interaction.response.defer()
# try opening picam, otherwise open opencv cam
data = test_sensors()
await interaction.channel.send(f"Sensor data: {data}")
@discord.ui.button(
label="Shut down bot script", style=discord.ButtonStyle.red, disabled=False
)
async def kill_bot_script_button(
self, interaction: discord.Interaction, button: discord.ui.Button
):
if self.confirm_stop:
button.label = "Confirm shut down?"
self.confirm_stop = False
await interaction.response.edit_message(view=self)
else:
button.label = "Shut down."
button.style = discord.ButtonStyle.grey
await interaction.response.edit_message(view=self)
sys.exit(0)
def get_wifi_name():
subprocess_result = subprocess.Popen("iwgetid", shell=True, stdout=subprocess.PIPE)
subprocess_output = subprocess_result.communicate()[0], subprocess_result.returncode
network_name = subprocess_output[0].decode("utf-8")
return network_name
def test_sensors():
sensor_queue = Queue()
sensors = Sensors(sensor_queue)
data = sensors.read_data()
return json.dumps(data)
@client.event
async def on_connect():
print("Printout from Python: starting Discord Python Bot")
channel = client.get_channel(1226017894417043498) or await client.fetch_channel(
1226017894417043498
)
current_time = datetime.now()
# Format the time
formatted_time = current_time.strftime("%Y-%m-%d, %I,%M %p")
await channel.send(
f"Raspberry Pi is online at {formatted_time}.\n Network status: {get_wifi_name()}",
view=Buttons(),
)
token = os.environ["DISCORD_TOKEN"]
client.run(token)