Skip to content

Commit

Permalink
設定ファイルでリクエストのたびにレスポンスが早くなるプロパティを作成できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
banban525 committed Oct 28, 2024
1 parent a0a57a7 commit 21f645b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
10 changes: 10 additions & 0 deletions server/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export interface Settings {
};
}
nodeProfileId?:string;
delayProperties?:DelayPropertySettings[];
}

export interface DelayPropertySettings
{
delayTime:number;
eoj:string;
epc:string;
esv:"SETC"|"GET";
delayTimeDecrementPerRequest:number;
}

export class Settings
Expand Down
55 changes: 52 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Controller, EchoObject, EchoStatus, ILogger } from "./controller";
import os from "os";
import ip from "ip";
import fs from "fs";
import { Settings } from "./Settings";
import { DelayPropertySettings, Settings } from "./Settings";

let echonetTargetNetwork = ""; //"192.168.1.0/24";
let echonetDelayTime = 0;
Expand Down Expand Up @@ -77,6 +77,17 @@ class Logger implements ILogger {
}
}

interface DelayPropertyStatus extends DelayPropertySettings {
currentDelayTime: number;
}
const delayPropertyStatusList: {[key:string]:DelayPropertyStatus} = {};
(settings.delayProperties ?? []).forEach(_=>{
delayPropertyStatusList[_.esv + "_" +_.eoj.toLowerCase() + "_" + _.epc.toLowerCase()] = {
..._,
currentDelayTime:_.delayTime
};
});

const logger = new Logger(debugLog);

const app = express();
Expand Down Expand Up @@ -228,7 +239,26 @@ async function userFunc(rinfo: rinfo, els: eldata): Promise<void> {

logger.log(`Requested: ${els.DEOJ} ${propertyCode}`);
sleeping = true;
await sleep(echonetDelayTime);

let delayTime = echonetDelayTime;
const key = `GET_${els.DEOJ}_${propertyCode}`;
if(key in delayPropertyStatusList)
{
const delayPropertyStatus = delayPropertyStatusList[key];

if(delayTime < delayPropertyStatus.currentDelayTime)
{
delayTime = delayPropertyStatus.currentDelayTime;
}
delayPropertyStatus.currentDelayTime -= delayPropertyStatus?.delayTimeDecrementPerRequest ?? 0;
if(delayPropertyStatus.currentDelayTime < 0)
{
delayPropertyStatus.currentDelayTime = 0;
}
delayPropertyStatus.delayTimeDecrementPerRequest = 0;
}

await sleep(delayTime);
sleeping = false;

EL.sendOPC1(
Expand Down Expand Up @@ -274,8 +304,27 @@ async function userFunc(rinfo: rinfo, els: eldata): Promise<void> {
EL.toHexArray(els.DETAILs[propertyCode])
);
if (result) {

let delayTime = echonetDelayTime;
const key = `SETC_${els.DEOJ}_${propertyCode}`;
if(key in delayPropertyStatusList)
{
const delayPropertyStatus = delayPropertyStatusList[key];

if(delayTime < delayPropertyStatus.currentDelayTime)
{
delayTime = delayPropertyStatus.currentDelayTime;
}
delayPropertyStatus.currentDelayTime -= delayPropertyStatus?.delayTimeDecrementPerRequest ?? 0;
if(delayPropertyStatus.currentDelayTime < 0)
{
delayPropertyStatus.currentDelayTime = 0;
}
delayPropertyStatus.delayTimeDecrementPerRequest = 0;
}

sleeping = true;
await sleep(echonetDelayTime);
await sleep(delayTime);
sleeping = false;

EL.sendOPC1(
Expand Down

0 comments on commit 21f645b

Please sign in to comment.