Changeset - 319b08c756e5
[Not reviewed]
default
0 1 0
Dennis Fink - 9 years ago 2016-03-17 23:54:27
dennis.fink@c3l.lu
Some state adjustments
1 file changed with 8 insertions and 4 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
@@ -38,54 +38,58 @@ class ActiveStatus(Singleton, dict):
 
            self.update(json.load(f))
 

	
 
        if os.path.exists(last_state_file) and os.path.isfile(last_state_file):
 
            with open(last_state_file, encoding='utf-8') as f:
 
                last_state_json = json.load(f)
 

	
 
            self['state'] = last_state_json['state']
 
            self['sensors'].update(last_state_json['sensors'])
 

	
 
    def save_last_state(self):
 

	
 
        with open(last_state_file, mode='w', encoding='utf-8') as f:
 
            last_state = {}
 
            last_state['state'] = self['state']
 
            last_state['sensors'] = self['sensors']
 
            json.dump(last_state, f, sort_keys=True)
 

	
 
    def add_user_present(self, username):
 
        if self['state']['open']:
 
            if 'people_now_present' not in self['sensors']:
 
                self['sensors']['people_now_present'] = {'value': 0}
 

	
 
            people_now_present = self['sensors']['people_now_present']
 

	
 
            people_now_present['value'] += 1
 
            if 'names' in people_now_present and username not in people_now_present['names']:
 
                people_now_present['value'] += 1
 

	
 
            if username in current_app.config['PEOPLE_NOW_PRESENT_ALLOWED']:
 
                if 'names' in people_now_present and username not in people_now_present['names']:
 
                if username in current_app.config['PEOPLE_NOW_PRESENT_ALLOWED']:
 
                    people_now_present['names'].append(username)
 
                else:
 

	
 
            elif 'names' not in people_now_present:
 
                people_now_present['value'] += 1
 

	
 
                if username in current_app.config['PEOPLE_NOW_PRESENT_ALLOWED']:
 
                    people_now_present['names'] = [username]
 

	
 
            self['sensors']['people_now_present'] = people_now_present
 
        else:
 
            pass
 

	
 
    def remove_user_present(self, username):
 
        if self['state']['open'] and 'people_now_present' in self['sensors']:
 
            people_now_present = self['sensors']['people_now_present']
 

	
 
            if people_now_present['value'] > 0:
 
                people_now_present['value'] -= 1
 

	
 
            if 'names' in people_now_present:
 
                if people_now_present['value'] == 0:
 
                    del people_now_present['names']
 
                elif username in people_now_present['names']:
 
                    people_now_present['names'].remove(username)
 
                    if not people_now_present['names']:
 
                        del people_now_present['names']
 
            self['sensors']['people_now_present'] = people_now_present
 
        else:
 
            pass
 

	
0 comments (0 inline, 0 general)