def _messageHandler(self, *args, **kwargs): ''' Calback method envoked by the firmata library. Handles the string message sent from the arduino. Grabs the sensor data from the message and persists in the DB table. :param args: :param kwargs: :return: ''' readings = json.loads(util.two_byte_iter_to_str(args)) sky_temperature = readings['sky'] ambient_temperature = readings['ambient'] rain = readings['rain'] if self.should_persist_sensor_reading(sky_temperature, ambient_temperature, rain): conn = sqlite3.connect('weather_sensor.db') c = conn.cursor() print('inserting observation') c.execute( "INSERT INTO weather_sensor (rain,sky_temperature,ambient_temperature) VALUES (?,?,?)", (rain, sky_temperature, ambient_temperature)) c.execute( "DELETE FROM weather_sensor WHERE date_sensor_read <= date('now','-365 day')" ) print('inserted') # new_id = c.lastrowid conn.commit() self.last_rain_reading_saved = rain self.last_sky_temperature_reading_saved = sky_temperature self.last_ambient_temperature_reading_saved = ambient_temperature self.last_reading_saved_time = time.time() c.close()
def parseString(self, *args, **kwargs): ''' Обработать полученную от Arduino-исполнителя строку. Вызывается автоматически при получении строкового сообщения. :returns: None ''' received = ArduinoUtil.two_byte_iter_to_str(args) self.receivedFirmataMessages.append( self.FirmataStringResponse(received))
def test_two_byte_iter_to_str(self): string, s = 'StandardFirmata', [] for i in string: s.append(i) s.append('\x00') self.assertEqual(two_byte_iter_to_str(s), 'StandardFirmata')
def on_string_received(*args, **kwargs): print(util.two_byte_iter_to_str(args)) # converts the incoming 2 byte data to character and then forms String