Skip to content

Commit

Permalink
added support for refreshing (ping) devices
Browse files Browse the repository at this point in the history
  • Loading branch information
trembon committed Sep 20, 2023
1 parent 57c4231 commit ff3b30f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Basic example of the configuration (config.json) file.
{
"server": {
"port": 3000,
"deviceReconnectWait": 5000
"deviceReconnectWait": 5000,
"refreshTime": 60000
},
"webhooks": ["http://localhost:4321/webhook"],
"devices": [
Expand Down
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"server": {
"port": 3000,
"deviceReconnectWait": 5000
"deviceReconnectWait": 5000,
"refreshTime": 60000
},
"webhooks": ["http://localhost:4321/webhook"],
"devices": [
Expand Down
8 changes: 8 additions & 0 deletions src/active-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export default class ActiveDevice {
await this.tuya.disconnect();
}

async refresh(): Promise<void> {
if(this.device.disabled || !this.tuya.isConnected()){
return;
}

await this.tuya.refresh({schema: true});
}

async set(
data: ITuyaMultipleProperties | ITuyaSingleProperty
): Promise<{ [dps: string]: any }> {
Expand Down
7 changes: 6 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import {
import Logger from "./logger";
import processDevices from "./process-devices";
import PublicDevice from "./public-device.model";
import refreshDevices from "./refresh-devices";

Logger.Info(`Starting`);

let devices: { [id: string]: ActiveDevice } = {};

const config = new Configuration(async () => processDevices(devices));
const config = new Configuration(async () => {
processDevices(devices);
refreshDevices(devices);
});
config.initialize();

const app = express();
Expand All @@ -22,6 +26,7 @@ app.use(express.json());
const port = config.server().port ?? 3000;

processDevices(devices);
refreshDevices(devices);

app.get("/", (req, res) => {
res.json([
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/config/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface IConfigServer {
port: number;
deviceReconnectWait: number;
refreshTime?: number;
}
30 changes: 30 additions & 0 deletions src/refresh-devices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ActiveDevice from "./active-device";
import Configuration from "./configuration";
import Logger from "./logger";

let refreshDevicesInterval = undefined;
const refreshDevices = (activeDevices: { [id: string]: ActiveDevice }) => {
if (refreshDevicesInterval) {
clearInterval(refreshDevicesInterval);
refreshDevicesInterval = undefined;
}

const refreshTime = Configuration.instance.server().refreshTime;
if (refreshTime && refreshTime > 0) {
refreshDevicesInterval = setInterval(() => {
for (const key in activeDevices) {
try {
activeDevices[key].refresh();
} catch (ex: unknown) {
Logger.Error(
`${activeDevices[key].tuya.device.id} - Error refreshing device: ${
(<Error>ex).message
}`
);
}
}
}, refreshTime);
}
};

export default refreshDevices;

0 comments on commit ff3b30f

Please sign in to comment.