-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapimon.py
103 lines (74 loc) · 2.54 KB
/
apimon.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
import logging
import atexit
from flask import Flask, jsonify
from flask_apscheduler import APScheduler
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
from dotenv import load_dotenv
from app.jira_ticket_fetcher import JiraTicketFetcher
from app.ticket_led_mapper import TicketLedMapper
from app.gitinfo import GitInfo
#try:
from app.neopixel_controller import NeoPixelController
#except NotImplementedError:
# from app.nopixel_controller import NoPixelController as NeoPixelController
LED_COUNT = 40
git_info = GitInfo()
# Load environment variables from .env file
load_dotenv()
ticket_fetcher = JiraTicketFetcher()
ticket_led_mapper = TicketLedMapper(LED_COUNT)
neopixel_controller = NeoPixelController(LED_COUNT)
# set configuration values
class Config:
SCHEDULER_API_ENABLED = False
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
app.config.from_object(Config())
# initialize scheduler
scheduler = APScheduler()
def scheduler_listener(event):
if event.exception:
logging.error(f"Scheduler task {event.job_id} failed: {event.exception}")
neopixel_controller.set_error(True)
else:
neopixel_controller.set_error(False)
scheduler.add_listener(scheduler_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
scheduler.init_app(app)
scheduler.start()
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING)
@app.route('/', methods=['GET'])
def get_api_info():
return jsonify({
'name': __name__,
'git_info': {
'version': git_info.description,
'commit': git_info.commit,
'branch': git_info.branch,
},
'tickets': ticket_fetcher.tickets,
'leds': [color.tuple_str for color in neopixel_controller.leds],
'status': neopixel_controller.status.name,
})
@scheduler.task('cron', id='do_job_update_tickets', minute='*/1')
def job_update_tickets():
try:
ticket_fetcher.update_tickets()
except Exception as e:
logging.error(f'*** {e} ***')
neopixel_controller.set_connection_error(True)
return
else:
neopixel_controller.set_connection_error(False)
colors = ticket_fetcher.colors
ticket_led_mapper.set_ticket(colors)
leds = ticket_led_mapper.leds
neopixel_controller.set_leds(leds)
@scheduler.task('interval', id='do_job_update_pixels', seconds=0.1)
def job_update_pixels():
neopixel_controller.update()
def cleanup():
scheduler.shutdown()
atexit.register(cleanup)
job_update_tickets()
if __name__ == '__main__':
app.run()