Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a03fa0
commit c503ac4
Showing
23 changed files
with
9,699 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,3 +87,6 @@ ENV/ | |
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
config.py | ||
web/static/img/bg/*.jpg |
Empty file.
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,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 |
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,7 @@ | ||
CREATE TABLE ht ( | ||
id serial, | ||
sid varchar(50), | ||
temperature integer, | ||
humidity integer, | ||
dt timestamp without time zone | ||
); |
Empty file.
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,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 |
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,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 = {} |
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,2 @@ | ||
psycopg2==2.6.2 | ||
tornado==4.4.2 |
Empty file.
Oops, something went wrong.