def changeLine(): # place the first line to the last line in config.txt in u-disk # Call this method after calling method doStaConfig and before calling method doApConfig text = [] with open(path.join(get_U_path(), 'config.txt')) as f: for i, each_line in zip(range(9), f): if i == 0: first_line = each_line else: text.append(each_line) text.append(first_line) with open(path.join(get_U_path(), 'config.txt'), 'w') as f: f.writelines(text)
def setMark_inUSB(): # get text of configured.txt in U-disk and get number of round try: # judge if the file "configured.txt" already exists in U-disk with open(path.join(get_U_path(), 'configured.txt'), 'r') as f: # file exist firstLine = f.readline() # type: str endIndex = firstLine.index('.') roundOfUSB = int(firstLine[:endIndex]) except FileNotFoundError: # File is not found. with open(path.join(get_U_path(), 'configured.txt'), 'w+') as s: s.write('0.round configuration') roundOfUSB = 0 # increase the number of round with open(path.join(get_U_path(), 'configured.txt'), 'w+') as f: firstLine = str(roundOfUSB + 1) + '.round configuration' f.write(firstLine)
def setMark_inSlave(): # change the first line "At x.round configured" to "At x+1.round configured" in configuredOfPi.txt in slave_RPi with open(path.join(get_U_path(), 'configured.txt'), 'r') as s: # get text of configured.txt in U-disk and get number of round firstLine = s.readline() # type: str endIndex = firstLine.index('.') roundOfUSB = firstLine[:endIndex] local_path = path.split(path.realpath(__file__))[0] with open(path.join(local_path, 'configuredOfPi.txt'), 'w+') as f: # change the first line "At x.round configured" to "At x+1.round configured" in configuredOfPi.txt firstLine = 'At ' + roundOfUSB + '.round configured' f.write(firstLine)
def isConfigured(): # Used to determine if the network of slave_RPi has been configured. # The configuredOfPi.txt file in the local path stores how many times the Raspberry Pi has been configured, # and the configured.txt file in the U disk stores the information about which round Raspberry Pi needs to be configured. # The difference between the two information will trigger the new network configuration of slave_RPi, # and after being configured, the configuration information in the configuredOfPi.txt file of the Raspberry Pi # will be synchronized with the configuration information in the configured.txt file of the U disk. local_path = path.split(path.realpath(__file__))[0] try: with open(path.join(local_path, 'configuredOfPi.txt'), 'r') as f: # get text of configuredOfPi.txt in Pi and get number of round firstLine = f.readline() # type: str startIndex = search('At ', firstLine).end() endIndex = firstLine.index('.') roundOfPi = int(firstLine[startIndex:endIndex]) except FileNotFoundError: # Raspberry Pi has not been configured yet. with open(path.join(local_path, 'configuredOfPi.txt'), 'w+') as s: s.write('At 0.round configured') roundOfPi = 0 try: with open(path.join(get_U_path(), 'configured.txt'), 'r') as s: # get text of configured.txt in U-disk and get number of round firstLine = s.readline() # type: str endIndex = firstLine.index('.') roundOfUSB = int(firstLine[:endIndex]) except FileNotFoundError: # new U-disk without any configuration-file return True # nothing happen and run main program of slave except PermissionError: system('sudo rmdir ' + get_U_path()) system('sudo reboot') # Determine whether the configuration information of the Raspberry Pi is synchronized with the configuration information in the u disk. if roundOfPi == roundOfUSB: return True else: return False
def checkUSBatBegin(self): # Check if usb is connected. When the USB flash drive is not connected, a warning message box will pop up. # It is called at the beginning to prompt the user to insert a USB flash drive. if get_U_path() == None: # No U disk is connected. # The warning message box has an "Ok" button by default, which is selected by default. self.msgBox.setText( 'Opening GUI for the network configration......\nU-Disk not found, please insert a readable U-Disk first.\nYou can also cancel the network configuration by clicking the cancel button or closing this message box.' ) self.msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) self.msgBox.setDefaultButton(QMessageBox.Ok) self.msgBox.setIcon(QMessageBox.Warning) self.msgBox.setWindowTitle('Warning') clicked = self.msgBox.exec_() # The message box will close automatically when the "Cancel" button is pressed. # The graphical interface for network configuration no longer appears. A graphical interface for presenting data will appear. if clicked == QMessageBox.Cancel: self.do_config = False else: # The message box will also close automatically when the "Ok" button is pressed. # After the message box is closed, it will detect whether the USB disk is inserted again. # If not, the warning message box still pops up. if get_U_path() == None: self.checkUSBatBegin()
def usbPulledOut(self): # Function that will be triggered when a USB disk is pulled out self.msgBox.setText( 'U-Disk was pulled out, please insert your U-Disk.') self.msgBox.setStandardButtons(QMessageBox.Ok) self.msgBox.setDefaultButton(QMessageBox.Ok) self.msgBox.setIcon(QMessageBox.Warning) self.msgBox.setWindowTitle('Warning') # Pop up message dialog, return button being pressed. This is a blocking function. clicked = self.msgBox.exec_() if clicked == QMessageBox.Ok: # The message dialog only has the ok button. After being pressed, it is judged whether the USB disk is inserted. # If the u disk is not inserted, the message dialog box will pop up. if get_U_path() == None: self.usbPulledOut()
def saveConfig(self, sender): # get the path of U-disk. For example: /media/pi/KINSTON usbPath = get_U_path() # Save the local config.txt file to the U disk configFilePath = path.join( usbPath, 'config.txt' ) # the path plus filename. For example: /media/pi/KINSTON/config.txt assert isinstance(configFilePath, object) if sender == self.configuration_window.Ok: # The sender of the signal is the ok button, i.e. the ok button is pressed. # Copy the local config.txt file into the USB flash drive copyfile(path.join(self.local_path, 'config.txt'), configFilePath) else: # sender == self.configuration_window.Cancel # The sender of the signal is the cancel button, i.e. the cancel button is pressed. # Save the local standardConfig.txt file to the USB flash drive. copyfile(path.join(self.local_path, 'standardConfig.txt'), configFilePath)
def getSSIDandPASS(): # patch infos from the first line of config.txt in u-disk with open(path.join(get_U_path(), 'config.txt')) as f: for each_line in f: firstLine = each_line break endIndex = search(':', firstLine).start() RPi = firstLine[:endIndex] startIndex = search('SSID:', firstLine).end() endIndex = search('Password', firstLine).start() SSID = firstLine[startIndex:endIndex].rstrip() startIndex = search('Password:', firstLine).end() Password = firstLine[startIndex:].rstrip() return [RPi, SSID, Password]
# If the NO button is pressed, a graphic window pops up showing the pressure sensor and temperature sensor data from slave_RPi. else: try: displayMain(app, local_path) except Exception as error: recurseDisplayMain(error) else: # detectDisplay() == 'No Display' # execute the program for slave RPi # check if this slave RPi was already configured. # If not, change the internal configuration file to make the slave RPi connect with the access point of the specific RPi # and create a access point of itself. # !!!important: if man want to do configuration, he must put the U-disk into RPi before he boot the RPi. sleep(3) # wait for mounting the u-disk if get_U_path( ) != None: # judge if a USB-Stick for network configuration is connected print(111) # judge if the Raspberry Pi network has been configured if isConfigured( ) == False: # If the Raspberry Pi network has not been configured yet configAndMark4slaveRPi() # After the configuration is complete, # the system automatically restarts to make the new network configuration take effect. system('sudo reboot') # If no U disk is connected or RPi is already configured, # execute the program for getting sensor data from itself and the slave_RPi connected with its access point # and sending all collected sensor data to the RPi it connect with. try: # Prevent the Raspberry Pi from causing program errors because the network has never been configured with open(path.join(local_path, 'wpa_supplicant-wlan0.conf'),