示例#1
0
    def associate_tag(self, instance):
        for entry in self._entry_list:
            self._box_layout.remove_widget(entry)

        self._box_layout.remove_widget(self._tag_association_button)

        for text_box_label in self._text_box_labels:
            self._box_layout.remove_widget(text_box_label)

        if self._part_info.uid is None:
            self._association_label_image = AsyncImage(source=os.getcwd() +
                                                       "/tap.png")
            self._association_label = Label(
                text="Tap an NFC tag now to associate")

            self._box_layout.add_widget(self._association_label_image)
            self._box_layout.add_widget(self._association_label)

            _thread.start_new_thread(self._get_next_nfc_tag_uid, ())
        else:
            try:
                uid_sheet_info_modifier = UidSheetInfoModifier()
            except:
                # No connection... Use CSV Backup
                uid_sheet_info_modifier = UidCsvInfoModifier()

            self._update_part_info()

            uid_sheet_info_modifier.add_part(self._part_info)

            self.go_back()
示例#2
0
    def __init__(self, download_lock: Lock = None):
        credentials = ServiceAccountCredentials. \
            from_json_keyfile_name(GOOGLE_AUTH_JSON_PATH, SCOPES)

        self._sheets_service = gspread.authorize(credentials)

        self._current_sheet = self._sheets_service.open_by_key(GOOGLE_SPREADSHEET_KEY).sheet1
        self._download_lock = download_lock

        Path(str(Path.home()) + "/Barnyard-2/").mkdir(exist_ok=True)

        try:
            with open(str(Path.home()) + "/Barnyard-2/offlinesheet.csv",
                      "r") as csv_file:
                for row in csv.reader(csv_file):
                    if row[0] == "Time Last Updated":
                        if len(row) > 2 and row[2] == "needs_sync":
                            print("syncing sheet")
                            self.sync_offline_sheet(True)

                            UidCsvInfoModifier()._update_edit_time(False)

        except OSError:
            pass  # Fine if csv file isn't found yet

        _thread.start_new_thread(self.make_data_offline, ())
        _thread.start_new_thread(self.make_images_offline, ())
示例#3
0
    def delete_tag(self, instance):
        try:
            uid_sheet_info_modifier = UidSheetInfoModifier()
        except:
            # No connection... Use CSV Backup
            uid_sheet_info_modifier = UidCsvInfoModifier()

        uid_sheet_info_modifier.delete_part(self._part_info)

        self.go_back()
示例#4
0
    def modify_tag(self, instance):
        try:
            uid_sheet_info_modifier = UidSheetInfoModifier()
        except:
            # No connection... Use CSV Backup
            uid_sheet_info_modifier = UidCsvInfoModifier()

        self._update_part_info()

        uid_sheet_info_modifier.edit_part(self._part_info)

        self.go_back(False)
示例#5
0
    def _attempt_sheets_connection(self):
        if self._internet_status_label is None:
            self._internet_status_label = Label()

        try:
            self._uid_sheet_info_modifier = UidSheetInfoModifier(
                self._download_lock)

            self._internet_status_label.color = [0, 1, 0, 1]
            self._internet_status_label.text = "Connected to Google Sheets"

            self.connected = True
        except:
            print("No connection")
            self._uid_sheet_info_modifier = UidCsvInfoModifier()

            self._internet_status_label.text = "Not connected to Google " +\
                                               "Sheets. Using backed up CSV " +\
                                               "file last modified at " +\
                                               self._uid_sheet_info_modifier.\
                                               get_last_update()
            self._internet_status_label.color = [1, 0, 0, 1]

            self.connected = False
示例#6
0
    def __init__(self, main_screen):
        super().__init__(name="Tag Searching Screen")

        try:
            self._uid_sheet_info_modifier = UidSheetInfoModifier()
        except:
            self._uid_sheet_info_modifier = UidCsvInfoModifier()

        self._main_screen = main_screen

        self._box_layout = BoxLayout(orientation='vertical', spacing=10)
        self._scroll_view = ScrollView(size_hint=(1, None),
                                       size=(Window.width, Window.height))

        self._back_button = Button(text="Back", on_press=self.go_back)
        self._keyword_field = TextInput(multiline=False)
        self._search_name_button = ToggleButton(text="by Name",
                                                group="search_type",
                                                state='down',
                                                on_press=self.on_search_query)
        self._search_description_button = ToggleButton(
            text="by Description",
            group="search_type",
            on_press=self.on_search_query)
        self._search_label = Label(text="Enter search keyword")
        self._search_location_button = ToggleButton(
            text="by Location",
            group="search_type",
            on_press=self.on_search_query)

        self._keyword_field.bind(focus=self._adapt_keyboard,
                                 text=self.on_search_query)

        self.add_widget(self._scroll_view)

        self._scroll_view.add_widget(self._box_layout)

        self._box_layout.add_widget(self._back_button)
        self._box_layout.add_widget(self._search_label)
        self._box_layout.add_widget(self._keyword_field)

        self._box_layout.add_widget(self._search_name_button)
        self._box_layout.add_widget(self._search_description_button)
        self._box_layout.add_widget(self._search_location_button)
示例#7
0
    def _get_next_nfc_tag_uid(self):
        while True:
            next_uid = self._main_screen.tag_uid_extractor.\
                get_uid_from_next_tag()

            try:
                uid_sheet_info_modifier = UidSheetInfoModifier()
            except:
                # No connection... Use CSV Backup
                uid_sheet_info_modifier = UidCsvInfoModifier()

            if next_uid is not None:
                self._part_info.uid = next_uid

                self._update_part_info()

                uid_sheet_info_modifier.add_part(self._part_info)

                self.go_back()

                break
示例#8
0
class MainScreen(Screen):
    connected = True
    screen_active = True
    tag_uid_extractor = None

    _box_layout = None
    _download_lock = None
    _instruction_label = None
    _internet_status_label = None
    _new_tag_button = None
    _past_register_bind = None
    _register_tag_button = None
    _search_tag_button = None
    _signal_sender = None
    _uid_sheet_info_modifier = None
    _updater_button = None

    _part_info_labels = []
    _part_uid = None

    _window = None

    @staticmethod
    def generate_image(image_url, part_uid, connected, lock=None):
        if lock is not None:
            lock.acquire()

        if image_url == "locallystored":
            if os.path.isfile(
                    str(Path.home()) + "/Barnyard-2/" + part_uid + ".jpg"):
                path = str(Path.home()) + "/Barnyard-2/" + part_uid + ".jpg"

                return_value = AsyncImage(source=path)
            else:
                path = str(Path.home()) + "/Barnyard-2/" + part_uid + ".png"

                return_value = AsyncImage(source=path)
        elif connected:
            return_value = AsyncImage(source=image_url)
        else:
            # Offline mode: get downloaded image on hdd
            path = str(Path.home()
                       ) + "/Barnyard-2/" + "downloaded_" + part_uid + ".png"

            print(path)

            return_value = AsyncImage(source=path)

        if lock is not None:
            lock.release()

        return return_value

    def _attempt_sheets_connection(self):
        if self._internet_status_label is None:
            self._internet_status_label = Label()

        try:
            self._uid_sheet_info_modifier = UidSheetInfoModifier(
                self._download_lock)

            self._internet_status_label.color = [0, 1, 0, 1]
            self._internet_status_label.text = "Connected to Google Sheets"

            self.connected = True
        except:
            print("No connection")
            self._uid_sheet_info_modifier = UidCsvInfoModifier()

            self._internet_status_label.text = "Not connected to Google " +\
                                               "Sheets. Using backed up CSV " +\
                                               "file last modified at " +\
                                               self._uid_sheet_info_modifier.\
                                               get_last_update()
            self._internet_status_label.color = [1, 0, 0, 1]

            self.connected = False

    def _init_screen_elements(self):
        self._instruction_label = Label(text="Scan NFC Part Tag")
        self._new_tag_button = Button(text="Scan New Tag")
        self._register_tag_button = Button(text="Register Tag")
        self._search_tag_button = Button(text="Search Registered Tags")

        self._new_tag_button.bind(on_press=self.scan_tag)
        self._search_tag_button.bind(on_press=self.search_tags)

        self._box_layout.add_widget(self._instruction_label)
        self._box_layout.add_widget(self._internet_status_label)

    def __init__(self, signal_sender):
        super().__init__(name="Main Screen")

        self._download_lock = Lock()
        self._signal_sender = signal_sender

        self._attempt_sheets_connection()

        self._box_layout = BoxLayout(orientation="vertical")

        print(os.getcwd() + "/libNFCWrapper.so")
        self.tag_uid_extractor = TagUidExtractor(os.getcwd() +
                                                 "/libNFCWrapper.so")

        self.add_widget(self._box_layout)

        if self.tag_uid_extractor.init_device():
            self._init_screen_elements()
        else:
            self.error_label = Label(text="FATAL ERROR: Couldn't initialize " +
                                     "NFC reader/writer!")
            self._box_layout.add_widget(self.error_label)

        update = UpdaterScreen.get_update()

        if update is not None:
            self._updater_button = Button(text="Update Barnyard",
                                          on_press=partial(
                                              self.update_barnyard, update))

            self._box_layout.add_widget(self._updater_button)

    def switch_screen(self, screen_name, data=None):
        self._signal_sender.switch_screen(screen_name, data)

    def _scanning_thread(self):
        print("scanning")
        while self.screen_active:
            print("scanningnextuid")
            next_uid = self.tag_uid_extractor.get_uid_from_next_tag()
            print("UID GOT! " + next_uid)

            if next_uid is not None:
                self._part_uid = next_uid

                self.refresh_part_data()

                break

    def scan_tag(self, instance=None):
        if self._instruction_label is None:
            return

        self._instruction_label.text = "Scan NFC Part Tag"

        for label in self._part_info_labels:
            self._box_layout.remove_widget(label)

        self._part_info_labels.clear()

        self._box_layout.remove_widget(self._register_tag_button)
        self._box_layout.remove_widget(self._new_tag_button)
        self._box_layout.add_widget(self._search_tag_button)

        _thread.start_new_thread(self._scanning_thread, ())

    def search_tags(self, instance=None):
        self.switch_screen("search_tag")

    def refresh_part_data(self):
        self._box_layout.remove_widget(self._search_tag_button)

        self._box_layout.remove_widget(self._new_tag_button)
        self._box_layout.remove_widget(self._register_tag_button)

        self._attempt_sheets_connection()

        part_info = self._uid_sheet_info_modifier.get_part_info(self._part_uid)

        if part_info is None:
            if self._part_uid is None:
                self.scan_tag()
            else:
                self._instruction_label.text = "No Part Found!"
                self._register_tag_button.text = "Add Part"

                part_info = PartInfo(self._part_uid)

                for label in self._part_info_labels:
                    self._box_layout.remove_widget(label)

                self._part_info_labels.clear()

                self._part_info_labels.append(
                    Label(text="Tag UID:" + self._part_uid))

                self._box_layout.add_widget(self._part_info_labels[0])
        else:
            self._instruction_label.text = "Part Found!"
            self._register_tag_button.text = "Modify/Delete Part"

            if len(self._part_info_labels) > 1:
                self._box_layout.remove_widget(self._part_info_labels[-1])

                self._part_info_labels[1].text = "Name:" + part_info.name
                self._part_info_labels[2].text = "Description:" + \
                                                  part_info.description
                self._part_info_labels[
                    3].text = "Location:" + part_info.location

                self._part_info_labels[4] = self.generate_image(
                    part_info.image_url, self._part_uid, self.connected,
                    self._download_lock)

                self._box_layout.add_widget(self._part_info_labels[-1])
            else:
                for label in self._part_info_labels:
                    self._box_layout.remove_widget(label)

                self._part_info_labels.clear()

                self._part_info_labels.append(
                    Label(text="Tag UID:" + self._part_uid))
                self._part_info_labels.append(
                    Label(text="Name:" + part_info.name))
                self._part_info_labels.append(
                    Label(text="Description:" + part_info.description))
                self._part_info_labels.append(
                    Label(text="Location:" + part_info.location))
                self._part_info_labels.append(
                    self.generate_image(part_info.image_url, self._part_uid,
                                        self.connected, self._download_lock))

                for label in self._part_info_labels:
                    self._box_layout.add_widget(label)

            self._part_info_labels[-1].reload()

        if self._past_register_bind is not None:
            self._register_tag_button.unbind(on_press=self._past_register_bind)

        self._past_register_bind = partial(self.register_tag, part_info)

        self._register_tag_button.bind(on_press=self._past_register_bind)

        self._box_layout.add_widget(self._register_tag_button)
        self._box_layout.add_widget(self._new_tag_button)

    def register_tag(self, part_info, instance):
        print(part_info.name)
        self.switch_screen("register_tag", part_info)

    def update_barnyard(self, update_dict: dict, instance):
        print("Update: " + str(update_dict))

        self.switch_screen("updater", update_dict)