This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from myoung34/sqlite
Add support for sqlite emitter
- Loading branch information
Showing
9 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters