-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathask_prog.py
124 lines (107 loc) · 3.51 KB
/
ask_prog.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
# Copyright 2017 loblab
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import sys
import time
import signal
import os.path
import argparse
import RPi.GPIO as GPIO
from bitstring import *
from ask_signal import *
from ask_device import *
from ask_config import *
VERSION = "Ver 0.7, 5/26/2018, loblab"
class ProgramBase:
def __init__(self, name, description):
self.name = name
self.commands = {}
self.quit_flag = False
signal.signal(signal.SIGINT, self.sig_handler)
signal.signal(signal.SIGTERM, self.sig_handler)
self.argps = argparse.ArgumentParser(description=description)
self.argps.add_argument('-v', '--version', action='version', version=VERSION)
self.init_arguments()
self.args = self.argps.parse_args()
self.load_commands()
GPIO.setmode(GPIO.BCM)
self.init_device()
def cleanup(self):
GPIO.cleanup()
def log_msg(self, msg):
tstr = time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(time.time()))
with open(LOG_FILE, 'a') as fp:
fp.write("%s [%s] - %s\r\n" % (tstr, self.name, msg))
def sig_handler(self, signum, frame):
msg = "Got system signal %d... quit now." % signum
print(msg)
if self.args.log:
self.log_msg(msg)
self.quit_flag = True
def load_commands(self):
for key in ASK_CMD:
cfg = ASK_CMD[key]
self.commands[key] = Signal(cfg)
def find_command(self, signal):
for key in self.commands:
if signal == self.commands[key]:
return key
return 'Unknown'
def list_commands(self, debug=0):
if debug == 0:
print("Commands:", end=' ')
for key in sorted(self.commands):
if debug == 0:
print(key, end=' ')
elif debug >= 1:
print("CMD %9s:" % key, end=' ')
self.commands[key].show()
if debug == 0:
print()
def run_action(self, cmd):
script = ASK_ACTION.get(cmd)
if script:
os.system(script)
def next_file_index(self):
i = 0
while True:
i += 1
filename = DATA_FILE % i
if not os.path.isfile(filename):
return i
return 0
def list_files(self, debug=0):
i = 0
while not self.quit_flag:
i += 1
filename = DATA_FILE % i
if not os.path.isfile(filename):
break
if debug >= 1:
sig = Signal()
with open (filename, 'rb') as fp:
sig.load(fp)
print("FILE %6s:" % i, end=' ')
sig.show()
if (i == 1):
print("No file as %s" % DATA_FILE)
return
if debug == 0:
print("Files: 1~%d" % (i - 1))
def main(self):
try:
rc = self.process()
finally:
self.cleanup()
return rc