def reset(self): # remove db collection self.client.drop_database('esp8266_workshop') # create a new db db_handle = self.client['esp8266_workshop'] # create rooms collection and init values config = ConfigManager() groups_collection = db_handle['groups'] group_list = [] for group in range(1, self._number_of_groups + 1): new_group = {key: 0 for key in self._group_keys} new_group['number'] = group new_group['current_task'] = 1 new_group['name'] = '' new_group['dbg_msg'] = '' new_group['degrees'] = config.get_group_degrees(group) new_group['mac'] = config.get_group_mac_address(group) group_list.append(new_group) groups_collection.insert_many(group_list) # create tasks collection and init values tasks_collection = db_handle['tasks'] for task in self._tasks: new_task = { 'name': task['name'], 'mode': task['mode'], 'completed_by': '' } tasks_collection.insert_one(new_task) # create config collection and init values config_collection = db_handle['config'] for config_item in self._config: new_config = { 'name': config_item, 'value': config.get_item(config_item) } config_collection.insert_one(new_config) devices_ip_collection = db_handle['devices_ip'] devices_ip_list = [] for group in range(1, self._number_of_groups + 1): devices_ip_list.append({ 'mac': config.get_group_mac_address(group), 'ip': '' }) devices_ip_collection.insert_many(devices_ip_list)
def reload_yaml(self): config = ConfigManager() config.reload_yaml() db_handle = self.client['esp8266_workshop'] # update all groups mac address and degrees groups_collection = db_handle['groups'] for group in range(1, self._number_of_groups + 1): group_query = {'number': group} groups = [] for g in groups_collection.find(group_query, {'_id': 0}): groups.append(g) if len(groups) > 0: new_values = { '$set': { 'mac': config.get_group_mac_address(group), 'degrees': config.get_group_degrees(group) } } groups_collection.update_one(group_query, new_values) db_handle.drop_collection('config') config_collection = db_handle['config'] for config_item in self._config: new_config = { 'name': config_item, 'value': config.get_item(config_item) } config_collection.insert_one(new_config) db_handle.drop_collection('devices_ip') devices_ip_collection = db_handle['devices_ip'] devices_ip_list = [] for group in range(1, self._number_of_groups + 1): devices_ip_list.append({ 'mac': config.get_group_mac_address(group), 'ip': '' }) devices_ip_collection.insert_many(devices_ip_list)