From 775fef2147622db4c46977dfe4ae35db331896ce Mon Sep 17 00:00:00 2001 From: Feilner Date: Sun, 8 Dec 2024 17:52:08 +0100 Subject: [PATCH] connect working --- package.json | 3 +-- src/platform.ts | 3 ++- src/step7.cjs | 63 ------------------------------------------------- src/step7.ts | 55 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 66 deletions(-) delete mode 100644 src/step7.cjs create mode 100644 src/step7.ts diff --git a/package.json b/package.json index 9ab33b1..cebb039 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/platform.ts b/src/platform.ts index 89b60bf..11c0975 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -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 @@ -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'); diff --git a/src/step7.cjs b/src/step7.cjs deleted file mode 100644 index f70c230..0000000 --- a/src/step7.cjs +++ /dev/null @@ -1,63 +0,0 @@ -import { Logging } from 'homebridge'; -import snap7 from 'node-snap7'; -import { isIPv4 } from 'net'; -import dns from 'dns'; - - -export class PLC { - private client: any; - private type: number; - 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.client = new snap7.S7Client(); - this.log = log; - this.ip = ip; - this.rack = rack; - this.slot = slot; - this.type = isOP ? snap7.CONNTYPE_OP : snap7.CONNTYPE_PG; - this.isConnectOngoing = false; - - } - - //PLC connection check function - connect() { - const typeName = ['invalid', 'PG-Communication', 'OP-Communication']; - - let rv = false; - - - if (this.client.Connected()) { - rv = true; - } else { - this.log.info('Connecting to %s (%s:%s) %s', this.ip, this.rack, this.slot, typeName[this.type]); - - if (!this.isConnectOngoing) { - this.isConnectOngoing = true; - let ok = this.client.SetConnectionType(this.type); - if(ok) { - - ok = this.client.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.type]); - rv = true; - } else { - this.log.error('Connection to %s (%s:%s) %s failed', this.ip, this.rack, this.slot, typeName[this.type]); - } - } else { - this.isConnectOngoing = false; - this.log.error('Set connection type to %s (%s:%s) %s failed', typeName[this.type], this.ip, this.rack, this.slot); - } - } - } - return rv; - } -} - - - diff --git a/src/step7.ts b/src/step7.ts new file mode 100644 index 0000000..c7599b1 --- /dev/null +++ b/src/step7.ts @@ -0,0 +1,55 @@ + +import { Logging } from 'homebridge'; +import snap7 from 'node-snap7'; +import { isIPv4 } from 'net'; +import dns from 'dns'; + +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; + } +} + +