Skip to content

Commit

Permalink
<refact>: add thingtalk topic
Browse files Browse the repository at this point in the history
  • Loading branch information
hidaris committed Feb 2, 2021
1 parent 1591e89 commit e03da5d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "thingtalk"
version = "0.7.13"
version = "0.7.14"
description = "Web of Things framework, high performance, easy to learn, fast to code, ready for production"
authors = ["hidaris <[email protected]>"]
readme = "README.md"
Expand Down Expand Up @@ -40,6 +40,8 @@ mkdocs-material = { version = "^6.1.6", optional = true }
async-cron = "^1.6.2"
gmqtt = "^0.6.9"
dynaconf = "^3.1.2"
click = "^7.1.2"
rich = "^9.10.0"

[tool.poetry.dev-dependencies]
pytest = "^6.0"
Expand All @@ -49,3 +51,7 @@ black = {version = "^20.8b1", allow-prereleases = true}

[tool.poetry.extras]
docs = ["mkdocs-material"]

[tool.poetry.scripts]
hello = 'thingtalk.app:hello'

2 changes: 1 addition & 1 deletion thingtalk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .models.action import Action
from .models.event import Event
from .models.property import Property
from .app import app
from .app import app, hello
from .models.containers import SingleThing, MultipleThings
from .models.thing import Thing
from .domains.iot import Device
Expand Down
14 changes: 12 additions & 2 deletions thingtalk/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import socket
import click

from fastapi import FastAPI, APIRouter
from loguru import logger
Expand All @@ -21,8 +22,8 @@
description="Web of Things framework, high performance, easy to learn, fast to code, ready for production",
)
server = Server()
server.href_prefix = f"/things/{server._id}"
app.state.things = MultipleThings({server._id: server}, "things")
server.href_prefix = f"/things/{server.id}"
app.state.things = MultipleThings({server.id: server}, "things")

zeroconf = Zeroconf()

Expand Down Expand Up @@ -106,3 +107,12 @@ async def publish(topic: str, payload: OutMsg):

app.include_router(restapi)
app.include_router(websockets.router)


@click.command()
@click.option("--count", default=1, e=int, help="Number of greetings.")
@click.option("--name", type=str, prompt="Your name", help="The person to greet.")
def hello(count: int, name: str):
"""Simple program that greets NAME for a total of COUNT times."""
for _ in range(count):
click.echo(f"Hello, {name}!")
6 changes: 3 additions & 3 deletions thingtalk/models/thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def href_prefix(self) -> str:
return self._href_prefix

@href_prefix.setter
def href_prefix(self, prefix: str):
def href_prefix(self, prefix: str) -> None:
"""
Set the prefix of any hrefs associated with this thing.
prefix -- the prefix
Expand All @@ -203,7 +203,7 @@ def ui_href(self) -> str:
return self._ui_href

@ui_href.setter
def ui_href(self, href):
def ui_href(self, href) -> None:
"""
Set the href of this thing's custom UI.
href -- the href
Expand All @@ -227,7 +227,7 @@ def title(self):
return self._title

@title.setter
def title(self, title):
def title(self, title) -> None:
"""
Set the new title of this thing.
title -- the new title
Expand Down
4 changes: 4 additions & 0 deletions thingtalk/toolkits/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ 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}")
if topic == 'thingtalk/bridge/':
pass
if topic == 'thingtalk/+/config':
pass

def on_disconnect(self, client: Client, packet, exc=None):
logger.info(f"[DISCONNECTED {client._client_id}]")
Expand Down
50 changes: 50 additions & 0 deletions thingtalk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import asyncio

from contextlib import contextmanager
from loguru import logger
from threading import Thread


Expand Down Expand Up @@ -88,3 +89,52 @@ def run_forever(loop):
finally:
loop.call_soon_threadsafe(loop.stop)
thread.join()


class PortDetect:
"""
扫描本机的可用端口。
eg:
pd = PortDetect()
pd()
print pd.avaliable
==>
50000
默认会从本机的50000端口开始扫描,依次递增到55000,如果发现可用的端口则把端口号赋值给
类的成员变量avaliable,如果扫描完成后avaliable的值是0,说明所有端口都被占用了。
也可以单独使用check_port方法,通过传入端口的方式来检查端口是否被占用。
eg:
pd = PortDetect()
pd.check_port(9999)
==>
True
"""
def __init__(self, range_s=50000, range_e=55000):
self.range_s = range_s
self.range_e = range_e
self.available = 0

def check_port(self, port):
"""
单独检查端口是否可用
:param port: int, 端口号
:return: Bool, True表示端口可用,False表示端口不可用
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex(("127.0.0.1", int(port)))
if result == 0:
return False
else:
return True

def __call__(self, *args, **kwargs):
for x in range(self.range_s, self.range_e):
rst = self.check_port(x)
if rst:
self.available = x
break
else:
logger.error("all port used")

0 comments on commit e03da5d

Please sign in to comment.