5 Commits

Author SHA1 Message Date
nhkefus
885a7a3692 refactor: improve memory safety in web logging and add connection check for MQTT power reporting 2026-04-08 23:16:13 +08:00
nhkefus
8374484d0a 去除web后台没数据时的默认值 2025-04-28 15:57:54 +08:00
Your Name
c3d9db335e 调试用 2025-04-25 11:46:19 +08:00
nhkefus
3470d373eb 尝试增加一个外网mqtt客户端 2025-04-24 13:00:44 +08:00
Your Name
3caf76f4c6 开发基于udp的app控制协议 2025-04-23 23:32:04 +08:00
19 changed files with 1231 additions and 81 deletions

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

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

679
.idea/caches/deviceStreaming.xml generated Normal file
View File

@@ -0,0 +1,679 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DeviceStreaming">
<option name="deviceSelectionList">
<list>
<PersistentDeviceSelectionData>
<option name="api" value="27" />
<option name="brand" value="DOCOMO" />
<option name="codename" value="F01L" />
<option name="id" value="F01L" />
<option name="labId" value="google" />
<option name="manufacturer" value="FUJITSU" />
<option name="name" value="F-01L" />
<option name="screenDensity" value="360" />
<option name="screenX" value="720" />
<option name="screenY" value="1280" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OnePlus" />
<option name="codename" value="OP535DL1" />
<option name="id" value="OP535DL1" />
<option name="labId" value="google" />
<option name="manufacturer" value="OnePlus" />
<option name="name" value="CPH2409" />
<option name="screenDensity" value="401" />
<option name="screenX" value="1080" />
<option name="screenY" value="2412" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OnePlus" />
<option name="codename" value="OP5552L1" />
<option name="id" value="OP5552L1" />
<option name="labId" value="google" />
<option name="manufacturer" value="OnePlus" />
<option name="name" value="CPH2415" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2412" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="OPPO" />
<option name="codename" value="OP573DL1" />
<option name="id" value="OP573DL1" />
<option name="labId" value="google" />
<option name="manufacturer" value="OPPO" />
<option name="name" value="CPH2557" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="28" />
<option name="brand" value="DOCOMO" />
<option name="codename" value="SH-01L" />
<option name="id" value="SH-01L" />
<option name="labId" value="google" />
<option name="manufacturer" value="SHARP" />
<option name="name" value="AQUOS sense2 SH-01L" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2160" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a15" />
<option name="id" value="a15" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A15" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a15x" />
<option name="id" value="a15x" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A15 5G" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a16x" />
<option name="id" value="a16x" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A16 5G" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="a35x" />
<option name="id" value="a35x" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="A35" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="31" />
<option name="brand" value="samsung" />
<option name="codename" value="a51" />
<option name="id" value="a51" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy A51" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="akita" />
<option name="id" value="akita" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="motorola" />
<option name="codename" value="arcfox" />
<option name="id" value="arcfox" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="razr plus 2024" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1080" />
<option name="screenY" value="1272" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="motorola" />
<option name="codename" value="austin" />
<option name="id" value="austin" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="moto g 5G (2022)" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="samsung" />
<option name="codename" value="b0q" />
<option name="id" value="b0q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S22 Ultra" />
<option name="screenDensity" value="600" />
<option name="screenX" value="1440" />
<option name="screenY" value="3088" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="32" />
<option name="brand" value="google" />
<option name="codename" value="bluejay" />
<option name="id" value="bluejay" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 6a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="caiman" />
<option name="id" value="caiman" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro" />
<option name="screenDensity" value="360" />
<option name="screenX" value="960" />
<option name="screenY" value="2142" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="comet" />
<option name="default" value="true" />
<option name="id" value="comet" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro Fold" />
<option name="screenDensity" value="390" />
<option name="screenX" value="2076" />
<option name="screenY" value="2152" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="comet" />
<option name="default" value="true" />
<option name="id" value="comet" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro Fold" />
<option name="screenDensity" value="390" />
<option name="screenX" value="2076" />
<option name="screenY" value="2152" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="29" />
<option name="brand" value="samsung" />
<option name="codename" value="crownqlteue" />
<option name="id" value="crownqlteue" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Note9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2220" />
<option name="screenY" value="1080" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="dm2q" />
<option name="id" value="dm2q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="S23 Plus" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="dm3q" />
<option name="id" value="dm3q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S23 Ultra" />
<option name="screenDensity" value="600" />
<option name="screenX" value="1440" />
<option name="screenY" value="3088" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="e1q" />
<option name="default" value="true" />
<option name="id" value="e1q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S24" />
<option name="screenDensity" value="480" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="e3q" />
<option name="id" value="e3q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S24 Ultra" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1440" />
<option name="screenY" value="3120" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="eos" />
<option name="id" value="eos" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Eos" />
<option name="screenDensity" value="320" />
<option name="screenX" value="384" />
<option name="screenY" value="384" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="felix" />
<option name="id" value="felix" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="felix" />
<option name="id" value="felix" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="felix_camera" />
<option name="id" value="felix_camera" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Fold (Camera-enabled)" />
<option name="screenDensity" value="420" />
<option name="screenX" value="2208" />
<option name="screenY" value="1840" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="motorola" />
<option name="codename" value="fogona" />
<option name="id" value="fogona" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="moto g play - 2024" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="g0q" />
<option name="id" value="g0q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-S906U1" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gta9pwifi" />
<option name="id" value="gta9pwifi" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-X210" />
<option name="screenDensity" value="240" />
<option name="screenX" value="1200" />
<option name="screenY" value="1920" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gts7xllite" />
<option name="id" value="gts7xllite" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-T738U" />
<option name="screenDensity" value="340" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="samsung" />
<option name="codename" value="gts8uwifi" />
<option name="formFactor" value="Tablet" />
<option name="id" value="gts8uwifi" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S8 Ultra" />
<option name="screenDensity" value="320" />
<option name="screenX" value="1848" />
<option name="screenY" value="2960" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gts8wifi" />
<option name="formFactor" value="Tablet" />
<option name="id" value="gts8wifi" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S8" />
<option name="screenDensity" value="274" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="gts9fe" />
<option name="id" value="gts9fe" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Tab S9 FE 5G" />
<option name="screenDensity" value="280" />
<option name="screenX" value="1440" />
<option name="screenY" value="2304" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="husky" />
<option name="id" value="husky" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8 Pro" />
<option name="screenDensity" value="390" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="motorola" />
<option name="codename" value="java" />
<option name="id" value="java" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="G20" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="komodo" />
<option name="id" value="komodo" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro XL" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="komodo" />
<option name="id" value="komodo" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9 Pro XL" />
<option name="screenDensity" value="360" />
<option name="screenX" value="1008" />
<option name="screenY" value="2244" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="lynx" />
<option name="id" value="lynx" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 7a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="motorola" />
<option name="codename" value="maui" />
<option name="id" value="maui" />
<option name="labId" value="google" />
<option name="manufacturer" value="Motorola" />
<option name="name" value="moto g play - 2023" />
<option name="screenDensity" value="280" />
<option name="screenX" value="720" />
<option name="screenY" value="1600" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="o1q" />
<option name="id" value="o1q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S21" />
<option name="screenDensity" value="421" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="31" />
<option name="brand" value="google" />
<option name="codename" value="oriole" />
<option name="id" value="oriole" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 6" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="panther" />
<option name="id" value="panther" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 7" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="q5q" />
<option name="id" value="q5q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Z Fold5" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1812" />
<option name="screenY" value="2176" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="q6q" />
<option name="id" value="q6q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy Z Fold6" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1856" />
<option name="screenY" value="2160" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="google" />
<option name="codename" value="r11" />
<option name="formFactor" value="Wear OS" />
<option name="id" value="r11" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Watch" />
<option name="screenDensity" value="320" />
<option name="screenX" value="384" />
<option name="screenY" value="384" />
<option name="type" value="WEAR_OS" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="r11q" />
<option name="id" value="r11q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-S711U" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="30" />
<option name="brand" value="google" />
<option name="codename" value="redfin" />
<option name="id" value="redfin" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 5" />
<option name="screenDensity" value="440" />
<option name="screenX" value="1080" />
<option name="screenY" value="2340" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="shiba" />
<option name="id" value="shiba" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 8" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="t2q" />
<option name="id" value="t2q" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="Galaxy S21 Plus" />
<option name="screenDensity" value="394" />
<option name="screenX" value="1080" />
<option name="screenY" value="2400" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="33" />
<option name="brand" value="google" />
<option name="codename" value="tangorpro" />
<option name="formFactor" value="Tablet" />
<option name="id" value="tangorpro" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel Tablet" />
<option name="screenDensity" value="320" />
<option name="screenX" value="1600" />
<option name="screenY" value="2560" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="tegu" />
<option name="id" value="tegu" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9a" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="google" />
<option name="codename" value="tokay" />
<option name="default" value="true" />
<option name="id" value="tokay" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="35" />
<option name="brand" value="google" />
<option name="codename" value="tokay" />
<option name="default" value="true" />
<option name="id" value="tokay" />
<option name="labId" value="google" />
<option name="manufacturer" value="Google" />
<option name="name" value="Pixel 9" />
<option name="screenDensity" value="420" />
<option name="screenX" value="1080" />
<option name="screenY" value="2424" />
</PersistentDeviceSelectionData>
<PersistentDeviceSelectionData>
<option name="api" value="34" />
<option name="brand" value="samsung" />
<option name="codename" value="xcover7" />
<option name="id" value="xcover7" />
<option name="labId" value="google" />
<option name="manufacturer" value="Samsung" />
<option name="name" value="SM-G556B" />
<option name="screenDensity" value="450" />
<option name="screenX" value="1080" />
<option name="screenY" value="2408" />
</PersistentDeviceSelectionData>
</list>
</option>
</component>
</project>

View File

@@ -0,0 +1,21 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HtmlUnknownTag" enabled="true" level="WARNING" enabled_by_default="true">
<option name="myValues">
<value>
<list size="7">
<item index="0" class="java.lang.String" itemvalue="nobr" />
<item index="1" class="java.lang.String" itemvalue="noembed" />
<item index="2" class="java.lang.String" itemvalue="comment" />
<item index="3" class="java.lang.String" itemvalue="noscript" />
<item index="4" class="java.lang.String" itemvalue="embed" />
<item index="5" class="java.lang.String" itemvalue="script" />
<item index="6" class="java.lang.String" itemvalue="h5" />
</list>
</value>
</option>
<option name="myCustomValuesEnabled" value="true" />
</inspection_tool>
</profile>
</component>

View File

@@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="FlashPlus_v1.0.10.msi">
<CLASSES>
<root url="jar://$PROJECT_DIR$/doc/FlashPlus_v1.0.10.msi.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

9
.idea/libraries/mkfatimg.xml generated Normal file
View File

@@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="mkfatimg">
<CLASSES>
<root url="jar://$PROJECT_DIR$/mico-os/libraries/filesystem/FatFs/ver0_11/doc/img/mkfatimg.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

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

@@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</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/zTC1.iml" filepath="$PROJECT_DIR$/.idea/zTC1.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

BIN
TC1/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -34,7 +34,8 @@ $(NAME)_SOURCES := main.c\
user_power.c\ user_power.c\
timed_task/timed_task.c\ timed_task/timed_task.c\
http_server/web_log.c\ http_server/web_log.c\
http_server/app_httpd.c http_server/app_httpd.c\
udp_server/udp_server.c
$(NAME)_COMPONENTS := protocols/SNTP\ $(NAME)_COMPONENTS := protocols/SNTP\
protocols/mqtt\ protocols/mqtt\

View File

@@ -438,20 +438,6 @@
<th>循环</th> <th>循环</th>
<th>操作</th> <th>操作</th>
</tr> </tr>
<tr>
<td>02-15 07:11</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td><a>删除</a></td>
</tr>
<tr>
<td>2020-02-15<br>07:11:08</td>
<td>1</td>
<td>0</td>
<td>6</td>
<td><a>删除</a></td>
</tr>
</table> </table>
</div> </div>
@@ -610,23 +596,23 @@
<table class="mdl-data-table mdl-js-data-table"> <table class="mdl-data-table mdl-js-data-table">
<tr> <tr>
<th>版本</th> <th>版本</th>
<th id="info_version">v1.0.33</th> <th id="info_version"></th>
</tr> </tr>
<tr> <tr>
<td>IP</td> <td>IP</td>
<td id="info_ip">192.168.33.222</td> <td id="info_ip"></td>
</tr> </tr>
<tr> <tr>
<td>子网掩码</td> <td>子网掩码</td>
<td id="info_mask">255.255.255.0</td> <td id="info_mask"></td>
</tr> </tr>
<tr> <tr>
<td>网关</td> <td>网关</td>
<td id="info_gateway">192.168.33.1</td> <td id="info_gateway"></td>
</tr> </tr>
<tr> <tr>
<td>启动时间</td> <td>启动时间</td>
<td id="uptime">10:13:43</td> <td id="uptime"></td>
</tr> </tr>
</table> </table>
<div class="mdl-card__actions mdl-card--border" style="display: flex; align-items: center; flex-wrap: wrap;"> <div class="mdl-card__actions mdl-card--border" style="display: flex; align-items: center; flex-wrap: wrap;">
@@ -638,7 +624,7 @@
</svg> </svg>
</i> </i>
<!-- <span>2020-02-22</span> --> <!-- <span>2020-02-22</span> -->
<a class="mdl-button" id="st-date">2020-02-22</a> <a class="mdl-button" id="st-date"></a>
</div> </div>
</div> </div>

View File

@@ -20,13 +20,16 @@ void WebLog(const char *M, ...);
extern LogRecord log_record; extern LogRecord log_record;
extern char* LOG_TMP; extern char* LOG_TMP;
extern time_t LOG_NOW; extern time_t LOG_NOW;
#define web_log(N, M, ...) \ #define web_log(N, M, ...) do { \
LOG_TMP = (char*)malloc(sizeof(char)*LOG_LEN); \ char* _wl_tmp = (char*)malloc(sizeof(char)*LOG_LEN); \
LOG_NOW = time(NULL) + 28800; \ if (_wl_tmp) { \
strftime(LOG_TMP, TIME_LEN, "[%Y-%m-%d %H:%M:%S]", localtime(&LOG_NOW)); \ time_t _wl_now = time(NULL) + 28800; \
LOG_TMP[TIME_LEN - 1] = ' '; \ strftime(_wl_tmp, TIME_LEN, "[%Y-%m-%d %H:%M:%S]", localtime(&_wl_now)); \
snprintf(LOG_TMP + TIME_LEN, LOG_LEN - TIME_LEN, "["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__); \ _wl_tmp[TIME_LEN - 1] = ' '; \
SetLogRecord(&log_record, LOG_TMP); \ snprintf(_wl_tmp + TIME_LEN, LOG_LEN - TIME_LEN, "[" N " %s:%d] " M, SHORT_FILE, __LINE__, ##__VA_ARGS__); \
SetLogRecord(&log_record, _wl_tmp); \
} \
} while(0)
#define web_log0(N, M, ...) WebLog("["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__) #define web_log0(N, M, ...) WebLog("["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__)

View File

@@ -10,7 +10,9 @@
#include "user_wifi.h" #include "user_wifi.h"
#include "time_server/user_rtc.h" #include "time_server/user_rtc.h"
#include "user_power.h" #include "user_power.h"
#include "mqtt_server/user_mqtt_client.h"
#include "http_server/app_httpd.h" #include "http_server/app_httpd.h"
#include "udp_server/udp_server.h"
#include "timed_task/timed_task.h" #include "timed_task/timed_task.h"
char rtc_init = 0; //sntp校时成功标志位 char rtc_init = 0; //sntp校时成功标志位
@@ -112,7 +114,9 @@ void schedule_p_count_task(mico_thread_arg_t arg) {
void reportMqttPowerInfoThread() { void reportMqttPowerInfoThread() {
while (1) { while (1) {
if (UserMqttIsConnect()) {
UserMqttHassPower(); UserMqttHassPower();
}
int freq = user_config->mqtt_report_freq; int freq = user_config->mqtt_report_freq;
if (freq == 0) { if (freq == 0) {
@@ -189,7 +193,10 @@ int application_start(void) {
require_noerr(err, exit); require_noerr(err, exit);
PowerInit(); PowerInit();
AppHttpdStart(); // start http server thread AppHttpdStart(); // start http server thread
// udp_server_start();
//if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
// UserMqttInit();
// }
UserLedSet(user_config->power_led_enabled); UserLedSet(user_config->power_led_enabled);
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count", err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count",

View File

@@ -36,10 +36,18 @@ typedef struct {
uint32_t datalen; uint32_t datalen;
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t; } mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;
typedef struct {
Client client;
Network network;
// 其他状态、配置参数……
} mqtt_context_t;
static void MqttClientThread(mico_thread_arg_t arg); static void MqttClientThread(mico_thread_arg_t arg);
static void MessageArrived(MessageData *md); static void MessageArrived(MessageData *md);
static void MqttClientThread2(mico_thread_arg_t arg);
static OSStatus static OSStatus
MqttMsgPublish(Client *c, const char *topic, char qos, char retained, const unsigned char *msg, MqttMsgPublish(Client *c, const char *topic, char qos, char retained, const unsigned char *msg,
uint32_t msg_len); uint32_t msg_len);
@@ -49,13 +57,15 @@ OSStatus UserRecvHandler(void *arg);
void ProcessHaCmd(char *cmd); void ProcessHaCmd(char *cmd);
bool isconnect = false; bool isconnect = false;
bool isconnect2 = false;
mico_queue_t mqtt_msg_send_queue = NULL; mico_queue_t mqtt_msg_send_queue = NULL;
mico_queue_t mqtt_msg_send_queue2 = NULL;
Client c; // mqtt client object
Network n; // socket network for mqtt client
volatile bool mqtt_thread_should_exit = false; volatile bool mqtt_thread_should_exit = false;
static bool mqtt_initialized = false;
static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */ static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */
//static mico_timed_event_t mqtt_client_send_event; //static mico_timed_event_t mqtt_client_send_event;
char topic_state[MAX_MQTT_TOPIC_SIZE]; char topic_state[MAX_MQTT_TOPIC_SIZE];
@@ -63,6 +73,8 @@ char topic_set[MAX_MQTT_TOPIC_SIZE];
mico_timer_t timer_handle; mico_timer_t timer_handle;
static char timer_status = 0; static char timer_status = 0;
mico_timer_t timer_handle2;
static char timer_status_2 = 0;
void UserMqttTimerFunc(void *arg) { void UserMqttTimerFunc(void *arg) {
LinkStatusTypeDef LinkStatus; LinkStatusTypeDef LinkStatus;
@@ -99,11 +111,47 @@ void UserMqttTimerFunc(void *arg) {
} }
} }
void UserMqttTimerFunc2(void *arg) {
LinkStatusTypeDef LinkStatus;
micoWlanGetLinkStatus(&LinkStatus);
if (LinkStatus.is_connected != 1) {
mico_stop_timer(&timer_handle2);
return;
}
if (mico_rtos_is_queue_empty(&mqtt_msg_send_queue2)) {
switch (timer_status_2) {
case 0:
UserMqttHassAutoLed();
UserMqttHassAutoTotalSocket();
UserMqttHassAutoChildLock();
UserMqttHassAutoRebootButton();
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
UserMqttHassAuto(timer_status_2);
break;
case 7:
UserMqttHassAutoPower();
break;
default:
mico_stop_timer(&timer_handle2);
break;
}
timer_status_2++;
}
}
OSStatus UserMqttDeInit(void) { OSStatus UserMqttDeInit(void) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
// 1. 请求线程退出 // 1. 请求线程退出
mqtt_thread_should_exit = true; mqtt_thread_should_exit = true;
mqtt_initialized = false;
return err; return err;
} }
@@ -112,6 +160,11 @@ OSStatus UserMqttDeInit(void) {
OSStatus UserMqttInit(void) { OSStatus UserMqttInit(void) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if (mqtt_initialized) {
mqtt_log("MQTT already initialized, skip re-init.");
return kNoErr;
}
sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1); sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac); sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac);
//TODO size:0x800 //TODO size:0x800
@@ -127,17 +180,39 @@ OSStatus UserMqttInit(void) {
MAX_MQTT_SEND_QUEUE_SIZE); MAX_MQTT_SEND_QUEUE_SIZE);
require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue err=%d.", err)); require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue err=%d.", err));
/* create mqtt msg send queue 2 */
err = mico_rtos_init_queue(&mqtt_msg_send_queue2, "mqtt_msg_send_queue2",
sizeof(p_mqtt_send_msg_t),
MAX_MQTT_SEND_QUEUE_SIZE);
require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue2 err=%d.", err));
/* start mqtt client */ /* start mqtt client */
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client", mqtt_context_t *ctx1 = malloc(sizeof(mqtt_context_t));
(mico_thread_function_t) MqttClientThread, memset(ctx1, 0, sizeof(mqtt_context_t));
mqtt_thread_stack_size, 0);
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client_1",
(mico_thread_function_t)MqttClientThread,
mqtt_thread_stack_size, ctx1);
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread."); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread.");
// 第二个客户端
mqtt_context_t *ctx2 = malloc(sizeof(mqtt_context_t));
memset(ctx2, 0, sizeof(mqtt_context_t));
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client_2",
(mico_thread_function_t)MqttClientThread2,
mqtt_thread_stack_size, ctx2);
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread2.");
/* Create a worker thread for user handling MQTT data event */ /* Create a worker thread for user handling MQTT data event */
err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY,
0x800, 5); 0x1000, 5);
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread."); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread.");
mqtt_initialized = true;
exit: exit:
if (kNoErr != err)mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr); if (kNoErr != err)mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr);
return err; return err;
@@ -190,10 +265,15 @@ void registerMqttEvents(void) {
timer_status = 0; timer_status = 0;
mico_start_timer(&timer_handle); mico_start_timer(&timer_handle);
} }
void registerMqttEvents2(void) {
timer_status_2 = 0;
mico_start_timer(&timer_handle2);
}
void MqttClientThread(mico_thread_arg_t arg) { void MqttClientThread(mico_thread_arg_t arg) {
OSStatus err = kUnknownErr; OSStatus err = kUnknownErr;
mqtt_context_t *ctx = (mqtt_context_t *)arg;
// 后续一直用 ctx->client 和 ctx->network
int rc = -1; int rc = -1;
fd_set readfds; fd_set readfds;
struct timeval t = {0, MQTT_YIELD_TMIE * 1000}; struct timeval t = {0, MQTT_YIELD_TMIE * 1000};
@@ -207,8 +287,8 @@ void MqttClientThread(mico_thread_arg_t arg) {
mqtt_log("MQTT client thread started..."); mqtt_log("MQTT client thread started...");
memset(&c, 0, sizeof(c)); memset(&ctx->client, 0, sizeof(ctx->client));
memset(&n, 0, sizeof(n)); memset(&ctx->network, 0, sizeof(ctx->network));
/* create msg send queue event fd */ /* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue); msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
@@ -234,7 +314,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
continue; continue;
} }
rc = NewNetwork(&n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings); rc = NewNetwork(&ctx->network, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings);
if (rc == MQTT_SUCCESS) break; if (rc == MQTT_SUCCESS) break;
//mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc); //mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc);
@@ -242,7 +322,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
/* 2. init mqtt client */ /* 2. init mqtt client */
//c.heartbeat_retry_max = 2; //c.heartbeat_retry_max = 2;
rc = MQTTClientInit(&c, &n, MQTT_CMD_TIMEOUT); rc = MQTTClientInit(&ctx->client, &ctx->network, MQTT_CMD_TIMEOUT);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err."); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err.");
mqtt_log("MQTT client init success!"); mqtt_log("MQTT client init success!");
@@ -256,7 +336,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE; connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
connectData.cleansession = 1; connectData.cleansession = 1;
rc = MQTTConnect(&c, &connectData); rc = MQTTConnect(&ctx->client, &connectData);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err."); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
mqtt_log("MQTT client connect success!"); mqtt_log("MQTT client connect success!");
@@ -264,7 +344,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
UserLedSet(RelayOut() && user_config->power_led_enabled); UserLedSet(RelayOut() && user_config->power_led_enabled);
/* 4. mqtt client subscribe */ /* 4. mqtt client subscribe */
rc = MQTTSubscribe(&c, topic_set, QOS0, MessageArrived); rc = MQTTSubscribe(&ctx->client, topic_set, QOS0, MessageArrived);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");mqtt_log( require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");mqtt_log(
"MQTT client subscribe success! recv_topic=[%s].", topic_set); "MQTT client subscribe success! recv_topic=[%s].", topic_set);
/*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/ /*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/
@@ -279,6 +359,8 @@ void MqttClientThread(mico_thread_arg_t arg) {
UserMqttSendTotalSocketState(); UserMqttSendTotalSocketState();
UserMqttSendChildLockState(); UserMqttSendChildLockState();
mico_stop_timer(&timer_handle);
mico_deinit_timer(&timer_handle);
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg); mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
registerMqttEvents(); registerMqttEvents();
/* 5. client loop for recv msg && keepalive */ /* 5. client loop for recv msg && keepalive */
@@ -286,13 +368,13 @@ void MqttClientThread(mico_thread_arg_t arg) {
isconnect = true; isconnect = true;
no_mqtt_msg_exchange = true; no_mqtt_msg_exchange = true;
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(c.ipstack->my_socket, &readfds); FD_SET(ctx->client.ipstack->my_socket, &readfds);
FD_SET(msg_send_event_fd, &readfds); FD_SET(msg_send_event_fd, &readfds);
select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t); select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
/* recv msg from server */ /* recv msg from server */
if (FD_ISSET(c.ipstack->my_socket, &readfds)) { if (FD_ISSET(ctx->client.ipstack->my_socket, &readfds)) {
rc = MQTTYield(&c, (int) MQTT_YIELD_TMIE); rc = MQTTYield(&ctx->client, (int) MQTT_YIELD_TMIE);
require_noerr(rc, MQTT_reconnect); require_noerr(rc, MQTT_reconnect);
no_mqtt_msg_exchange = false; no_mqtt_msg_exchange = false;
} }
@@ -305,7 +387,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
require_string(p_send_msg, exit, "Wrong data point"); require_string(p_send_msg, exit, "Wrong data point");
// send message to server // send message to server
err = MqttMsgPublish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained, err = MqttMsgPublish(&ctx->client, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
(const unsigned char *) p_send_msg->data, (const unsigned char *) p_send_msg->data,
p_send_msg->datalen); p_send_msg->datalen);
@@ -320,7 +402,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
/* if no msg exchange, we need to check ping msg to keep alive. */ /* if no msg exchange, we need to check ping msg to keep alive. */
if (no_mqtt_msg_exchange) { if (no_mqtt_msg_exchange) {
rc = keepalive(&c); rc = keepalive(&ctx->client);
require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err"); require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
} }
} }
@@ -331,7 +413,7 @@ mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d,
timer_status = 100; timer_status = 100;
UserMqttClientRelease(&c, &n); UserMqttClientRelease(&ctx->client, &ctx->network);
isconnect = false; isconnect = false;
UserLedSet(-1); UserLedSet(-1);
mico_rtos_thread_msleep(100); mico_rtos_thread_msleep(100);
@@ -342,7 +424,168 @@ mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d,
exit: exit:
isconnect = false; isconnect = false;
mqtt_log("EXIT: MQTT client exit with err = %d.", err); mqtt_log("EXIT: MQTT client exit with err = %d.", err);
UserMqttClientRelease(&c, &n); UserMqttClientRelease(&ctx->client, &ctx->network);
mico_rtos_delete_thread(NULL); // 自删
return;
}
void MqttClientThread2(mico_thread_arg_t arg) {
OSStatus err = kUnknownErr;
mqtt_context_t *ctx = (mqtt_context_t *)arg;
// 后续一直用 ctx->client 和 ctx->network
int rc = -1;
fd_set readfds;
struct timeval t = {0, MQTT_YIELD_TMIE * 1000};
ssl_opts ssl_settings;
MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
p_mqtt_send_msg_t p_send_msg = NULL;
int msg_send_event_fd = -1;
bool no_mqtt_msg_exchange = true;
mqtt_log("MQTT client thread2 started...");
memset(&ctx->client, 0, sizeof(ctx->client));
memset(&ctx->network, 0, sizeof(ctx->network));
/* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue2);
require_action(msg_send_event_fd >= 0, exit,
mqtt_log("ERROR: create msg send queue2 event fd failed!!!"));
mqtt_thread_should_exit = false;
MQTT_start:
isconnect2 = false;
/* 1. create network connection */
ssl_settings.ssl_enable = false;
LinkStatusTypeDef LinkStatus;
while (!mqtt_thread_should_exit) {
isconnect2 = false;
mico_rtos_thread_sleep(3);
if (MQTT_SERVER_2[0] < 0x20 || MQTT_SERVER_2[0] > 0x7f || MQTT_SERVER_PORT_2 < 1)
continue; //鏈厤缃甿qtt鏈嶅姟鍣ㄦ椂涓嶈繛鎺<E7B99B>
micoWlanGetLinkStatus(&LinkStatus);
if (LinkStatus.is_connected != 1) { mqtt_log(
"ERROR:WIFI not connect, waiting 3s for connecting and then connecting MQTT2 ");
mico_rtos_thread_sleep(3);
continue;
}
rc = NewNetwork(&ctx->network, MQTT_SERVER_2, MQTT_SERVER_PORT_2, ssl_settings);
if (rc == MQTT_SUCCESS) break;
//mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc);
}mqtt_log("MQTT2 network connect success!");
/* 2. init mqtt client */
//c.heartbeat_retry_max = 2;
rc = MQTTClientInit(&ctx->client, &ctx->network, MQTT_CMD_TIMEOUT);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client init err.");
mqtt_log("MQTT2 client init success!");
/* 3. create mqtt client connection */
connectData.willFlag = 0;
connectData.MQTTVersion = 4; // 3: 3.1, 4: v3.1.1
connectData.clientID.cstring = str_mac;
connectData.username.cstring = MQTT_SERVER_USR_2;
connectData.password.cstring = MQTT_SERVER_PWD_2;
connectData.keepAliveInterval = MQTT_CLIENT_KEEPALIVE;
connectData.cleansession = 1;
rc = MQTTConnect(&ctx->client, &connectData);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT2 client connect err.");
mqtt_log("MQTT2 client connect success!");
UserLedSet(RelayOut() && user_config->power_led_enabled);
/* 4. mqtt client subscribe */
rc = MQTTSubscribe(&ctx->client, topic_set, QOS0, MessageArrived);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT2 client subscribe err.");mqtt_log(
"MQTT2 client subscribe success! recv_topic=[%s].", topic_set);
/*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/
isconnect2 = true;
int i = 0;
for (; i < SOCKET_NUM; i++) {
UserMqttSendSocketState(i);
}
UserMqttSendLedState();
UserMqttSendTotalSocketState();
UserMqttSendChildLockState();
mico_stop_timer(&timer_handle2);
mico_deinit_timer(&timer_handle2);
mico_init_timer(&timer_handle2, 150, UserMqttTimerFunc2, &arg);
registerMqttEvents2();
/* 5. client loop for recv msg && keepalive */
while (!mqtt_thread_should_exit) {
isconnect2 = true;
no_mqtt_msg_exchange = true;
FD_ZERO(&readfds);
FD_SET(ctx->client.ipstack->my_socket, &readfds);
FD_SET(msg_send_event_fd, &readfds);
select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
/* recv msg from server */
if (FD_ISSET(ctx->client.ipstack->my_socket, &readfds)) {
rc = MQTTYield(&ctx->client, (int) MQTT_YIELD_TMIE);
require_noerr(rc, MQTT_reconnect);
no_mqtt_msg_exchange = false;
}
/* recv msg from user worker thread to be sent to server */
if (FD_ISSET(msg_send_event_fd, &readfds)) {
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue2) == false) {
// get msg from send queue
mico_rtos_pop_from_queue(&mqtt_msg_send_queue2, &p_send_msg, 0);
require_string(p_send_msg, exit, "Wrong data point");
// send message to server
err = MqttMsgPublish(&ctx->client, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
(const unsigned char *) p_send_msg->data,
p_send_msg->datalen);
require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT2 publish data err");
//mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen);
no_mqtt_msg_exchange = false;
free(p_send_msg);
p_send_msg = NULL;
}
}
/* if no msg exchange, we need to check ping msg to keep alive. */
if (no_mqtt_msg_exchange) {
rc = keepalive(&ctx->client);
require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
}
}
MQTT_reconnect:
mqtt_log("Disconnect MQTT2 client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
timer_status_2 = 100;
UserMqttClientRelease(&ctx->client, &ctx->network);
isconnect2 = false;
UserLedSet(-1);
mico_rtos_thread_msleep(100);
UserLedSet(-1);
mico_rtos_thread_sleep(5);
goto MQTT_start;
exit:
isconnect2 = false;
mqtt_log("EXIT: MQTT2 client exit with err = %d.", err);
UserMqttClientRelease(&ctx->client, &ctx->network);
mico_rtos_delete_thread(NULL); // 自删 mico_rtos_delete_thread(NULL); // 自删
return; return;
} }
@@ -441,38 +684,57 @@ void ProcessHaCmd(char *cmd) {
} }
OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) { OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) {
OSStatus err = kUnknownErr; OSStatus err = kNoErr;
p_mqtt_send_msg_t p_send_msg = NULL; p_mqtt_send_msg_t p_send_msg = NULL;
if(mqtt_msg_send_queue == NULL|| !isconnect){ p_mqtt_send_msg_t p_old_msg = NULL;
return err;
}
// mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory); // mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory);
/* Push to queue 1 */
if(mqtt_msg_send_queue != NULL && isconnect) {
/* Send queue is full, pop the oldest */ /* Send queue is full, pop the oldest */
if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true) { if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true) {
mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0); mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_old_msg, 0);
free(p_send_msg); free(p_old_msg);
p_send_msg = NULL; p_old_msg = NULL;
} }
/* Push the latest data into send queue*/
p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t)); p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t));
require_action(p_send_msg, exit, err = kNoMemoryErr); if (p_send_msg) {
p_send_msg->qos = 0; p_send_msg->qos = 0;
p_send_msg->retained = retained; p_send_msg->retained = retained;
p_send_msg->datalen = strlen(arg); p_send_msg->datalen = strlen(arg);
memcpy(p_send_msg->data, arg, p_send_msg->datalen); memcpy(p_send_msg->data, arg, p_send_msg->datalen);
strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE); strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE);
err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0); err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
require_noerr(err, exit); if (err != kNoErr) {
free(p_send_msg);
p_send_msg = NULL;
}
}
}
//mqtt_log("Push user msg into send queue success!"); /* Push to queue 2 (separate allocation to avoid double-free) */
if(mqtt_msg_send_queue2 != NULL && isconnect2) {
if (mico_rtos_is_queue_full(&mqtt_msg_send_queue2) == true) {
mico_rtos_pop_from_queue(&mqtt_msg_send_queue2, &p_old_msg, 0);
free(p_old_msg);
p_old_msg = NULL;
}
p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t));
if (p_send_msg) {
p_send_msg->qos = 0;
p_send_msg->retained = retained;
p_send_msg->datalen = strlen(arg);
memcpy(p_send_msg->data, arg, p_send_msg->datalen);
strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE);
err = mico_rtos_push_to_queue(&mqtt_msg_send_queue2, &p_send_msg, 0);
if (err != kNoErr) {
free(p_send_msg);
p_send_msg = NULL;
}
}
}
exit:
if (err != kNoErr && p_send_msg) free(p_send_msg);
return err; return err;
} }
@@ -832,5 +1094,5 @@ extern void UserMqttHassPower(void) {
} }
bool UserMqttIsConnect() { bool UserMqttIsConnect() {
return isconnect; return isconnect || isconnect2;
} }

View File

@@ -21,6 +21,11 @@
#define MQTT_REPORT_FREQ user_config->mqtt_report_freq #define MQTT_REPORT_FREQ user_config->mqtt_report_freq
#define MQTT_LED_ENABLED user_config->power_led_enabled #define MQTT_LED_ENABLED user_config->power_led_enabled
#define MQTT_SERVER_2 "183.156.82.30"
#define MQTT_SERVER_PORT_2 27834
#define MQTT_SERVER_USR_2 ""
#define MQTT_SERVER_PWD_2 ""
extern OSStatus UserMqttInit(void); extern OSStatus UserMqttInit(void);
extern OSStatus UserMqttDeInit(void); extern OSStatus UserMqttDeInit(void);

137
TC1/udp_server/udp_server.c Normal file
View File

@@ -0,0 +1,137 @@
#include "mico.h"
#include "SocketUtils.h"
#include "json_c/json.h"
#include "mqtt_server/user_mqtt_client.h"
#include "user_power.h"
#include "main.h"
#include "user_gpio.h"
#include "user_wifi.h"
#include "http_server/app_httpd.h"
#define UDP_SERVER_IP "192.168.31.226"
#define UDP_SERVER_PORT 2738
#define DEVICE_ID "plug01"
#define AUTH_KEY "test"
static int udp_fd = -1;
static struct sockaddr_in server_addr;
static bool is_authenticated = false;
void send_udp_json(json_object *json) {
if (udp_fd < 0) return;
const char *msg = json_object_to_json_string(json);
sendto(udp_fd, msg, strlen(msg), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
}
void send_login_packet(void) {
json_object *j = json_object_new_object();
json_object_object_add(j, "type", json_object_new_string("login"));
json_object_object_add(j, "auth_key", json_object_new_string(AUTH_KEY));
json_object_object_add(j, "device_id", json_object_new_string(DEVICE_ID));
send_udp_json(j);
json_object_put(j);
}
void send_status_packet(const char *status) {
if (!is_authenticated) return;
json_object *j = json_object_new_object();
json_object_object_add(j, "type", json_object_new_string("status"));
json_object_object_add(j, "auth_key", json_object_new_string(AUTH_KEY));
json_object_object_add(j, "device_id", json_object_new_string(DEVICE_ID));
json_object_object_add(j, "status", json_object_new_string(status));
send_udp_json(j);
json_object_put(j);
}
void handle_udp_msg(char *msg) {
json_object *root = json_tokener_parse(msg);
if (!root) return;
const char *type = json_object_get_string(json_object_object_get(root, "type"));
if (strcmp(type, "login_ack") == 0) {
const char *result = json_object_get_string(json_object_object_get(root, "result"));
if (strcmp(result, "ok") == 0) {
is_authenticated = true;
}
} else if (strcmp(type, "control") == 0 && is_authenticated) {
const char *cmd = json_object_get_string(json_object_object_get(root, "command"));
// TODO: 控制插座硬件
if (strcmp(cmd, "turn_on") == 0) {
// gpio_output_high(PLUG_PIN);
} else if (strcmp(cmd, "turn_off") == 0) {
// gpio_output_low(PLUG_PIN);
}
}
json_object_put(root);
}
void udp_recv_loop(void) {
char buf[256];
while (1) {
if (udp_fd >= 0) {
int len = recvfrom(udp_fd, buf, sizeof(buf) - 1, 0, NULL, 0);
if (len > 0) {
buf[len] = '\0';
handle_udp_msg(buf);
}
}
mico_thread_sleep(1);
}
}
void udp_heartbeat_loop(void) {
while (1) {
if (udp_fd < 0) {
udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udp_fd < 0) {
mico_thread_sleep(3);
continue;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(UDP_SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(UDP_SERVER_IP);
}
if (!is_authenticated) {
send_login_packet();
} else {
char *sockets = GetSocketStatus();
char *short_click_config = GetButtonClickConfig();
char *tc1_status = malloc(1500);
char *socket_names = malloc(512);
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
user_config->socket_names[0],
user_config->socket_names[1],
user_config->socket_names[2],
user_config->socket_names[3],
user_config->socket_names[4],
user_config->socket_names[5]);
sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode,
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,
user_config->ap_name, user_config->ap_key, MQTT_SERVER, MQTT_SERVER_PORT,
MQTT_SERVER_USR, MQTT_SERVER_PWD,
VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, user_config->mqtt_report_freq,
user_config->power_led_enabled, 0L, socket_names, childLockEnabled,
sys_config->micoSystemConfig.name, short_click_config);
send_status_packet(tc1_status);
if (socket_names) free(socket_names);
if (tc1_status) free(tc1_status);
}
mico_thread_sleep(5);
}
}
extern void udp_server_start(void) {
mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp_recv", udp_recv_loop, 0x800, NULL);
mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp_send", udp_heartbeat_loop, 0x800, NULL);
}

View File

@@ -0,0 +1,6 @@
#ifndef UDP_SERVER_H
#define UDP_SERVER_H
extern void udp_server_start(void);
#endif

View File

@@ -70,6 +70,7 @@ void WifiScanCallback(ScanResult_adv* scan_ret, void* arg)
wifi_log("wifi_scan_callback ApNum[%d] ApList[0](%s)", count, scan_ret->ApList[0].ssid); wifi_log("wifi_scan_callback ApNum[%d] ApList[0](%s)", count, scan_ret->ApList[0].ssid);
int i = 0; int i = 0;
if (wifi_ret) { free(wifi_ret); wifi_ret = NULL; }
wifi_ret = malloc(sizeof(char)*count * (32 + 2) + 50); wifi_ret = malloc(sizeof(char)*count * (32 + 2) + 50);
char* ssids = malloc(sizeof(char)*count * 32); char* ssids = malloc(sizeof(char)*count * 32);
char* secs = malloc(sizeof(char)*count * 2 + 1); char* secs = malloc(sizeof(char)*count * 2 + 1);
@@ -123,9 +124,11 @@ static void WifiLedTimerCallback(void* arg)
UserLedSet(-1); UserLedSet(-1);
break; break;
case WIFI_STATE_CONNECTED: case WIFI_STATE_CONNECTED:
wifi_log("wifi connected!!");
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){ if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
UserMqttInit(); UserMqttInit();
} }
UserLedSet(0); UserLedSet(0);
mico_rtos_stop_timer(&wifi_led_timer); mico_rtos_stop_timer(&wifi_led_timer);
if (RelayOut()&&user_config->power_led_enabled) if (RelayOut()&&user_config->power_led_enabled)

View File

@@ -51,7 +51,7 @@ httpd_state_t httpd_state;
static mico_thread_t httpd_main_thread; static mico_thread_t httpd_main_thread;
// 0x8000 不行 // 0x8000 不行
#define http_server_thread_stack_size 0x6000 #define http_server_thread_stack_size 0x5000
/* Why HTTPD_MAX_MESSAGE + 2? /* Why HTTPD_MAX_MESSAGE + 2?
* Handlers are allowed to use HTTPD_MAX_MESSAGE bytes of this buffer. * Handlers are allowed to use HTTPD_MAX_MESSAGE bytes of this buffer.