Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aluminiumgeek committed Dec 30, 2016
1 parent 0a03fa0 commit c503ac4
Show file tree
Hide file tree
Showing 23 changed files with 9,699 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -87,3 +87,6 @@ ENV/

# Rope project settings
.ropeproject

config.py
web/static/img/bg/*.jpg
Empty file added __init__.py
Empty file.
20 changes: 20 additions & 0 deletions config.py.example
@@ -0,0 +1,20 @@
DEBUG = True

# PostgreSQL connection
DBNAME = 'mihome'
DBUSER = 'mihome'
DBPASS = 'password'

# Gateway IP
MIHOME_GATEWAY_IP = '192.168.100.103'

# Sensors - SID : Human-readable name
SENSORS = {
'158d000116a58e': 'Office',
'158d0001264ac3': 'Bedroom 1',
'158d0001264ac3': 'Bedroom 2',
'158d00010850da': 'Outside'
}

# Web application port
WEB_APP_PORT = 8888
7 changes: 7 additions & 0 deletions init.sql
@@ -0,0 +1,7 @@
CREATE TABLE ht (
id serial,
sid varchar(50),
temperature integer,
humidity integer,
dt timestamp without time zone
);
Empty file added plugins/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions plugins/sensor_ht.py
@@ -0,0 +1,24 @@
from datetime import datetime


def process(conn, cursor, current, message, data):
if message.get('model') != 'sensor_ht':
return False

save = False
if message.get('cmd') == 'report':
if current:
save = True
if 'temperature' in data:
current['temperature'] = data['temperature']
if 'humidity' in data:
current['humidity'] = data['humidity']
else:
current = data
if not save:
return False

query = "INSERT INTO ht(sid, temperature, humidity, dt) VALUES ('{}', {}, {}, '{}')".format(message['sid'], current['temperature'], current['humidity'], datetime.now().isoformat())
cursor.execute(query)
conn.commit()
return True
44 changes: 44 additions & 0 deletions receiver.py
@@ -0,0 +1,44 @@
from datetime import datetime
import json
import socket
import binascii
import struct
import psycopg2

import config
from plugins import sensor_ht

conn = psycopg2.connect("dbname={} user={} password={}".format(config.DBNAME, config.DBUSER, config.DBPASS))
cursor = conn.cursor()

UDP_IP = config.MIHOME_GATEWAY_IP
UDP_PORT_FROM = 54322
UDP_PORT = 54321

MULTICAST_PORT = 9898
SERVER_PORT = 4321

MULTICAST_ADDRESS = '224.0.0.50'
SOCKET_BUFSIZE = 1024
MESSAGE = binascii.unhexlify('21310020ffffffffffffffffffffffffffffffffffffffffffffffffffffffff')

sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP

sock.bind(("0.0.0.0", MULTICAST_PORT))

mreq = struct.pack("=4sl", socket.inet_aton(MULTICAST_ADDRESS), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_BUFSIZE)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

current = {}

while True:
data, addr = sock.recvfrom(SOCKET_BUFSIZE) # buffer size is 1024 bytes
message = json.loads(data.decode())
data = json.loads(message['data'])
if message.get('model') == 'sensor_ht' and not sensor_ht.process(conn, cursor, current, message, data):
continue
current = {}
2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
psycopg2==2.6.2
tornado==4.4.2
Empty file added web/__init__.py
Empty file.

0 comments on commit c503ac4

Please sign in to comment.