Skip to content

Commit fd28b44

Browse files
committed
Merged code from external repo and refactored folder structure
1 parent 6c6421f commit fd28b44

File tree

18 files changed

+152
-0
lines changed

18 files changed

+152
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Installation
2+
3+
To build package:
4+
* pip install build
5+
* python -m build
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .database_storage import DatabasePersistence, DatabaseRetrieval
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
db_storage:
3+
image: influxdb:2.7 # or a different version
4+
ports:
5+
- "127.0.0.1:8086:8086"
6+
volumes:
7+
- ./influxdb_data:/app/db_storage/data
8+
environment:
9+
DOCKER_INFLUXDB_INIT_MODE: setup
10+
DOCKER_INFLUXDB_INIT_USERNAME: admin
11+
DOCKER_INFLUXDB_INIT_PASSWORD: Arduino15
12+
DOCKER_INFLUXDB_INIT_ORG: arduino
13+
DOCKER_INFLUXDB_INIT_BUCKET: db_storage
14+
DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: 392edbf2-b8a2-481f-979d-3f188b2c05f0
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from influxdb_client import InfluxDBClient, Point, WritePrecision
2+
from influxdb_client.client.write_api import SYNCHRONOUS
3+
import yaml
4+
import time
5+
6+
class _InfluxPersistence:
7+
def __init__(self, host:str = "db_storage", port:int = 8086):
8+
self.name = "db_storage"
9+
self.host = host
10+
self.port = port
11+
infra = self.load_default_infra()
12+
env_dict = infra['services']['db_storage']['environment']
13+
self.url = f"http://{self.host}:{self.port}"
14+
self.token = env_dict['DOCKER_INFLUXDB_INIT_ADMIN_TOKEN']
15+
self.org = env_dict['DOCKER_INFLUXDB_INIT_ORG']
16+
self.bucket = env_dict['DOCKER_INFLUXDB_INIT_BUCKET']
17+
self._connect()
18+
19+
def load_default_infra(self):
20+
with open("compose.yaml", "r") as file:
21+
config = yaml.safe_load(file)
22+
return config
23+
24+
def _connect(self):
25+
try:
26+
with InfluxDBClient(url=self.url, token=self.token, org=self.org) as client:
27+
self.client = client
28+
self.write_api = client.write_api(write_precision=WritePrecision.MS)
29+
self.query_api = client.query_api()
30+
#Test connection
31+
query = f'from(bucket: "{self.bucket}") |> range(start: -1m)'
32+
self.query_api.query(query)
33+
print("Connection and query successful!")
34+
35+
except Exception as e:
36+
print(f"Error: {e}")
37+
38+
def close(self):
39+
self.client.close()
40+
41+
class DatabasePersistence(_InfluxPersistence):
42+
def __init__(self, host:str, port:int):
43+
super().__init__(host, port)
44+
45+
def write_sample(self, measurement_name :str, measure :str, value :any, ts :int = 0):
46+
try:
47+
if ts <= 0:
48+
ts = int(time.time_ns() / 1000000)
49+
point = Point(measurement_name).field(measure, value).time(ts, WritePrecision.MS)
50+
51+
self.write_api.write(bucket=self.bucket, record=point)
52+
print("Data written successfully!")
53+
54+
except Exception as e:
55+
print(f"Error: {e}")
56+
57+
class DatabaseRetrieval(_InfluxPersistence):
58+
def __init__(self, host:str, port:int):
59+
super().__init__(host, port)
60+
61+
def read_last_sample(self, measurement_name :str, measure :str):
62+
try:
63+
query = f"""
64+
from(bucket: "{self.bucket}")
65+
|> range(start: -1d) # look back 1 day
66+
|> filter(fn: (r) => r["_measurement"] == "{measurement_name}")
67+
|> filter(fn: (r) => r["_field"] == "{measure}")
68+
|> last()
69+
"""
70+
71+
result = self.query_api.query(org=self.org, query=query)
72+
73+
if result:
74+
for table in result:
75+
for record in table.records:
76+
return record.get_time(), record.get_value()
77+
else:
78+
print(f"No data found for {measurement_name} and {measure}")
79+
80+
except Exception as e:
81+
print(f"Error: {e}")
82+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: db_storage
2+
module_description: "Simplified database storage layer for Arduino sensor data"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from database_storage import DatabasePersistence
2+
import time
3+
4+
## TODO define unit test properly, now just a simple main
5+
6+
db = DatabasePersistence(host="localhost")
7+
8+
i = 0
9+
while i < 2:
10+
db.write_sample("test_measurement", 1234)
11+
time.sleep(1)
12+
i += 1
13+
14+
time.sleep(1)
15+
db.close()
16+
print("Test complete!")
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)