-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
394 lines (331 loc) · 13.2 KB
/
config.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#
# Copyright (c) 2020-2037 duxman.
#
# This file is part of Duxman Luces
# (see https://github.com/duxman/luces).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import json
from Util import logger
from PIL import Image # Use apt-get install python-imaging to install this
class programacion(object):
HoraDesde = ""
HoraHasta = ""
Estado = ""
Programa = ""
Repeticiones = -1
WaitTime = 10
data = ""
Secuencia = []
Logger = None
def __init__(self, path="./web/static/config"):
self.Logger = logger.clienteLog.logger
self.Logger.debug("Cargamos configuracion programacion")
self.data = json.load(open(path + '/programacion.json'))
self.HoraDesde = self.data["StartTime"]
self.HoraHasta = self.data["EndTime"]
self.Estado = self.data["State"]
self.Repeticiones = self.data["Repeats"]
self.WaitTime = int(self.data["WaitTime"])
class Zone(object):
ZonePinType = ""
MQTT_TOKEN = "PinManager"
ZoneName = ""
ZonePins: dict = {}
ZoneId = 0
ZoneType = ""
def __cmp__(self, other):
return __cmp__(self.ZoneId, other.ZoneId)
def __eq__(self, other):
return self.ZoneId == other.ZoneId
def __lt__(self, other):
return self.ZoneId < other.ZoneId
def __gt__(self, other):
return self.ZoneId > other.ZoneId
def __init__(self, name, PinsArray, id, type, PinType, MQTT_TOKEN):
self.ZoneName = name
self.ZonePins = dict()
for zp in PinsArray:
pins = zp["ZonePinId"].split(",")
self.ZonePins[zp["ZonePinValue"]] = pins
self.ZoneId = id
self.ZoneType = type
self.ZonePinType = PinType
self.MQTT_TOKEN = MQTT_TOKEN
class Zones(object):
Logger = None
DefinedZones = []
SpectrumPins = []
AlonePins = []
Tokens = []
MaxPinValue = 0
def __init__(self, path="./web/static/config"):
self.Logger = logger.clienteLog.logger
self.Logger.debug("Cargamos configuracion Zones.json ")
self.data = json.load(open(path + '/Zones.json'))
self.Tokens = []
definedzonestemp = []
for definedzone in self.data["Zones"]:
definedzonestemp.append( Zone(definedzone["ZoneName"],
definedzone["ZonePinsArray"],
definedzone["ZoneId"],
definedzone["ZoneType"],
definedzone["ZonePinType"],
definedzone["MQTT_TOKEN"]) )
# Ordenamos la lista
self.DefinedZones = sorted(definedzonestemp)
max = 0
for z in self.DefinedZones:
for k in z.ZonePins.keys():
if k > max:
max=k
self.MaxPinValue = max
# Montamos la lista para no tener que calcular los pines
for d in self.DefinedZones:
if d.ZoneType == "ALONE":
self.AlonePins.extend(d.ZonePins)
if d.ZoneType == "SPECTRUM":
for zp in d.ZonePins:
self.SpectrumPins.extend(d.ZonePins[zp])
if d.ZonePinType == "REMOTE":
self.Tokens.append(d.MQTT_TOKEN)
# Eliminamos pins duplicados
self.AlonePins = list(dict.fromkeys(self.AlonePins))
self.SpectrumPins = list(dict.fromkeys(self.SpectrumPins))
# Eliminamos Tokens duplicados
self.Tokens = list(dict.fromkeys(self.Tokens))
# Ordenamos pins duplicados
self.AlonePins.sort()
self.SpectrumPins.sort()
# mostramos el resultado por si acaso
for d in self.SpectrumPins:
self.Logger.info(' '.join(d))
class ProgramConfiguration(object):
Logger = None
ProgramName = ""
ProgramType = ""
ProgramInterval = 0.0
MusicFiles = []
Sequences = []
def __init__(self, path="./web/static/config"):
self.Logger = logger.clienteLog.logger
self.Logger.debug("Cargamos configuracion ProgramConfiguration.json ")
self.data = json.load(open(path + '/ProgramConfiguration.json'))
self.ProgramName = self.data["ProgramName"]
self.ProgramType = self.data["ProgramType"]
self.ProgramInterval = float(self.data["ProgramInterval"])
for fichero in self.data["MusicFiles"]:
self.MusicFiles.append(fichero["File"])
for sequence in self.data["Sequences"]:
self.Sequences.append(sequence["Activate Zone"])
class GeneralConfiguration(object):
RutaFFMPEG = None
RutaMusica = None
WebServerPort = 8000
Programacion = None
MQTT_HOST = "localhost"
MQTT_PORT = 1883
ProgramConfiguration = None
Zones = None
I2CDevicesConf = None
Logger = None
def __init__(self, path="./web/static/config"):
self.Logger = logger.clienteLog.logger
self.Logger.debug("Cargamos configuracion general ")
self.data = json.load(open(path + '/configuracion.json'))
self.RutaMusica = self.data["MusicPath"]
self.RutaFFMPEG = self.data["FfmpegPath"]
self.WebServerPort = self.data["WebServerPort"]
self.MQTT_HOST = self.data["MQTT_HOST"]
self.MQTT_PORT = self.data["MQTT_PORT"]
try:
self.ProgramConfiguration = ProgramConfiguration()
except IOError:
self.Logger.debug("No hay ProgramConfiguration")
try:
self.Programacion = programacion()
except IOError:
self.Logger.debug("No hay Programacion")
try:
self.Zones = Zones()
except IOError:
self.Logger.debug("No hay Zonas definidas")
def calculateMatrix(MatrixHeight, MatrixWidth, Side="LEFT", PanelsV=1, PanelsH=1):
if Side == "LEFT":
bInc = False
else:
bInc = True
CountLeds = MatrixHeight * MatrixWidth
VerticalHeight = MatrixHeight * PanelsV
myMatrix = []
for i in range(VerticalHeight, 0, -1):
rangeMatrixLine = []
linetemp = [];
# Calculate Max and min led
maxled = (i * MatrixWidth)
minled = (maxled - int(MatrixWidth))
# For calculate go and return
if bInc == False:
rangeMatrixLine.extend(range(maxled - 1, minled - 1, -1))
bInc = True
else:
rangeMatrixLine.extend(range(minled, maxled, 1))
bInc = False;
print("range line " + str(i))
print(rangeMatrixLine)
for p in range(PanelsH):
valorSuma = p * CountLeds
line = list(map(lambda x: x + valorSuma, rangeMatrixLine))
print("line " + str(i))
print(line)
myMatrix.extend(line)
return myMatrix
class LedMatrix(object):
################################################################
# In configuration File
################################################################
# Size of your matrix
MatrixWidth = 32
MatrixHeight = 16
LedPin = 18 # GPIO pin connected to the pixels (must support PWM!).
MQTT_HOST = "127.0.0.1"
MQTT_PORT = 8080
MQTT_TOKEN = "DATA"
MatrixStartLed = "RIGHT"
MatrixType = "NONE"
Animations = []
VerticalPanels = 1
HorizontalPanels = 1
################################################################
# In configuration File
################################################################
################################################################
# Probably not configurable
################################################################
LedFreqHz = 800000 # LED signal frequency in hertz (usually 800khz)
LedDma = 5 # DMA channel to use for generating signal (try 5)
LedBrigh = 255 # Set to 0 for darkest and 255 for brightest
LedInvert = False # True to invert the signal (when using NPN transistor level shift)
################################################################
# Probably not configurable
################################################################
################################################################
# Auto calculate
################################################################
myMatrix = []
LedCount = 0
################################################################
# Auto calculate
################################################################
def __init__(self, MatrixWidth, MatrixHeight, LedPin, MQTT_HOST, MQTT_PORT, MQTT_TOKEN, MatrixStartLed, MatrixType,
PanelsV, PanelsH, AnimationsData):
self.MatrixWidth = MatrixWidth
self.MatrixHeight = MatrixHeight
self.LedPin = LedPin # GPIO pin connected to the pixels (must support PWM!).
self.MQTT_HOST = MQTT_HOST
self.MQTT_PORT = MQTT_PORT
self.MQTT_TOKEN = MQTT_TOKEN
self.VerticalPanels = PanelsV
self.HorizontalPanels = PanelsH
self.MatrixStartLed = MatrixStartLed
self.MatrixType = MatrixType
data = json.loads(AnimationsData)
for anim in data:
anim["width"] = self.MatrixWidth
anim["height"] = self.MatrixHeight
animtemp = animation(json.dumps(anim))
self.Animations.append(animtemp)
self.myMatrix = calculateMatrix(self.MatrixHeight, self.MatrixWidth, self.MatrixStartLed, self.VerticalPanels,
self.HorizontalPanels)
class configurationLedMatrix(object):
# Array of images
Matrix: LedMatrix = []
def __init__(self, path="./web/static/config"):
self.data = json.load(open(path + '/LedMatrix.json'))
for mtx in self.data["Matrix"]:
lm = LedMatrix(mtx["MatrixWidth"],
mtx["MatrixHeight"],
mtx["LedPin"],
mtx["MQTT_HOST"],
mtx["MQTT_PORT"],
mtx["MQTT_TOKEN"],
mtx["MatrixStartLed"],
mtx["MatrixType"],
mtx["VerticalPanels"],
mtx["HorizontalPanels"],
json.dumps(mtx["Animations"]))
self.Matrix.append(lm)
class commandLine(object):
IniFrame = 0
EndFrame = 0
Instruction = ""
Value = 0.0
def __init__(self, ini, end, ins, val):
self.IniFrame = ini
self.EndFrame = end
self.Instruction = ins
self.Value = val
class animation(object):
ImageFile = ""
CommandFile = ""
image = None
width = 0
heigth = 0
type = ""
# instructions: commandLine = []
instructions = []
Repetitions = -1
# Speed of movement, in seconds (recommend 0.1-0.3)
Speed = 0.075
# Example
# "{'ImageFile': 'file', 'CommandFile': '', 'width': 128, 'heigth': 8, 'Repetitions': 5, 'Speed': 0.1 }"
def __init__(self, TxtData):
data = json.loads(TxtData)
self.ImageFile = data["ImageFile"]
self.command = data["CommandFile"]
self.width = data["width"]
self.height = data["height"]
self.Repetitions = data["Repetitions"]
self.Speed = data["Speed"]
# Procesamos
self.loadImage()
self.loadAnimationCommand()
def loadImage(self):
# Open the image file given as the command line parameter
try:
loadIm = Image.open(self.ImageFile)
except:
raise Exception("Image file %s could not be loaded" % self.ImageFile)
if loadIm.size[1] != self.height:
origIm = loadIm.resize(
(loadIm.size[0] / (loadIm.size[1] // self.height), self.height),
Image.BICUBIC)
else:
origIm = loadIm.copy()
# If the input is a very small portrait image, then no amount of resizing will save us
if origIm.size[0] < self.width:
raise Exception("Picture is too narrow. Must be at least %s pixels wide" % self.width)
# Add a copy of the start of the image, to the end of the image,
# so that it loops smoothly at the end of the image
self.image = Image.new('RGB', (origIm.size[0] + self.width, self.height))
self.image.paste(origIm, (0, 0, origIm.size[0], self.height))
self.image.paste(origIm.crop((0, 0, self.width, self.height)),
(origIm.size[0], 0, origIm.size[0] + self.width, self.height))
def loadAnimationCommand(self):
if self.CommandFile != "":
data = json.load(open(self.CommandFile))
for inst in data["Commands"]:
cLine = commandLine(inst["IniFrame"], inst["EndFrame"], inst["Instruction"], inst["Value"])
self.instructions.append(cLine)