-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
import dns from 'dns'; | ||
Check failure on line 5 in src/step7.ts
|
||
|
||
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; | ||
} | ||
} | ||
|
||
|