Skip to content

Commit

Permalink
upates to load process and config file read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Yates authored and Jason Yates committed Sep 16, 2018
1 parent d88c903 commit 8403c0b
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 583 deletions.
123 changes: 57 additions & 66 deletions genmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@
from subprocess import PIPE, Popen

try:
from ConfigParser import RawConfigParser
except ImportError as e:
from configparser import RawConfigParser

try:
from genmonlib import mymail, mylog, mythread, mypipe, mysupport, generac_evolution, generac_HPanel, myplatform, myweather
from genmonlib import mymail, mylog, mythread, mypipe, mysupport, generac_evolution, generac_HPanel, myplatform, myweather, myconfig
except Exception as e1:
print("\n\nThis program requires the modules located in the genmonlib directory in the github repository.\n")
print("Please see the project documentation at https://github.com/jgyates/genmon.\n")
print("Error: " + str(e1))
sys.exit(2)

GENMON_VERSION = "V1.9.42"
GENMON_VERSION = "V1.10.0"

#------------ Monitor class --------------------------------------------
class Monitor(mysupport.MySupport):
Expand Down Expand Up @@ -99,14 +94,20 @@ def __init__(self, ConfigFilePath = None):
self.LogConsole("Missing config file : " + self.ConfigFilePath + 'mymail.conf')
sys.exit(1)

# log errors in this module to a file
self.log = mylog.SetupLogger("genmon", self.LogLocation + "genmon.log")

self.config = myconfig.MyConfig(filename = self.ConfigFilePath + 'genmon.conf', section = "GenMon", log = self.console)
# read config file
if not self.GetConfig():
raise Exception("Failure in Monitor GetConfig: " + str(e1))
return None
self.LogConsole("Failure in Monitor GetConfig")
sys.exit(1)

# log errors in this module to a file
self.log = mylog.SetupLogger("genmon", self.LogLocation + "genmon.log")

self.config.log = self.log

if self.IsLoaded():
self.LogConsole("ERROR: genmon.py is already loaded.")
self.LogError("ERROR: genmon.py is already loaded.")
Expand Down Expand Up @@ -146,9 +147,9 @@ def __init__(self, ConfigFilePath = None):
self.ControllerSelected = "generac_evo_nexus"

if self.ControllerSelected.lower() == "h_100" :
self.Controller = generac_HPanel.HPanel(self.log, newinstall = self.NewInstall, simulation = self.Simulation, simulationfile = self.SimulationFile, message = self.MessagePipe, feedback = self.FeedbackPipe)
self.Controller = generac_HPanel.HPanel(self.log, newinstall = self.NewInstall, simulation = self.Simulation, simulationfile = self.SimulationFile, message = self.MessagePipe, feedback = self.FeedbackPipe, config = self.config)
else:
self.Controller = generac_evolution.Evolution(self.log, self.NewInstall, simulation = self.Simulation, simulationfile = self.SimulationFile, message = self.MessagePipe, feedback = self.FeedbackPipe)
self.Controller = generac_evolution.Evolution(self.log, self.NewInstall, simulation = self.Simulation, simulationfile = self.SimulationFile, message = self.MessagePipe, feedback = self.FeedbackPipe, config = self.config)
self.Threads = self.MergeDicts(self.Threads, self.Controller.Threads)

except Exception as e1:
Expand Down Expand Up @@ -184,85 +185,75 @@ def StartThreads(self, reload = False):
# -------------------- Monitor::GetConfig-----------------------------------
def GetConfig(self):

ConfigSection = "GenMon"
try:
# read config file
config = RawConfigParser()
# config parser reads from current directory, when running form a cron tab this is
# not defined so we specify the full path
config.read(self.ConfigFilePath + 'genmon.conf')

# getfloat() raises an exception if the value is not a float
# getint() and getboolean() also do this for their respective types

if config.has_option(ConfigSection, 'sitename'):
self.SiteName = config.get(ConfigSection, 'sitename')
if self.config.HasOption('sitename'):
self.SiteName = self.config.ReadValue('sitename')

if config.has_option(ConfigSection, 'incoming_mail_folder'):
self.IncomingEmailFolder = config.get(ConfigSection, 'incoming_mail_folder') # imap folder for incoming mail
if self.config.HasOption('incoming_mail_folder'):
self.IncomingEmailFolder = self.config.ReadValue('incoming_mail_folder') # imap folder for incoming mail

if config.has_option(ConfigSection, 'processed_mail_folder'):
self.ProcessedEmailFolder = config.get(ConfigSection, 'processed_mail_folder') # imap folder for processed mail
if self.config.HasOption('processed_mail_folder'):
self.ProcessedEmailFolder = self.config.ReadValue('processed_mail_folder') # imap folder for processed mail
# server_port, must match value in myclient.py and check_monitor_system.py and any calling client apps
if config.has_option(ConfigSection, 'server_port'):
self.ServerSocketPort = config.getint(ConfigSection, 'server_port')
if self.config.HasOption('server_port'):
self.ServerSocketPort = self.config.ReadValue('server_port', return_type = int)

if config.has_option(ConfigSection, 'loglocation'):
self.LogLocation = config.get(ConfigSection, 'loglocation')
if self.config.HasOption('loglocation'):
self.LogLocation = self.config.ReadValue('loglocation')

if config.has_option(ConfigSection, 'syncdst'):
self.bSyncDST = config.getboolean(ConfigSection, 'syncdst')
if config.has_option(ConfigSection, 'synctime'):
self.bSyncTime = config.getboolean(ConfigSection, 'synctime')
if self.config.HasOption('syncdst'):
self.bSyncDST = self.config.ReadValue('syncdst', return_type = bool)
if self.config.HasOption('synctime'):
self.bSyncTime = self.config.ReadValue('synctime', return_type = bool)

if config.has_option(ConfigSection, 'disableplatformstats'):
self.bDisablePlatformStats = config.getboolean(ConfigSection, 'disableplatformstats')
if self.config.HasOption('disableplatformstats'):
self.bDisablePlatformStats = self.config.ReadValue('disableplatformstats', return_type = bool)

if config.has_option(ConfigSection, 'simulation'):
self.Simulation = config.getboolean(ConfigSection, 'simulation')
if self.config.HasOption('simulation'):
self.Simulation = self.config.ReadValue('simulation', return_type = bool)

if config.has_option(ConfigSection, 'simulationfile'):
self.SimulationFile = config.get(ConfigSection, 'simulationfile')
if self.config.HasOption('simulationfile'):
self.SimulationFile = self.config.ReadValue('simulationfile')

if config.has_option(ConfigSection, 'controllertype'):
self.ControllerSelected = config.get(ConfigSection, 'controllertype')
if self.config.HasOption('controllertype'):
self.ControllerSelected = self.config.ReadValue('controllertype')

if config.has_option(ConfigSection, 'disableweather'):
self.DisableWeather = config.getboolean(ConfigSection, 'disableweather')
if self.config.HasOption('disableweather'):
self.DisableWeather = self.config.ReadValue('disableweather', return_type = bool)
else:
self.DisableWeather = False

if config.has_option(ConfigSection, 'weatherkey'):
self.WeatherAPIKey = config.get(ConfigSection, 'weatherkey')
if self.config.HasOption('weatherkey'):
self.WeatherAPIKey = self.config.ReadValue('weatherkey')

if config.has_option(ConfigSection, 'weatherlocation'):
self.WeatherLocation = config.get(ConfigSection, 'weatherlocation')
if self.config.HasOption('weatherlocation'):
self.WeatherLocation = self.config.ReadValue('weatherlocation')

if config.has_option(ConfigSection, 'metricweather'):
self.UseMetric = config.getboolean(ConfigSection, 'metricweather')
if self.config.HasOption('metricweather'):
self.UseMetric = self.config.ReadValue('metricweather', return_type = bool)

if config.has_option(ConfigSection, 'minimumweatherinfo'):
self.WeatherMinimum = config.getboolean(ConfigSection, 'minimumweatherinfo')
if self.config.HasOption('minimumweatherinfo'):
self.WeatherMinimum = self.config.ReadValue('minimumweatherinfo', return_type = bool)

if config.has_option(ConfigSection, 'readonlyemailcommands'):
self.ReadOnlyEmailCommands = config.getboolean(ConfigSection, 'readonlyemailcommands')
if self.config.HasOption('readonlyemailcommands'):
self.ReadOnlyEmailCommands = self.config.ReadValue('readonlyemailcommands', return_type = bool)

if config.has_option(ConfigSection, 'optimizeforslowercpu'):
self.SlowCPUOptimization = config.getboolean(ConfigSection, 'optimizeforslowercpu')
if self.config.HasOption('optimizeforslowercpu'):
self.SlowCPUOptimization = self.config.ReadValue('optimizeforslowercpu', return_type = bool)

if config.has_option(ConfigSection, 'version'):
self.Version = config.get(ConfigSection, 'version')
if self.config.HasOption('version'):
self.Version = self.config.ReadValue('version')
if not self.Version == GENMON_VERSION:
self.AddItemToConfFile('version', GENMON_VERSION)
self.config.WriteValue('version', GENMON_VERSION)
self.NewInstall = True
else:
self.AddItemToConfFile('version', GENMON_VERSION)
self.config.WriteValue('version', GENMON_VERSION)
self.NewInstall = True
self.Version = GENMON_VERSION
if config.has_option(ConfigSection, "autofeedback"):
self.FeedbackEnabled = config.getboolean(ConfigSection, 'autofeedback')
if self.config.HasOption("autofeedback"):
self.FeedbackEnabled = self.config.ReadValue('autofeedback', return_type = bool)
else:
self.AddItemToConfFile('autofeedback', "False")
self.config.WriteValue('autofeedback', "False")
self.FeedbackEnabled = False
# Load saved feedback log if log is present
if os.path.isfile(self.FeedbackLogFile):
Expand All @@ -272,7 +263,7 @@ def GetConfig(self):
except Exception as e1:
os.remove(self.FeedbackLogFile)
except Exception as e1:
raise Exception("Missing config file or config file entries (genmon): " + str(e1))
self.LogErrorLine("Missing config file or config file entries (genmon): " + str(e1))
return False

return True
Expand Down Expand Up @@ -376,7 +367,7 @@ def SendLogFiles(self):
msgbody += self.Controller.DisplayRegisters(AllRegs = True)

LogList = []
FilesToSend = ["genmon.log", "genserv.log", "mymail.log", "myserial.log", "mymodbus.log"]
FilesToSend = ["genmon.log", "genserv.log", "mymail.log", "myserial.log", "mymodbus.log", "gengpio.log", "gengpioin.log", "gensms.log", "gensms_modem.log", "genmqtt.log", "genpushover.log", "gensyslog.log", "genloader.log"]
for File in FilesToSend:
LogFile = self.LogLocation + File
if os.path.isfile(LogFile):
Expand Down
117 changes: 58 additions & 59 deletions genmonlib/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,35 @@
import threading, datetime, collections, os, time
# NOTE: collections OrderedDict is used for dicts that are displayed to the UI

try:
from ConfigParser import RawConfigParser
except ImportError as e:
from configparser import RawConfigParser

import mysupport, mypipe, mythread

class GeneratorController(mysupport.MySupport):
#---------------------GeneratorController::__init__-------------------------
def __init__(self, log, newinstall = False, simulation = False, simulationfile = None, message = None, feedback = None, ConfigFilePath = None):
def __init__(self,
log,
newinstall = False,
simulation = False,
simulationfile = None,
message = None,
feedback = None,
ConfigFilePath = None,
config = None):

super(GeneratorController, self).__init__(simulation = simulation)
self.log = log
self.NewInstall = newinstall
self.Simulation = simulation
self.SimulationFile = simulationfile
self.FeedbackPipe = feedback
self.MessagePipe = message
self.config = config
if ConfigFilePath == None:
self.ConfigFilePath = "/etc/"
else:
self.ConfigFilePath = ConfigFilePath


self.Address = None
self.SerialPort = "/dev/serial0"
self.BaudRate = 9600
Expand Down Expand Up @@ -80,61 +88,52 @@ def __init__(self, log, newinstall = False, simulation = False, simulationfile =
self.OutageStartTime = self.ProgramStartTime # if these two are the same, no outage has occured
self.LastOutageDuration = self.OutageStartTime - self.OutageStartTime

# Read conf entries common to all controllers
ConfigSection = "GenMon"
try:
# read config file
config = RawConfigParser()
# config parser reads from current directory, when running form a cron tab this is
# not defined so we specify the full path
config.read(self.ConfigFilePath + 'genmon.conf')

# getfloat() raises an exception if the value is not a float
# getint() and getboolean() also do this for their respective types
if config.has_option(ConfigSection, 'sitename'):
self.SiteName = config.get(ConfigSection, 'sitename')

if config.has_option(ConfigSection, 'port'):
self.SerialPort = config.get(ConfigSection, 'port')

if config.has_option(ConfigSection, 'loglocation'):
self.LogLocation = config.get(ConfigSection, 'loglocation')

if config.has_option(ConfigSection, 'optimizeforslowercpu'):
self.SlowCPUOptimization = config.getboolean(ConfigSection, 'optimizeforslowercpu')
# optional config parameters, by default the software will attempt to auto-detect the controller
# this setting will override the auto detect

if config.has_option(ConfigSection, 'metricweather'):
self.UseMetric = config.getboolean(ConfigSection, 'metricweather')

if config.has_option(ConfigSection, 'enabledebug'):
self.EnableDebug = config.getboolean(ConfigSection, 'enabledebug')

if config.has_option(ConfigSection, 'displayunknown'):
self.bDisplayUnknownSensors = config.getboolean(ConfigSection, 'displayunknown')
if config.has_option(ConfigSection, 'outagelog'):
self.OutageLog = config.get(ConfigSection, 'outagelog')

if config.has_option(ConfigSection, 'kwlog'):
self.PowerLog = config.get(ConfigSection, 'kwlog')
if config.has_option(ConfigSection, 'kwlogmax'):
self.PowerLogMaxSize = config.getint(ConfigSection, 'kwlogmax')

if config.has_option(ConfigSection, 'nominalfrequency'):
self.NominalFreq = config.get(ConfigSection, 'nominalfrequency')
if config.has_option(ConfigSection, 'nominalRPM'):
self.NominalRPM = config.get(ConfigSection, 'nominalRPM')
if config.has_option(ConfigSection, 'nominalKW'):
self.NominalKW = config.get(ConfigSection, 'nominalKW')
if config.has_option(ConfigSection, 'model'):
self.Model = config.get(ConfigSection, 'model')

if config.has_option(ConfigSection, 'fueltype'):
self.FuelType = config.get(ConfigSection, 'fueltype')

if config.has_option(ConfigSection, 'tanksize'):
self.TankSize = config.get(ConfigSection, 'tanksize')
if self.config != None:
if self.config.HasOption('sitename'):
self.SiteName = self.config.ReadValue('sitename')

if self.config.HasOption('port'):
self.SerialPort = self.config.ReadValue('port')

if self.config.HasOption('loglocation'):
self.LogLocation = self.config.ReadValue('loglocation')

if self.config.HasOption('optimizeforslowercpu'):
self.SlowCPUOptimization = self.config.ReadValue('optimizeforslowercpu', return_type = bool)
# optional config parameters, by default the software will attempt to auto-detect the controller
# this setting will override the auto detect

if self.config.HasOption('metricweather'):
self.UseMetric = self.config.ReadValue('metricweather', return_type = bool)

if self.config.HasOption('enabledebug'):
self.EnableDebug = self.config.ReadValue('enabledebug', return_type = bool)

if self.config.HasOption('displayunknown'):
self.bDisplayUnknownSensors = self.config.ReadValue('displayunknown', return_type = bool)
if self.config.HasOption('outagelog'):
self.OutageLog = self.config.ReadValue('outagelog')

if self.config.HasOption('kwlog'):
self.PowerLog = self.config.ReadValue('kwlog')
if self.config.HasOption('kwlogmax'):
self.PowerLogMaxSize = self.config.ReadValue('kwlogmax', return_type = int)

if self.config.HasOption('nominalfrequency'):
self.NominalFreq = self.config.ReadValue('nominalfrequency')
if self.config.HasOption('nominalRPM'):
self.NominalRPM = self.config.ReadValue('nominalRPM')
if self.config.HasOption('nominalKW'):
self.NominalKW = self.config.ReadValue('nominalKW')
if self.config.HasOption('model'):
self.Model = self.config.ReadValue('model')

if self.config.HasOption('fueltype'):
self.FuelType = self.config.ReadValue('fueltype')

if self.config.HasOption('tanksize'):
self.TankSize = self.config.ReadValue('tanksize')

except Exception as e1:
if not reload:
Expand Down
Loading

0 comments on commit 8403c0b

Please sign in to comment.