-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinit.py
More file actions
executable file
·253 lines (225 loc) · 9.01 KB
/
init.py
File metadata and controls
executable file
·253 lines (225 loc) · 9.01 KB
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
#
# Filename: init.py
# Version : 0.1
# Author : Lisa Esselink
# Purpose : Init App to control and read SOC's
# Usage : Initialize the app
#
# Copyright (c) Lisa Esselink. All rights reserved.
# Licensend under the Creative Commons Attribution-NonCommercial 4.0 International License.
# See LICENSE file in the project root for full license information.
#
import uos, gc, ulog
from . import core
from . import db
from .utils import utils
from .db import _dbc
from .hal import hal
from .plugin import plugins
from .protocol import protocol
from .script import scripts
class init (object):
def __init__(self, debug=2):
logger = ulog.get_config()
logger['name'] = core.initial_upyeasyname
self._log = ulog.Log(logger)
core._log = self._log
self.debug = debug
#Set initial log levels
self._log.changelevel('syslog',2)
self._log.changelevel('console',debug)
self._log.changelevel('log',2)
core._log.debug("Init: Init constructor")
def init(self):
# Init Config
self._log.debug("Init: Entering init")
# Init utils
core._utils = utils()
\
# init hal
self._hal = hal()
core._hal = self._hal
self._log.changehal(self._hal)
# create dir structure
root_dir = ""
try:
self._log.debug("Init: Create directory config")
uos.mkdir(root_dir+"config")
except OSError as e:
self._log.debug("Init: Create directory config exception: "+repr(e))
# create dir structure
root_dir = ""
try:
self._log.debug("Init: Create directory plugins")
uos.mkdir(root_dir+"plugins")
except OSError as e:
self._log.debug("Init: Create directory plugins exception: "+repr(e))
try:
self._log.debug("Init: Create directory protocols")
uos.mkdir(root_dir+"protocols")
except OSError as e:
self._log.debug("Init: Create directory protocols exception: "+repr(e))
try:
self._log.debug("Init: Create directory scripts")
uos.mkdir(root_dir+"scripts")
except OSError as e:
self._log.debug("Init: Create directory scripts exception: "+repr(e))
try:
self._log.debug("Init: Create directory rules")
uos.mkdir(root_dir+"rules")
except OSError as e:
self._log.debug("Init: Create directory rules exception: "+repr(e))
gc.collect()
#connect to database
_dbc.connect()
#init ONLY!
try:
self._log.debug("Init: config Table")
db.configTable.create_table(True)
except OSError as e:
self._log.error("Init: config Table exception: "+repr(e))
try:
self._log.debug("Init: network Table")
db.networkTable.create_table(True)
except OSError as e:
self._log.error("Init: network Table exception: "+repr(e))
try:
self._log.debug("Init: protocol Table")
db.protocolTable.create_table(True)
except OSError as e:
self._log.error("Init: protocol Table exception: "+repr(e))
try:
self._log.debug("Init: controller Table")
db.controllerTable.create_table(True)
except OSError as e:
self._log.error("Init: controller Table exception: "+repr(e))
try:
self._log.debug("Init: hardware Table")
db.hardwareTable.create_table(True)
except OSError as e:
self._log.error("Init: hardware Table exception: "+repr(e))
try:
self._log.debug("Init: dxpin Table")
db.dxpinTable.create_table(True)
except OSError as e:
self._log.error("Init: dxpin Table exception: "+repr(e))
try:
self._log.debug("Init: dxmap Table")
db.dxmapTable.create_table(True)
except OSError as e:
self._log.error("Init: dxmap Table exception: "+repr(e))
try:
self._log.debug("Init: plugin Table")
db.pluginTable.create_table(True)
except OSError as e:
self._log.error("Init: plugin Table exception: "+repr(e))
try:
self._log.debug("Init: pluginstore Table")
db.pluginstoreTable.create_table(True)
except OSError as e:
self._log.error("Init: pluginstore Table exception: "+repr(e))
try:
self._log.debug("Init: device Table")
db.deviceTable.create_table(True)
except OSError as e:
self._log.debug("Init: device Table exception: "+repr(e))
try:
self._log.debug("Init: service Table")
db.serviceTable.create_table(True)
except OSError as e:
self._log.error("Init: service Table exception: "+repr(e))
try:
self._log.debug("Init: notification Table")
db.notificationTable.create_table(True)
except OSError as e:
self._log.error("Init: notification Table exception: "+repr(e))
try:
self._log.debug("Init: advanced Table")
db.advancedTable.create_table(True)
except OSError as e:
self._log.error("Init: advanced Table exception: "+repr(e))
try:
self._log.debug("Init: script Table")
db.scriptTable.create_table(True)
except OSError as e:
self._log.error("Init: script Table exception: "+repr(e))
try:
self._log.debug("Init: rule Table")
db.ruleTable.create_table(True)
except OSError as e:
self._log.error("Init: rule Table exception: "+repr(e))
gc.collect()
#Config table init
config = db.configTable.getrow()
#Test is config table = empty, if so create initial record
if not config:
self._log.debug("Init: Create Config Record")
#create table
cid = db.configTable.create(name=core.initial_upyeasyname)
#Network table init
network = db.networkTable.getrow()
#Test is network table = empty, if so create initial record
if not network:
self._log.debug("Init: Create Network Record")
#create table
cid = db.networkTable.create(ssid="")
#Hardware table init
hardware = db.hardwareTable.getrow()
#Test is hardware table = empty, if so create initial record
if not hardware:
self._log.debug("Init: Create Hardware Record")
#create platform dependent hardware table
self._hal.hardwaredb_init()
#dxpin table init
dxpin = db.dxpinTable.getrow()
#Test is dxpin table = empty, if so create initial record
if not dxpin:
self._log.debug("Init: Create dxpin Record")
# create pin record
db.dxpinTable.create(d0="")
#dxmap table init
dxmap = db.dxmapTable.getrow()
#Test is dxmap table = empty, if so create initial record
if not dxmap:
self._log.debug("Init: Create dxmap Record")
#create pin mapping record
self._hal.dxmaps_init()
#advanced table init
advanced = db.advancedTable.getrow()
#Test is advanced table = empty, if so create initial record
if not advanced:
self._log.debug("Init: Create advanced Record")
#create table
cid = db.advancedTable.create(mqttretain=0)
# Init network!
netconnected = self._hal.init_network()
# Init all protocols
if core.initial_upyeasywifi != core.NET_AP:
self._protocols = protocol()
core._protocols = self._protocols
self._protocols.init()
# Init all plugins
self._plugins = plugins()
core._plugins = self._plugins
self._plugins.init()
# Init all scripts
self._scripts = scripts()
core._scripts = self._scripts
self._scripts.init()
#Get advanced record key
advanced = db.advancedTable.getrow()
# set log levels!
self._log.changelevel('syslog',advanced['sysloglevel'])
if self.debug > advanced['serialloglevel']:
self._log.changelevel('console',advanced['serialloglevel'])
self._log.changelevel('log',advanced['webloglevel'])
#Set right syslog hostname
if core._utils.get_syslog_hostname():
self._log.changehost(core.__logname__+"-"+core._utils.get_upyeasy_name(),core._utils.get_syslog_hostname())
else:
self._log.changehost(core.__logname__+"-"+core._utils.get_upyeasy_name(),'0.0.0.0')
#Set the right time!
if core.initial_upyeasywifi != core.NET_AP:
self._hal.settime()
gc.collect()
return netconnected