From 4076189110e3cbd75146a335c8954923e3959d27 Mon Sep 17 00:00:00 2001
From: am0ebe <am0ebe@gmx.de>
Date: Thu, 10 Jun 2021 15:21:57 +0200
Subject: [PATCH] catch OLA Module not found exception for integration from
 dataSync script in ecotron. change skyglow-list construction from dict.

---
 skyglow.py | 68 +++++++++++++++++++++++++++---------------------------
 1 file changed, 34 insertions(+), 34 deletions(-)

diff --git a/skyglow.py b/skyglow.py
index 72e7a4b..f426e47 100755
--- a/skyglow.py
+++ b/skyglow.py
@@ -1,18 +1,8 @@
 #!/usr/bin/python3
 
-#TODO catch if no ola is installed
-# Traceback (most recent call last):
-#   File "./skyglow.py", line 5, in <module>
-#     from ola.ClientWrapper import ClientWrapper
-# ImportError: No module named ola.ClientWrapper
-
-
 from __future__ import print_function #python 2
-import os
-from ola.ClientWrapper import ClientWrapper
+import os, sys, array
 from datetime import datetime as dt
-import array
-import sys
 
 # error / return codes
 # # # # # # #
@@ -21,6 +11,7 @@ FAIL_DMX_SEND=4
 FAIL_DMX_RECEIVE=5
 FAIL_LOG=6
 FAIL_MOON=7
+FAIL_NO_OLA=8
 
 # either fullpath or just name (then logFile will be created in scriptdir)
 # or fullpath containing '/'
@@ -36,35 +27,38 @@ dmxDataReceived = False
 
 MAX_DMX_FRAME_SIZE = 512
 
+skyglowDict = { 
+			 15: 0.08,	# one
+			 54: 0.3,		# one
+			  4: 1,			# all
+			 19: 0.1,		# one
+			  0: 0,			# none
+			  1: 0.01,	# one
+			  5: 0.03,	# one
+			  0: 0,			# none
+			 40: 10,		# all
+			 12: 3,			# all
+			123: 30,		# all
+			123: 30,		# all
+}
+skyglow = list( skyglowDict.keys() )
+skyglow.insert(7,0) #exit twice
+skyglow.append(123) #exit twice
+assert len(skyglow)==12, "skyglow len doesn't fit number pf ecounits!"
+
+
 #change according to needs
 dmx_addr = [10,20,30,40,50,60,70,80,90,100,110,120]
+NUM_ECO_UNITS = len(dmx_addr)
 
-# assert len(dmx_addr) == NUM_ECO_UNITS 
-# assert len(skyglow) == NUM_ECO_UNITS
-
-#Mapping byte to lux
-skyglow = [ 15, # one 0.08
-			54, # one 0.3
-			 4, # all 1
-			19,	# one 0.1
-			 0,	# none 0
-			 1,	# one 0.01
-			 5,	# one 0.03
-			 0,	# none 0
-			40,	# all 10
-			12,	# all 3
-		   123,	# all 30
-		   123,	# all 30
-]
-
-NUM_ECO_UNITS = len(skyglow)
 sunrise = 0
 sunset = 0
 #UTC: utcfromtimestamp() / datetime.now(timezone.utc)
 now = dt.utcnow().replace(second=0, microsecond=0) # ignore seconds and microseconds
 
 #mlx (idx is also dmx-byte value)
-moon = [ 1.409,
+moon = [ 
+		 1.409,
 		 8.369,
 		12.333,
 		16.210,
@@ -125,8 +119,8 @@ moon = [ 1.409,
 		277.267,
 		281.433,
 		287.467, #60
-	]
-	
+]
+
 
 #moonFile
 DATE_COLUMN=0
@@ -181,7 +175,7 @@ def readLux():
 	return moonLux
 
 def log(msg):
-	msg = str(now) + msg + "\n"
+	msg = str(now) + "\t" + msg + "\n"
 	
 	if not os.access(logFile, os.X_OK) or os.access(logFile, os.W_OK):
 		print("writing log to file ", logFile)
@@ -263,6 +257,11 @@ def main(args=""):
 	print("welcome to skyglow!")
 	print(args)
 
+	try:
+		from ola.ClientWrapper import ClientWrapper
+	except Exception as e:
+		error(FAIL_NO_OLA, e)
+
 	global data, moonLux, moonDmx
 	init()
 	
@@ -299,6 +298,7 @@ def main(args=""):
 
 	print("DMX-Data:",data)
 
+
 	try:
 		global wrapper
 		wrapper = ClientWrapper()
-- 
GitLab