Commit 8f5c055d authored by Daniel Kolibár's avatar Daniel Kolibár
Browse files

.....

parent 33fa4695
......@@ -8,5 +8,7 @@ ENV PYTHONUNBUFFERED True
COPY ./app /app
#RUN ls -R /app
RUN apt update; apt install -y build-essential
# Install production dependencies.
RUN pip install --no-cache-dir fastapi fastapi_mqtt motor
RUN pip install --no-cache-dir fastapi fastapi_mqtt motor aioinflux influxdb
import time
from datetime import datetime, timedelta
import os
import motor.motor_asyncio
from bson import ObjectId
from fastapi import FastAPI, Body
......@@ -10,6 +11,10 @@ from typing import Optional, List
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse
import json
from influxdb import InfluxDBClient
from aioinflux import *
from typing import NamedTuple
#######################################################################################################################
......@@ -81,6 +86,40 @@ class Photo(BaseModel):
}
@lineprotocol
class Temperature(NamedTuple):
timestamp: TIMEINT
temperature: INT
house_id: STR
@lineprotocol
class Humidity(NamedTuple):
timestamp: TIMEINT
humidity: INT
house_id: STR
@lineprotocol
class Pressure(NamedTuple):
timestamp: TIMEINT
pressure: INT
house_id: STR
@lineprotocol
class Wind(NamedTuple):
timestamp: TIMEINT
wind: INT
house_id: STR
@lineprotocol
class Smoke(NamedTuple):
timestamp: TIMEINT
smoke: INT
house_id: STR
#######################################################################################################################
......@@ -104,7 +143,8 @@ app.add_middleware(
allow_methods=["*"],
allow_headers=["DELETE", "GET", "POST", "PUT"],
)
influx_client = InfluxDBClient(mode='blocking', db='birdie_data', host=os.getenv('INFLUX_HOST', default='localhost'), port=os.getenv('INFLUX_PORT', default=8086), username='admin', password='birdie123')
influx_client.mode = 'async'
#######################################################################################################################
......@@ -128,7 +168,46 @@ async def message(client, topic, payload, qos, properties):
document = {'houseId': ObjectId(recevieddata["houseId"]), 'base64Data': recevieddata["data"]}
result = await DB.photos.insert_one(document)
print('result %s' % repr(result.inserted_id))
if topic == 'birdie/temperature':
recevieddata = json.loads(payload.decode())
temperature = Temperature(
timestamp=int(time.time()),
temperature=recevieddata["temperature"],
house_id=recevieddata["houseId"]
)
await influx_client.write(temperature, time_precision='ms')
if topic == 'birdie/humidity':
recevieddata = json.loads(payload.decode())
humidity = Humidity(
timestamp=int(time.time()),
humidity=recevieddata["humidity"],
house_id=recevieddata["houseId"]
)
await influx_client.write(humidity, time_precision='ms')
if topic == 'birdie/windspeed':
recevieddata = json.loads(payload.decode())
wind = Wind(
timestamp=int(time.time()),
wind=recevieddata["windspeed"],
house_id=recevieddata["houseId"]
)
await influx_client.write(wind, time_precision='ms')
if topic == 'birdie/pressure':
recevieddata = json.loads(payload.decode())
pressure = Pressure(
timestamp=int(time.time()),
pressure=recevieddata["pressure"],
house_id=recevieddata["houseId"]
)
await influx_client.write(pressure, time_precision='ms')
if topic == 'birdie/smoke':
recevieddata = json.loads(payload.decode())
smoke = Smoke(
timestamp=int(time.time()),
smoke=recevieddata["smoke"],
house_id=recevieddata["houseId"]
)
await influx_client.write(smoke, time_precision='ms')
@app.get("/health", response_model=HealthResponse)
async def health():
......
......@@ -34,12 +34,40 @@ services:
- main
volumes:
- grafana_data:/var/lib/grafana
influxdb:
image: influxdb:1.7-alpine
container_name: influxdb
#command: ["influx", "-host", "influxdb", "-port", "8086", "-execute", "'CREATE DATABASE birdie_data'"]
environment:
- INFLUXDB_ADMIN_ENABLED=true
- INFLUXDB_ADMIN_USER=${INFLUXDB_ADMIN_USER:-admin}
- INFLUXDB_ADMIN_PASSWORD=${INFLUXDB_ADMIN_PASSWORD:-birdie123}
- INFLUXDB_DB=prometheus
- INFLUXDB_HTTP_LOG_ENABLED=false
- INFLUXDB_REPORTING_DISABLED=true
- INFLUXDB_USER=${INFLUXDB_USER:-prometheus}
- INFLUXDB_USER_PASSWORD=${INFLUXDB_USER_PASSWORD:-prompass}
volumes:
- ./influx_data:/var/lib/influxdb
healthcheck:
test: "ln -sf /bin/busybox /bin/wget && /bin/wget -q -Y off http://localhost:8086/metrics -O /dev/null > /dev/null 2>&1"
interval: 25s
timeout: 3s
start_period: 30s
ports:
- 8086:8086
networks:
- main
api:
depends_on:
- influxdb
container_name: birdie_API
image: birdieiot/api
environment:
- MONGO_URL=mongodb://root:pass12345@mongodb:27017
- INFLUX_HOST=influxdb
- INFLUX_PORT=8086
ports:
- 27099:80
networks:
......@@ -56,7 +84,77 @@ services:
# volumes:
# #- ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
# - mosquitto_data:/data
fakebudka0:
container_name: fakeBudka0
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b28eb3650633ec0f34d69
fakebudka1:
container_name: fakeBudka1
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b29183650633ec0f34d6b
fakebudka2:
container_name: fakeBudka2
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b29473650633ec0f34d6c
fakebudka3:
container_name: fakeBudka3
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b29563650633ec0f34d6d
fakebudka4:
container_name: fakeBudka4
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b29633650633ec0f34d6e
fakebudka5:
container_name: fakeBudka5
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b29703650633ec0f34d6f
fakebudka6:
container_name: fakeBudka6
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b297b3650633ec0f34d70
fakebudka7:
container_name: fakeBudka7
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b29873650633ec0f34d71
fakebudka8:
container_name: fakeBudka8
image: birdieiot/fakebudka
networks:
- main
environment:
- HOUSE_ID=608b28fb3650633ec0f34d6a
volumes:
mongo_data:
grafana_data:
......
......@@ -2,6 +2,7 @@ import paho.mqtt.client as mqtt
import json
import random
import time
import os
def on_connect(client, userdata, flags, rc):
if rc == 0:
......@@ -10,7 +11,7 @@ def on_connect(client, userdata, flags, rc):
print(f"Connecting to MQTT broker failed with code {rc}")
HOUSE_ID = '6088716f4d60d0230034f87d'
HOUSE_ID = os.getenv('HOUSE_ID')
client = mqtt.Client()
client.on_connect = on_connect
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment