This commit is contained in:
quantulr
2024-01-22 17:30:06 +08:00
commit fcd4bb2d9d
13 changed files with 1143 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/hik_push.iml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Poetry (hik_push)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Poetry (hik_push)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/hik_push.iml" filepath="$PROJECT_DIR$/.idea/hik_push.iml" />
</modules>
</component>
</project>

0
README.md Normal file
View File

0
hik_push/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

57
hik_push/main.py Normal file
View File

@ -0,0 +1,57 @@
import asyncio
import json
from typing import Dict
from redis import asyncio as aioredis
import uvicorn
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
async def add_stream(data: dict):
redis = await aioredis.from_url("redis://localhost", db=0)
await redis.xadd("hik-event", {"event": json.dumps(data)})
await redis.close()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.post("/eventRcv")
async def event_rcv(data: dict, background_tasks: BackgroundTasks):
background_tasks.add_task(add_stream, data)
return {"data": data}
@app.on_event("startup")
async def background_task():
redis = await aioredis.from_url("redis://localhost", db=0)
# await redis.xgroup_create(groupname="consumer-group", name="hik-event", mkstream=True)
while True:
# result = await redis.xread(streams={"hik-event": 0})
result = await redis.xreadgroup(groupname="consumer-group", consumername="consumer",
streams={"hik-event": 0},
)
print(result)
if len(result[0][1]) == 0:
print("wuyou")
# await redis.close()
continue
id = result[0][1][0][0].decode("utf-8")
await redis.xack("hik-event", "consumer-group", id)
# print(result[0][1][0][0].decode("utf-8"))
print(result[0][1][0][1][b'event'].decode("utf-8"))
await asyncio.sleep(1)
await redis.close()
def run_app():
uvicorn.run("main:app", host="0.0.0.0", port=8000)
if __name__ == "__main__":
run_app()

1029
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

20
pyproject.toml Normal file
View File

@ -0,0 +1,20 @@
[tool.poetry]
name = "hik-push"
version = "0.1.0"
description = ""
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
fastapi = { extras = ["all"], version = "^0.109.0" }
redis = "^5.0.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
hik-push = 'hik_push.main:run_app'

0
tests/__init__.py Normal file
View File