def __init__(self): self.config = ConfigParser.RawConfigParser() self.config.read(__config_file__) self.hub_id = self.config.get('hub', 'hub_id') # Username self.last_updated = self.config.getint('hub', 'last_update') self.password = self.config.get('hub', 'password') self.token = self.config.get('hub', 'token') self.server_url = self.config.get('hub', 'server_url') self.server_interval = self.config.getint('hub', 'server_interval') self.server_wait = self.config.getint('hub', 'server_wait') self.sensors = [] self.json_posting = JsonPosting() self.sensor_factory = SensorFactory() self.init_sensors()
class Hub(): def __init__(self): self.config = ConfigParser.RawConfigParser() self.config.read(__config_file__) self.hub_id = self.config.get('hub', 'hub_id') # Username self.last_updated = self.config.getint('hub', 'last_update') self.password = self.config.get('hub', 'password') self.token = self.config.get('hub', 'token') self.server_url = self.config.get('hub', 'server_url') self.server_interval = self.config.getint('hub', 'server_interval') self.server_wait = self.config.getint('hub', 'server_wait') self.sensors = [] self.json_posting = JsonPosting() self.sensor_factory = SensorFactory() self.init_sensors() def init_sensors(self): """ Initializes and connects to the sensors of this hub @return: A list of active sensors that can be interfaced with """ for sensor in self.config.items('sensors'): self.sensors.append(self.sensor_factory.get_sensor_instance(sensor[0], self.config)) log_to_console("Initialized sensor: " + sensor[0]) # TODO try to connect to BLE sensors def config_write(self): config_file = open(__config_file__, 'w') self.config.write(config_file) config_file.close() def add_sensor(self, name, sensor_type, mac_address=None): """ Takes a mac adress and a name and adds it to the list of sensors of this hub @param name: The name of the sensor @param sensor_type: The type of sensor @param mac_address: The MAC address of the sensor @return: True if sensor was successfully added to the list, false otherwise """ log_str = "Adding sensor: " + name + ", type: " + sensor_type + ', MAC address: ' + str( mac_address) log_to_console(log_str) if not [s for s in self.sensors if s.name == name]: self.config.set('sensors', name, sensor_type) self.config.add_section(name) self.config.set(name, 'type', sensor_type) self.config.set(name, 'mac_address', mac_address) self.config.set(name, 'last_update', int(calendar.timegm(dt.utcnow().timetuple()))) self.config_write() self.sensors.append(self.sensor_factory.get_sensor_instance(name, self.config)) else: log_to_console("A sensor with that name already exists") def get_sensor_data(self, sensor): end_time = dt.utcnow() start_time = dt.utcfromtimestamp(sensor.last_updated) measurements = sensor.get_all_measurements(start_time, end_time) if measurements: log_str = 'Caching measurements for ' + sensor.name log_to_console(log_str) if caching.cache_measurements(sensor, measurements, self): self.config.set(sensor.name, 'last_update', sensor.last_updated) self.config_write() return measurements