diff --git a/settings.toml b/settings.toml index 5c837ab..5feb088 100644 --- a/settings.toml +++ b/settings.toml @@ -1,3 +1,3 @@ -MQTT_HOST = '10.10.10.8' +MQTT_HOST = '127.0.0.1' MQTT_USERNAME = 'longan' MQTT_PASSWORD = 'longan' \ No newline at end of file diff --git a/thingtalk/app.py b/thingtalk/app.py index 1d6433e..89d4e14 100644 --- a/thingtalk/app.py +++ b/thingtalk/app.py @@ -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 @@ -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() @@ -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") diff --git a/thingtalk/models/containers.py b/thingtalk/models/containers.py index 0dd9e93..e379159 100644 --- a/thingtalk/models/containers.py +++ b/thingtalk/models/containers.py @@ -1,5 +1,6 @@ from .event import ThingPairedEvent, ThingRemovedEvent from .thing import Thing +from ..routers.mqtt import mqtt class SingleThing: @@ -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.""" @@ -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), diff --git a/thingtalk/models/thing.py b/thingtalk/models/thing.py index a7d4d18..8ba7a9b 100644 --- a/thingtalk/models/thing.py +++ b/thingtalk/models/thing.py @@ -167,6 +167,8 @@ def as_thing_description(self): if self._type: thing["@type"] = list(self._type) + thing["href"] = self.href + return thing @property diff --git a/thingtalk/routers/mqtt.py b/thingtalk/routers/mqtt.py index 0386d3f..3041669 100644 --- a/thingtalk/routers/mqtt.py +++ b/thingtalk/routers/mqtt.py @@ -1,6 +1,7 @@ import gmqtt from loguru import logger +from config import settings from ..toolkits.mqtt import Mqtt, Client @@ -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}]") @@ -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) diff --git a/thingtalk/routers/things.py b/thingtalk/routers/things.py index 1724160..4e490d2 100644 --- a/thingtalk/routers/things.py +++ b/thingtalk/routers/things.py @@ -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}", diff --git a/thingtalk/toolkits/mqtt.py b/thingtalk/toolkits/mqtt.py index 6f33975..ca007bd 100644 --- a/thingtalk/toolkits/mqtt.py +++ b/thingtalk/toolkits/mqtt.py @@ -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}]")