def store_settings():
    # Stores the setting info from the GUI into
    # the Mini-Monitor settings file.  Returns the value 0
    # if Success, -1 if an error.

    try:

        # gather the data from the interface
        logger_id = site_entry.get().replace(' ', '')
        meter_ids = meter_ids_entry.get()
        internet_type = inet_type.get()
        w_ssid = wifi_ssid_entry.get()
        w_pass = wifi_pass_entry.get()

        # Do a few input checks
        if len(logger_id.strip()) == 0:
            tkinter.messagebox.showerror('No Site Name',
                                         'You must enter a Site Name.')
            return -1

        if internet_type == 'wifi':
            if len(w_ssid.strip()) == 0:
                tkinter.messagebox.showerror(
                    'No WiFi Name', 'You must enter a WiFi Network Name.')
                return -1

        substitutions = [(r'LOGGER_ID\s*=', "LOGGER_ID = '%s'" % logger_id)]

        substitutions += [
            (r'ENABLE_METER_READER\s*=', 'ENABLE_METER_READER = True'),
            (r'METER_IDS\s*=', 'METER_IDS = [%s]' % meter_ids),
        ]

        # read settings file and perform the substitutions.
        # First need to identify the directory where this program
        # is located.  The method used to find it depends on whether
        # this is running as a script or as a pyinstaller frozen
        # executable.  See https://pythonhosted.org/PyInstaller/runtime-information.html.
        if getattr(sys, 'frozen', False):
            THIS_DIR = os.path.dirname(sys.executable)
        else:
            THIS_DIR = os.path.dirname(os.path.abspath(__file__))

        settings_fn = os.path.join(THIS_DIR, 'pi_logger/settings.py')
        with open(settings_fn) as f:
            file_contents = f.read()
        file_contents = setup_utils.replace_line(file_contents, substitutions)

        # Write the revised settings file.
        with io.open(settings_fn, 'w', newline='\n') as f:
            f.write(str(file_contents))

        # Make the proper wpa_supplicant.conf file.
        if internet_type == 'wifi':
            file_contents = setup_utils.wpa_sup_file(ssid=w_ssid, psk=w_pass)
        else:
            file_contents = setup_utils.wpa_sup_file()
        sup_fn = os.path.join(THIS_DIR, 'wpa_supplicant.conf')
        with io.open(sup_fn, 'w', newline='\n') as f:
            f.write(str(file_contents))

        msg = 'The Settings were Successfully Stored!  You can now close the application or modify settings and Store again.'
        tkinter.messagebox.showinfo('Success', msg)
        return 0

    except Exception as e:
        tkinter.messagebox.showerror(
            'Error Occurred',
            'An Error occurred while attempting to store settings:\n%s' %
            str(e))
        return -1
Beispiel #2
0
def store_settings():
    # Stores the setting info from the GUI into
    # the Mini-Monitor settings file.  Returns the value 0
    # if Success, -1 if an error.

    try:

        # gather the data from the interface
        logger_id = site_entry.get().replace(' ', '')
        use_gas = True if enable_gas.get()=='1' else False
        meter_ids = meter_ids_entry.get()
        internet_type = inet_type.get()
        w_ssid = wifi_ssid_entry.get()
        w_pass = wifi_pass_entry.get()
        modem_type = modem_values[cell_modem_combo.current()]
        # other_settings_fn is global and already set

        # Do a few input checks
        if len(logger_id.strip())==0:
            tkMessageBox.showerror('No Site Name', 'You must enter a Site Name.')
            return -1

        if internet_type=='wifi':
            if len(w_ssid.strip())==0:
                tkMessageBox.showerror('No WiFi Name', 'You must enter a WiFi Network Name.')
                return -1

        substitutions = [
            (r'LOGGER_ID\s*=', "LOGGER_ID = '%s'" % logger_id)
        ]

        if use_gas:
            substitutions += [
                (r'ENABLE_METER_READER\s*=', 'ENABLE_METER_READER = True'),
                (r'METER_IDS\s*=', 'METER_IDS = [%s]' % meter_ids),
            ]
        else:
            substitutions += [
                (r'ENABLE_METER_READER\s*=', 'ENABLE_METER_READER = False'),
            ]

        substitutions += [
            (r'USE_CELL_MODEM\s*=', 'USE_CELL_MODEM = %s' % ('True' if internet_type=='cellular' else 'False')),
            (r'CELL_MODEM_MODEL\s*=', "CELL_MODEM_MODEL = '%s'" % modem_type),
        ]

        # loop through the file containing other settings and
        # add to the substitution list
        if other_settings_fn:
            for line in open(other_settings_fn):
                line = line.strip()   # get rid of the line ending characters
                if re.search(r'^\s*#', line) is None:   # ignore comment lines
                    flds = line.split('\t')
                    if len(flds) == 2:
                        substitutions.append(flds)

        # read settings file and perform the substitutions.
        # First need to identify the directory where this program
        # is located.  The method used to find it depends on whether
        # this is running as a script or as a pyinstaller frozen
        # executable.  See https://pythonhosted.org/PyInstaller/runtime-information.html.
        if getattr(sys, 'frozen', False):
            THIS_DIR = os.path.dirname(sys.executable)
        else:
            THIS_DIR = os.path.dirname(os.path.abspath(__file__))

        settings_fn = os.path.join(THIS_DIR, 'pi_logger/settings.py')
        with open(settings_fn) as f:
            file_contents = f.read()
        file_contents = setup_utils.replace_line(file_contents, substitutions)

        # Write the revised settings file.
        with io.open(settings_fn, 'w', newline='\n') as f:
            f.write(unicode(file_contents))

        # Make the proper wpa_supplicant.conf file.
        if internet_type=='wifi':
            file_contents = setup_utils.wpa_sup_file(ssid=w_ssid, psk=w_pass)
        else:
            file_contents = setup_utils.wpa_sup_file()
        sup_fn = os.path.join(THIS_DIR, 'pi_logger/wpa_supplicant.conf')
        with io.open(sup_fn, 'w', newline='\n') as f:
            f.write(unicode(file_contents))

        msg = 'The Settings were Successfully Stored!  You can now close the application or modify settings and Store again.'
        tkMessageBox.showinfo('Success', msg)
        return 0

    except Exception as e:
        tkMessageBox.showerror('Error Occurred', 'An Error occurred while attempting to store settings:\n%s' % str(e))
        return -1
def store_settings():
    # Stores the setting info from the GUI into
    # the Mini-Monitor settings file.  Returns the value 0
    # if Success, -1 if an error.

    try:

        # gather the data from the interface
        logger_id = site_entry.get().replace(' ', '')
        internet_type = inet_type.get()
        w_ssid = wifi_ssid_entry.get()
        w_pass = wifi_pass_entry.get()
        modem_type = modem_values[cell_modem_combo.current()]
        # other_settings_fn is global and already set

        # Do a few input checks
        if len(logger_id.strip())==0:
            tkMessageBox.showerror('No Site Name', 'You must enter a Site Name.')
            return -1

        if internet_type=='wifi':
            if len(w_ssid.strip())==0:
                tkMessageBox.showerror('No WiFi Name', 'You must enter a WiFi Network Name.')
                return -1

        substitutions = [
            (r'LOGGER_ID\s*=', "LOGGER_ID = '%s'" % logger_id),
            (r'USE_CELL_MODEM\s*=', 'USE_CELL_MODEM = %s' % ('True' if internet_type=='cellular' else 'False')),
            (r'CELL_MODEM_MODEL\s*=', "CELL_MODEM_MODEL = '%s'" % modem_type),
            (r'outage_monitor.OutageMonitor', "'outage_monitor.OutageMonitor',   # Detects Power Outages through state of GPIO pin"),
            (r'sys_info.SysInfo', "'sys_info.SysInfo',               # System uptime, CPU temperature, software version"),
        ]

        # loop through the file containing other settings and
        # add to the substitution list
        if other_settings_fn:
            for line in open(other_settings_fn):
                line = line.strip()   # get rid of the line ending characters
                if re.search(r'^\s*#', line) is None:   # ignore comment lines
                    flds = line.split('\t')
                    if len(flds) == 2:
                        substitutions.append(flds)

        # read settings file and perform the substitutions.
        # First need to identify the directory where this program
        # is located.  The method used to find it depends on whether
        # this is running as a script or as a pyinstaller frozen
        # executable.  See https://pythonhosted.org/PyInstaller/runtime-information.html.
        if getattr(sys, 'frozen', False):
            THIS_DIR = os.path.dirname(sys.executable)
        else:
            THIS_DIR = os.path.dirname(os.path.abspath(__file__))

        settings_fn = os.path.join(THIS_DIR, 'pi_logger/settings.py')
        with open(settings_fn) as f:
            file_contents = f.read()
        file_contents = setup_utils.replace_line(file_contents, substitutions)

        # Write the revised settings file.
        with io.open(settings_fn, 'w', newline='\n') as f:
            f.write(unicode(file_contents))

        # Make the proper wpa_supplicant.conf file.
        if internet_type=='wifi':
            file_contents = setup_utils.wpa_sup_file(ssid=w_ssid, psk=w_pass)
        else:
            file_contents = setup_utils.wpa_sup_file()
        sup_fn = os.path.join(THIS_DIR, 'pi_logger/wpa_supplicant.conf')
        with io.open(sup_fn, 'w', newline='\n') as f:
            f.write(unicode(file_contents))

        msg = 'The Settings were Successfully Stored!  You can now close the application or modify settings and Store again.'
        tkMessageBox.showinfo('Success', msg)
        return 0

    except Exception as e:
        tkMessageBox.showerror('Error Occurred', 'An Error occurred while attempting to store settings:\n%s' % str(e))
        return -1
def store_settings():
    # Stores the setting info from the GUI into
    # the Mini-Monitor settings file.  Returns the value 0
    # if Success, -1 if an error.

    try:

        # gather the data from the interface
        logger_id = site_entry.get().replace(' ', '')
        meter_ids = meter_ids_entry.get()
        internet_type = inet_type.get()
        w_ssid = wifi_ssid_entry.get()
        w_pass = wifi_pass_entry.get()

        # Do a few input checks
        if len(logger_id.strip())==0:
            tkMessageBox.showerror('No Site Name', 'You must enter a Site Name.')
            return -1

        if internet_type=='wifi':
            if len(w_ssid.strip())==0:
                tkMessageBox.showerror('No WiFi Name', 'You must enter a WiFi Network Name.')
                return -1

        substitutions = [
            (r'LOGGER_ID\s*=', "LOGGER_ID = '%s'" % logger_id)
        ]

        substitutions += [
            (r'ENABLE_METER_READER\s*=', 'ENABLE_METER_READER = True'),
            (r'METER_IDS\s*=', 'METER_IDS = [%s]' % meter_ids),
        ]

        # read settings file and perform the substitutions.
        # First need to identify the directory where this program
        # is located.  The method used to find it depends on whether
        # this is running as a script or as a pyinstaller frozen
        # executable.  See https://pythonhosted.org/PyInstaller/runtime-information.html.
        if getattr(sys, 'frozen', False):
            THIS_DIR = os.path.dirname(sys.executable)
        else:
            THIS_DIR = os.path.dirname(os.path.abspath(__file__))

        settings_fn = os.path.join(THIS_DIR, 'pi_logger/settings.py')
        with open(settings_fn) as f:
            file_contents = f.read()
        file_contents = setup_utils.replace_line(file_contents, substitutions)

        # Write the revised settings file.
        with io.open(settings_fn, 'w', newline='\n') as f:
            f.write(unicode(file_contents))

        # Make the proper wpa_supplicant.conf file.
        if internet_type=='wifi':
            file_contents = setup_utils.wpa_sup_file(ssid=w_ssid, psk=w_pass)
        else:
            file_contents = setup_utils.wpa_sup_file()
        sup_fn = os.path.join(THIS_DIR, 'pi_logger/wpa_supplicant.conf')
        with io.open(sup_fn, 'w', newline='\n') as f:
            f.write(unicode(file_contents))

        msg = 'The Settings were Successfully Stored!  You can now close the application or modify settings and Store again.'
        tkMessageBox.showinfo('Success', msg)
        return 0

    except Exception as e:
        tkMessageBox.showerror('Error Occurred', 'An Error occurred while attempting to store settings:\n%s' % str(e))
        return -1