This commit is contained in:
2025-11-30 12:53:03 +08:00
parent e782a97a2e
commit 769f15aaf2
5 changed files with 242 additions and 29 deletions

View File

@@ -0,0 +1,9 @@
```sh
# 安装 python 环境 ,安装项目
uv sync
uv run python main.py
pm2 start "uv run app.py" --name "dnake-socket-app"
```

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/me-dnake-docs",
"version": "0.0.1",
"version": "0.0.2",
"description": "",
"main": "index.js",
"basename": "/root/me-dnake-docs",
@@ -8,7 +8,7 @@
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"pub": "envision deploy ./dist -k me-dnake-docs -v 0.0.1 -u",
"pub": "envision deploy ./dist -k me-dnake-docs -v 0.0.2 -u",
"sn": "pnpm dlx shadcn@latest add "
},
"keywords": [],

48
docs/src/pages/home.mdx Normal file
View File

@@ -0,0 +1,48 @@
# home配置文档
## configuration.yaml
配置调用rest_command服务文件地址在 docker 内部根目录下,`config/configuration.yaml` 位置
添加以下内容
```yaml
rest_command:
set_floor_notify:
url: http://host.docker.internal:3001/api/router?path=floor
method: POST
content_type: application/json
```
然后配置脚本位置 `config/scripts.yaml` 文件,添加以下内容
(scripts 被 configuration.yaml 引用)
```yaml
notify_floor:
alias: "发送楼层消息"
description: "通过本地服务发送楼层通知"
sequence:
- service: rest_command.set_floor_notify
```
## 触发使用
home 可视化页面可以编辑自动化,编辑后台也可以,在 automations.yaml 文件中添加以下类似内容
```yaml
- id: '1760892271622'
alias: 打开电梯
description: 打开电梯上13楼来
triggers:
- trigger: state
entity_id:
- event.xiaomi_cn_1076466578_hub1_virtual_event_e_4_1
from:
to:
conditions:
- condition: state
entity_id: event.xiaomi_cn_1076466578_hub1_virtual_event_e_4_1
attribute: 事件名称
state: 打开电梯
actions:
- action: script.notify_floor
metadata: {}
data: {}
mode: single
```

View File

@@ -2,6 +2,7 @@
import '../styles/global.css';
const links = [
{
link: './adb/',
title: 'ADB 文档',
@@ -13,41 +14,52 @@ const links = [
{
link: './ip/',
title: 'IP 相关文档, IPTABLES 配置如何从A 局域网 ping 同B 局域网',
},
{
link: './python/',
title: 'Python 脚本运行文档',
},
{
link: './home/',
title: 'homeassistant 触发脚本文档方法',
}
];
---
<html lang='zh-CN'>
<head>
<meta charset="UTF-8" />
<meta charset='UTF-8' />
<title>My Homepage</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src='https://cdn.tailwindcss.com'></script>
</head>
<body class="bg-gray-50 min-h-screen">
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold text-gray-800 mb-8 text-center">文档导航</h1>
<div class="max-w-2xl mx-auto">
<ul class="space-y-4">
{links.map((item) => (
<li>
<a
href={item.link}
class="block p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-300 border border-gray-200 hover:border-blue-300 group"
>
<h2 class="text-xl font-semibold text-gray-800 group-hover:text-blue-600 transition-colors duration-200">
{item.title}
</h2>
<p class="text-gray-600 mt-2">点击查看详细文档</p>
<div class="flex items-center mt-3 text-blue-500 group-hover:text-blue-700">
<span class="text-sm">查看文档</span>
<svg class="w-4 h-4 ml-2 transform group-hover:translate-x-1 transition-transform duration-200" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
</svg>
</div>
</a>
</li>
))}
<body class='bg-gray-50 min-h-screen'>
<div class='container mx-auto px-4 py-8'>
<h1 class='text-3xl font-bold text-gray-800 mb-8 text-center'>文档导航</h1>
<div class='max-w-2xl mx-auto'>
<ul class='space-y-4'>
{
links.map((item) => (
<li>
<a
href={item.link}
class='block p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-300 border border-gray-200 hover:border-blue-300 group'>
<h2 class='text-xl font-semibold text-gray-800 group-hover:text-blue-600 transition-colors duration-200'>{item.title}</h2>
<p class='text-gray-600 mt-2'>点击查看详细文档</p>
<div class='flex items-center mt-3 text-blue-500 group-hover:text-blue-700'>
<span class='text-sm'>查看文档</span>
<svg
class='w-4 h-4 ml-2 transform group-hover:translate-x-1 transition-transform duration-200'
fill='none'
stroke='currentColor'
viewBox='0 0 24 24'>
<path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M9 5l7 7-7 7' />
</svg>
</div>
</a>
</li>
))
}
</ul>
</div>
</div>

144
docs/src/pages/python.mdx Normal file
View File

@@ -0,0 +1,144 @@
# python 代码示例
```sh
# 安装 python 环境 ,安装项目
uv sync
```
## 运行 python 脚本
main.py 主模块
```python
import socket
import time
def send_sip_message():
# 配置参数
target_ip = "192.168.3.3" # 设备eth1的IP转发入口
target_port = 5060 # 转发端口
mac_local_ip = "192.168.3.33"# Mac的本地IP与Via头部一致
sender_sip = "3011302" # 发送方SIP用户
sender_ip = "192.168.9.57" # 设备eth0的IP转发后源IP
receiver_sip = "3019901" # 接收方SIP用户
receiver_ip = "192.168.9.4" # 目标IP
# 生成唯一标识
call_id = f"{int(time.time())}" # 基于时间戳的Call-ID
tag = f"{int(time.time() % 1000000)}" # 随机tag
branch = f"z9hG4bK{int(time.time())}" # Via分支标识
# XML消息体业务数据
xml_body = '''<?xml version="1.0" encoding="UTF-8" ?>
<params>
<to>sip:{receiver_sip}@{receiver_ip}:5060</to>
<elev>1</elev>
<direct>2</direct>
<floor>13</floor>
<family>2</family>
<app>elev</app>
<event>appoint</event>
<event_url>/elev/appoint</event_url>
</params>'''.format(receiver_sip=receiver_sip, receiver_ip=receiver_ip)
# 计算消息体长度(字节数)
content_length = len(xml_body.encode('utf-8'))
# 构造SIP MESSAGE请求严格使用\r\n换行
sip_message = f'''MESSAGE sip:{receiver_sip}@{receiver_ip}:5060 SIP/2.0
Via: SIP/2.0/UDP {mac_local_ip}:5060;rport;branch={branch}
From: <sip:{sender_sip}@{sender_ip}:5060>;tag={tag}
To: <sip:{receiver_sip}@{receiver_ip}:5060>
Call-ID: {call_id}@{mac_local_ip}
CSeq: 20 MESSAGE
Content-Type: text/plain
Max-Forwards: 70
User-Agent: DnakeVoip v1.0
Content-Length: {content_length}
{xml_body}'''
# 替换换行符为\r\n确保符合SIP协议
sip_message = sip_message.replace('\n', '\r\n')
try:
# 创建UDP socket并发送消息
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
# 设置超时时间5秒
sock.settimeout(5)
# 发送消息
sock.sendto(sip_message.encode('utf-8'), (target_ip, target_port))
print(f"已发送SIP消息到 {target_ip}:{target_port}")
print("\n发送的消息内容")
print(sip_message)
# 尝试接收响应(可选)
try:
response, addr = sock.recvfrom(4096)
print(f"\n收到来自 {addr} 的响应:")
print(response.decode('utf-8', errors='ignore'))
except socket.timeout:
print("\n未收到响应可能对方未回复或网络延迟")
except Exception as e:
print(f"发送失败:{str(e)}")
if __name__ == "__main__":
send_sip_message()
```
app.py
```python
from fastapi import FastAPI, Query
import httpx
import asyncio
import uvicorn
from main import send_sip_message
app = FastAPI()
async def main():
"""主函数内容"""
print("执行main函数...")
# 执行发送SIP消息
send_sip_message()
return {"message": "main function executed - SIP message sent"}
async def test():
"""测试函数内容"""
print("执行test函数...")
# 在这里添加你的test函数逻辑
return {"message": "test function executed"}
@app.api_route("/api/router", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
async def router(path: str = Query(...)):
"""路由处理器"""
try:
# 根据path参数执行不同的函数
if path == "floor":
result = await main()
elif path == "test":
result = await test()
else:
return {"error": f"Unknown path: {path}"}
return {"success": True, "data": result}
except Exception as e:
return {"success": False, "error": str(e)}
@app.api_route("/", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
async def root():
"""健康检查端点"""
return {"message": "FastAPI server is running"}
if __name__ == "__main__":
# 监听所有IPv6地址的3001端口
uvicorn.run(
"app:app",
host="::", # 监听所有IPv6地址
port=3001,
reload=True
)
```