TCW120

Aus FHEMWiki

TERACOM TCW120

Das TCW120 ist ein einfaches Netzwerkinterface mit 2 digitalen und 2 analogen Eingängen, 2 Relais-Ausgängen und einem Eingang für einen Termperatur-Sensor.

Dieses Modul ist nur ein schneller Hack, der allerdings schon fast 2 Jahre stabil bei mir läuft.

$ cat 00_TCW.pm 
package main;

# This is a quick and dirty implementation to use fhem with a TCW120 Network Interface

use strict;
use warnings;
use Time::HiRes qw(gettimeofday);

sub TCW_defer
{
  my $time = shift;
  my $cmd = shift;
  InternalTimer(gettimeofday()+$time, "TCW_onDefer", $cmd, 0);
}
sub TCW_onDefer() {
  my $cmd = shift;
  fhem ($cmd);
}


sub TCW_Initialize($)
{
	my ($hash) = @_;

	$hash->{DefFn} = "TCW_Define";
	$hash->{ShutdownFn} = "TCW_Shutdown";
	$hash->{ReadFn} = "TCW_Read";
	$hash->{SetFn} = "TCW_Set";
	$hash->{GetFn} = "TCW_Get";
}

#####################################
sub
TCW_Shutdown($)
{
	my ($hash) = @_;
	return undef;
}

#####################################
sub
TCW_Define($$)
{
	my ($hash, $def) = @_;
	my @a = split("[ \t][ \t]*", $def);

	if(@a != 5) {
		my $msg = "wrong syntax: define <name> TCW host readCommunity writeCommunity";
		Log 2, $msg;
		return $msg;
	}

	$hash->{host}=$a[2];
	$hash->{readcom}=$a[3];
	$hash->{writecom}=$a[4];
	$hash->{STATE} = "Initialized";
	$hash->{INTERVAL} = 1;

        InternalTimer(gettimeofday()+$hash->{INTERVAL}, "TCW_Update", $hash, 1);

	return 0;
}

################################
#
#
sub TCW_Set
{
	my ($hash, @a) = @_;

	my $oid;
	if ($a[1] eq "dout1") { $oid = "iso.3.6.1.4.1.38783.3.1.1.0"; }
	elsif ($a[1] eq "dout2") { $oid = "iso.3.6.1.4.1.38783.3.1.2.0"; }
	if (defined $oid) {
		my $newval;
		if (lc $a[2] eq 'on') {
			$newval = 1;
		}
		elsif (lc $a[2] eq 'off') {
			$newval = 0;
		}
		elsif (lc $a[2] eq 'on-for-timer') {
			$newval = 1;
			TCW_defer ($a[3], "set ".$hash->{NAME}." $a[1] off");
		}
		else {
			return 'SYNTAX: set (out1|out2) (on|off|on-for-timer sec)';
		}
		my $cmd = "snmpset -Oqv -v 1 -c '".$hash->{'writecom'}."' '".$hash->{'host'}."' '".$oid."' 'i' '".$newval."'";
		if (defined $newval) { `$cmd`; }
	}
}

################################
#
#
sub
TCW_Get($@)
{
	my ($hash,@a) = @_;
	return "get: <OID>" if (@a !=2);
	my $oid = $a[1];
	my $cmd = "snmpget -Oqv -v 1 -c '".$hash->{'readcom'}."' '".$hash->{'host'}."' '".$oid."'";
	my $reading = `$cmd`;
	$reading =~ s/\r|\n//g;
	return $reading;
}

sub TCW_Update($)
{
  my ($hash) = @_;
  InternalTimer(gettimeofday()+$hash->{INTERVAL}, "TCW_Update", $hash, 0);
  TCW_Update_private($hash, 'DIN1', 'iso.3.6.1.4.1.38783.3.3.1.0');
  TCW_Update_private($hash, 'DIN2', 'iso.3.6.1.4.1.38783.3.3.2.0');
  TCW_Update_private($hash, 'AIN1', 'iso.3.6.1.4.1.38783.3.2.1.0');
  TCW_Update_private($hash, 'AIN2', 'iso.3.6.1.4.1.38783.3.2.3.0');
  TCW_Update_private($hash, 'TEMPERATURE', 'iso.3.6.1.4.1.38783.3.4.0');
  TCW_Update_private($hash, 'DOUT1', 'iso.3.6.1.4.1.38783.3.1.1.0');
  TCW_Update_private($hash, 'DOUT2', 'iso.3.6.1.4.1.38783.3.1.2.0');
}

sub TCW_Update_private($$)
{
  my ($hash, $key, $oid) = @_;
  my $oldval = $hash->{READINGS}{$key}{'VAL'};
  my $newval = TCW_Get($hash, 'GET', $oid);
  $newval =~ s/"//g;
  my @changed;
  if (!defined $oldval || $newval ne $oldval) {
    $hash->{READINGS}{$key}{'VAL'} = $newval;
    $hash->{READINGS}{$key}{'TIME'} = TimeNow();
    push @changed, "$key: $newval";
  }
  $hash->{CHANGED} = \@changed;
  DoTrigger($hash->{NAME}, undef) if (scalar(@changed) > 0);
}

1;