#!/usr/bin/tclsh set ::termon 39 set ::termoff 37 set ::test 0 set ::debug 1 set ::owfs /mnt/1wire set ::driver 12.646A26000000 set ::driverpio $::driver/PIO.A set ::driverinit [list \ [list $::driver/set_alarm 311] \ [list $::driverpio 0] \ ] set ::sensor 28.FB450B000000 set ::sensortemp $::sensor/temperature set ::sensorhigh $::sensor/temphigh set ::sensorlow $::sensor/templow set ::sensorinit [list \ [list $::sensorhigh $::termon] \ [list $::sensorlow 0] \ ] proc logger {msg {priority err}} { syslog -facility daemon -ident fan $priority $msg } proc owget {name} { if {[catch {open $::owfs/$name} f]} { logger "ERROR: $f" return "" } set res [read -nonewline $f] close $f return [string trim $res] } proc owput {name {val ""}} { if {[catch {open $::owfs/$name WRONLY} f]} { logger "ERROR(owput): $f" return 0 } puts $f $val close $f return 1 } proc DoInit {} { if {$::debug} {logger "fan init" info} foreach l $::driverinit {owput [lindex $l 0] [lindex $l 1]} foreach l $::sensorinit {owput [lindex $l 0] [lindex $l 1]} } proc TurnOn {} { if {$::debug} { logger "driver ON" info } owput $::driverpio 1 owput $::sensorhigh 50 owput $::sensorlow $::termoff } proc TurnOff {} { if {$::debug} { logger "driver OFF" info } owput $::driverpio 0 owput $::sensorhigh $::termon owput $::sensorlow 0 } proc Check {} { set high [owget $::sensorhigh] set low [owget $::sensorlow] set temp [owget $::sensortemp] set t [owget 26.0A3026000000/temperature] set d [clock format [clock seconds] -format {%c}] if {($high == $::termon) && ($temp >= $high)} { TurnOn exec echo "On high: $high, low: $low, temp: $temp, env: $t, date: $d" >>/tmp/fan.log } elseif {($low == $::termoff) && ($temp <= $low)} { TurnOff exec echo "Off high: $high, low: $low, temp: $temp, env: $t, date: $d" >>/tmp/fan.log } else { DoInit exec echo "Init high: $high, low: $low, temp: $temp, env: $t, date: $d" >>/tmp/fan.log } } set mode "" if {($argc > 0) && (([set mode [lindex $argv 0]] != "init") && ($mode != "on") && ($mode != "off") && ($mode != "check"))} { puts stderr "Usage: $argv0 init \[device\]|on|off|check" exit 1 } package require Syslog if {[file exists $::owfs/uncached]} {set ::uncached uncached/} {set ::uncached ""} if {$mode == "init"} { DoInit TurnOff exit 0 } if {$mode == "on"} { TurnOn exit 0 } if {$mode == "off"} { TurnOff exit 0 } if {$mode == "check"} { Check exit 0 } exit 0