Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #18 from myoung34/sqlite
Browse files Browse the repository at this point in the history
Add support for sqlite emitter
  • Loading branch information
myoung34 authored Jul 31, 2020
2 parents c8e99b6 + 11ecc79 commit 0905269
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.5.0

IMPROVEMENTS:

* Added support for SQLite emitter: https://github.com/myoung34/tilty/pull/18

## 0.4.0

IMPROVEMENTS:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The Tilt supports writing to a google doc which you could use with something lik
* Webhooks
* InfluxDB
* Datadog (dogstatsd)
* SQLite

## Usage ##

Expand All @@ -34,6 +35,10 @@ $ cat <<EOF >config.ini
[general]
sleep_interval = 1
# SQLite example
[sqlite]
file = /etc/tilty/tilt.sqlite
# Generic application/json example
[webhook]
url = http://www.foo.com
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Tilty"
version = "0.4.0"
version = "0.5.0"
description = "A pluggable system to receive and transmit bluetooth events from the Tilt Hydrometer"
authors = ["Marcus Young <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
py_modules=['tilty', 'blescan'],
version='0.4.0',
version='0.5.0',
packages=find_packages(exclude=['tests*']),
install_requires=[
'Click',
Expand Down
4 changes: 4 additions & 0 deletions tests/mock_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def __init__(self, section):
self.section = section

def __getitem__(self, key):
if self.section == 'sqlite':
return {
'file': '/foo.sqlite',
}
if self.section == 'webhook':
return {
'url': 'http://www.google.com',
Expand Down
23 changes: 23 additions & 0 deletions tests/test_sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from unittest import mock

from tilty.emitters import sqlite


@mock.patch('tilty.emitters.sqlite.sqlite3')
def test_sqlite(
mock_sqlite_client,
):
config = {
'file': '/etc/tilty/tilt.sqlite',
'color': 'black',
'mac': '00:0a:95:9d:68:16',
'gravity': 1000,
'temp': 80,
}
sqlite.SQLite(config=config).emit()
assert mock_sqlite_client.mock_calls == [
mock.call.connect('/etc/tilty/tilt.sqlite'),
mock.call.connect().execute('\n CREATE TABLE IF NOT EXISTS data(\n id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n gravity INTEGER,\n temp INTEGER,\n color VARCHAR(16),\n mac VARCHAR(17),\n timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)\n '), # noqa
mock.call.connect().execute('insert into data (gravity,temp,color,mac) values (?,?,?,?)', (1000, 80, 'black', '00:0a:95:9d:68:16')) # noqa
]
26 changes: 26 additions & 0 deletions tests/test_tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ def test_scan_for_tilt_data(
bt_events.assert_called()


@mock.patch('tilty.emitters.sqlite.SQLite')
def test_scan_for_tilt_data_parse_sqlite(
mock_sqlite,
):
config = MockConfigParser('sqlite')
tilty.emit(
config,
{
'color': 'black',
'gravity': 1,
'temp': 32,
'mac': '00:0a:95:9d:68:16',
}
)
assert mock_sqlite.mock_calls == [
mock.call(config={
'file': '/foo.sqlite',
'gravity': 1,
'temp': 32,
'mac': '00:0a:95:9d:68:16',
'color': 'black'
}),
mock.call().emit(),
]


@mock.patch('tilty.emitters.webhook.Webhook')
def test_scan_for_tilt_data_parse_webhook(
mock_webhook,
Expand Down
42 changes: 42 additions & 0 deletions tilty/emitters/sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
""" SQLite emitter """
import logging
import sqlite3

LOGGER = logging.getLogger()


class SQLite: # pylint: disable=too-few-public-methods
""" SQLite wrapper class """

def __init__(self, config):
""" Initializer
Args:
config: (dict) represents the configuration for the emitter
"""
self.color = config['color']
self.gravity = config['gravity']
self.temp = config['temp']
self.mac = config['mac']
self.conn = sqlite3.connect(config['file'])
self.conn.execute('''
CREATE TABLE IF NOT EXISTS data(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
gravity INTEGER,
temp INTEGER,
color VARCHAR(16),
mac VARCHAR(17),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)
''')

def emit(self, **kwargs): # pylint: disable=no-self-use,unused-argument
""" Initializer
Args:
"""
LOGGER.info('[sqlite] creating row')
self.conn.execute(
"insert into data (gravity,temp,color,mac) values (?,?,?,?)",
(self.gravity, self.temp, self.color, self.mac)
)
17 changes: 16 additions & 1 deletion tilty/tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from jinja2 import Template

from tilty.emitters import datadog, influxdb, webhook
from tilty.emitters import datadog, influxdb, sqlite, webhook


def emit(config, tilt_data):
Expand All @@ -15,6 +15,21 @@ def emit(config, tilt_data):
"""
if tilt_data is None:
return

# <start config sample>
# [sqlite]
# file = /etc/tilty/tilt.sqlite
if config.has_section('sqlite'):
_config = {
'file': config['sqlite']['file'],
'gravity': tilt_data['gravity'],
'temp': tilt_data['temp'],
'mac': tilt_data['mac'],
'color': tilt_data['color'],
}
_sqlite = sqlite.SQLite(config=_config)
_sqlite.emit()

# <start config sample>
# [webhook]
# url = http://www.foo.com
Expand Down

0 comments on commit 0905269

Please sign in to comment.