Exemplo n.º 1
0
    def SetupAddOnProgram(prog_name):
        console = SetupLogger(prog_name + "_console", log_file="", stream=True)

        if not MySupport.PermissionsOK():
            console.error(
                "\nYou need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.\n"
            )
            sys.exit(2)

        HelpStr = '\nsudo python ' + prog_name + '.py -a <IP Address or localhost> -c <path to ' + prog_name + ' config file>\n'

        ConfigFilePath = ProgramDefaults.ConfPath
        address = ProgramDefaults.LocalHost

        try:
            opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                       ["help", "configpath=", "address="])
        except getopt.GetoptError:
            console.error("Invalid command line argument.")
            sys.exit(2)

        for opt, arg in opts:
            if opt == '-h':
                console.error(HelpStr)
                sys.exit()
            elif opt in ("-a", "--address"):
                address = arg
            elif opt in ("-c", "--configpath"):
                ConfigFilePath = arg.strip()

        try:
            port, loglocation, multi_instance = MySupport.GetGenmonInitInfo(
                ConfigFilePath, log=console)

            log = SetupLogger("client_" + prog_name,
                              os.path.join(loglocation, prog_name + ".log"))

            if not prog_name.lower().endswith(".py"):
                prog_name += ".py"

            attempts = 0
            while True:
                if MySupport.IsRunning(prog_name=prog_name,
                                       log=log,
                                       multi_instance=multi_instance):
                    if attempts >= 4:
                        raise Exception("The program %s is already loaded" %
                                        prog_name)
                    else:
                        attempts += 1
                        time.sleep(1)
                else:
                    break

        except Exception as e1:
            console.error("Error : " + str(e1))
            log.error("Error : " + str(e1))
            sys.exit(1)

        return console, ConfigFilePath, address, port, loglocation, log
Exemplo n.º 2
0
    try:
        MyClientInterface.Close()
    except Exception as e1:
        log.error("Error: signal_handler: " + str(e1))
    sys.exit(0)

#------------------- Command-line interface for gengpioin ----------------------
if __name__=='__main__':
    address=ProgramDefaults.LocalHost

    try:
        console = SetupLogger("genalexa_console", log_file = "", stream = True)

        if os.geteuid() != 0:
            console.error("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
            sys.exit(2)

        HelpStr = '\nsudo python genalexa.py -a <IP Address or localhost> -c <path to genmon config file>\n'
        try:
            ConfigFilePath = ProgramDefaults.ConfPath
            opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["help","configpath=","address="])
        except getopt.GetoptError:
            console.error("Invalid command line argument.")
            sys.exit(2)

        for opt, arg in opts:
            if opt == '-h':
                console.error(HelpStr)
                sys.exit()
            elif opt in ("-a", "--address"):
Exemplo n.º 3
0
    sys.exit(0)


#------------------- Command-line interface for gengpio ------------------------
if __name__ == '__main__':  # usage program.py [server_address]
    address = ProgramDefaults.LocalHost

    try:
        console = SetupLogger("gengpio_console", log_file="", stream=True)
        HelpStr = '\nsudo python gengpio.py -a <IP Address or localhost> -c <path to genmon config file>\n'
        try:
            ConfigFilePath = ProgramDefaults.ConfPath
            opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                       ["help", "configpath=", "address="])
        except getopt.GetoptError:
            console.error("Invalid command line argument.")
            sys.exit(2)

        for opt, arg in opts:
            if opt == '-h':
                console.error(HelpStr)
                sys.exit()
            elif opt in ("-a", "--address"):
                address = arg
            elif opt in ("-c", "--configpath"):
                ConfigFilePath = arg
                ConfigFilePath = ConfigFilePath.strip()

        port, loglocation = MySupport.GetGenmonInitInfo(ConfigFilePath,
                                                        log=console)
        log = SetupLogger("client", loglocation + "gengpio.log")
Exemplo n.º 4
0
    signal.signal(signal.SIGINT, signal_handler)

    if os.geteuid() != 0:
        print(
            "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
        )
        sys.exit(2)

    log = SetupLogger("client", "/var/log/gensyslog.log")
    console = SetupLogger("gensyslog_console", log_file="", stream=True)
    try:

        GenNotify = GenNotify(host=address,
                              onready=OnReady,
                              onexercise=OnExercise,
                              onrun=OnRun,
                              onrunmanual=OnRunManual,
                              onalarm=OnAlarm,
                              onservice=OnService,
                              onoff=OnOff,
                              onmanual=OnManual,
                              onutilitychange=OnUtilityChange,
                              log=log)

        while True:
            time.sleep(1)

    except Exception as e1:
        log.error("Error: " + str(e1))
        console.error("Error: " + str(e1))
Exemplo n.º 5
0
    except Exception as e1:
        log.error("Error in SendNotice: " + str(e1))
        console.error("Error in SendNotice: " + str(e1))

#------------------- Command-line interface for gengpio ------------------------
if __name__=='__main__':
    address=ProgramDefaults.LocalHost

    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)

    console = SetupLogger("slack_console", log_file = "", stream = True)
    HelpStr = '\nsudo python genslack.py -a <IP Address or localhost> -c <path to genmon config file>\n'
    if os.geteuid() != 0:
        console.error("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
        sys.exit(2)

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["configpath=","address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
Exemplo n.º 6
0
class MyMQTT(MyCommon):

    #------------ MyMQTT::init--------------------------------------------------
    def __init__(self,
        log = None,
        loglocation = ProgramDefaults.LogPath,
        host = ProgramDefaults.LocalHost,
        port = ProgramDefaults.ServerPort,
        configfilepath = ProgramDefaults.ConfPath):

        super(MyMQTT, self).__init__()

        self.LogFileName = loglocation + "genmqtt.log"

        if log != None:
            self.log = log
        else:
            # log errors in this module to a file
            self.log = SetupLogger("client", self.LogFileName)

        # cleanup
        # test
        self.console = SetupLogger("mymqtt_console", log_file = "", stream = True)

        self.Username = None
        self.Password = None
        self.Topic = None

        self.MQTTAddress = None
        self.MonitorAddress = host
        self.MQTTPort = 1883
        self.Topic = "generator"
        self.TopicRoot = None
        self.BlackList = None
        self.UseNumeric = False
        self.PollTime = 2
        self.FlushInterval = float('inf')   # default to inifite flush interval (e.g., never)
        self.Debug = False

        try:
            config = MyConfig(filename =  configfilepath + 'genmqtt.conf', section = 'genmqtt', log = log)

            self.Username = config.ReadValue('username')

            self.Password = config.ReadValue('password')

            self.MQTTAddress = config.ReadValue('mqtt_address')

            if self.MQTTAddress == None or not len(self.MQTTAddress):
                log.error("Error: invalid MQTT server address")
                console.error("Error: invalid MQTT server address")
                sys.exit(1)

            self.MonitorAddress = config.ReadValue('monitor_address', default = self.MonitorAddress)
            if self.MonitorAddress == None or not len(self.MonitorAddress):
                self.MonitorAddress = ProgramDefaults.LocalHost

            self.MQTTPort = config.ReadValue('mqtt_port', return_type = int, default = 1883)

            self.PollTime = config.ReadValue('poll_interval', return_type = float, default = 2.0)

            self.UseNumeric = config.ReadValue('numeric_json', return_type = bool, default = False)

            self.TopicRoot = config.ReadValue('root_topic')

            #http://www.steves-internet-guide.com/mosquitto-tls/
            self.CertificateAuthorityPath =  config.ReadValue('cert_authority_path', default = "")
            self.TLSVersion = config.ReadValue('tls_version', return_type = str, default = "1.0")
            self.CertReqs = config.ReadValue('cert_reqs', return_type = str, default = "Required")

            BlackList = config.ReadValue('blacklist')

            if BlackList != None:
                if len(BlackList):
                    BList = BlackList.strip().split(",")
                    if len(BList):
                        self.BlackList = []
                        for Items in BList:
                            self.BlackList.append(Items.strip())

            self.Debug = config.ReadValue('debug', return_type = bool, default = False)

            if config.HasOption('flush_interval'):
                self.FlushInterval = config.ReadValue('flush_interval', return_type = float, default = float('inf'))
                if self.FlushInterval == 0:
                    self.FlushInterval = float('inf')
            else:
                self.FlushInterval = float('inf')
        except Exception as e1:
            self.LogErrorLine("Error reading " + configfilepath + "genmqtt.conf: " + str(e1))
            self.console.error("Error reading " + configfilepath + "genmqtt.conf: " + str(e1))
            sys.exit(1)

        try:
            self.MQTTclient = mqtt.Client(client_id = "genmon")
            if self.Username != None and len(self.Username) and self.Password != None:
                self.MQTTclient.username_pw_set(self.Username, password=self.Password)

            self.MQTTclient.on_connect = self.on_connect
            self.MQTTclient.on_message = self.on_message


            if len(self.CertificateAuthorityPath):
                if os.path.isfile(self.CertificateAuthorityPath):
                    cert_reqs = ssl.CERT_REQUIRED
                    if self.CertReqs.lower() == "required":
                        cert_reqs = ssl.CERT_REQUIRED
                    elif self.CertReqs.lower() == "optional":
                        cert_reqs = ssl.CERT_REQUIRED
                    elif self.CertReqs.lower() == "none":
                        cert_reqs = ssl.CERT_NONE
                    else:
                        self.LogError("Error: invalid cert required specified, defaulting to required: " + self.self.CertReq)

                    use_tls = ssl.PROTOCOL_TLSv1
                    if self.TLSVersion == "1.0" or self.TLSVersion == "1":
                        use_tls = ssl.PROTOCOL_TLSv1
                    elif self.TLSVersion == "1.1":
                        use_tls = ssl.PROTOCOL_TLSv1_1
                    elif self.TLSVersion == "1.2":
                        use_tls = ssl.PROTOCOL_TLSv1_2
                    else:
                        self.LogError("Error: invalid TLS version specified, defaulting to 1.0: " + self.TLSVersion)
                    self.MQTTclient.tls_set(ca_certs = self.CertificateAuthorityPath,cert_reqs = cert_reqs, tls_version = use_tls )
                    self.MQTTPort = 8883    # port for SSL
                else:
                    self.LogError("Error: Unable to  find CA cert file: " + self.CertificateAuthorityPath)

            self.MQTTclient.connect(self.MQTTAddress, self.MQTTPort, 60)

            self.Push = MyGenPush(host = self.MonitorAddress,
                log = self.log, callback = self.PublishCallback,
                polltime = self.PollTime , blacklist = self.BlackList,
                flush_interval = self.FlushInterval, use_numeric = self.UseNumeric,
                debug = self.Debug, port = port, loglocation = loglocation)

            atexit.register(self.Close)
            signal.signal(signal.SIGTERM, self.Close)
            signal.signal(signal.SIGINT, self.Close)

            self.MQTTclient.loop_start()
        except Exception as e1:
            self.LogErrorLine("Error in MyMQTT init: " + str(e1))
            self.console.error("Error in MyMQTT init: " + str(e1))
            sys.exit(1)
    #------------ MyMQTT::PublishCallback---------------------------------------
    # Callback to publish data via MQTT
    def PublishCallback(self, name, value):

        try:
            if self.TopicRoot != None and len(self.TopicRoot):
                FullPath = self.TopicRoot + "/" + str(name)
            else:
                FullPath = str(name)

            if self.Debug:
                self.console.info("Publish:  " + FullPath  + ": " + str(value) + ": " + str(type(value)))

            self.MQTTclient.publish(FullPath, value)
        except Exception as e1:
            self.LogErrorLine("Error in MyMQTT:PublishCallback: " + str(e1))

    #------------ MyMQTT::on_connect--------------------------------------------
    # The callback for when the client receives a CONNACK response from the server.
    def on_connect(self, client, userdata, flags, rc):
        if rc != 0:
            self.LogError("Error connecting to MQTT server: return code: " + str(rc))
        self.console.info("Connected with result code "+str(rc))

        # Subscribing in on_connect() means that if we lose the connection and
        # reconnect then subscriptions will be renewed.
        self.MQTTclient.subscribe(self.Topic + "/#")

    #------------ MyMQTT::on_message--------------------------------------------
    # The callback for when a PUBLISH message is received from the server.
    def on_message(self, client, userdata, msg):

        if self.Debug:
            self.console.info("Confirmed: " + msg.topic + ": " + str(msg.payload))

    # ---------- MyMQTT::Close--------------------------------------------------
    def Close(self):
        self.Push.Close()
Exemplo n.º 7
0
        console.info(message.sid)

    except Exception as e1:
        log.error("Error: " + str(e1))
        console.error("Error: " + str(e1))

#------------------- Command-line interface for gengpio ------------------------
if __name__=='__main__':
    address = ProgramDefaults.LocalHost

    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)
    console = SetupLogger("sms_console", log_file = "", stream = True)
    HelpStr = '\nsudo python gensyslog.py -a <IP Address or localhost> -c <path to genmon config file>\n'
    if os.geteuid() != 0:
        console.error("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
        sys.exit(2)

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["help","configpath=","address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
Exemplo n.º 8
0
class SerialTCPDevice(MySupport):
    def __init__(self, log=None, host="127.0.0.1", port=8899, config=None):

        super(SerialTCPDevice, self).__init__()
        self.DeviceName = "serialTCP"
        self.config = config
        self.Buffer = []
        self.BufferLock = threading.Lock()
        self.DiscardedBytes = 0
        self.Restarts = 0
        self.SerialStartTime = datetime.datetime.now()  # used for com metrics
        self.rxdatasize = 2000
        self.SocketTimeout = 1

        self.host = host
        self.port = port

        if self.config != None:
            self.loglocation = self.config.ReadValue('loglocation',
                                                     default='/var/log/')
            self.host = self.config.ReadValue('serial_tcp_address',
                                              return_type=str,
                                              default=None)
            self.port = self.config.ReadValue('serial_tcp_port',
                                              return_type=int,
                                              default=None,
                                              NoLog=True)
        else:
            self.loglocation = default = './'

        # log errors in this module to a file
        self.console = SetupLogger("myserialtcp_console",
                                   log_file="",
                                   stream=True)
        if log == None:
            self.log = SetupLogger("myserialtcp",
                                   self.loglocation + "myserialtcp.log")
        else:
            self.log = log

        #Starting serial connection
        self.Connect()

        self.IsOpen = True
        self.StartReadThread()

    #----------  SerialTCPDevice::Connect --------------------------------------
    def Connect(self):

        try:
            #create an INET, STREAMing socket
            self.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.Socket.settimeout(self.SocketTimeout)
            #now connect to the server on our port
            self.Socket.connect((self.host, self.port))
            self.Flush()
            return True
        except Exception as e1:
            self.LogError("Error: Connect : " + str(e1))
            self.console.error("Unable to make TCP connection.")
            self.Socket = None
            return False

    # ---------- SerialTCPDevice::ResetSerialStats------------------------------
    def ResetSerialStats(self):
        # resets status that are time based (affected by a time change)
        self.SerialStartTime = datetime.datetime.now()  # used for com metrics

    # ---------- SerialTCPDevice::StartReadThread-------------------------------
    def StartReadThread(self):

        # start read thread to monitor incoming data commands
        self.Threads["SerialTCPReadThread"] = MyThread(
            self.ReadThread, Name="SerialTCPReadThread")

        return self.Threads["SerialTCPReadThread"]

    # ---------- SerialTCPDevice::ReadThread------------------------------------
    def ReadThread(self):
        while True:
            try:
                self.Flush()
                while True:

                    if self.Socket == None:
                        self.Restarts += 1
                        if not self.Connect():
                            if self.WaitForExit("SerialTCPReadThread",
                                                10):  # 10 seconds
                                return
                            continue
                    for c in self.Read():
                        with self.BufferLock:
                            if sys.version_info[0] < 3:
                                self.Buffer.append(ord(c))  # PYTHON2
                            else:
                                self.Buffer.append(c)  # PYTHON3
                        # first check for SignalStopped is when we are receiving
                        if self.IsStopSignaled("SerialTCPReadThread"):
                            return
                    # second check for SignalStopped is when we are not receiving
                    if self.IsStopSignaled("SerialTCPReadThread"):
                        return

            except Exception as e1:
                self.LogErrorLine(
                    "Resetting SerialTCPDevice:ReadThread Error: " +
                    self.DeviceName + ":" + str(e1))
                # if we get here then this is likely due to the following exception:
                #  "device reports readiness to read but returned no data (device disconnected?)"
                #  This is believed to be a kernel issue so let's just reset the device and hope
                #  for the best (actually this works)
                if self.Socket != None:
                    self.Socket.close()
                    self.Socket = None
                self.Connect()

    #------------SerialTCPDevice::DiscardByte-----------------------------------
    def DiscardByte(self):

        if len(self.Buffer):
            discard = self.Buffer.pop(0)
            self.DiscardedBytes += 1
            return discard

    # ---------- SerialTCPDevice::Close-----------------------------------------
    def Close(self):
        try:
            if self.IsOpen:
                self.KillThread("SerialTCPReadThread")
                # close socket
                if self.Socket != None:
                    self.Socket.close()
                    self.Socket = None
                self.IsOpen = False
        except Exception as e1:
            self.LogErrorLine("Error in SerialTCPDevice:Close : " + str(e1))

    # ---------- SerialTCPDevice::Flush-----------------------------------------
    def Flush(self):
        try:
            # Flush socket
            with self.BufferLock:  # will block if lock is already held
                del self.Buffer[:]

        except Exception as e1:
            self.LogErrorLine("Error in SerialTCPDevice:Flush : " + str(e1))

    # ---------- SerialTCPDevice::Read------------------------------------------
    def Read(self):
        try:
            if self.Socket == None:
                return ""
            data = self.Socket.recv(self.rxdatasize)
            if data == None:
                return ""
            return data
        except socket.timeout as err:
            return ""
        except socket.error as err:
            self.LogErrorLine("Error in SerialTCPDevice:Read socket error: " +
                              str(err))
            if self.Socket != None:
                self.Socket.close()
                self.Socket = None
        except Exception as e1:
            self.LogErrorLine("Error in SerialTCPDevice:Read : " + str(e1))
            if self.Socket != None:
                self.Socket.close()
                self.Socket = None
            return ""

    # ---------- SerialTCPDevice::Write-----------------------------------------
    def Write(self, data):

        try:
            if self.Socket == None:
                return None
            return self.Socket.sendall(data)
        except Exception as e1:
            if self.Socket != None:
                self.Socket.close()
                self.Socket = None
            self.LogErrorLine("Error in SerialTCPDevice:Write : " + str(e1))
            return 0

    # ---------- SerialTCPDevice::GetRxBufferAsString---------------------------
    def GetRxBufferAsString(self):

        try:
            if not len(self.Buffer):
                return ""
            with self.BufferLock:
                str1 = ''.join(chr(e) for e in self.Buffer)
            return str1
        except Exception as e1:
            self.LogErrorLine(
                "Error in SerialTCPDevice:GetRxBufferAsString: " + str(e1))
            return ""
Exemplo n.º 9
0
        sys.exit(2)

    console = SetupLogger("sms_console", log_file = "", stream = True)
    log = SetupLogger("client", "/var/log/gensms.log")

    try:

        config = MyConfig(filename = '/etc/gensms.conf', section = 'gensms', log = log)

        account_sid = config.ReadValue('accountsid', default = "")
        auth_token = config.ReadValue('authtoken', default = "")
        to_number = config.ReadValue('to_number', default = "")
        from_number = config.ReadValue('from_number', default = "")

        if account_sid == "" or auth_token == "" or to_number == "" or from_number == "":
            log.error("Missing parameter in /etc/gensms.conf")
            console.error("Missing parameter in /etc/gensms.conf")
            sys.exit(1)

    except Exception as e1:
        log.error("Error reading /etc/gensms.conf: " + str(e1))
        console.error("Error reading /etc/gensms.conf: " + str(e1))
        sys.exit(1)
    try:
        GenNotify = GenNotify(
                                        host = address,
                                        onready = OnReady,
                                        onexercise = OnExercise,
                                        onrun = OnRun,
                                        onrunmanual = OnRunManual,
                                        onalarm = OnAlarm,
Exemplo n.º 10
0
class GenExercise(MySupport):

    #------------ GenExercise::init---------------------------------------------
    def __init__(self,
        log = None,
        loglocation = ProgramDefaults.LogPath,
        ConfigFilePath = MyCommon.DefaultConfPath,
        host = ProgramDefaults.LocalHost,
        port = ProgramDefaults.ServerPort):

        super(GenExercise, self).__init__()

        self.LogFileName = loglocation + "genexercise.log"
        self.AccessLock = threading.Lock()
        # log errors in this module to a file
        self.log = SetupLogger("genexercise", self.LogFileName)

        self.console = SetupLogger("genexercise_console", log_file = "", stream = True)

        self.MonitorAddress = host
        self.PollTime =  2
        self.ExerciseActive = False
        self.Debug = False

        try:
            self.config = MyConfig(filename = ConfigFilePath + 'genexercise.conf', section = 'genexercise', log = self.log)

            self.ExerciseType = self.config.ReadValue('exercise_type', default = "Normal")
            self.ExerciseHour = self.config.ReadValue('exercise_hour', return_type = int, default = 12)
            self.ExerciseMinute = self.config.ReadValue('exercise_minute', return_type = int, default = 0)
            self.ExerciseDayOfMonth = self.config.ReadValue('exercise_day_of_month', return_type = int, default = 1)
            self.ExerciseDayOfWeek = self.config.ReadValue('exercise_day_of_week',  default = "Monday")
            self.ExerciseDuration = self.config.ReadValue('exercise_duration', return_type = float, default = 12)
            self.ExerciseWarmup = self.config.ReadValue('exercise_warmup', return_type = float, default = 0)
            self.ExerciseFrequency = self.config.ReadValue('exercise_frequency',  default = "Monthly")
            self.MonitorAddress = self.config.ReadValue('monitor_address', default = ProgramDefaults.LocalHost)
            self.LastExerciseTime = self.config.ReadValue('last_exercise',  default = None)
            self.UseGeneratorTime = self.config.ReadValue('use_gen_time',  return_type = bool, default = False)
            self.Debug = self.config.ReadValue('debug', return_type = bool, default = False)

            # Validate settings
            if not self.ExerciseType.lower() in ["normal","quiet", "transfer"]:
                self.ExerciseType = "normal"
            if self.ExerciseHour  > 23 or self.ExerciseHour < 0:
                self.ExerciseHour = 12
            if self.ExerciseMinute  > 59 or self.ExerciseMinute < 0:
                self.ExerciseMinute = 0
            if not self.ExerciseDayOfWeek.lower() in ["monday", "tuesday", "wednesday","thursday","friday","saturday","sunday"]:
                self.ExerciseDayOfWeek = "Monday"
            if self.ExerciseDayOfMonth  > 28 or self.ExerciseDayOfMonth < 1:
                self.ExerciseDayOfMonth = 1
            if self.ExerciseDuration  > 60:
                self.ExerciseDuration = 60
            if self.ExerciseDuration  < 5:
                self.ExerciseDuration = 5
            if self.ExerciseWarmup  > 30:
                self.ExerciseWarmup = 30
            if self.ExerciseWarmup  < 0:
                self.ExerciseWarmup = 0
            if not self.ExerciseFrequency.lower() in ["weekly","biweekly", "monthly"]:
                self.ExerciseFrequency = "Monthly"

            if self.MonitorAddress == None or not len(self.MonitorAddress):
                self.MonitorAddress = ProgramDefaults.LocalHost

        except Exception as e1:
            self.LogErrorLine("Error reading " + ConfigFilePath + "genexercise.conf: " + str(e1))
            self.console.error("Error reading " + ConfigFilePath + "genexercise.conf: " + str(e1))
            sys.exit(1)

        try:

            try:
                startcount = 0
                while startcount <= 10:
                    try:
                        self.Generator = ClientInterface(host = self.MonitorAddress, port = port, log = self.log)
                        break
                    except Exception as e1:
                        startcount += 1
                        if startcount >= 10:
                            self.console.info("genmon not loaded.")
                            self.LogError("Unable to connect to genmon.")
                            sys.exit(1)
                        time.sleep(1)
                        continue

            except Exception as e1:
                self.LogErrorLine("Error in genexercise init: "  + str(e1))

            if not self.CheckGeneratorRequirement():
                self.LogError("Requirements not met. Exiting.")
                sys.exit(1)

            # start thread monitor time for exercise
            self.Threads["ExerciseThread"] = MyThread(self.ExerciseThread, Name = "ExerciseThread", start = False)
            self.Threads["ExerciseThread"].Start()

            try:
                if self.ExerciseFrequency.lower() == "monthly":
                    DayStr = "Day " + str(self.ExerciseDayOfMonth)
                else:
                    DayStr = str(self.ExerciseDayOfWeek)

                self.LogError("Execise: " + self.ExerciseType + ", " + self.ExerciseFrequency + " at "
                    + str(self.ExerciseHour) + ":" + str(self.ExerciseMinute) + " on " + DayStr + " for "
                    + str(self.ExerciseDuration) + " min. Warmup: " + str(self.ExerciseWarmup))
                self.DebugOutput("Debug Enabled")
            except Exception as e1:
                self.LogErrorLine(str(e1))
            atexit.register(self.Close)
            signal.signal(signal.SIGTERM, self.Close)
            signal.signal(signal.SIGINT, self.Close)

        except Exception as e1:
            self.LogErrorLine("Error in GenExercise init: " + str(e1))
            self.console.error("Error in GenExercise init: " + str(e1))
            sys.exit(1)

    #----------  GenExercise::SendCommand --------------------------------------
    def SendCommand(self, Command):

        if len(Command) == 0:
            return "Invalid Command"

        try:
            with self.AccessLock:
                data = self.Generator.ProcessMonitorCommand(Command)
        except Exception as e1:
            self.LogErrorLine("Error calling  ProcessMonitorCommand: " + str(Command))
            data = ""

        return data
    #----------  GenExercise::CheckGeneratorRequirement ------------------------
    def CheckGeneratorRequirement(self):

        try:
            data = self.SendCommand("generator: start_info_json")
            StartInfo = {}
            StartInfo = json.loads(data)
            if not "evolution" in StartInfo["Controller"].lower() and not "nexus" in StartInfo["Controller"].lower():
                self.LogError("Error: Only Evolution or Nexus controllers are supported for this feature: " + StartInfo["Controller"])
                return False
            return True
        except Exception as e1:
            self.LogErrorLine("Error in CheckGeneratorRequirement: " + str(e1))
            return False

    # ---------- GenExercise::PostWarmup----------------------------------------
    def PostWarmup(self):

        # check to see if the generator is running
        status = self.SendCommand("generator: getbase")
        if not status.lower() in ["running", "exercising"]:
            self.LogError("WARNING: generator not running post warmup. Transfer switch not activated.")
            self.SendCommand("generator: setremote=stop")
            return

        self.SendCommand("generator: setremote=starttransfer")
        self.DebugOutput("Starting transfer exercise cycle (post warmup).")
        # set timer to stop
        self.StopTimer = threading.Timer(float(self.ExerciseDuration  * 60.0), self.StopExercise)
        self.StopTimer.start()

    # ---------- GenExercise::ReadyToExercise-----------------------------------
    def ReadyToExercise(self):

        status = self.SendCommand("generator: getbase")
        if not status.lower() in ["ready"]:
            self.LogError("Generator not in Ready state, exercise cycle not started")
            return False
        return True
    # ---------- GenExercise::StartExercise-------------------------------------
    def StartExercise(self):

        if self.ExerciseActive:
            # already active
            return

        # Start generator
        if self.ExerciseType.lower() == "normal" and self.ReadyToExercise():
            self.SendCommand("generator: setremote=start")
            self.DebugOutput("Starting normal exercise cycle.")
            self.StopTimer = threading.Timer(float(self.ExerciseDuration  * 60.0), self.StopExercise)
            self.StopTimer.start()
        elif self.ExerciseType.lower() == "quiet" and self.ReadyToExercise():
            self.SendCommand("generator: setremote=startexercise")
            self.DebugOutput("Starting quiet exercise cycle.")
            self.StopTimer = threading.Timer(float(self.ExerciseDuration  * 60.0), self.StopExercise)
            self.StopTimer.start()
        elif self.ExerciseType.lower() == "transfer" and self.ReadyToExercise():
            if self.ExerciseWarmup == 0:
                self.SendCommand("generator: setremote=starttransfer")
                self.DebugOutput("Starting transfer exercise cycle.")
                self.StopTimer = threading.Timer(float(self.ExerciseDuration  * 60.0), self.StopExercise)
                self.StopTimer.start()
            else:
                self.SendCommand("generator: setremote=start")
                self.DebugOutput("Starting warmup for transfer exercise cycle.")
                # start timer for post warmup transition to starttransfer command
                self.WarmupTimer = threading.Timer(float(self.ExerciseWarmup  * 60.0), self.PostWarmup)
                self.WarmupTimer.start()
        else:
            self.LogError("Invalid mode in StartExercise: " + str(self.ExerciseType))
            return
        self.WriteLastExerciseTime()
        self.ExerciseActive = True

    # ---------- GenExercise::StopExercise--------------------------------------
    def StopExercise(self):

        if self.ExerciseActive:
            self.SendCommand("generator: setremote=stop")
            self.DebugOutput("Stopping exercise cycle.")
            self.ExerciseActive = False
        else:
            self.DebugOutput("Calling Stop Exercise (not needed)")

    # ---------- GenExercise::DebugOutput-----------------------------
    def DebugOutput(self, Message):

        if self.Debug:
            self.LogError(Message)

    # ---------- GenExercise::WriteLastExerciseTime-----------------------------
    def WriteLastExerciseTime(self):

        try:
            NowString = datetime.datetime.now().strftime("%A %B %d, %Y %H:%M:%S")
            if self.ExerciseFrequency.lower() == "biweekly":
                self.config.WriteValue("last_exercise", NowString)
                self.config.LastExerciseTime = NowString
            self.DebugOutput("Last Exercise Cycle: " + NowString)
        except Exception as e1:
            self.LogErrorLine("Error in WriteLastExerciseTime: " + str(e1))

    # ---------- GenExercise::TimeForExercise-----------------------------------
    def TimeForExercise(self):
        try:
            if self.UseGeneratorTime:
                TimeNow = self.GetGeneratorTime()
            else:
                TimeNow = datetime.datetime.now()
            if TimeNow.hour != self.ExerciseHour or TimeNow.minute != self.ExerciseMinute:
                return False

            weekDays = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")

            WeekDayString = weekDays[TimeNow.weekday()]

            if not self.ExerciseFrequency.lower() in ["weekly", "biweekly", "monthly"]:
                self.LogError("Invalid Exercise Frequency in TimeForExercise: " + str(self.ExerciseFrequency))
                return False
            if self.ExerciseFrequency.lower() == "weekly" and self.ExerciseDayOfWeek.lower() == WeekDayString.lower():
                return True
            elif self.ExerciseFrequency.lower() == "biweekly"  and self.ExerciseDayOfWeek.lower() == WeekDayString.lower():
                if self.LastExerciseTime == None:
                    return True
                LastExerciseTime = datetime.datetime.strptime(self.LastExerciseTime, "%A %B %d, %Y %H:%M:%S")
                if (TimeNow -  LastExerciseTime).days >= 14:
                    return True
                return False
            elif self.ExerciseFrequency.lower() == "monthly" and TimeNow.day == self.ExerciseDayOfMonth:
                return True
            else:
                return False
        except Exception as e1:
            self.LogErrorLine("Error in TimeForExercise: " + str(e1))
        return False
    # ---------- GenExercise::GetGeneratorTime----------------------------------
    def GetGeneratorTime(self):
        try:
            GenTimeStr = ""
            data = self.SendCommand("generator: status_json")
            Status = {}
            Status = json.loads(data)
            TimeDict = self.FindDictValueInListByKey("Time", Status["Status"])
            if TimeDict != None:
                TimeDictStr = self.FindDictValueInListByKey("Generator Time", TimeDict)
                if TimeDictStr != None or not len(TimeDictStr):
                    GenTimeStr = TimeDictStr
                    # Format is "Wednesday March 6, 2019 13:10" or " "Friday May 3, 2019 11:11"
                    GenTime = datetime.datetime.strptime(GenTimeStr, "%A %B %d, %Y %H:%M")
                else:
                    self.LogError("Error getting generator time! Genmon may be starting up.")
                    GenTime = datetime.datetime.now()
            else:
                self.LogError("Error getting generator time (2)!")
                GenTime = datetime.datetime.now()
            return GenTime
        except Exception as e1:
            self.LogErrorLine("Error in GetGeneratorTime: " + str(e1) + ": " + GenTimeStr)
            return datetime.datetime.now()
    # ---------- GenExercise::ExerciseThread------------------------------------
    def ExerciseThread(self):

        time.sleep(1)
        while True:
            try:
                if not self.ExerciseActive:
                    if self.TimeForExercise():
                        self.StartExercise()
                if self.WaitForExit("ExerciseThread", float(self.PollTime)):
                    return
            except Exception as e1:
                self.LogErrorLine("Error in ExerciseThread: " + str(e1))
                if self.WaitForExit("ExerciseThread", float(self.PollTime)):
                    return

    # ----------GenExercise::Close----------------------------------------------
    def Close(self):
        self.KillThread("ExerciseThread")

        if self.ExerciseActive:
            try:
                self.WarmupTimer.cancel()
            except:
                pass
            try:
                self.StopTimer.cancel()
            except:
                pass
            self.StopExercise()
        self.Generator.Close()
Exemplo n.º 11
0
    if os.geteuid() != 0:
        print(
            "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
        )
        sys.exit(2)

    console = SetupLogger("emailsms_console", log_file="", stream=True)
    HelpStr = '\nsudo python genemail2sms.py -a <IP Address or localhost> -c <path to genmon config file>\n'

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                   ["help", "configpath=", "address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
        elif opt in ("-c", "--configpath"):
            ConfigFilePath = arg
            ConfigFilePath = ConfigFilePath.strip()

    port, loglocation = MySupport.GetGenmonInitInfo(ConfigFilePath,
                                                    log=console)
    log = SetupLogger("client", os.path.join(loglocation, "genemail2sms.log"))
Exemplo n.º 12
0
    # log errors in this module to a file
    console = SetupLogger("client_console", log_file="", stream=True)
    log = SetupLogger("client", "client.log")

    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)

    startcount = 0
    while startcount <= 2:
        try:
            MyClientInterface = ClientInterface(host=address, log=log)
            break
        except Exception as e1:
            startcount += 1
            if startcount >= 2:
                console.error("Error: genmon not loaded.")
                sys.exit(1)
            time.sleep(1)
            continue

    try:

        while True:
            try:
                line = raw_input(">")
            except NameError:
                pass
                line = input(">")

            if line.lower() == "exit":
                break
Exemplo n.º 13
0
        log.error("Error: " + str(e1))
        console.error("Error: " + str(e1))


#------------------- Command-line interface for gengpio ------------------------
if __name__ == '__main__':
    address = ProgramDefaults.LocalHost

    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)
    console = SetupLogger("pushover_console", log_file="", stream=True)
    HelpStr = '\nsudo python genpushover.py -a <IP Address or localhost> -c <path to genmon config file>\n'

    if os.geteuid() != 0:
        console.error(
            "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
        )
        sys.exit(2)

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                   ["help", "configpath=", "address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
Exemplo n.º 14
0
            Settings += " Resitor Pull Up "
        elif DefaultPullup == GPIO.PUD_DOWN:
            Settings += " Resitor Pull Down "
        else:
            Settings += " Resitor Pull Unknown "

        if DefaultTrigger == GPIO.RISING:
            Settings += " Trigger Rising "
        elif DefaultTrigger == GPIO.FALLING:
            Settings += " Trigger Falling "
        elif DefaultTrigger == GPIO.BOTH:
            Settings += " Trigger Both "
        else:
            Settings += " Trigger Unknown "

        log.error("Settings: " + Settings + " bounce = " + str(DefaultBounce))
        MyClientInterface = ClientInterface(host=address, log=log)

        #setup GPIO using Board numbering
        GPIO.setmode(GPIO.BOARD)

        #log.info( GPIO.RPI_INFO)
        #log.info(GPIO.VERSION)

        ChannelList = {
            INPUT_STOP: StopCallBack,
            INPUT_START: StartCallBack,
            INPUT_START_TRANSFER: StartTransferCallBack
        }

        GPIO.setwarnings(True)
Exemplo n.º 15
0
        log.error("Error: " + str(e1))
        console.error("Error: " + str(e1))


#------------------- Command-line interface for gengpio -----------------------#
if __name__ == '__main__':
    address = ProgramDefaults.LocalHost

    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)
    console = SetupLogger("sms_console_modem", log_file="", stream=True)
    HelpStr = '\nsudo python gensmns_modem.py -a <IP Address or localhost> -c <path to genmon config file>\n'

    if os.geteuid() != 0:
        console.error(
            "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
        )
        sys.exit(2)

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                   ["help", "configpath=", "address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
Exemplo n.º 16
0
    sys.exit(0)


#------------------- Command-line interface for monitor -----------------#
if __name__ == '__main__':  # usage program.py [server_address] [port]
    address = ProgramDefaults.LocalHost
    port = ProgramDefaults.ServerPort

    # log errors in this module to a file
    console = SetupLogger("client_console", log_file="", stream=True)
    HelpStr = '\npython ClientInterface.py -a <IP Address or none for localhost> -p <port or none for default port>\n'
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hp:a:",
                                   ["help", "port=", "address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    try:
        for opt, arg in opts:
            if opt == '-h':
                console.error(HelpStr)
                sys.exit()
            elif opt in ("-a", "--address"):
                address = arg
            elif opt in ("-p", "--port"):
                port = int(arg)
    except Exception as e1:
        console.error("Error parsing: " + str(e1))
        sys.exit(2)
Exemplo n.º 17
0
    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)

    if os.geteuid() != 0:
        print("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
        sys.exit(2)

    console = SetupLogger("emailsms_console", log_file = "", stream = True)
    HelpStr = '\nsudo python genemail2sms.py -a <IP Address or localhost> -c <path to genmon config file>\n'

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["help","configpath=","address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
        elif opt in ("-c", "--configpath"):
            ConfigFilePath = arg
            ConfigFilePath = ConfigFilePath.strip()

    port, loglocation = MySupport.GetGenmonInitInfo(ConfigFilePath, log = console)
    log = SetupLogger("client", loglocation + "genemail2sms.log")
Exemplo n.º 18
0
class GenTankData(MySupport):

    # The device is the ADS1115 I2C ADC
    # reference python http://www.smartypies.com/projects/ads1115-with-raspberrypi-and-python/ads1115runner/
    RESET_ADDRESS = 0b0000000
    RESET_COMMAND = 0b00000110
    POINTER_CONVERSION = 0x0
    POINTER_CONFIGURATION = 0x1
    POINTER_LOW_THRESHOLD = 0x2
    POINTER_HIGH_THRESHOLD = 0x3

    #------------ GenTankData::init---------------------------------------------
    def __init__(self,
                 log=None,
                 loglocation=ProgramDefaults.LogPath,
                 ConfigFilePath=MyCommon.DefaultConfPath,
                 host=ProgramDefaults.LocalHost,
                 port=ProgramDefaults.ServerPort):

        super(GenTankData, self).__init__()

        self.LogFileName = os.path.join(loglocation, "gentankdiy.log")
        self.AccessLock = threading.Lock()
        # log errors in this module to a file
        self.log = SetupLogger("gentankdiy", self.LogFileName)

        self.console = SetupLogger("gentankdiy_console",
                                   log_file="",
                                   stream=True)

        self.MonitorAddress = host
        self.PollTime = 2
        self.debug = False

        configfile = os.path.join(ConfigFilePath, 'gentankdiy.conf')
        try:
            if not os.path.isfile(configfile):
                self.LogConsole("Missing config file : " + configfile)
                self.LogError("Missing config file : " + configfile)
                sys.exit(1)

            self.config = MyConfig(filename=configfile,
                                   section='gentankdiy',
                                   log=self.log)

            self.PollTime = self.config.ReadValue('poll_frequency',
                                                  return_type=float,
                                                  default=60)
            self.debug = self.config.ReadValue('debug',
                                               return_type=bool,
                                               default=False)
            self.i2c_address = self.config.ReadValue('i2c_address',
                                                     return_type=int,
                                                     default=72)
            self.mv_per_step = self.config.ReadValue('mv_per_step',
                                                     return_type=int,
                                                     default=125)
            self.Multiplier = self.config.ReadValue(
                'volts_to_percent_multiplier', return_type=float, default=20.0)
            # I2C channel 1 is connected to the GPIO pins
            self.i2c_channel = self.config.ReadValue('i2c_channel',
                                                     return_type=int,
                                                     default=1)

            if self.MonitorAddress == None or not len(self.MonitorAddress):
                self.MonitorAddress = ProgramDefaults.LocalHost

        except Exception as e1:
            self.LogErrorLine("Error reading " + configfile + ": " + str(e1))
            self.LogConsole("Error reading " + configfile + ": " + str(e1))
            sys.exit(1)

        try:

            try:
                startcount = 0
                while startcount <= 10:
                    try:
                        self.Generator = ClientInterface(
                            host=self.MonitorAddress, port=port, log=self.log)
                        break
                    except Exception as e1:
                        startcount += 1
                        if startcount >= 10:
                            self.console.info("genmon not loaded.")
                            self.LogError("Unable to connect to genmon.")
                            sys.exit(1)
                        time.sleep(1)
                        continue

            except Exception as e1:
                self.LogErrorLine("Error in GenTankData init: " + str(e1))

            # start thread monitor time for exercise
            self.Threads["TankCheckThread"] = MyThread(self.TankCheckThread,
                                                       Name="TankCheckThread",
                                                       start=False)

            if not self.InitADC():
                self.LogError("InitADC failed, exiting")
                sys.exit(1)

            self.Threads["TankCheckThread"].Start()

            atexit.register(self.Close)
            signal.signal(signal.SIGTERM, self.Close)
            signal.signal(signal.SIGINT, self.Close)

        except Exception as e1:
            self.LogErrorLine("Error in GenTankData init: " + str(e1))
            self.console.error("Error in GenTankData init: " + str(e1))
            sys.exit(1)

    #----------  GenTankData::SendCommand --------------------------------------
    def SendCommand(self, Command):

        if len(Command) == 0:
            return "Invalid Command"

        try:
            with self.AccessLock:
                data = self.Generator.ProcessMonitorCommand(Command)
        except Exception as e1:
            self.LogErrorLine("Error calling  ProcessMonitorCommand: " +
                              str(Command))
            data = ""

        return data

    # ---------- GenTankData::InitADC-------------------------------------------
    def InitADC(self):

        try:

            # I2C channel 1 is connected to the GPIO pins
            self.I2Cbus = smbus.SMBus(self.i2c_channel)

            # Reset ADC
            self.I2Cbus.write_byte(self.RESET_ADDRESS, self.RESET_COMMAND)

            # set config register  and start conversion
            # ANC1 and GND, 4.096v, 128s/s
            # Customized - Port A0 and 4.096 V input
            # 0b11000011; # bit 15-8  = 0xC3
            # bit 15 flag bit for single shot
            # Bits 14-12 input selection:
            # 100 ANC0; 101 ANC1; 110 ANC2; 111 ANC3
            # Bits 11-9 Amp gain. Default to 010 here 001 P19
            # Bit 8 Operational mode of the ADS1115.
            # 0 : Continuous conversion mode
            # 1 : Power-down single-shot mode (default)
            CONFIG_VALUE_1 = 0xC3
            # bits 7-0  0b10000101 = 0x85
            # Bits 7-5 data rate default to 100 for 128SPS
            # Bits 4-0  comparator functions see spec sheet.
            CONFIG_VALUE_2 = 0x85
            self.I2Cbus.write_i2c_block_data(self.i2c_address,
                                             self.POINTER_CONFIGURATION,
                                             [CONFIG_VALUE_1, CONFIG_VALUE_2])

            self.LogDebug("I2C Init complete: success")

        except Exception as e1:
            self.LogErrorLine("Error calling InitADC: " + str(e1))
            return False

        return True

    # ---------- GenTankData::GetGaugeData--------------------------------------
    def GetGaugeData(self):
        try:

            val = self.I2Cbus.read_i2c_block_data(self.i2c_address,
                                                  self.POINTER_CONVERSION, 2)

            self.LogDebug(str(val))
            # convert display results
            reading = val[0] << 8 | val[1]

            if (reading < 0):
                reading = 0

            #reading = self.I2Cbus.read_word_data(self.i2c_address, self.i2c_channel)
            volts = round(
                float(reading * (float(self.mv_per_step) / 1000000.0)), 2)
            gauge_data = float(self.Multiplier) * volts
            self.LogDebug("Reading Gauge Data: %4.2f%%" % gauge_data)
            return gauge_data

        except Exception as e1:
            self.LogErrorLine("Error calling  GetGaugeData: " + str(e1))
            return 0.0

    # ---------- GenTankData::TankCheckThread-----------------------------------
    def TankCheckThread(self):

        time.sleep(1)

        while True:
            try:

                dataforgenmon = {}

                tankdata = self.GetGaugeData()
                if tankdata != None:
                    dataforgenmon["Tank Name"] = "External Tank"
                    dataforgenmon["Capacity"] = 0
                    dataforgenmon["Percentage"] = self.GetGaugeData()

                    retVal = self.SendCommand("generator: set_tank_data=" +
                                              json.dumps(dataforgenmon))
                    self.LogDebug(retVal)
                if self.WaitForExit("TankCheckThread",
                                    float(self.PollTime * 60)):
                    return
            except Exception as e1:
                self.LogErrorLine("Error in TankCheckThread: " + str(e1))
                if self.WaitForExit("TankCheckThread",
                                    float(self.PollTime * 60)):
                    return

    # ----------GenTankData::Close----------------------------------------------
    def Close(self):
        self.KillThread("TankCheckThread")
        self.Generator.Close()
Exemplo n.º 19
0
class GenTankData(MySupport):

    #------------ GenTankData::init---------------------------------------------
    def __init__(self,
                 log=None,
                 loglocation=ProgramDefaults.LogPath,
                 ConfigFilePath=MyCommon.DefaultConfPath,
                 host=ProgramDefaults.LocalHost,
                 port=ProgramDefaults.ServerPort):

        super(GenTankData, self).__init__()

        self.LogFileName = loglocation + "gentankutil.log"
        self.AccessLock = threading.Lock()
        # log errors in this module to a file
        self.log = SetupLogger("gentankutil", self.LogFileName)

        self.console = SetupLogger("gentankutil_console",
                                   log_file="",
                                   stream=True)

        self.MonitorAddress = host
        self.PollTime = 2
        self.TankID = ""
        self.debug = False
        configfile = ConfigFilePath + 'gentankutil.conf'
        try:
            if not os.path.isfile(configfile):
                self.LogConsole("Missing config file : " + configfile)
                self.LogError("Missing config file : " + configfile)
                sys.exit(1)

            self.config = MyConfig(filename=configfile,
                                   section='gentankutil',
                                   log=self.log)

            self.PollTime = self.config.ReadValue('poll_frequency',
                                                  return_type=float,
                                                  default=60)
            self.debug = self.config.ReadValue('debug',
                                               return_type=bool,
                                               default=False)
            self.username = self.config.ReadValue('username', default="")
            self.password = self.config.ReadValue('password', default="")
            self.tank_name = self.config.ReadValue('tank_name', default="")

            if self.MonitorAddress == None or not len(self.MonitorAddress):
                self.MonitorAddress = ProgramDefaults.LocalHost

        except Exception as e1:
            self.LogErrorLine("Error reading " + configfile + ": " + str(e1))
            self.LogConsole("Error reading " + configfile + ": " + str(e1))
            sys.exit(1)

        if self.username == "" or self.username == None or self.password == "" or self.password == None:
            self.LogError("Invalid user name or password, exiting")
            sys.exit(1)

        try:

            try:
                startcount = 0
                while startcount <= 10:
                    try:
                        self.Generator = ClientInterface(
                            host=self.MonitorAddress, port=port, log=self.log)
                        break
                    except Exception as e1:
                        startcount += 1
                        if startcount >= 10:
                            self.console.info("genmon not loaded.")
                            self.LogError("Unable to connect to genmon.")
                            sys.exit(1)
                        time.sleep(1)
                        continue

            except Exception as e1:
                self.LogErrorLine("Error in GenTankData init: " + str(e1))

            #if not self.CheckGeneratorRequirement():
            #    self.LogError("Requirements not met. Exiting.")
            #    sys.exit(1)

            self.tank = tankutility(self.username,
                                    self.password,
                                    self.log,
                                    debug=self.debug)
            # start thread monitor time for exercise
            self.Threads["TankCheckThread"] = MyThread(self.TankCheckThread,
                                                       Name="TankCheckThread",
                                                       start=False)
            self.Threads["TankCheckThread"].Start()

            atexit.register(self.Close)
            signal.signal(signal.SIGTERM, self.Close)
            signal.signal(signal.SIGINT, self.Close)

        except Exception as e1:
            self.LogErrorLine("Error in GenTankData init: " + str(e1))
            self.console.error("Error in GenTankData init: " + str(e1))
            sys.exit(1)

    #----------  GenTankData::SendCommand --------------------------------------
    def SendCommand(self, Command):

        if len(Command) == 0:
            return "Invalid Command"

        try:
            with self.AccessLock:
                data = self.Generator.ProcessMonitorCommand(Command)
        except Exception as e1:
            self.LogErrorLine("Error calling  ProcessMonitorCommand: " +
                              str(Command))
            data = ""

        return data

    #----------  GenTankData::CheckGeneratorRequirement ------------------------
    def CheckGeneratorRequirement(self):

        try:
            data = self.SendCommand("generator: start_info_json")
            StartInfo = {}
            StartInfo = json.loads(data)
            if not "evolution" in StartInfo["Controller"].lower(
            ) and not "nexus" in StartInfo["Controller"].lower():
                self.LogError(
                    "Error: Only Evolution or Nexus controllers are supported for this feature: "
                    + StartInfo["Controller"])
                return False
            return True
        except Exception as e1:
            self.LogErrorLine("Error in CheckGeneratorRequirement: " + str(e1))
            return False

    # ---------- GenTankData::Login---------------------------------------------
    def Login(self, force=False):

        if force:
            self.TankID = ""
        if len(self.TankID):
            # already logged in
            return True
        if not self.tank.Login():
            return False
        self.TankID = self.tank.GetIDFromName(self.tank_name)
        if not len(self.TankID):
            return False
        return True

    # ---------- GenTankData::TankCheckThread-----------------------------------
    def TankCheckThread(self):

        time.sleep(1)
        LastLoginTime = datetime.datetime.now()
        while True:
            try:
                NUMBER_OF_SECONDS = 60 * 60 * 12  # 12 hours

                if ((datetime.datetime.now() - LastLoginTime).total_seconds() >
                        NUMBER_OF_SECONDS) or not len(self.TankID):
                    self.LogDebug("Login ")
                    if not self.Login(force=True):
                        self.LogError(
                            "Error logging in in TankCheckThread, retrying")

                dataforgenmon = {}

                tankdata = self.tank.GetData(self.TankID)
                if tankdata != None:
                    dataforgenmon["Tank Name"] = tankdata["name"]
                    dataforgenmon["Capacity"] = self.tank.GetCapacity()
                    dataforgenmon["Percentage"] = self.tank.GetPercentage()

                    retVal = self.SendCommand("generator: set_tank_data=" +
                                              json.dumps(dataforgenmon))
                    self.LogDebug(retVal)
                if self.WaitForExit("TankCheckThread",
                                    float(self.PollTime * 60)):
                    return
            except Exception as e1:
                self.LogErrorLine("Error in TankCheckThread: " + str(e1))
                if self.WaitForExit("TankCheckThread",
                                    float(self.PollTime * 60)):
                    return

    # ----------GenTankData::Close----------------------------------------------
    def Close(self):
        self.KillThread("TankCheckThread")
        self.Generator.Close()
Exemplo n.º 20
0
    console = SetupLogger("pushover_console", log_file="", stream=True)
    log = SetupLogger("client", "/var/log/genpushover.log")

    try:

        config = MyConfig(filename='/etc/genpushover.conf',
                          section='genpushover',
                          log=log)

        appid = config.ReadValue('appid')
        userid = config.ReadValue('userid')
        pushsound = config.ReadValue('pushsound', default='updown')

        if appid == None or not len(appid):
            log.error("Error:  invalid app ID")
            console.error("Error:  invalid app ID")
            sys.exit(2)

        if userid == None or not len(userid):
            log.error("Error:  invalid user ID")
            console.error("Error:  invalid user ID")
            sys.exit(2)

    except Exception as e1:
        log.error("Error reading /etc/genpushover.conf: " + str(e1))
        console.error("Error reading /etc/genpushover.conf: " + str(e1))
        sys.exit(1)
    try:
        GenNotify = GenNotify(host=address,
                              onready=OnReady,
Exemplo n.º 21
0
        if self.Debug:
            self.console.info("Confirmed: " + msg.topic + ": " + str(msg.payload))

    # ---------- MyMQTT::Close--------------------------------------------------
    def Close(self):
        self.Push.Close()
#-------------------------------------------------------------------------------
if __name__ == "__main__":

    address=ProgramDefaults.LocalHost

    console = SetupLogger("genmqtt_console_", log_file = "", stream = True)
    HelpStr = '\nsudo python genmqtt.py -a <IP Address or localhost> -c <path to genmon config file>\n'
    if os.geteuid() != 0:
        console.error("\nYou need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.\n")
        sys.exit(2)

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["help","configpath=","address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
Exemplo n.º 22
0
    except getopt.GetoptError:
        print(HelpStr)
        sys.exit(2)

    configfilename = "gentankdiy.conf"
    configfile = os.path.join(MyCommon.DefaultConfPath, configfilename)

    for opt, arg in opts:
        if opt == '-h':
            print(HelpStr)
            sys.exit()
        elif opt in ("-c"):
            configfile = os.path.join(arg, configfilename)

    if not os.path.isfile(configfile):
        console.error("Missing config file: {0}".format(configfile))
        sys.exit(1)

    console.error("Using config file %s" % configfile)

    config = MyConfig(filename=configfile, section='gentankdiy')

    gauge = GaugeDIY2(config, console=console, log=console)

    if not gauge.InitADC():
        console.error("Error initializing ADC, exiting")
        exit(2)

    average_n = 10

    while True:
Exemplo n.º 23
0
    address = ProgramDefaults.LocalHost
    fileName = ""

    HelpStr = '\npython genlog.py -a <IP Address or localhost> -f <outputfile> -c <config file path>\n'

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        console = SetupLogger("genlog_console", log_file="", stream=True)

        port, loglocation, multi_instance = MySupport.GetGenmonInitInfo(
            ConfigFilePath, log=console)

        if not MySupport.PermissionsOK():
            console.error(
                "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
            )
            sys.exit(2)

        if MySupport.IsRunning(os.path.basename(__file__),
                               multi_instance=multi_instance):
            console.error("The program %s is already loaded" %
                          os.path.basename(__file__))
            sys.exit(2)

        opts, args = getopt.getopt(
            sys.argv[1:], "ha:f:c:",
            ["help", "address=", "filename=", "configpath="])
    except getopt.GetoptError:
        console.error(HelpStr)
        sys.exit(2)
Exemplo n.º 24
0
                    GPIO.output(ER_WARNING, GPIO.LOW)

            # Get Genmon status
            try:
                data = MyClientInterface.ProcessMonitorCommand(
                    "generator: monitor_json")
                TempDict = {}
                TempDict = json.loads(data)
                HealthStr = TempDict["Monitor"][0]["Generator Monitor Stats"][
                    0]["Monitor Health"]
                if HealthStr.lower() == "ok":
                    GPIO.output(ER_INTERNET, GPIO.LOW)
                else:
                    GPIO.output(ER_INTERNET, GPIO.HIGH)
            except Exception as e1:
                log.error("Error getting monitor health: " + str(e1))
            # get Internet Status
            try:
                data = MyClientInterface.ProcessMonitorCommand(
                    "generator: network_status")
                if data.lower() == "ok":
                    GPIO.output(ER_INTERNET, GPIO.LOW)
                else:
                    GPIO.output(ER_INTERNET, GPIO.HIGH)
                time.sleep(3)
            except Exception as e1:
                log.error("Error getting internet status: " + str(e1))

    except Exception as e1:
        log.error("Error: " + str(e1))
        console.error("Error: " + str(e1))
Exemplo n.º 25
0
class ClientInterface(MyCommon):
    def __init__(self,
                 host=ProgramDefaults.LocalHost,
                 port=ProgramDefaults.ServerPort,
                 log=None,
                 loglocation=ProgramDefaults.LogPath):
        super(ClientInterface, self).__init__()
        if log != None:
            self.log = log
        else:
            # log errors in this module to a file
            self.log = SetupLogger("client",
                                   os.path.join(loglocation, "myclient.log"))

        self.console = SetupLogger("client_console", log_file="", stream=True)

        self.AccessLock = threading.RLock()
        self.EndOfMessage = "EndOfMessage"
        self.rxdatasize = 2000
        self.host = host
        self.port = port
        self.max_reties = 10
        self.Connect()

    #----------  ClientInterface::Connect --------------------------------------
    def Connect(self):

        retries = 0
        while True:

            try:
                #create an INET, STREAMing socket
                self.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

                #now connect to the server on our port
                self.Socket.connect((self.host, self.port))
                sRetData, data = self.Receive(
                    noeom=True)  # Get initial status before commands are sent
                self.console.info(data)
                return
            except Exception as e1:
                retries += 1
                if retries >= self.max_reties:
                    self.LogErrorLine("Error: Connect : " + str(e1))
                    self.console.error("Genmon not loaded.")
                    sys.exit(1)
                else:
                    time.sleep(1)
                    continue

    #----------  ClientInterface::SendCommand ----------------------------------
    def SendCommand(self, cmd):

        try:
            self.Socket.sendall(cmd.encode("utf-8"))
        except Exception as e1:
            self.LogErrorLine("Error: TX: " + str(e1))
            self.Close()
            self.Connect()

    #----------  ClientInterface::Receive --------------------------------------
    def Receive(self, noeom=False):

        with self.AccessLock:
            RetStatus = True
            try:
                bytedata = self.Socket.recv(self.rxdatasize)
                data = bytedata.decode("utf-8")
                if len(data):
                    if not self.CheckForStarupMessage(data) or not noeom:
                        while not self.EndOfMessage in data:
                            morebytes = self.Socket.recv(self.rxdatasize)
                            more = morebytes.decode("utf-8")
                            if len(more):
                                if self.CheckForStarupMessage(more):
                                    data = ""
                                    RetStatus = False
                                    break
                                data += more

                        if data.endswith(self.EndOfMessage):
                            data = data[:-len(self.EndOfMessage)]
                            RetStatus = True
                else:
                    self.Connect()
                    return False, data
            except Exception as e1:
                self.LogErrorLine("Error: RX:" + str(e1))
                self.Close()
                self.Connect()
                RetStatus = False
                data = "Retry"

            return RetStatus, data

    #----------  ClientInterface::CheckForStarupMessage ------------------------
    def CheckForStarupMessage(self, data):

        # check for initial status response from monitor
        if data.startswith("OK") or data.startswith(
                "CRITICAL:") or data.startswith("WARNING:"):
            return True
        else:
            return False

    #----------  ClientInterface::Close ----------------------------------------
    def Close(self):
        self.Socket.close()

    #----------  ClientInterface::ProcessMonitorCommand ------------------------
    def ProcessMonitorCommand(self, cmd):

        data = ""
        try:
            with self.AccessLock:
                RetStatus = False
                while RetStatus == False:
                    self.SendCommand(cmd)
                    RetStatus, data = self.Receive()
        except Exception as e1:
            self.LogErrorLine("Error in ProcessMonitorCommand:" + str(e1))
        return data
Exemplo n.º 26
0
    except Exception as e1:
        log.error("Error: " + str(e1))
        console.error("Error: " + str(e1))

#------------------- Command-line interface for gengpio ------------------------
if __name__=='__main__': #

    address=ProgramDefaults.LocalHost

    HelpStr = '\nsudo python gensyslog.py -a <IP Address or localhost> -c <path to genmon config file>\n'
    # Set the signal handler
    signal.signal(signal.SIGINT, signal_handler)

    console = SetupLogger("gensyslog_console", log_file = "", stream = True)
    if os.geteuid() != 0:
        console.error("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
        sys.exit(2)

    try:
        ConfigFilePath = ProgramDefaults.ConfPath
        opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["help","configpath=","address="])
    except getopt.GetoptError:
        console.error("Invalid command line argument.")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
Exemplo n.º 27
0
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    lineno = exc_tb.tb_lineno
    return fname + ":" + str(lineno)


#------------------- Command-line interface for gengpioin ----------------------
if __name__ == '__main__':
    address = ProgramDefaults.LocalHost

    try:
        console = SetupLogger("gengpioin_console", log_file="", stream=True)

        if os.geteuid() != 0:
            console.error(
                "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
            )
            sys.exit(2)

        HelpStr = '\nsudo python gengpioin.py -a <IP Address or localhost> -c <path to genmon config file>\n'
        try:
            ConfigFilePath = ProgramDefaults.ConfPath
            opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                       ["help", "configpath=", "address="])
        except getopt.GetoptError:
            console.error("Invalid command line argument.")
            sys.exit(2)

        for opt, arg in opts:
            if opt == '-h':
                console.error(HelpStr)
Exemplo n.º 28
0

#------------------- Command-line interface for genlog -------------------------
if __name__ == '__main__':

    address = '127.0.0.1'
    fileName = ""

    HelpStr = '\npython genlog.py -a <IP Address or localhost> -f <outputfile>\n'

    try:
        console = SetupLogger("genlog_console", log_file="", stream=True)
        opts, args = getopt.getopt(sys.argv[1:], "ha:f:",
                                   ["address=", "filename="])
    except getopt.GetoptError:
        console.error(HelpStr)
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            console.error(HelpStr)
            sys.exit()
        elif opt in ("-a", "--address"):
            address = arg
        elif opt in ("-f", "--filename"):
            fileName = arg
            fileName = fileName.strip()

    console.error('Address is ' + address)
    console.error('Output file is ' + fileName)
    if not len(fileName):
Exemplo n.º 29
0
    GPIO.cleanup()
    MyClientInterface.Close()
    sys.exit(0)


#------------------- Command-line interface for gengpio ------------------------
if __name__ == '__main__':  # usage program.py [server_address]
    address = ProgramDefaults.LocalHost

    try:
        console = SetupLogger("gengpio_console", log_file="", stream=True)

        if os.geteuid() != 0:
            console.error(
                "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
            )
            sys.exit(2)

        HelpStr = '\nsudo python gengpio.py -a <IP Address or localhost> -c <path to genmon config file>\n'
        try:
            ConfigFilePath = ProgramDefaults.ConfPath
            opts, args = getopt.getopt(sys.argv[1:], "hc:a:",
                                       ["help", "configpath=", "address="])
        except getopt.GetoptError:
            console.error("Invalid command line argument.")
            sys.exit(2)

        for opt, arg in opts:
            if opt == '-h':
                console.error(HelpStr)
Exemplo n.º 30
0
    if os.geteuid() != 0:
        print(
            "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting."
        )
        sys.exit(2)

    console = SetupLogger("sms_console_modem", log_file="", stream=True)
    log = SetupLogger("client", "/var/log/gensms_modem.log")

    try:

        SMS = LTEPiHat(log=log)
        if not SMS.InitComplete:
            SMS.Close()
            log.error("Modem Init FAILED!")
            console.error("Modem Init FAILED!")
            sys.exit(2)

    except Exception as e1:
        log.error("Error on modem init:" + str(e1))
        console.error("Error modem init: " + str(e1))
        sys.exit(1)
    try:
        GenNotify = GenNotify(host=address,
                              onready=OnReady,
                              onexercise=OnExercise,
                              onrun=OnRun,
                              onrunmanual=OnRunManual,
                              onalarm=OnAlarm,
                              onservice=OnService,