Skip to content

Commit

Permalink
improved logging and networking is now done via env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ede1998 committed Mar 2, 2019
1 parent 7209938 commit 83b2279
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,5 @@ layout.yaml
network.yaml
group-address-types.yaml
website/website/settings_deploy.py
config/logging.yaml

13 changes: 13 additions & 0 deletions config/logging-debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 1
formatters:
simple:
format: '%(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
root:
level: DEBUG
handlers: [console]
13 changes: 13 additions & 0 deletions config/logging-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: WARN
formatter: simple
stream: ext://sys.stdout
root:
level: WARN
handlers: [console]
3 changes: 0 additions & 3 deletions config/network-example.yaml

This file was deleted.

14 changes: 12 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
version: '3'
version: '3.4'
services:
redis:
image: "redis"
ports:
- "127.0.0.1:6379:6379"
web:
build:
context: .
dockerfile: website.dockerfile
environment:
- "REDIS_SERVER=smartknx_redis_1:6379"
- "WS_PORT=8765"
command: python manage.py runserver 0.0.0.0:80 --noreload #--settings=website.settings_deploy
volumes:
- ./config:/config
ports:
- "80:80"
- "127.0.0.1:8765:8765"
- "8765:8765"
depends_on:
- redis
knx:
build:
context: .
dockerfile: knxbus.dockerfile
network_mode: host
environment:
- "KNX_GATEWAY=192.168.140.21:3671"
- "REDIS_SERVER=localhost:6379"
- "WS_PORT=8765"
command: python main.py
volumes:
- ./config:/config
Expand Down
7 changes: 7 additions & 0 deletions general_utilities/communication/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import logging
import oyaml as yaml
import logging.config

with open('../config/logging.yaml', 'r') as f:
config = yaml.safe_load(f)
logging.config.dictConfig(config)
10 changes: 4 additions & 6 deletions general_utilities/communication/pubsub.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import aioredis
import oyaml as yaml
import logging
import os

LOGGER = logging.getLogger(__name__)

Expand All @@ -16,12 +16,10 @@ def __init__(self, position_bux_interface=True):

async def create_con_pool(self):
try:
with open('../config/network.yaml', 'r') as f:
config = yaml.safe_load(f)
url = config['redis']
self.con_pool = await aioredis.create_redis_pool(url)
address = os.environ.get('REDIS_SERVER', 'localhost:6379')
self.con_pool = await aioredis.create_redis_pool("redis://" + address)
self.is_connected = True
except OSError as e:
except (OSError, aioredis.RedisError) as e:
LOGGER.error(e)

async def psubscribe(self):
Expand Down
10 changes: 4 additions & 6 deletions general_utilities/communication/rt_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import websockets
from .converters import *
from .pubsub import RedisConnector
import oyaml as yaml
import os
from threading import Thread
import logging

Expand Down Expand Up @@ -72,18 +72,16 @@ async def redis_handler():
async def main():
global _loop
_loop = asyncio.get_running_loop()
with open('../config/network.yaml', 'r') as f:
config = yaml.safe_load(f)
ip = config['ws_host']
port = 8765
port = os.environ.get('WS_PORT', '8765')


# if we don't get a connection to redis, websockets would also return immediately
# so we need a helper coro, that keeps the loop running
async def waiter():
while True:
await asyncio.sleep(100)

ws_coro = websockets.serve(ws_handler, ip, port)
ws_coro = websockets.serve(ws_handler, '0.0.0.0', port)
redis_coro = redis_handler()
waiter_coro = waiter()

Expand Down
34 changes: 13 additions & 21 deletions knxbus/knx/bus/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, future, loop=None, group_monitor=True):
self.sequence_counter_incoming = -1

def connection_made(self, transport):
LOGGER.debug('Connection opened to knx gateway')
self.transport = transport
self.peername = self.transport.get_extra_info('peername')
self.sockname = self.transport.get_extra_info('sockname')
Expand Down Expand Up @@ -144,25 +145,16 @@ def print_message(self, message):
dst_addr = message.parse_knx_group_address(cemi.knx_destination)
elif cemi.knx_destination:
dst_addr = message.parse_knx_address(cemi.knx_destination)
if self.group_monitor:
format = ('[ chan_id: {chan_id}, seq_no: {seq_no}, message_code: {msg_code}, '
'source_addr: {src_addr}, dest_addr: {dst_addr}, tpci_type: {tpci_type}, '
'tpci_seq: {tpci_seq}, apci_type: {apci_type}, apci_data: {apci_data} ]').format(
chan_id=message.communication_channel,
seq_no=message.sequence_counter,
msg_code=CEMI_PRIMITIVES.get(cemi.message_code),
src_addr=message.parse_knx_address(cemi.knx_source),
dst_addr=dst_addr,
tpci_type=_CEMI_TPCI_TYPES.get(tpci.tpci_type),
tpci_seq=tpci.sequence,
apci_type=_CEMI_APCI_TYPES.get(apci.apci_type),
apci_data=apci.apci_data)
else:
format = ('[ chan_id: {chan_id}, seq_no: {seq_no}, message_code: {msg_code}, '
'timestamp: {timestamp}, raw_frame: {raw_frame} ]').format(
chan_id=message.communication_channel,
seq_no=message.sequence_counter,
msg_code=CEMI_PRIMITIVES.get(cemi.message_code),
timestamp=codecs.encode(cemi.additional_information.get('timestamp'), 'hex'),
raw_frame=codecs.encode(cemi.raw_frame, 'hex'))
format = ('[chan_id:{chan_id},seq_no:{seq_no},message_code:{msg_code},'
'source_addr:{src_addr},dest_addr:{dst_addr},tpci_type:{tpci_type},'
'tpci_seq:{tpci_seq},apci_type:{apci_type},apci_data:{apci_data}]').format(
chan_id=message.communication_channel,
seq_no=message.sequence_counter,
msg_code=CEMI_PRIMITIVES.get(cemi.message_code),
src_addr=message.parse_knx_address(cemi.knx_source),
dst_addr=dst_addr,
tpci_type=_CEMI_TPCI_TYPES.get(tpci.tpci_type),
tpci_seq=tpci.sequence,
apci_type=_CEMI_APCI_TYPES.get(apci.apci_type),
apci_data=apci.apci_data)
LOGGER.info(format)
1 change: 0 additions & 1 deletion knxbus/knx/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ async def monitor(self, targets=None, group_monitor_mode=False):
functools.partial(KnxBusMonitor, future, group_monitor=group_monitor_mode),
remote_addr=list(self.targets)[0])
self.bus_protocols.append(protocol)
LOGGER.debug('here i am')
await future

async def group_writer(self, target, value=0, routing=False, desc_timeout=2,
Expand Down
8 changes: 7 additions & 1 deletion knxbus/knx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from knx.communicator import KnxCommunicator
from knx.targets import Targets
from knx.misc import setup_logger
import os
import logging

LOGGER = logging.getLogger(__name__)

# asyncio requires at least Python 3.3
if sys.version_info.major < 3 or \
Expand All @@ -23,14 +27,16 @@
def start():
setup_logger(2)
loop = asyncio.get_event_loop()
ip, port = os.environ.get('KNX_GATEWAY', 'localhost:3671').split(':')
LOGGER.info('Connecting to knx gateway: (%s,%s)', ip, port)

knxcom = KnxCommunicator()
try:
# loop.run_until_complete(knxcom.group_writer(
# target=args.group_write_address,
# value=args.group_write_value))
loop.run_until_complete(knxcom.monitor(
targets=Targets('192.168.140.21', 3671).targets,
targets=Targets(ip, port).targets,
group_monitor_mode=True))
except KeyboardInterrupt:
for t in asyncio.Task.all_tasks():
Expand Down
4 changes: 2 additions & 2 deletions website/smartknx/static/smartknx/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

"use strict";

let group_states = {}
let group_states = {};

let knxSocket = new WebSocket(
'ws://' + window.location.hostname + ":8765")
'ws://' + window.location.hostname + ":" + WS_PORT);

knxSocket.onmessage = function (e) {
let msg = JSON.parse(e.data);
Expand Down
4 changes: 4 additions & 0 deletions website/smartknx/templates/smartknx/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static %}
{% load smartknx_extra %}
<!doctype html>
<html lang="en">

Expand All @@ -12,6 +13,9 @@
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<link rel="icon" href="{% static 'smartknx/favicon.ico' %}" type="image/ico">
<script type="text/javascript">
const WS_PORT = "{% get_ws_port %}";
</script>
<script src="{% static 'smartknx/websockets.js' %}" type="text/javascript"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/10.6.1/css/bootstrap-slider.min.css">
Expand Down
7 changes: 6 additions & 1 deletion website/smartknx/templatetags/smartknx_extra.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import template
import os

register = template.Library()

Expand All @@ -8,4 +9,8 @@ def classname(obj):

@register.filter
def group_address_to_tag(ga):
return "address" + str(ga).replace("/", "-")
return "address" + str(ga).replace("/", "-")

@register.simple_tag
def get_ws_port():
return os.environ.get('WS_PORT', '8765')

0 comments on commit 83b2279

Please sign in to comment.