-
Notifications
You must be signed in to change notification settings - Fork 1
/
exitcondition.py
90 lines (68 loc) · 1.99 KB
/
exitcondition.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
'''
This script handles what the program does on a hard error or GPIO intterupt signals using the ExitHooks class.
[Ensure we can do a controlled exit when pressing ctrl+c]
'''
### Failure on exit params
import sys,time,datetime,traceback
import RPi.GPIO as GPIO
from .log_manager import getlog
log = getlog(__file__)
'''
test script for hardware interrupt
Connect the bottom right pin (GPIO 21) to ground (bottom left) and then release
'''
# for GPIO numbering, choose BCM
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
'''
On Hard Exit
'''
class ExitHooks(object):
def __init__(self):
'''
Hooks to control program termination.
'''
self.exit_code = None
self.exception = None
def hook(self):
self._orig_exit = sys.exit
sys.exit = self.exit
sys.excepthook = self.exc_handler
def exit(self, code=0):
self.exit_code = code
self._orig_exit(code)
def exc_handler(self, exc_type, exc, *args):
self.exception = exc
log.critical("".join(traceback.format_exception(exc_type, exc, *args)))
hooks = ExitHooks()
hooks.hook()
def onexit():
import os
if hooks.exit_code is not None:
log.critical("death by sys.exit(%d)" % hooks.exit_code)
elif hooks.exception is not None:
log.critical("death by exception: %s" % hooks.exception)
else:
log.critical("natural death")
log.print('Attempting to exit in a controlled Manner \n',datetime.datetime.now(),'\n')
from . import R1
try : R1.alpha.off()
except:None
from . import db
try:db.conn.commit()
except db.sqlite3.ProgrammingError: None
try:db.conn.close()
except:None
try:
from . import gps
gps.pinoff()
except:None
from . import power
power.ledon()
try:
from ..oled import standby
standby(message = " -- ZAAPP. -- ")
except ImportError:None
sys.exit('Ending Python')
import atexit
atexit.register(onexit)