-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
153 lines (119 loc) · 4.21 KB
/
app.py
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
153
from flask import Flask, request, jsonify
import leds.led_main as led
import threading
import json
from os import environ
import logging
import PythonTelegramWraper.bot as BotWrapper
from telegram import InlineKeyboardButton
from telegram import InlineKeyboardMarkup
from telegram import ReplyKeyboardMarkup
from telegram import ReplyKeyboardRemove
from telegram.ext.filters import Filters
from telegram.ext import CallbackQueryHandler
import PythonTelegramWraper.config as config
import mqttUtil.mqttconfig as mqtt_config
app=Flask(__name__)
def die(*args: object):
raise SystemExit(*args)
def load_from_env(name: str, default=None) -> str:
return environ.get(name) or default or die(f"Need the {name} in env")
MQTT_BROKER: str = load_from_env("MQTT_BROKER",mqtt_config.MQTT_BROKER_ADDRESS)
MQTT_PORT: int = int(load_from_env("MQTT_PORT", "1883"))
MQTT_USER: str = load_from_env("MQTT_USER",mqtt_config.MQTT_USERNAME)
MQTT_PW: str = load_from_env("MQTT_PW",mqtt_config.MQTT_PASSWORD)
MQTT_TOPIC_DOOR_STATE: str = load_from_env("MQTT_TOPIC_DOOR_STATE")
LOGLEVEL = load_from_env('LOGLEVEL', 'INFO').upper()
logging.basicConfig(level=LOGLEVEL)
led.run(MQTT_BROKER, MQTT_PORT, MQTT_USER, MQTT_PW, MQTT_TOPIC_DOOR_STATE)
@app.route("/color", methods=['POST'])
def color():
''' Input as json in form of [r,g,b] with 0 <= r,g,b <= 255 '''
content=request.json
if content is not None:
led.led_state['color']=content
led.setLedType(4)
return "Ok"
return "Wrong format"
@app.route("/animationType", methods=['POST'])
def animationType():
'''Input as json in form of {"type":5} '''
content=request.json
led.setLedType(content['type']*2)
return "State ist now "+str(content['type'])
@app.route("/get")
def get():
print("test")
return "Hi"
@app.route("/doorOpen")
def doorOpen():
print("test")
return "Hi"
@app.route("/doorClosed")
def doorClosed():
print("test")
return "Hi"
@app.route("/printer", methods=['POST'])
def printer():
'''
Input as json in form of {"color":0.1} - Number in 0<=n<=1
0 = Email red
0.6 = Telegram blue
'''
content=request.json
led.led_state['printer-color']=content['color']
led.led_state['printerStart']=600
return "Ok"
@app.route("/getAnimations", methods=['GET'])
def animations():
'''
Return the available animations
'''
out=[]
for i in led.led_functions:
name=i.__module__
if name not in out:
out.append(name)
return str(out)
def admin(update, context):
user = update.message.from_user
chatID = BotWrapper.chatID(update)
print(chatID)
BotWrapper.sendMessage(chatID, "Request has been sent...")
button_list = [
InlineKeyboardButton("Ja", callback_data=chatID),
InlineKeyboardButton("Nein", callback_data="no")
]
reply_markup = InlineKeyboardMarkup(
BotWrapper.build_menu(button_list, n_cols=1))
message = '{} (@{}) wants to admin, accept request?'.format(
user['first_name'], user['username'])
BotWrapper.getBot().sendMessage(config.admin, message, reply_markup=reply_markup)
def adminResponse(update, context):
chatID = BotWrapper.chatID(update)
try:
BotWrapper.getBot().delete_message(chat_id=update.effective_chat.id,
message_id=update.effective_message.message_id)
except Exception as e:
print(e)
inp = str(update.callback_query.data)
if inp is not "no":
BotWrapper.modifyUser(int(inp), True)
BotWrapper.sendMessage(inp, "You have been accepted")
BotWrapper.sendMessage(chatID, "Request has been accepted")
else:
BotWrapper.sendMessage(chatID, "Request has been denied")
def modeChange(update, context):
chatID = BotWrapper.chatID(update)
if str(chatID) in BotWrapper.getUserData():
msg = update.message.text.split()[0][1:]
led.stringToMode(msg.lower())
BotWrapper.sendMessage(chatID, "Switched to "+str(msg))
BotWrapper.botBackend.dispatcher.add_handler(
CallbackQueryHandler(adminResponse))
BotWrapper.addBotCommand("admin", admin)
for i in led.modeSwitchCase:
print(i)
BotWrapper.addBotCommand(i,modeChange)
BotWrapper.startBot()
app.run(host="0.0.0.0")