forked from BernhardSchlegel/BierBot-Bricks-RaspberryPi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
152 lines (127 loc) · 5.31 KB
/
setup.py
File metadata and controls
152 lines (127 loc) · 5.31 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
147
148
149
150
151
152
import os
import time
import uuid
import click
import yaml
from w1thermsensor import W1ThermSensor
config = {
"meta": {"created": int(round(time.time() * 1000)), "platform": ""},
"apikey": "",
"device_id": "",
"temperature_sensors": [],
"relays": [],
"start_fullscreen": False,
}
@click.command()
@click.option(
"--apikey",
"-a",
prompt="Please enter your API key from bricks.bierbot.com",
required=1,
)
@click.option(
"--platform",
"-p",
type=click.Choice(["RaspberryPi", "other"]),
multiple=False,
show_default=True,
default="RaspberryPi",
prompt="Select the platform you're on. Hit RETURN for RaspberryPi!",
required=1,
)
@click.option("--relays", "-r", prompt="How many relays do you want to configure?", required=1)
def main(apikey: str, platform: str, relays: int) -> None:
"""Simple program that greets NAME for a total of COUNT times."""
config["meta"]["platform"] = platform # type: ignore [attr-defined,index]
for i in range(0, int(relays)):
gpio = click.prompt(
f"Please enter the GPIO number for relay {i+1} (e.g. GPIO26 would be 37)",
type=click.INT,
)
invert = click.prompt(f"Do you want to invert relay {i+1}?", default="n", type=click.BOOL)
click.echo(f"setting relais {i+1} to GPRIO{gpio} (inverted={invert})..")
config["relays"].append({"gpio": gpio, "invert": invert}) # type: ignore [attr-defined,index]
scan = click.confirm(f"Do you want to scan for temperature probes now?", default=True)
n_temperature_probes_found = 0
if scan:
temperature_sensor_ids = []
for sensor in W1ThermSensor.get_available_sensors():
click.echo("Sensor found: %s (T=%.2f°C)" % (sensor.id, sensor.get_temperature()))
temperature_sensor_ids.append(sensor.id)
n_temperature_probes_found = len(temperature_sensor_ids)
click.echo(f"{n_temperature_probes_found} temperature probes found")
for tsId in temperature_sensor_ids:
click.echo(f"saving sensor {tsId} to config..")
config["temperature_sensors"].append(tsId) # type: ignore [attr-defined]
config["apikey"] = apikey
config["device_id"] = "python_" + platform + "_" + str(uuid.uuid1())
create_autostart = click.prompt(
f"Do you want us to add the BierBot Bricks service to autostart / bootup?",
default="y",
type=click.BOOL,
)
if create_autostart:
click.echo("creating autostart...")
current_directory = os.getcwd()
click.echo("creating autostart file ./sys/bierbot.service...")
# Using readlines()
template_file = open("./sys/bierbot.service.template", "r")
lines = template_file.readlines()
lines_ready = [line.replace("$$$REPO_ROOT$$$", current_directory) for line in lines]
template_file.close()
# writing to file
out_file = open("./sys/bierbot.service", "w")
out_file.writelines(lines_ready)
out_file.close()
click.echo("copying service file to final location...")
res = os.system("sudo cp ./sys/bierbot.service /etc/systemd/system/bierbot.service")
click.echo(f"returned {res}. OK={res==0}")
res = os.system("sudo chmod 644 /etc/systemd/system/bierbot.service")
click.echo(f"chmodding went OK={res==0}")
click.echo("enabling autostart...")
res = os.system("sudo systemctl enable bierbot.service")
click.echo(f"returned {res}. OK={res==0}")
start_fullscreen = False
start_ui = click.prompt(
f"Do you the BierBot Bricks UI to be started on startup?",
default="y",
type=click.BOOL,
)
if create_autostart and start_ui:
start_fullscreen = click.prompt(
f"Do you want the status screen to be started in fullscreen?",
default=True,
type=click.BOOL,
)
if start_fullscreen:
# sudo tee necessary instead of &>> because of "sudo" requirements
os.system(
'echo "chromium-browser --start-fullscreen --disable-session-crashed-bubble --disable-infobars '
'https://bricks.bierbot.com/#/status" | sudo tee -a /etc/xdg/lxsession/LXDE-pi/autostart'
)
else:
os.system(
'echo "chromium-browser --disable-session-crashed-bubble --disable-infobars '
'https://bricks.bierbot.com/#/status" | sudo tee -a /etc/xdg/lxsession/LXDE-pi/autostart'
)
config["start_ui"] = start_ui
config["start_fullscreen"] = start_fullscreen
config["meta"]["create_autostart"] = create_autostart # type: ignore [index]
if n_temperature_probes_found + int(relays) > 3:
click.secho(
"WARNING: Currently, only 3 interfaces (Relay + Temperaure) are supported in the FREE tier.",
fg="yellow",
bold=True,
)
with open("bricks.yaml", "w") as outfile:
yaml.dump(config, outfile, default_flow_style=False)
click.echo("config file bricks.yml created.")
reboot = click.confirm(
f"all done. Setup will exit. Do you want to reboot your {platform} (recommended)?",
default=True,
)
if reboot:
click.echo("rebooting now")
res = os.system("sudo shutdown -r now")
if __name__ == "__main__":
main()