Skip to content

Commit

Permalink
<feat>: update containers
Browse files Browse the repository at this point in the history
  • Loading branch information
hidaris committed Feb 7, 2021
1 parent 1933251 commit 1038f0e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion settings.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
MQTT_HOST = '10.10.10.8'
MQTT_HOST = '127.0.0.1'
MQTT_USERNAME = 'longan'
MQTT_PASSWORD = 'longan'
20 changes: 8 additions & 12 deletions thingtalk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from zeroconf import Zeroconf, ServiceInfo
from functools import partial

from config import settings
from .routers.mqtt import ThingMqtt

from .routers.mqtt import mqtt
from .utils import get_ip
from .models.thing import Server
from .models.containers import MultipleThings
Expand All @@ -22,7 +22,8 @@
)
server = Server()
server.href_prefix = f"/things/{server.id}"
app.state.things = MultipleThings({server.id: server}, "things")
# app.state.things = MultipleThings({server.id: server}, "things")
app.state.things = MultipleThings({}, "things")

zeroconf = Zeroconf()

Expand Down Expand Up @@ -54,18 +55,13 @@ async def stop_mdns():
zeroconf.close()


username = settings.MQTT_USERNAME
password = settings.MQTT_PASSWORD
host = settings.MQTT_HOST

mqtt = ThingMqtt(host, "1883", username=username, password=password)


@app.on_event("startup")
async def startup():
logger.debug(app.state.mode)
app.state.mode = "gateway"
await mqtt.connect()
await mqtt.publish("thingtalk/test", "online")
await mqtt.set_app(app)
await mqtt.publish("thingtalk/bridge/state", "online")
await app.state.things.add_thing(server)


@app.on_event("shutdown")
Expand Down
6 changes: 6 additions & 0 deletions thingtalk/models/containers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .event import ThingPairedEvent, ThingRemovedEvent
from .thing import Thing
from ..routers.mqtt import mqtt


class SingleThing:
Expand All @@ -24,6 +25,9 @@ def get_name(self):
"""Get the mDNS server name."""
return self.thing.title

async def add_thing(self, thing: Thing):
await mqtt.publish(f"things/{thing.id}/config", thing.as_thing_description())


class MultipleThings:
"""A container for multiple things."""
Expand Down Expand Up @@ -56,6 +60,8 @@ def get_name(self):
async def add_thing(self, thing: Thing):
self.things.update({thing.id: thing})
await thing.subscribe_broadcast()
things = [thing.as_thing_description() for _, thing in self.get_things()]
await mqtt.publish(f"thingtalk/things", things)

# await self.server.add_event(ThingPairedEvent({
# '@type': list(thing._type),
Expand Down
2 changes: 2 additions & 0 deletions thingtalk/models/thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ def as_thing_description(self):
if self._type:
thing["@type"] = list(self._type)

thing["href"] = self.href

return thing

@property
Expand Down
17 changes: 17 additions & 0 deletions thingtalk/routers/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gmqtt

from loguru import logger
from config import settings
from ..toolkits.mqtt import Mqtt, Client


Expand All @@ -14,6 +15,15 @@ def on_connect(self, client: Client, flags, rc, properties):
async def on_message(self, client: Client, topic, payload, qos, properties):
logger.info(
f"[RECV MSG {client._client_id}] TOPIC: {topic} PAYLOAD: {payload} QOS: {qos} PROPERTIES: {properties}")
topic_words = topic.split("/")

if topic == 'thingtalk/bridge/state':
logger.debug(payload)

if client.app.state.mode == "gateway":
logger.debug("gateway")
if topic[2] == 'config':
logger.debug(payload)

def on_disconnect(self, client: Client, packet, exc=None):
logger.info(f"[DISCONNECTED {client._client_id}]")
Expand All @@ -30,3 +40,10 @@ def on_subscribe(self, client: Client, mid, qos, properties):
client.resubscribe(subscription)
logger.info('[SUBSCRIBED {}] mid {}, QOS: {}, properties {}'.format(
client._client_id, mid, granted_qos, properties))


username = settings.MQTT_USERNAME
password = settings.MQTT_PASSWORD
host = settings.MQTT_HOST

mqtt = ThingMqtt(host, "1883", username=username, password=password)
2 changes: 1 addition & 1 deletion thingtalk/routers/things.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def get_things(request: Request) -> UJSONResponse:
descriptions = []
for idx, thing in tuple(things.get_things()):
description = thing.as_thing_description()
description["href"] = thing.href

description["links"].append({
"rel": "alternate",
"href": f"{get_ws_href(request)}{thing.href}",
Expand Down
8 changes: 6 additions & 2 deletions thingtalk/toolkits/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ def on_connect(self, client: Client, flags, rc, properties):
async def on_message(self, client: Client, topic, payload, qos, properties):
logger.info(
f"[RECV MSG {client._client_id}] TOPIC: {topic} PAYLOAD: {payload} QOS: {qos} PROPERTIES: {properties}")
logger.debug("on message")
if topic == 'thingtalk/bridge/':
pass
if topic == 'thingtalk/+/config':
pass

if client.app.state.mode == "gateway":
logger.debug("gateway")
if topic == 'thingtalk/+/config':
pass

def on_disconnect(self, client: Client, packet, exc=None):
logger.info(f"[DISCONNECTED {client._client_id}]")
Expand Down

0 comments on commit 1038f0e

Please sign in to comment.