class LedStripper: def __init__(self): if debug or (len(sys.argv) > 1 and sys.argv[1] == "debug"): self._debug = True else: self._debug = False self.mapper = LedMapper('staticThreeColorMap') if self._debug == True: self.measurement_poll_frequency = 30 self.writer = MockedWriter() else: # 2 Minutes self.measurement_poll_frequency = 2 * 60 self.writer = LedWriter() def setup_netatmo(self): config = ConfigParser.ConfigParser() # get the directory where this script resides mydir = os.path.join(os.path.dirname(os.path.abspath(__file__))) # read the config file in that dir config.read(os.path.join(mydir, 'netatmo.conf')) # netatmo instance self.netatmo_api = NetatmoApi(config.get('api', 'client_id'), config.get('api', 'client_secret')) self.selected_netatmo_device_id = config.get('api', 'device_id') self.selected_netatmo_device_index = 0 def gather_measurement(self): # Init includes authentication which is necessary in regular intervals self.setup_netatmo() devicelist = self.netatmo_api.cmd("devicelist") devicelist_body = devicelist["body"] pp = pprint.PrettyPrinter(depth=9) print "Netatmo devicelist:" for i, device in enumerate(devicelist_body["devices"]): print i, ": ", device["station_name"] if device["_id"] == self.selected_netatmo_device_id: self.selected_netatmo_device = i print "***SELECTED***" print "--------------" pp.pprint(device) print "\n\n" co2 = devicelist_body["devices"][self.selected_netatmo_device]["dashboard_data"]["CO2"] return Measurement(co2) def map_co2_to_color(self, measurement): return self.mapper.map(measurement) def write_color(self, color): return self.writer.write(color) def run(self): while True: try: measurement = self.gather_measurement() except Exception as e: print "Gathering measurement failed." traceback.print_exc() continue if self._debug == True: print "CO2: ", measurement.co2 color = self.map_co2_to_color(measurement) self.write_color(color) time.sleep(self.measurement_poll_frequency)
def test_mapStatic0ppmShouldBeGreen(self): measurement = Measurement(0) mapper = LedMapper() color = mapper.map(measurement) self.assertEqual(color, [0,255,0])
def test_mapStaticAbove2000pmShouldBeRed(self): measurement = Measurement(3000) mapper = LedMapper() color = mapper.map(measurement) self.assertEqual(color, [255,0,0])
def test_staticThreeColorMapShouldBeRed(self): measurement = Measurement(2000) mapper = LedMapper('staticThreeColorMap') color = mapper.map(measurement) self.assertEqual(color, [255,0,0])
def test_mapStatic1000pmShouldBeYellow(self): measurement = Measurement(1000) mapper = LedMapper() color = mapper.map(measurement) self.assertEqual(color, [255,245,0])