#! /usr/bin/env python # $Id: one_wire_sensors.py 23 2004-07-22 04:35:36Z peter $ # # need to check for /1-wire mounted but Vendor=04fa is not in the usb # devices file. this can occur when the usb dongle is no longer # connected. # # check that /1-wire is mounted # if not, check that Vendor=04fa is in the usb devices file. # if it is, then kick /etc/init.d/owfs to start up again # if not, then ? import os import syslog import csv import time import util Debug = True OWUSBVendor = '04fa' USBDevices = '/proc/bus/usb/devices' MountFile = '/proc/mounts' OWUSBMount = '/1-wire' MountCommand = '/etc/init.d/owfs.init start' UnmountCommand = '/etc/init.d/owfs.init stop' SensorLogName = 'sensor.log' Sensors = { 'Temperature' : os.path.join( OWUSBMount, '10.B7B64D000800' ), #'Humidity1' : os.path.join( OWUSBMount, '12.386623000000' ), #'Humidity2' : os.path.join( OWUSBMount, '12.3B6F23000000' ), } Messages = { 'MountedNoPresence' : '1-wire mounted but no usb presence, unmounting 1-wire', 'UnmountedPresence' : '1-wire not mounted but usb presence, mounting 1-wire', 'UnmountedNoPresence' : '1-wire not mounted and no usb presence, nothing can be done', 'MountedNoSensors' : '1-wire mounted and usb presense but no sensors, remounting 1-wire', } def log_message( message ): if Debug: print message syslog.syslog( syslog.LOG_DAEMON | syslog.LOG_WARNING, message ) def check_mount( mount_point ): known_mounts = open( MountFile, 'r' ).readlines() for mount in known_mounts: if mount_point in mount: return True return False def check_usb_vendor( vendor ): usb_lines = open( USBDevices, 'r' ).readlines( ) vendor = 'Vendor=' + vendor for line in usb_lines: if vendor in line: return True return False def check_sensors( ): for sensor in Sensors: if not os.path.isdir( Sensors[ sensor ] ): return False return True def read_temperature( ): path = os.path.join( Sensors[ 'Temperature' ], 'temperature' ) temperature = open( path, 'r' ).readlines( ) return float( temperature[ 0 ] ) def log_sensors( ): current_time = time.time( ) path = util.log_path( '', SensorLogName ) temperature = read_temperature( ) writer = csv.writer( file( path, 'a' ) ) writer.writerow( ( current_time, temperature ) ) def one_wire( ): mounted = check_mount( OWUSBMount ) usb_presence = check_usb_vendor( OWUSBVendor ) sensors = check_sensors( ) if mounted: if not usb_presence: log_message( Messages[ 'MountedNoPresence' ] ) os.system( UnmountCommand ) elif not sensors: log_message( Messages[ 'MountedNoSensors' ] ) os.system( UnmountCommand ) os.system( MountCommand ) elif Debug: print 'all is right with the world' elif usb_presence: log_message( Messages[ 'UnmountedPresence' ] ) os.system( MountCommand ) else: log_message( Messages[ 'UnmountedNoPresence' ] ) if __name__ == '__main__': one_wire( ) if check_mount( OWUSBMount ): log_sensors( )