Completed
Push — master ( d47136...8eb192 )
by Tinghui
57s
created

CASASHome.get_all_sensors()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
import json
2
import os
3
import logging
4
5
logger = logging.getLogger(__name__)
6
7
8
class CASASHome:
9
    """Load Home Data Structure from JSON file
10
11
    Attributes:
12
        data_dict (:obj:`dict`): A dictionary contains information about smart home.
13
14
    Parameters:
15
        directory (:obj:`str`): Directory that stores CASAS smart home data
16
    """
17
    def __init__(self, directory):
18
        dataset_json_fname = directory + '/dataset.json'
19
        if os.path.exists(dataset_json_fname):
20
            f = open(dataset_json_fname, 'r')
21
            self.data_dict = json.load(f)
22
        else:
23
            logger.error('Smart home metadata file %s does not exist. Create an empty CASASHome Structure'
24
                        % dataset_json_fname)
25
            raise FileNotFoundError('File %s not found.' % dataset_json_fname)
26
            # self.data_dict = {
27
            #     'name': '',
28
            #     'floorplan': '',
29
            #     'sensors': [],
30
            #     'activities': [],
31
            #     'residents': []
32
            # }
33
34
    def get_name(self):
35
        """Get the smart home name
36
37
        Returns:
38
            :obj:`str`: smart home name
39
        """
40
        return self.data_dict['name']
41
42
    def get_all_activities(self):
43
        """Get All Activities
44
45
        Returns:
46
            :obj:`list` of :obj:`str`: list of activity names
47
        """
48
        names = [activity['name'] for activity in self.data_dict['activities']]
49
        return names
50
51
    def get_activity(self, label):
52
        """Find the information about the activity
53
54
        Parameters:
55
            label (:obj:`str`): activity label
56
57
        Returns:
58
            :obj:`dict`: A dictionary containing activity information
59
        """
60
        for activity in self.data_dict['activities']:
61
            if activity['name'] == label:
62
                return activity
63
        return None
64
65
    def get_activity_color(self, label):
66
        """Find the color string of the activity
67
68
        Parameters:
69
            label (:obj:`str`): activity label
70
71
        Returns:
72
            :obj:`str`: RGB color string
73
        """
74
        activity = self.get_activity(label)
75
        if activity is not None:
76
            return "#" + activity['color'][3:9]
77
        else:
78
            raise ValueError('Activity %s Not Found' % label)
79
80
    def get_sensor(self, name):
81
        """Get the information about the sensor
82
83
        Parameters:
84
            name (:obj:`str`): name of the sensor
85
86
        Returns:
87
            :obj:`dict`: A dictionary that stores sensor information
88
        """
89
        for sensor in self.data_dict['sensors']:
90
            if sensor['name'] == name:
91
                return sensor
92
        return None
93
94
    def get_all_sensors(self):
95
        """Get All Sensor Names
96
97
        Returns:
98
            :obj:`list` of :obj:`str`: a list of sensor names
99
        """
100
        names = [sensor['name'] for sensor in self.data_dict['sensors']]
101
        return names
102
103
    def get_resident(self, name):
104
        """Get Information about the resident
105
106
        Parameters:
107
            name (:obj:`str`): name of the resident
108
109
        Returns:
110
            :obj:`dict`: A Dictionary that stores resident information
111
        """
112
        for resident in self.data_dict['residents']:
113
            if resident['name'] == name:
114
                return resident
115
        return None
116
117
    def get_resident_color(self, name):
118
        """Get the color string for the resident
119
120
        Parameters:
121
            name (:obj:`str`): name of the resident
122
123
        Returns:
124
            :obj:`str`: RGB color string representing the resident
125
        """
126
        resident = self.get_resident(name)
127
        if resident is not None:
128
            return "#" + resident['color'][3:9]
129
        else:
130
            raise ValueError('Resident %s Not Found' % name)
131
132
    def get_all_residents(self):
133
        """Get All Resident Names
134
135
        Returns:
136
            :obj:`list` of :obj:`str`: A list of resident names
137
        """
138
        names = [resident['name'] for resident in self.data_dict['residents']]
139
        return names
140