def on_add_devices_clicked(self, widget): fs_utils.mount_usb_key() dialog = Gtk.FileChooserDialog( title='Select Device Credentials zip file', parent=self, action=Gtk.FileChooserAction.OPEN) dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) #dialog.maximize() self.screen_width = self.get_screen().get_width() self.screen_height = self.get_screen().get_height() self.set_default_size(self.screen_width, self.screen_height) credentials_filter = Gtk.FileFilter() credentials_filter.set_name("Credential Zip files") credentials_filter.add_pattern("*.zip") dialog.add_filter(credentials_filter) position_box = gtk_utils.StringComboBox('Position', 1, MAXIMUM_NUMBER_OF_DEVICES) dialog.set_extra_widget(position_box.get_widget()) response = dialog.run() if response == Gtk.ResponseType.OK: filename = dialog.get_filename() position = position_box.get_widget().get_active_text() for key in [ key for key, value in self.devices_dict.items() if filename == value ]: del self.devices_dict[key] self.devices_dict[position] = filename gtk_utils.delete_buffer(self.devices_textbuffer) for key, value in sorted(self.devices_dict.items()): gtk_utils.write_to_buffer(self.devices_textbuffer, '%s: %s\n' % \ (key, os.path.basename(value))) self.set_buttons_status() dialog.destroy()
def on_run(self, progress_bar_window): progress_bar_window.set_text('Restarting AWS Greengrass...') gtk_utils.write_to_buffer(self.console_textbuffer, 'Restarting AWS Greengrass...') #aws_utils.restart_aws_greengrass() gtk_utils.write_to_buffer(self.console_textbuffer, 'Done.\nRunning the application...\n') self.pmp_process = self.execute_command_and_write_to_buffer( PMP_COMMAND, self.console_textbuffer)
def on_configure(self, progress_bar_window): fs_utils.mount_usb_key() progress_bar_window.set_text('Installing credentials...') gtk_utils.write_to_buffer(self.console_textbuffer, 'Installing credentials...\n') fs_utils.create_file_from_json( definitions.PMP_CONFIGURATION_PATH, definitions.DEFAULT_PMP_CONFIGURATION_JSON) aws_utils.configure_edge_gateway_aws(self.edge_gateway, self.console_textbuffer) aws_utils.configure_devices_aws(self.devices_dict, self.console_textbuffer) fs_utils.unmount_usb_key() progress_bar_window.set_text('Restarting AWS Greengrass...') gtk_utils.write_to_buffer( self.console_textbuffer, 'Credentials installed.\nRestarting AWS Greengrass...') aws_utils.restart_aws_greengrass() progress_bar_window.set_text('Waiting for deployment...') gtk_utils.write_to_buffer(self.console_textbuffer, 'Done.\nWaiting for deployment...') aws_utils.wait_for_aws_deployment() gtk_utils.write_to_buffer(self.console_textbuffer, 'Done.\n') self.edge_gateway = None self.devices_dict = {} gtk_utils.delete_buffer(self.edge_gateway_textbuffer) gtk_utils.delete_buffer(self.devices_textbuffer) self.set_buttons_status()
def configure_devices_aws(devices_dict, textbuffer=None): try: with open(definitions.PMP_CONFIGURATION_PATH, 'r') as fp: pmp_configuration_json = json.load(fp) device_certificates_path = \ pmp_configuration_json["setup"]["device_certificates_path"] command = \ 'rm -rf %s && ' \ 'mkdir -p %s 2>&1' % \ (device_certificates_path, device_certificates_path) output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('utf-8') if textbuffer: gtk_utils.write_to_buffer(textbuffer, output) except subprocess.CalledProcessError as e: pass root_ca_path = json.load(open( definitions.GREENGRASS_CONFIG_PATH))["coreThing"]["caPath"] for position in devices_dict: device_path = devices_dict[position] device_basename = os.path.basename(device_path) try: command = \ 'cd %s && ' \ 'cp %s . && ' \ 'unzip -o %s && ' \ 'rm -rf %s %s 2>&1' % \ (device_certificates_path, device_path, device_basename, device_basename, root_ca_path) output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('utf-8') if textbuffer: gtk_utils.write_to_buffer(textbuffer, output) except subprocess.CalledProcessError as e: pass with open(definitions.PMP_CONFIGURATION_PATH, 'r') as fp: pmp_configuration_json = json.load(fp) device_dict = {} device_dict["name"] = device_basename[:device_basename.find('.')] device_dict["position"] = int(position.split(' ')[1]) pmp_configuration_json["setup"]["devices"].append(device_dict) with open(definitions.PMP_CONFIGURATION_PATH, 'w') as fp: json.dump(pmp_configuration_json, fp)
def configure_edge_gateway_aws(edge_gateway_path, textbuffer=None): edge_gateway_basename = os.path.basename(edge_gateway_path) try: command = \ 'cd %s && ' \ 'rm -rf certs config && ' \ 'cp %s . && ' \ 'unzip -o %s && ' \ 'rm -rf %s 2>&1' % \ (pmp_definitions.GREENGRASS_PATH, edge_gateway_path, edge_gateway_basename, edge_gateway_basename) output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('utf-8') if textbuffer: gtk_utils.write_to_buffer(textbuffer, output) except subprocess.CalledProcessError as e: pass endpoint = json.load(open(pmp_definitions.GREENGRASS_CONFIG_PATH))["coreThing"]["iotHost"]
def on_add_edge_gateway_clicked(self, widget): fs_utils.mount_usb_key() dialog = Gtk.FileChooserDialog( title='Select Edge Credentials zip file', parent=self, action=Gtk.FileChooserAction.OPEN) dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) #dialog.maximize() self.screen_width = self.get_screen().get_width() self.screen_height = self.get_screen().get_height() self.set_default_size(self.screen_width, self.screen_height) credentials_filter = Gtk.FileFilter() credentials_filter.set_name("Credential Zip files") credentials_filter.add_pattern("*.zip") dialog.add_filter(credentials_filter) response = dialog.run() if response == Gtk.ResponseType.OK: self.edge_gateway = dialog.get_filename() gtk_utils.delete_buffer(self.edge_gateway_textbuffer) gtk_utils.write_to_buffer(self.edge_gateway_textbuffer, '%s\n' % \ (os.path.basename(self.edge_gateway))) self.set_buttons_status() dialog.destroy()
def on_connect(self, progress_bar_window): gtk_utils.delete_buffer(self.console_textbuffer) connection_ssid = self.connection_ssid_entry.get_text() connection_password = self.connection_password_entry.get_text() if not (connection_ssid and connection_password): self.connect_button.set_sensitive(True) self.update_button.set_sensitive(True) gtk_utils.write_to_buffer(self.console_textbuffer, 'Insert valid SSID and password.\n') return progress_bar_window.set_text('Connecting...') gtk_utils.write_to_buffer(self.console_textbuffer, 'Connecting to %s...' % (connection_ssid)) fs_utils.create_wifi_scripts(connection_ssid, connection_password) ip_address = None try: os.system(fs_utils.DISCONNECT_WIFI_PATH) os.system(fs_utils.CONNECT_WIFI_PATH) output = subprocess.check_output( 'ifconfig | grep \'wlan\' -A1 | grep inet', stderr=subprocess.STDOUT, shell=True).decode('utf-8') ip_address = output.split()[1].split(':')[1] except subprocess.CalledProcessError as e: pass if ip_address: gtk_utils.write_to_buffer( self.console_textbuffer, 'Connected with ip address %s.\n' \ 'From Bash use \"%s\" or \"%s\".\n' \ % (ip_address, fs_utils.CONNECT_WIFI_PATH, fs_utils.DISCONNECT_WIFI_PATH)) else: gtk_utils.write_to_buffer( self.console_textbuffer, 'Impossible to connect to %s' % (connection_ssid)) self.connect_button.set_sensitive(True) self.update_button.set_sensitive(True)
def __init__(self): super(SetupPMPWindow, self).__init__() self.set_title('PREDICTIVE MAINTENANCE PLATFORM SETUP') self.maximize() self.set_border_width(gtk_utils.DEFAULT_SPACE) self.set_position(Gtk.WindowPosition.CENTER) self.connect('destroy', Gtk.Widget.destroy) self.main_grid = Gtk.Grid() self.main_grid.set_row_spacing(gtk_utils.DEFAULT_SPACE) self.main_grid.set_column_spacing(gtk_utils.DEFAULT_SPACE) self.main_grid.set_row_homogeneous(False) self.main_grid.set_column_homogeneous(True) self.main_grid.set_vexpand(True) self.main_grid.set_hexpand(True) self.add(self.main_grid) self.information_frame = Gtk.Frame() self.information_frame.set_label('Instructions') self.information_grid = Gtk.Grid() self.information_grid.set_row_spacing(gtk_utils.DEFAULT_SPACE) self.information_grid.set_column_spacing(gtk_utils.DEFAULT_SPACE) self.information_grid.set_column_homogeneous(True) self.information_grid.set_row_homogeneous(False) self.information_grid.set_border_width(gtk_utils.DEFAULT_SPACE) self.information_frame.add(self.information_grid) self.information_textbuffer = Gtk.TextBuffer() self.information_textview = Gtk.TextView.new_with_buffer( self.information_textbuffer) self.information_textview.connect('button-press-event', gtk_utils.textview_clicked) self.information_textview.set_border_width(gtk_utils.DEFAULT_SPACE) self.information_textview.set_editable(False) self.information_grid.add(self.information_textview) gtk_utils.write_to_buffer(self.information_textbuffer, INFORMATION) self.information_frame.set_hexpand(True) self.main_grid.attach(self.information_frame, 0, 0, 2, 1) self.edge_gateway_frame = Gtk.Frame() self.edge_gateway_frame.set_label('Edge Gateway Credentials') self.edge_gateway_frame.set_vexpand(True) self.edge_gateway_grid = Gtk.Grid() self.edge_gateway_grid.set_border_width(gtk_utils.DEFAULT_SPACE) self.edge_gateway_grid.set_column_spacing(gtk_utils.DEFAULT_SPACE) self.edge_gateway_frame.add(self.edge_gateway_grid) self.edge_gateway = None self.edge_gateway_textbuffer = Gtk.TextBuffer() self.edge_gateway_textview = Gtk.TextView.new_with_buffer( self.edge_gateway_textbuffer) self.edge_gateway_textview.connect('button-press-event', gtk_utils.textview_clicked) self.edge_gateway_textview.set_border_width(gtk_utils.DEFAULT_SPACE) self.edge_gateway_textview.set_editable(False) self.edge_gateway_textview_scrolling = Gtk.ScrolledWindow() self.edge_gateway_textview_scrolling.add(self.edge_gateway_textview) self.edge_gateway_textview_scrolling.set_vexpand(True) self.edge_gateway_textview_scrolling.set_hexpand(True) self.edge_gateway_add_button = Gtk.Button.new_with_label('Add') self.edge_gateway_add_button.connect('clicked', self.on_add_edge_gateway_clicked) self.edge_gateway_grid.attach(self.edge_gateway_add_button, 0, 0, 1, 1) self.edge_gateway_grid.attach(self.edge_gateway_textview_scrolling, 1, 0, 1, 1) self.main_grid.attach(self.edge_gateway_frame, 0, 1, 1, 1) self.devices_frame = Gtk.Frame() self.devices_frame.set_label('Devices Credentials') self.devices_frame.set_vexpand(True) self.devices_grid = Gtk.Grid() self.devices_grid.set_border_width(gtk_utils.DEFAULT_SPACE) self.devices_grid.set_column_spacing(gtk_utils.DEFAULT_SPACE) self.devices_frame.add(self.devices_grid) self.devices_dict = {} self.devices_textbuffer = Gtk.TextBuffer() self.devices_textview = Gtk.TextView.new_with_buffer( self.devices_textbuffer) self.devices_textview.connect('button-press-event', gtk_utils.textview_clicked) self.devices_textview.set_border_width(gtk_utils.DEFAULT_SPACE) self.devices_textview.set_editable(False) self.devices_textview_scrolling = Gtk.ScrolledWindow() self.devices_textview_scrolling.add(self.devices_textview) self.devices_textview_scrolling.set_vexpand(True) self.devices_textview_scrolling.set_hexpand(True) self.device_add_button = Gtk.Button.new_with_label('Add') self.device_add_button.connect('clicked', self.on_add_devices_clicked) self.devices_grid.attach(self.device_add_button, 0, 0, 1, 1) self.devices_grid.attach(self.devices_textview_scrolling, 1, 0, 1, 1) self.main_grid.attach(self.devices_frame, 1, 1, 1, 1) self.console_frame = Gtk.Frame() self.console_frame.set_label('Console') self.console_frame.set_vexpand(True) self.console_frame.set_hexpand(True) self.console_grid = Gtk.Grid() self.console_grid.set_row_spacing(gtk_utils.DEFAULT_SPACE) self.console_grid.set_column_spacing(gtk_utils.DEFAULT_SPACE) self.console_grid.set_column_homogeneous(True) self.console_grid.set_row_homogeneous(False) self.console_grid.set_border_width(gtk_utils.DEFAULT_SPACE) self.console_frame.add(self.console_grid) self.console_textbuffer = Gtk.TextBuffer() self.console_textview = Gtk.TextView.new_with_buffer( self.console_textbuffer) self.console_textview.connect('button-press-event', gtk_utils.textview_clicked) self.console_textview.set_editable(False) self.console_textview_scrolling = Gtk.ScrolledWindow() self.console_textview_scrolling.add(self.console_textview) self.console_textview_scrolling.set_vexpand(True) self.console_grid.attach(self.console_textview_scrolling, 0, 0, 2, 1) self.console_textview.connect('size-allocate', gtk_utils.on_textview_change, self.console_textview_scrolling) self.main_grid.attach(self.console_frame, 0, 2, 2, 1) self.configure_button = Gtk.Button.new_with_label( 'Install Credentials') self.configure_button.connect('clicked', self.on_configure_clicked) self.show_button = Gtk.Button.new_with_label('Show Configuration') self.show_button.connect('clicked', self.on_show_clicked) self.close_button = Gtk.Button.new_with_label('Close') self.close_button.connect('clicked', self.on_close_clicked) self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=gtk_utils.DEFAULT_SPACE) self.hbox.set_homogeneous(True) self.hbox.set_hexpand(True) self.hbox.add(self.configure_button) self.hbox.add(self.show_button) self.hbox.add(self.close_button) self.main_grid.attach(self.hbox, 0, 3, 2, 1) # self.edge_gateway = '/media/usb/AG_Edge.zip' # self.devices_dict['Position 1'] = '/media/usb/AG_Device_1.zip' # self.devices_dict['Position 4'] = '/media/usb/AG_Device_2.zip' self.set_buttons_status()
def on_configure(self, progress_bar_window): # Adding user and group to the system. progress_bar_window.set_text('Configuring...') gtk_utils.write_to_buffer(self.console_textbuffer, 'Adding user and group to the system...\n') try: command = 'adduser --system ggc_user 2>&1' output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('utf-8') gtk_utils.write_to_buffer(self.console_textbuffer, output) except subprocess.CalledProcessError as e: pass try: command = 'addgroup --system ggc_group 2>&1' output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('utf-8') gtk_utils.write_to_buffer(self.console_textbuffer, output) except subprocess.CalledProcessError as e: pass # Enabling hardlink and softlink protection at operating system start-up. gtk_utils.write_to_buffer( self.console_textbuffer, 'Enabling hardlink and softlink protection at operating system ' \ 'start-up...\n') fd = open(definitions.GATEWAY_RULES_PATH, 'w') fd.write('fs.protected_hardlinks = 1\nfs.protected_symlinks = 1') fd.close() # Installing Python packages. gtk_utils.write_to_buffer(self.console_textbuffer, 'Installing Python packages...\n') command = '%s 2>&1' % (definitions.PYTHON_PIP_UPGRADE) gtk_utils.execute_command_and_write_to_buffer(command, self.console_textbuffer) if self.check_setup(None): command = 'pip3 install %s 2>&1' % ( definitions.PYTHON_PACKAGES_TO_INSTALL) gtk_utils.execute_command_and_write_to_buffer( command, self.console_textbuffer) #GLib.timeout_add(TIMEOUT_ms, self.check_setup, None) while self.check_setup(None): pass else: gtk_utils.write_to_buffer(self.console_textbuffer, 'Python packages installed.\n') self.connect_button.set_sensitive(self.connect_button_status) self.update_button.set_sensitive(True)