-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
32 changed files
with
651 additions
and
13 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
*pyc |
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
Binary file not shown.
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,18 @@ | ||
{ | ||
"short_name": "Heating", | ||
"name": "House Heating", | ||
"icons": [ | ||
{ | ||
"src": "./web/heating/images/fire_192.png", | ||
"type": "image/png", | ||
"sizes": "192x192" | ||
}, | ||
{ | ||
"src": "./web/heating/images/fire.png", | ||
"type": "image/png", | ||
"sizes": "266x344" | ||
} | ||
], | ||
"start_url": "./web/heating/index.html", | ||
"display": "standalone" | ||
} |
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 @@ | ||
heat.log |
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,75 @@ | ||
import sys,os | ||
import json | ||
import logging as log | ||
import socket | ||
from collections import OrderedDict | ||
import datetime | ||
|
||
from platform import system as system_name # Returns the system/OS name | ||
from subprocess import call as system_call # Execute a shell command | ||
|
||
def ping(host): | ||
""" | ||
Returns True if host (str) responds to a ping request. | ||
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid. | ||
""" | ||
|
||
# Ping command count option as function of OS | ||
param = '-n' if system_name().lower()=='windows' else '-c' | ||
|
||
# Building the command. Ex: "ping -c 1 google.com" | ||
command = ['ping', param, '1', host] | ||
|
||
# Pinging | ||
return system_call(command) == 0 | ||
|
||
# -------------------- config -------------------- | ||
def get_local_json(): | ||
"""fetches the config.json file in the local directory | ||
if config_hostname.json is found it is used over the default one | ||
""" | ||
config = None | ||
dirname = os.path.dirname(sys.argv[0]) | ||
if(len(dirname) == 0): | ||
dirname = "." | ||
config_file = dirname+'/'+"config_"+socket.gethostname()+".json" | ||
if(os.path.isfile(config_file)): | ||
print("loading: ",config_file) | ||
config = json.load(open(config_file)) | ||
else: | ||
config_file = dirname+'/'+"config.json" | ||
if(os.path.isfile(config_file)): | ||
print("loading: %s",config_file) | ||
config = json.load(open(config_file)) | ||
else: | ||
print("Fatal error 'config.json' not found") | ||
return config | ||
|
||
# -------------------- config -------------------- | ||
def get_local_nodes(nodes_file): | ||
nodes = json.load(open(nodes_file),object_pairs_hook=OrderedDict) | ||
return nodes | ||
|
||
def configure_log(logger_name): | ||
global_config = get_local_json() | ||
config = global_config["log"] | ||
log_level_map = { | ||
"Debug" :10, | ||
"Info" :20, | ||
"Warning" :30, | ||
"Error" :40, | ||
"Critical" :50 | ||
} | ||
#if(os.path.isfile(config["logfile"])): | ||
for handler in log.root.handlers[:]: | ||
log.root.removeHandler(handler) | ||
log.basicConfig( filename=config["logfile"], | ||
level=log_level_map[config["level"]], | ||
format='%(asctime)s %(name)s %(levelname)-8s %(message)s', | ||
datefmt='%d %H:%M:%S' | ||
) | ||
log.getLogger('').addHandler(log.StreamHandler()) | ||
log.info("====> '%s' started logging with level '%s' @ '%s'"%(logger_name,config["level"],str(datetime.datetime.utcnow()))) | ||
#else: | ||
# print("Log file not available : %s"%(config["logfile"])) | ||
return global_config |
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,29 @@ | ||
{ | ||
"mqtt":{ | ||
"host":"10.0.0.42", | ||
"port":1883, | ||
"keepalive":60, | ||
"client_id":"heat_client", | ||
"subscriptions":[ "zig/balcony door", | ||
"zig/balcony window right", | ||
"zig/balcony window left" | ||
], | ||
"actions" :[], | ||
"publish" :true, | ||
"subscribe" :true | ||
}, | ||
"heatings":{ | ||
"living heat":{ | ||
"topic":"zig/living heat/set", | ||
"Apertures":[ | ||
"balcony door", | ||
"balcony window right", | ||
"balcony window left" | ||
] | ||
} | ||
}, | ||
"log":{ | ||
"logfile":"/home/pi/share/heat.log", | ||
"level":"Debug" | ||
} | ||
} |
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,52 @@ | ||
{ | ||
"mqtt":{ | ||
"host":"10.0.0.42", | ||
"port":1883, | ||
"keepalive":60, | ||
"client_id":"heat_client", | ||
"subscriptions":[ "+/balcony door", | ||
"+/balcony window right", | ||
"+/balcony window left", | ||
"+/office window right", | ||
"+/office window left", | ||
"+/bedroom window", | ||
"+/kitchen window" | ||
], | ||
"actions" :[], | ||
"publish" :true, | ||
"subscribe" :true | ||
}, | ||
"heatings":{ | ||
"living heat":{ | ||
"topic":"lzig/living heat/set", | ||
"Apertures":[ | ||
"balcony door", | ||
"balcony window right", | ||
"balcony window left" | ||
] | ||
}, | ||
"office heat":{ | ||
"topic":"mzig/office heat/set", | ||
"Apertures":[ | ||
"office window right", | ||
"office window left" | ||
] | ||
}, | ||
"bedroom heat":{ | ||
"topic":"lzig/bedroom heat/set", | ||
"Apertures":[ | ||
"bedroom window" | ||
] | ||
}, | ||
"kitchen heat":{ | ||
"topic":"lzig/kitchen heat/set", | ||
"Apertures":[ | ||
"kitchen window" | ||
] | ||
} | ||
}, | ||
"log":{ | ||
"logfile":"./heat.log", | ||
"level":"Debug" | ||
} | ||
} |
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,52 @@ | ||
{ | ||
"mqtt":{ | ||
"host":"10.0.0.42", | ||
"port":1883, | ||
"keepalive":60, | ||
"client_id":"heat_client", | ||
"subscriptions":[ "+/balcony door", | ||
"+/balcony window right", | ||
"+/balcony window left", | ||
"+/office window right", | ||
"+/office window left", | ||
"+/bedroom window", | ||
"+/kitchen window" | ||
], | ||
"actions" :[], | ||
"publish" :true, | ||
"subscribe" :true | ||
}, | ||
"heatings":{ | ||
"living heat":{ | ||
"topic":"lzig/living heat/set", | ||
"Apertures":[ | ||
"balcony door", | ||
"balcony window right", | ||
"balcony window left" | ||
] | ||
}, | ||
"office heat":{ | ||
"topic":"mzig/office heat/set", | ||
"Apertures":[ | ||
"office window right", | ||
"office window left" | ||
] | ||
}, | ||
"bedroom heat":{ | ||
"topic":"lzig/bedroom heat/set", | ||
"Apertures":[ | ||
"bedroom window" | ||
] | ||
}, | ||
"kitchen heat":{ | ||
"topic":"lzig/kitchen heat/set", | ||
"Apertures":[ | ||
"kitchen window" | ||
] | ||
} | ||
}, | ||
"log":{ | ||
"logfile":"/home/pi/share/heat.log", | ||
"level":"Info" | ||
} | ||
} |
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,95 @@ | ||
#https://github.com/studioimaginaire/phue | ||
|
||
|
||
#https://pypi.python.org/pypi/paho-mqtt/1.1 | ||
import paho.mqtt.client as mqtt | ||
import json | ||
from time import sleep | ||
import logging as log | ||
import sys,os | ||
import cfg | ||
from mqtt import mqtt_start | ||
|
||
eurotronic_system_mode = { | ||
"mirror display":1, | ||
"boost":2, | ||
"disable open window":4, | ||
"set open window":5, | ||
"child protection":7 | ||
} | ||
|
||
def heater_notify(name,command): | ||
topic = config["heatings"][name]["topic"] | ||
json_msg = {"eurotronic_system_mode":2**(eurotronic_system_mode[command])} | ||
text_msg = json.dumps(json_msg) | ||
clientMQTT.publish(topic,text_msg) | ||
return | ||
|
||
def check_all_contacts(apertures): | ||
''' if any is open then all are open and contact => False''' | ||
res = True | ||
for name,aperture in apertures.items(): | ||
if(aperture == False): | ||
res = False | ||
#print(f"res={res} for {apertures}") | ||
return res | ||
|
||
def window_open(aperture,payload): | ||
global state | ||
sensor = json.loads(payload) | ||
log.debug(f"{aperture} => contact = {sensor['contact']}") | ||
heater = topic_to_heater[aperture] | ||
prev_all_contacts = check_all_contacts(state[heater]["apertures"]) | ||
state[heater]["apertures"][aperture] = sensor["contact"] | ||
current_all_contacts = check_all_contacts(state[heater]["apertures"]) | ||
if(current_all_contacts != prev_all_contacts): | ||
if(current_all_contacts): | ||
heater_notify(heater,"disable open window") | ||
log.info(f"state changed : {heater} => disable open window") | ||
else: | ||
heater_notify(heater,"set open window") | ||
log.info(f"state changed : {heater} => set open window") | ||
else: | ||
log.debug(f" no state change all_contact = {current_all_contacts}") | ||
return | ||
|
||
|
||
def mqtt_on_message(client, userdata, msg): | ||
try: | ||
topic_parts = msg.topic.split('/') | ||
if(len(topic_parts) == 2): | ||
name = topic_parts[1] | ||
if(name in topics_list): | ||
window_open(name,msg.payload) | ||
else: | ||
log.error(f"topic: {msg.topic} size not matching") | ||
except Exception as e: | ||
log.error("mqtt_on_message> Exception :%s"%e) | ||
return | ||
|
||
# -------------------- main -------------------- | ||
config = cfg.configure_log(__file__) | ||
|
||
state = {} | ||
topics_list = [] | ||
topic_to_heater = {} | ||
for name,heater in config["heatings"].items(): | ||
state[name] = {} | ||
state[name]["apertures"] = {} | ||
for aperture in heater["Apertures"]: | ||
state[name]["apertures"][aperture] = True | ||
topics_list.append(aperture) | ||
topic_to_heater[aperture] = name | ||
|
||
|
||
# -------------------- Mqtt Client -------------------- | ||
#will start a separate thread for looping | ||
clientMQTT = mqtt_start(config,mqtt_on_message,True) | ||
|
||
|
||
|
||
|
||
while(True): | ||
sleep(0.2) | ||
#The MQTT keeps looping on a thead | ||
#All there is to do here is not to exit |
Oops, something went wrong.