-
Notifications
You must be signed in to change notification settings - Fork 1
/
RattAppEngine.py
259 lines (207 loc) · 10.3 KB
/
RattAppEngine.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------------
# _____ ______________
# | __ \ /\|__ ____ __|
# | |__) | / \ | | | |
# | _ / / /\ \ | | | |
# | | \ \/ ____ \| | | |
# |_| \_\/ \_\_| |_| ... RFID ALL THE THINGS!
#
# A resource access control and telemetry solution for Makerspaces
#
# Developed at MakeIt Labs - New Hampshire's First & Largest Makerspace
# http://www.makeitlabs.com/
#
# Copyright 2018 MakeIt Labs
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
#
# Author: Steve Richardson ([email protected])
#
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QVariant, QEvent, Qt, QCoreApplication, QUrl
from PyQt5 import QtCore
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType
from PyQt5.QtGui import QKeyEvent
from RattConfig import RattConfig
from NetWorker import NetWorker
from ACL import ACL
from MqttClient import MqttClient
from RFID import RFID
from Logger import Logger
import sys
import subprocess
import argparse
class RattAppEngine(QQmlApplicationEngine):
restart = pyqtSignal()
def __init__(self):
QQmlApplicationEngine.__init__(self)
parser = argparse.ArgumentParser(description='RATT Application (Personality state machine and GUI).')
parser.add_argument('--ini', dest='inifile', default='/data/ratt/ratt.ini', help='path to .ini file e.g. /tmp/ratt-test.ini, default is /data/ratt/ratt.ini')
args = parser.parse_args()
# Name of the Mender artifact
self.mender_artifact = 'unknown'
# Name of the app version
self.app_version = 'unknown'
# load the config
self.config = RattConfig(inifile=args.inifile, loglevel='DEBUG')
self.config.configChanged.connect(self.slotConfigChanged)
self.config.configError.connect(self.slotConfigError)
# create parent logger which will optionally log to file and console, and opionally enable Qt logging
self.logger = Logger(name='ratt',
filename=self.config.value('Log.File'), stream=self.config.value('Log.Console'),
qt=self.config.value('Log.Qt'), qtVerbose=self.config.value('Log.QtVerbose'))
self.logger.setLogLevelStr(self.config.value('Log.LogLevel'))
self.logger.info('Initializing RATT System')
self.debug = self.logger.isDebug()
self.__bootstrapSystem__()
def __setContextProperties__(self, reinit = False):
# create context properties so certain objects can be accessed from QML
if not reinit:
self.rootContext().setContextProperty("appEngine", self)
self.rootContext().setContextProperty("logger", self.logger)
self.rootContext().setContextProperty("config", self.config)
self.rootContext().setContextProperty("netWorker", self._netWorker)
self.rootContext().setContextProperty("acl", self._acl)
self.rootContext().setContextProperty("rfid", self._rfid)
self.rootContext().setContextProperty("mqtt", self.mqtt)
self.rootContext().setContextProperty("menderArtifact", self.mender_artifact)
self.rootContext().setContextProperty("appVersion", self.app_version)
self.rootContext().setContextProperty("personality", self.personality)
def __startSystem__(self):
# initialize the node personality and the other necessary modules
self.__initSystem__()
self.__initPersonality__()
self.__setContextProperties__()
# temporary for test; will move somewhere else eventually
self._acl.download()
# begin executing the personality state machine
self.personality.execute()
self.load("gui/main.qml")
@pyqtSlot()
def slotConfigChanged(self):
if self.config.haveInitialRemoteConfig:
self.logger.info('CONFIG CHANGED - RE-INIT')
#robjs = self.rootObjects()
#if robjs is not None:
# for ro in robjs:
# ro.close()
self.exit.emit(0)
else:
self.__startSystem__()
@pyqtSlot()
def slotConfigError(self):
self.__startSystem__()
def __initPersonality__(self):
# dynamically import and instantiate the correct 'Personality' class, which contains the specific logic
# implementation for a given tool
personalityClass = 'Personality' + self.config.value('Personality.Class')
try:
sys.path.append('personalities')
module = __import__(personalityClass)
self.personality = module.Personality(loglevel=self.config.value('Personality.LogLevel'), app=self)
except:
self.logger.exception('could not establish personality: ' + personalityClass)
exit(-1)
def __bootstrapSystem__(self):
# MQTT module, for publishing and subscribing to the MQTT broker
self._mqtt = MqttClient(loglevel=self.config.value('MQTT.LogLevel'),
baseTopic=self.config.value('MQTT.BaseTopic'))
# NetWorker handles fetching and maintaining ACLs, configs, and other network functions
self._netWorker = NetWorker(loglevel=self.config.value('Auth.LogLevel'),
ifcName=self.config.value('General.NetworkInterfaceName'),
nodeId=self.config.value('General.NodeId'),
mqtt=self._mqtt)
self._netWorker.setSSLClientCertConfig(enabled=self.config.value('HTTPS.ClientCertsEnabled'),
caCertFile=self.config.value('HTTPS.CaCertFile'),
clientCertFile=self.config.value('HTTPS.ClientCertFile'),
clientKeyFile=self.config.value('HTTPS.ClientKeyFile'))
self._netWorker.setAuth(user=self.config.value('Auth.HttpAuthUser'),
password=self.config.value('Auth.HttpAuthPassword'))
# setting the netWorker in the config module will trigger the load of the second stage config (remote)
self.config.setNetWorker(netWorker=self._netWorker)
# set the mqtt object for the config engine, this allows triggering of config reloads
self.config.setMQTT(mqtt=self._mqtt)
def __initSystem__(self):
# try to read mender artifact name from filesystem. this is the version of the base system that is running.
try:
with open('/etc/mender/artifact_info') as f:
for line in f:
(k,v) = line.split('=')
if k == 'artifact_name':
self.mender_artifact = v
self.logger.info('Mender artifact is %s' % self.mender_artifact)
except:
pass
try:
with open('./version_info') as f:
for line in f:
self.app_version = line
self.logger.info('App version is %s' % self.app_version)
except:
pass
# Access Control List module, for maintaining the database of allowed users for this resource
self._acl = ACL(loglevel=self.config.value('Auth.LogLevel'),
netWorker=self._netWorker,
mqtt=self._mqtt,
url=self.config.value('Auth.AclUrl'),
cacheFile=self.config.value('Auth.AclCacheFile'))
# RFID reader
self._rfid = RFID(portName=self.config.value('RFID.SerialPort'),
loglevel=self.config.value('RFID.LogLevel'))
self._rfid.monitor()
# Initialize and connect MQTT client
nid = self.config.value('General.NodeId')
if nid is None:
nid = self._netWorker.currentNodeId
self._mqtt.init_client(hostname=self.config.value('MQTT.BrokerHost'),
port=self.config.value('MQTT.BrokerPort'),
reconnectTime=self.config.value('MQTT.ReconnectTime'),
nodeId=nid,
sslEnabled=self.config.value('MQTT.SSLEnabled'),
caCertFile=self.config.value('MQTT.CACertFile'),
clientCertFile=self.config.value('MQTT.ClientCertFile'),
clientKeyFile=self.config.value('MQTT.ClientKeyFile'))
def shutdown(self):
self.logger.info('Shutting down.')
# de-init the personality
self.personality.deinit()
# stop raspi2fb
subprocess.run(['systemctl', 'stop', 'raspi2fb'])
# shut down system
subprocess.run(['/sbin/shutdown', '-h', 'now'])
self.quit.emit()
@property
def rfid(self):
return self._rfid
@property
def netWorker(self):
return self._netWorker
@property
def acl(self):
return self._acl
@property
def mqtt(self):
return self._mqtt
@pyqtSlot(int, bool)
def syntheticKeypressHandler(self, keycode, pressed):
evt = QKeyEvent(QEvent.KeyPress if pressed else QEvent.KeyRelease, keycode, Qt.NoModifier)
objs = self.rootObjects()
if len(objs) == 1:
QCoreApplication.postEvent(objs[0], evt)