Skip to content

Commit

Permalink
connect working
Browse files Browse the repository at this point in the history
  • Loading branch information
Feilner committed Dec 8, 2024
1 parent 867628a commit 775fef2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 66 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"version": "3.0.0-alpha",
"description": "Homebridge plugin for Siemens Step7 and compatible PLCs. (https://github.com/homebridge)",
"license": "Apache-2.0",

"author": "Feilner",
"license": "Apache-2.0",
"homepage": "https://github.com/Feilner/homebridge-plc#readme",
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,6 +43,7 @@
"@eslint/js": "^9.14.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.8.6",
"@types/node-snap7": "^0.2.24",
"eslint": "^9.14.0",
"homebridge": "^2.0.0-beta.0",
"nodemon": "^3.1.7",
Expand Down
3 changes: 2 additions & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { API, Characteristic, DynamicPlatformPlugin, Logging, PlatformAcces

import { ExamplePlatformAccessory } from './platformAccessory.js';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
import { PLC } from './step7.cjs';
import { PLC } from './step7.js';

/**
* HomebridgePlatform
Expand All @@ -25,6 +25,7 @@ export class ExampleHomebridgePlatform implements DynamicPlatformPlugin {
) {
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
this.log.error('nun wird es ernst' + this.config.ip);
this.PLC = new PLC(log, this.config.ip, this.config.rack, this.config.slot, 'communicationOP' in this.config && this.config.communicationOP );
if (!this.PLC.connect()) {
this.log.error('Initial connect failed');
Expand Down
63 changes: 0 additions & 63 deletions src/step7.cjs

This file was deleted.

55 changes: 55 additions & 0 deletions src/step7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

import { Logging } from 'homebridge';
import snap7 from 'node-snap7';
import { isIPv4 } from 'net';

Check failure on line 4 in src/step7.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'isIPv4' is defined but never used

Check failure on line 4 in src/step7.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'isIPv4' is defined but never used
import dns from 'dns';

Check failure on line 5 in src/step7.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'dns' is defined but never used

Check failure on line 5 in src/step7.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'dns' is defined but never used

export class PLC {
private s7: snap7.S7Client;
private connType: snap7.ConnectionType;
private slot: number;
private rack: number;
private ip: string;
private isConnectOngoing: boolean;
private log: Logging;

constructor(log: Logging, ip: string, rack: number, slot: number, isOP: boolean) {
this.s7 = new snap7.S7Client();
this.log = log;
this.ip = ip;
this.rack = rack;
this.slot = slot;
this.connType = isOP ? 0x1 : 0x2;
this.isConnectOngoing = false;
}

//PLC connection check function
connect() {
const typeName = ['invalid', 'PG-Communication', 'OP-Communication'];

let rv = false;


if (this.s7.Connected()) {
rv = true;
} else {
this.log.info('Connecting to %s (%s:%s) %s', this.ip, this.rack, this.slot, typeName[this.connType]);

if (!this.isConnectOngoing) {
this.isConnectOngoing = true;
this.s7.SetConnectionType(this.connType);
const ok: boolean = this.s7.ConnectTo(this.ip, this.rack, this.slot);
this.isConnectOngoing = false;
if(ok) {
this.log.info('Connected to %s (%s:%s) %s', this.ip, this.rack, this.slot, typeName[this.connType]);
rv = true;
} else {
this.log.error('Connection to %s (%s:%s) %s failed', this.ip, this.rack, this.slot, typeName[this.connType]);
}
}
}
return rv;
}
}


0 comments on commit 775fef2

Please sign in to comment.