Example #1
0
 def test_get_weather_data(self):
     config = ConfigController()
     apiTokenFromConfig = config.get_open_weather_token()
     testApiUrl = "https://api.openweathermap.org/data/2.5/weather?q=London,GB&APPID=" + apiTokenFromConfig
     data = Weather.get_weather_data(self, testApiUrl)
     assert 'weather' in data
     assert 'main' in data
     assert 'wind' in data
     assert 'clouds' in data
     assert 'sys' in data
     assert 'coord' in data
Example #2
0
class Weather:
    config = ConfigController()

    api_url_base = "https://api.openweathermap.org/data/2.5/weather?q="
    built_url = ""

    def __init__(self):
        built_api_url = []
        built_api_url = self.add_string_to_url_array(built_api_url,
                                                     self.api_url_base)
        built_api_url = self.add_string_to_url_array(
            built_api_url, self.config.get_location())
        built_api_url = self.add_string_to_url_array(built_api_url, ",")
        built_api_url = self.add_string_to_url_array(
            built_api_url, self.config.get_country_code())
        built_api_url = self.add_string_to_url_array(built_api_url, "&APPID=")
        built_api_url = self.add_string_to_url_array(
            built_api_url, self.config.get_open_weather_token())
        self.built_url = self.convert_array_to_string(built_api_url)

    def get_weather_data(self, url):
        r = requests.get(url)
        return r.json()

    def add_string_to_url_array(self, url_array, str_to_add):
        url_array.append(str_to_add)
        return url_array

    def convert_array_to_string(self, array):
        return ''.join(array)
Example #3
0
    def main(self, argv):
        
        self.logger.debug("The total numbers of args passed to the script: %d " % len(argv))
        self.logger.debug("Args: " + str(argv))

        # TODO: flesh out argument parsing and help, -h
        # Parse command line arguments
        sDescription = "no description yet"

        sEpilog = "Returns exit code 0 on success, non-zero on error.\n\n" \
                  "Use logging.conf to change logging information."

        parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=sDescription,
                                         epilog=sEpilog)
        parser.add_argument('-d', '--dir', help='directory to sort')
        parser.add_argument('-f', '--file', help='file containing list of directories to sort')
        parser.add_argument('-c', '--conf', default='app.conf', help='application config file to use (default: %(default)s)')
        args = parser.parse_args()
        self.logger.debug("Parsed args: " + str(args))

        appConfFile = os.path.join(os.path.dirname(__file__), args.conf)
        if not os.path.isfile(appConfFile):
            self.logger.error("File not found: " + appConfFile)
            return

        # Get the configuration parameters
        self.configController = ConfigController()
        self.configController.loadConfiguration(appConfFile)

        # Setup email controller
        self.emailConf = self.configController.getEmailTuple()
        self.emailController = EmailController(self.emailConf.username, self.emailConf.password, self.emailConf.server, self.emailConf.port, self.emailConf.sender_name)

        self.sortController.setExePath(self.configController.get7zaPath())

        file_extensions = self.configController.getFileExtensions()
        self.sortController.fileExts = file_extensions

        directories = []
        if args.dir:
            directories.append(args.dir)

        # Also get the list of directories from a file
        if args.file:
            if not os.path.isfile(args.file):
                self.logger.error("File does not exist: " + args.file)
                return 1
            else:
                with open(args.file, 'r') as file:
                    for line in file:
                        line = line.strip()
                        if line:
                            directories.append(line)

        self.logic(directories)
        return 0
Example #4
0
    def __init__(self):
        # Set up Configuration
        self.configController = ConfigController()

        # Set up System
        self.systemController = SystemController(
            SystemBuilder(ConfigController.config.systemConfig))
        self.sound = self.systemController.get_by_name('SPK')
        self.armedLight = self.systemController.get_by_name('LED0')
        self.alarmLight = self.systemController.get_by_name('LED1')

        self.systemController.register_system_triggered_handler(
            self.handle_sensor_trigger)

        # Set up Camera + system for images
        self.mediaController = MediaController(
            self.configController.config.primaryConfig.find('media'))
        self.cameraController = CameraController(self.mediaController)

        # Set up Authentication
        self.authController = AuthController(self)
        self.authController.configure(self.configController.config.authConfig)
        self.authController.register_accept_handler(self.on_accepted_handler)
        self.authController.register_deny_handler(self.on_denied_handler)

        # Set up the user interface
        self.interfaceController = InterfaceController()

        self.interfaceController.set_rfid(
            self.systemController.getFirstOf(RfidReaderComponent))
        self.interfaceController.set_keypad(
            self.systemController.getFirstOf(KeypadComponent))
        self.interfaceController.setup_server(
            self.configController.config.systemConfig.find('server'))
        self.interfaceController.setup_messenger(
            self.configController.config.systemConfig.find('email'))

        self.interfaceController.register_rfid_read_handler(
            self.handle_rfid_tag)
        self.interfaceController.register_keypad_read_handler(
            self.handle_keypad_code)

        print(len(self.systemController.components),
              "components built from system configuration.")
        for component in self.systemController.components:
            print('\t', component.__class__.__name__)

        # Set up Behaviour
        self.behaviour = NormalArmedBehaviour(self)
Example #5
0
    def send(self, message):
        self.logger.info("Sending email.")
        config = ConfigController()
        server = self.build_server(config)
        self.server_connect(server, config)
        self.server_ehlo(server)
        self.server_start_tls(server)
        self.server_ehlo(server)
        self.server_login(server, config)

        message = 'Subject: {}\n\n{}'.format(self.create_subject(message),
                                             self.create_email_body(message))

        self.send_mail(server, config, message)
        self.quit_server(server)
 def test_get_password(self):
     config = ConfigController()
     self.assertIsInstance(config.get_password(), str)
Example #7
0
class Controller:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.devices = {}
        self.configController = None
        self.databaseController = None
        self.pingController = None
        self.emailController = None
        self.emailConf = None
        self.pingCount = None
        return

    def __del__(self):
        return

    def main(self, argv):

        #
        # 1. Argument parsing
        #
        self.logger.debug("The total numbers of args passed to the script: %d " % len(argv))
        self.logger.debug("Args: " + str(argv))

        # TODO: flesh out argument parsing and help, -h
        # Parse command line arguments
        sDescription = "no description yet"

        sEpilog = "Returns exit code 0 on success, non-zero on error.\n\n" \
                  "Use app.conf to change script configuration.\n" \
                  "Use logging.conf to change logging information."

        parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=sDescription,
                                         epilog=sEpilog)
        parser.add_argument('-host', help='hostname to ping')
        parser.add_argument('-f', '--file', help='file containing list of hosts to ping')
        parser.add_argument('-c', '--conf', default='app.conf', help='application config file to use (default: %(default)s)')
        args = parser.parse_args()
        self.logger.debug("Parsed args: " + str(args))

        appConfFile = os.path.join(os.path.dirname(__file__), args.conf)
        if not os.path.isfile(appConfFile):
            self.logger.error(appConfFile + " not found.")
            return

        hostnames = []
        if args.host:
            hostnames.append(args.host)

        # Also get the list of hosts from a file
        if args.file:
            if not os.path.isfile(args.file):
                self.logger.error("File not found: " + args.file)
                return
            else:
                with open(args.file, 'r') as file:
                    for line in file:
                        line = line.strip()
                        if line:
                            hostnames.append(line)

        #
        # 2. Controller setup
        #

        # Get the configuration parameters
        self.configController = ConfigController()
        self.configController.loadConfiguration(appConfFile)

        # Establish a database connection
        self.databaseController = DatabaseController(True)
        self.databaseController.connect(self.configController.getDbConnectString())
        self.devices = self.databaseController.getDevices()

        # Get a ping controller instance
        self.pingController = PingController()
        self.pingCount = self.configController.getPingCount()

        # Setup email controller
        self.emailConf = self.configController.getEmailTuple()
        self.emailController = EmailController(self.emailConf.username, self.emailConf.password, self.emailConf.server, self.emailConf.port, self.emailConf.sender_name)

        #
        # 3. Script main run
        #
        # Finally we use the list of hostnames
        self.logic(hostnames)

        self.databaseController.disconnect()

        return 0

    # TODO: need a better function name than "logic"...
    def logic(self, hostnames):

        self.logger.debug("Hostnames: " + str(hostnames))

        # list of ping result tuples for failed pings
        failedPingResults = []

        # list of ping result tuples for successful pings
        successfulPingResults = []

        # list of ping result tuples from the database
        latestPingResults = self.databaseController.getLatestPingResults()

        # list of ping result tuples for successful pings that were previously failures
        failsuccessPingResults = []

        for hostname in hostnames:
            # If this is a new device then add it to the database
            if hostname not in self.devices:
                self.logger.debug("Found new device: " + hostname)
                device = Device(None, hostname, 1)
                newDevice = self.databaseController.addDevice(device)
                self.devices[hostname] = newDevice

            deviceId = self.devices[hostname].device_id

            pingResult = self.pingController.ping(hostname, self.pingCount, deviceId)
            if pingResult:
                successfulPingResults.append(pingResult)
            else:
                # New ping result with is_success=False
                pingResult = PingResult(device_id=deviceId, is_success=False)
                failedPingResults.append(pingResult)

            self.databaseController.addPingResult(pingResult)

        # Check if this device was previously down but is now up
        for pingResult in successfulPingResults:
            if pingResult.device_id in latestPingResults:
                prevIsSuccess = latestPingResults[pingResult.device_id].is_success
                datePinged = latestPingResults[pingResult.device_id].date_pinged
                now = datetime.datetime.now()
                minutesDownFor = math.ceil((now-datePinged).total_seconds()/60)
                if prevIsSuccess is not 1:
                    device = self.getDeviceById(pingResult.device_id)
                    self.logger.debug("Host '" + str(device.hostname) + "' was previously down for up to " + str(minutesDownFor) + " minutes but is now up.")
                    failsuccessPingResults.append(pingResult)

        # Check if this device was previously down and is still down
        # Copy failedPingResults into failedPingResultsCopy to avoid loop issues while deleting elements...
        # TODO: probably a better way of doing this
        failedPingResultsCopy = list(failedPingResults)
        for pingResult in failedPingResultsCopy:
            if pingResult.device_id in latestPingResults:
                prevIsSuccess = latestPingResults[pingResult.device_id].is_success
                if prevIsSuccess is not 1:
                    device = self.getDeviceById(pingResult.device_id)
                    self.logger.debug("Host '" + str(device.hostname) + "' was previously down and is still down.")
                    failedPingResults.remove(pingResult)

        # Send an email
        if failedPingResults or failsuccessPingResults:
            if not self.emailConf.enabled:
                self.logger.debug("Email is disabled. Skipping email...")
            else:
                self.logger.debug("Email is enabled: " + str(self.emailConf.enabled))
                subject = 'FoscamPing Email Controller'
                timeNow = time.strftime('%Y-%m-%d %H:%M:%S')
                text = ''

                for failedPingResult in failedPingResults:
                    device = self.getDeviceById(failedPingResult.device_id)
                    text += timeNow + ' - Failed to ping: ' + device.hostname + "\n"

                for pingResult in failsuccessPingResults:
                    device = self.getDeviceById(pingResult.device_id)
                    datePinged = latestPingResults[pingResult.device_id].date_pinged
                    now = datetime.datetime.now()
                    minutesDownFor = math.ceil((now-datePinged).total_seconds()/60)
                    text += timeNow + ' - Previously down for up to ' + str(minutesDownFor) + ' minutes but is now up: ' + device.hostname + "\n"

                self.emailController.sendEmail(self.emailConf.send_to, subject, text)

        return

    def getDeviceById(self, device_id):
        """
        Helper function to get a Device tuple by device_id
        :param device_id: device_id to search for.
        :return: Device tuple if found, else None.
        """
        for hostname in self.devices:
            # if device.device_id == device_id:
            if self.devices[hostname].device_id == device_id:
                return self.devices[hostname]
        self.logger.error("Device id not found" + str(device_id))
        return None
 def test_get_weather_codes(self):
     config = ConfigController()
     self.assertIsInstance(config.get_weather_codes(), list)
 def test_get_country_code(self):
     config = ConfigController()
     # check to see if returns type str
     self.assertIsInstance(config.get_country_code(), str)
     # check to see if returned value is 2 char long
     self.assertEqual(len(config.get_country_code()), 2)
 def test_get_open_weather_token(self):
     config = ConfigController()
     self.assertIsInstance(config.get_open_weather_token(), str)
 def test_get_program_name(self):
     config = ConfigController()
     self.assertIsInstance(config.get_program_name(), str)
Example #12
0
class Controller:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.sortController = SortController()

        return

    def __del__(self):
        return

    def main(self, argv):
        
        self.logger.debug("The total numbers of args passed to the script: %d " % len(argv))
        self.logger.debug("Args: " + str(argv))

        # TODO: flesh out argument parsing and help, -h
        # Parse command line arguments
        sDescription = "no description yet"

        sEpilog = "Returns exit code 0 on success, non-zero on error.\n\n" \
                  "Use logging.conf to change logging information."

        parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=sDescription,
                                         epilog=sEpilog)
        parser.add_argument('-d', '--dir', help='directory to sort')
        parser.add_argument('-f', '--file', help='file containing list of directories to sort')
        parser.add_argument('-c', '--conf', default='app.conf', help='application config file to use (default: %(default)s)')
        args = parser.parse_args()
        self.logger.debug("Parsed args: " + str(args))

        appConfFile = os.path.join(os.path.dirname(__file__), args.conf)
        if not os.path.isfile(appConfFile):
            self.logger.error("File not found: " + appConfFile)
            return

        # Get the configuration parameters
        self.configController = ConfigController()
        self.configController.loadConfiguration(appConfFile)

        # Setup email controller
        self.emailConf = self.configController.getEmailTuple()
        self.emailController = EmailController(self.emailConf.username, self.emailConf.password, self.emailConf.server, self.emailConf.port, self.emailConf.sender_name)

        self.sortController.setExePath(self.configController.get7zaPath())

        file_extensions = self.configController.getFileExtensions()
        self.sortController.fileExts = file_extensions

        directories = []
        if args.dir:
            directories.append(args.dir)

        # Also get the list of directories from a file
        if args.file:
            if not os.path.isfile(args.file):
                self.logger.error("File does not exist: " + args.file)
                return 1
            else:
                with open(args.file, 'r') as file:
                    for line in file:
                        line = line.strip()
                        if line:
                            directories.append(line)

        self.logic(directories)
        return 0

    # TODO: need a better function name than "logic"...
    # TODO:
    # Sorting is actually two functions: moving files and then archiving files.
    # Moving files can be done very quickly, but archiving files is very slow.
    # Split up the logic so we move all files in all directories in one go,
    # before we actually archive anything.
    def logic(self, directories):
        self.logger.debug("Directories: " + str(directories))
        for directory in directories:
            if not self.sortController.setRoot(directory):
                continue
            bSuccess, timeEpoch = self.sortController.checkLastSuccessfulRun(5)

            # TODO: get this out of the for loop
            # only send one email per script run,
            # simply bunch all possible errors into one email
            if not bSuccess:
                pass
                if not self.emailConf.enabled:
                    self.logger.debug("Email is disabled. Skipping email...")
                else:
                    self.logger.debug("Email is enabled: " + str(self.emailConf.enabled))
                    subject = 'FoscamSort Email Controller'
                    timeNow = time.strftime('%Y-%m-%d %H:%M:%S')
                    lastSuccessfulRun = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(timeEpoch)))
                    message = 'Warning: no files found in ' + directory + '\r\n' \
                              + 'Date last run: ' + lastSuccessfulRun
                    text = timeNow + ' ' + message
                    self.emailController.sendEmail(self.emailConf.send_to, subject, text)

            self.sortController.sortFilesIntoDirectories()
            self.sortController.archiveDirectories()
        return
Example #13
0
    def main(self, argv):

        #
        # 1. Argument parsing
        #
        self.logger.debug("The total numbers of args passed to the script: %d " % len(argv))
        self.logger.debug("Args: " + str(argv))

        # TODO: flesh out argument parsing and help, -h
        # Parse command line arguments
        sDescription = "no description yet"

        sEpilog = "Returns exit code 0 on success, non-zero on error.\n\n" \
                  "Use app.conf to change script configuration.\n" \
                  "Use logging.conf to change logging information."

        parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=sDescription,
                                         epilog=sEpilog)
        parser.add_argument('-host', help='hostname to ping')
        parser.add_argument('-f', '--file', help='file containing list of hosts to ping')
        parser.add_argument('-c', '--conf', default='app.conf', help='application config file to use (default: %(default)s)')
        args = parser.parse_args()
        self.logger.debug("Parsed args: " + str(args))

        appConfFile = os.path.join(os.path.dirname(__file__), args.conf)
        if not os.path.isfile(appConfFile):
            self.logger.error(appConfFile + " not found.")
            return

        hostnames = []
        if args.host:
            hostnames.append(args.host)

        # Also get the list of hosts from a file
        if args.file:
            if not os.path.isfile(args.file):
                self.logger.error("File not found: " + args.file)
                return
            else:
                with open(args.file, 'r') as file:
                    for line in file:
                        line = line.strip()
                        if line:
                            hostnames.append(line)

        #
        # 2. Controller setup
        #

        # Get the configuration parameters
        self.configController = ConfigController()
        self.configController.loadConfiguration(appConfFile)

        # Establish a database connection
        self.databaseController = DatabaseController(True)
        self.databaseController.connect(self.configController.getDbConnectString())
        self.devices = self.databaseController.getDevices()

        # Get a ping controller instance
        self.pingController = PingController()
        self.pingCount = self.configController.getPingCount()

        # Setup email controller
        self.emailConf = self.configController.getEmailTuple()
        self.emailController = EmailController(self.emailConf.username, self.emailConf.password, self.emailConf.server, self.emailConf.port, self.emailConf.sender_name)

        #
        # 3. Script main run
        #
        # Finally we use the list of hostnames
        self.logic(hostnames)

        self.databaseController.disconnect()

        return 0
 def test_get_sender_email(self):
     config = ConfigController()
     self.assertIsInstance(config.get_sender_email(), str)
 def test_get_smtp_port(self):
     config = ConfigController()
     self.assertIsInstance(config.get_smtp_port(), int)
 def test_get_smtp_server(self):
     config = ConfigController()
     self.assertIsInstance(config.get_smtp_server(), str)
 def test_get_longitude(self):
     config = ConfigController()
     self.assertIsInstance(config.get_longitude(), str)
 def test_get_location(self):
     config = ConfigController()
     self.assertIsInstance(config.get_location(), str)
 def test_get_recipient_email(self):
     config = ConfigController()
     self.assertIsInstance(config.get_recipient_email(), str)
Example #20
0
import os
from Weather import Weather
from TimeCompare import TimeCompare
import SunTimes
from SendMail import SendMail
from MiningState import MiningState
import psutil
from ConfigController import ConfigController
import sunmine_logger
import datetime

config = ConfigController()
send_mail = SendMail()

logger = sunmine_logger.get_logger()


def main():
    logger.info('Logging works!')

    weather = Weather()
    acceptable_weather_codes = config.get_weather_codes()

    logger.info('Started Sunmine application')

    current_state = MiningState.get_state()
    logger.info("Current state of the miner: " + current_state)  # weather

    # sunset/rise
    logger.info("Getting sunrise/sunset data from the internet...")
    sun_api_url = SunTimes.build_sun_api_url(config)