Skip to content

Commit

Permalink
feat: 無限ループを停止する
Browse files Browse the repository at this point in the history
  • Loading branch information
nana4rider committed Jan 17, 2025
1 parent 5006140 commit bf6ed4a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wisun2mqtt",
"version": "1.0.6",
"version": "1.0.7",
"main": "dist/index.js",
"type": "module",
"homepage": "https://github.com/nana4rider/wisun2mqtt",
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ async function main() {
logger.info("start");

const smartMeterClient = await initializeSmartMeterClient();
const mqtt = await setupMqttDeviceManager(smartMeterClient);
const { mqtt, stopAutoRequest } =
await setupMqttDeviceManager(smartMeterClient);
const http = await initializeHttpServer();
const {
device: { deviceId, entities },
} = smartMeterClient;
const availability = setupAvailability(deviceId, entities, mqtt);

const handleShutdown = async () => {
logger.info("shutdown");
logger.info("shutdown start");
await stopAutoRequest();
availability.close();
await mqtt.close(true);
await http.close();
await smartMeterClient.close();
logger.info("shutdown finished");
process.exit(0);
};

Expand Down
14 changes: 10 additions & 4 deletions src/manager/mqttDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export default async function setupMqttDeviceManager(
});

// 定期的にエンティティの状態を更新
void (async () => {
while (true) {
let isAutoRequestRunning = true;
const autoRequestTask = (async () => {
while (isAutoRequestRunning) {
logger.info("Starting periodic ECHONET property fetch...");
try {
const echonetData = await smartMeterClient.fetchData(
Expand All @@ -44,7 +45,7 @@ export default async function setupMqttDeviceManager(
echonetData.properties.forEach((property) => {
const entity = entities.find((entity) => entity.epc === property.epc);
if (entity === undefined) {
logger.warn(
logger.error(
`エンティティに存在しないプロパティ: epc=${property.epc} edt=${property.edt}`,
);
return;
Expand All @@ -65,5 +66,10 @@ export default async function setupMqttDeviceManager(
}
})();

return mqtt;
const stopAutoRequest = async () => {
isAutoRequestRunning = false;
await autoRequestTask;
};

return { mqtt, stopAutoRequest };
}

0 comments on commit bf6ed4a

Please sign in to comment.