3333import os
3434import datetime
3535import time
36- import json
3736import signal
3837import socket
3938import struct
@@ -68,7 +67,7 @@ def abortprogram(signal, frame):
6867timefile = '/var/www/html/openWB/ramdisk/smarthome_device_ret' + \
6968 str (devicenumber ) + '_time' # Dummy file needed for timestamp of last metering
7069# Logfile for additional output beside of the smarthome.log
71- debugfile = open ( '/var/www/html/openWB/ramdisk/smaem.log' , 'a' , newline = ' \r \n ' )
70+
7271
7372sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM , socket .IPPROTO_UDP )
7473sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
@@ -108,18 +107,18 @@ def abortprogram(signal, frame):
108107 "HomeManager (2) is sending in the network." )
109108
110109emparts = decode_speedwire (sock_data )
111- debugfile . write ( str ( datetime . datetime . now ()) + ' : smaserial: #' + str (smaserial ) + '# - Current SMA serial number:#' +
112- str (emparts ['serial' ]) + '# - watt:#' + str (int (emparts .get ("pconsume" ))) + '# - wattc:#' +
113- str ("{:.3f}" .format (int (emparts .get ('pconsumecounter' )* 1000 ))) + '#\n ' )
110+ log . debug ( 'SMA: : smaserial: #' + str (smaserial ) + '# - Current SMA serial number:#' +
111+ str (emparts ['serial' ]) + '# - watt:#' + str (int (emparts .get ("pconsume" ))) + '# - wattc:#' +
112+ str ("{:.3f}" .format (int (emparts .get ('pconsumecounter' )* 1000 ))) + '#' )
114113
115114# Remember: We assume that beside of our EnergyMeter there are more SMA devices present (like HomeManager 2.0 or other
116115# EnergyMeter) - so must not accept any data or smaserial = None
117116if str (emparts ['serial' ]) == str (smaserial ): # Scenario 1: Our EnergyMeter is sending, so we put the current values in
118117 # our output variables
119118 watt = str (int (emparts .get ("pconsume" )))
120119 wattc = str ("{:.3f}" .format (int (emparts .get ('pconsumecounter' )* 1000 )))
121- debugfile . write ( str ( datetime . datetime . now ()) + ' : 1 - Our SMA EM ' + str (smaserial ) +
122- ' is sending, everything fine. watt: #' + str (watt ) + '# - wattc: #' + str (wattc ) + '#\n ' )
120+ log . debug ( 'SMA: : 1 - Our SMA EM ' + str (smaserial ) +
121+ ' is sending, everything fine. watt: #' + str (watt ) + '# - wattc: #' + str (wattc ) + '#' )
123122# Scenario 2: Our EnergyMeter is not sending but we have a returnfile which is older than n seconds (parameter
124123# secondssincelastmetering)
125124elif ((os .path .isfile (returnfile )) and
@@ -128,29 +127,27 @@ def abortprogram(signal, frame):
128127 # We set "0" as current Power Consume (pconsume) and (from the existing ret-file) the last value for the Power
129128 # Consume Counter (pconsumecounter)
130129 watt = '0'
131- ret = open (returnfile , 'r' )
132- lastvalues = ret .read ()
133- ret .close ()
130+ with open (returnfile , 'r' ) as ret :
131+ lastvalues = ret .read ()
134132 timesincelastmetering = int (round (time .time (), 0 )- lastmodificationtime )
135133 wattc = lastvalues [int (lastvalues .rfind ('powerc' )) + 9 :lastvalues .find ('}' )]
136- debugfile . write ( str ( datetime . datetime . now ()) + ' : 2 - Debug: time.time(): #' + str (round (time .time (), 0 )) +
137- '# - lastmodificationtime: #' + str (lastmodificationtime ) +
138- '# - timesincelastmetering: #' + str (timesincelastmetering ) +
139- '# - int(secondssincelastmetering): #' + str (int (secondssincelastmetering )) + '#\n ' )
140- debugfile . write ( str ( datetime . datetime . now ()) + ' : 2 - We create a fake ret-file. watt: #' + str (watt ) +
141- '# - wattc: #' + str (
134+ log . debug ( 'SMA: : 2 - Debug: time.time(): #' + str (round (time .time (), 0 )) +
135+ '# - lastmodificationtime: #' + str (lastmodificationtime ) +
136+ '# - timesincelastmetering: #' + str (timesincelastmetering ) +
137+ '# - int(secondssincelastmetering): #' + str (int (secondssincelastmetering )) + '#' )
138+ log . debug ( 'SMA: : 2 - We create a fake ret-file. watt: #' + str (watt ) +
139+ '# - wattc: #' + str (
142140 wattc ) + '# - lastvalues: #' + lastvalues + '# - int-lastvalues.rfind-powerc: #' +
143- str ((lastvalues .rfind ('powerc' ))) + '#\n ' )
141+ str ((lastvalues .rfind ('powerc' ))) + '#' )
144142
145143# Scenario 3: Our EnergyMeter is not sending but we have a returnfile which is younger than n seconds
146144# (parameter secondssincelastmetering)
147145elif ((os .path .isfile (returnfile )) and
148146 (int ((round (time .time (), 0 )- lastmodificationtime )) < int (secondssincelastmetering ))):
149147 # We have a ret-file which is younger than n seconds. We do nothing as the existing ret-file is good enough.
150- debugfile .write (str (datetime .datetime .now ()) +
151- ': 3 - The existing ret-file is fine enough. round(time.time(),0): #' + str (round (time .time (), 0 ))
152- + '# - lastmodificationtime: #' + str (lastmodificationtime ) + '# - secondssincelastmetering: #'
153- + str (secondssincelastmetering ) + '#\n ' )
148+ log .debug ('SMA:: 3 - The existing ret-file is fine enough. round(time.time(),0): #' + str (round (time .time (), 0 ))
149+ + '# - lastmodificationtime: #' + str (lastmodificationtime ) + '# - secondssincelastmetering: #'
150+ + str (secondssincelastmetering ) + '#' )
154151 sys .exit ("Module SMAEM: No data received but we have historical data which is younger than " +
155152 str (secondssincelastmetering ) + " seconds." )
156153else :
@@ -162,20 +159,16 @@ def abortprogram(signal, frame):
162159 # Module SMAEM: No data received and no historical data since boot time
163160 # Leistungsmessung smaem [...] Fehlermeldung: [Errno 2] No such file or directory:
164161 # '/var/www/html/openWB/ramdisk/smarthome_device_ret1'
165- debugfile . write ( str ( datetime . datetime . now ()) + ': 4 - No data received and no historical data since boottime\n ' )
162+ log . debug ( 'SMA:: 4 - No data received and no historical data since boottime' )
166163 sys .exit (str (datetime .datetime .now ()) + ": Module SMAEM: No data received and no historical data since boottime" )
167164
168165# General output section
169166
170167answer = '{"power":' + watt + ',"powerc":' + wattc + '}'
171- f = open (returnfile , 'w' )
172- json .dump (answer , f )
173- f .close ()
174168
175- t = open (timefile , 'w' )
176- t .write (str (datetime .datetime .now ()) +
177- ': File is created by Smarthome module SMAEM for validating the timestamp of the last return-file creation.' )
178- t . close ( )
169+ with open (timefile , 'w' ) as t :
170+ t .write (str (datetime .datetime .now ()) +
171+ ': File is created by Smarthome module SMAEM for validating '
172+ 'the timestamp of the last return-file creation.' )
179173
180- debugfile .write (str (datetime .datetime .now ()) + ': 99 - Output answer: #' + answer + '#\n ' )
181- debugfile .close ()
174+ log .debug ('SMA:: 99 - Output answer: #' + answer + '#' )
0 commit comments