class BrickletDualRelay: def __init__(self, uid, switch_id, ip, connection, logging_daemon, queue): self._bricklet = DualRelay(uid, connection) self.uid = uid self.switch_id = switch_id self.ip = ip self._logging_daemon = logging_daemon self._queue = queue self._logging_daemon.info( 'Tinkerforge ... DualRelay-Bricklet "%s" initialisiert' % uid) def status(self, number): _tmp = self._bricklet.get_state() return _tmp[number] def set_switch(self, switch_to, arg_a, arg_b, arg_c, arg_d): self._bricklet.set_selected_state((arg_b + 1), switch_to) self._logging_daemon.debug( 'Tinkerforge ... DualRelay-Bricklet UID "%s" , geschaltet Relais %s , SOLL = %s , IST = %s' % (self.uid, arg_b, switch_to, self.status(arg_b))) tmp_json = json.dumps({ "usage": "switch_changed_status", "ip": self.ip, "id": self.switch_id, "value": switch_to }) for consumer in self._queue: consumer(tmp_json) self._logging_daemon.info( 'Tinkerforge ... DualRelay-Bricklet UID "%s" Relais %s , send %s -> SocketServer Warteschlange ' % (self.uid, arg_b, self.status(arg_b)))
def switchHeating( mySQLdbCursorObj, sw1, sw2, eventID, online_id, UID=TF_HEATSW_UID, writeLog=True ): ''' Switch Heating on or Off and report via mySQL ''' ipcon = getTFconn() try: dr = DualRelay(UID, ipcon) # Create device object isSw1, isSw2 = dr.get_state() except: Logger.exception("Unable to read switch status!") return now = datetime.datetime.now() if isSw1 == sw1 and isSw2 == sw2: Logger.debug("switchHeating: No change! " + str(sw1) +','+ str(sw2) ) return try: result = dr.set_state(sw1, sw2) Logger.info( "Changed switch to " + str(sw1) + ", " + str(sw2) + ". Result: " + str(result) ) except: Logger.exception("Unable to CHANGE switch status!") return # report into mySQL. Example: # INSERT INTO `heating_logbook` (`timestamp`, `eventID`, `eventStart`, `estimateOn`, `actualOn`, `actualOff`) VALUES (CURRENT_TIMESTAMP, '', '', '', '14:38:00', ''); sqlCmd = "INSERT INTO heating_logbook (`eventID`, `actualOn`, `actualOff`) VALUES ('" + str(eventID) + "', " actualOn = actualOff = '00:00' if sw1 == True: actualOn = now.strftime( "%H:%M" ) else: actualOff = now.strftime( "%H:%M" ) sqlCmd+= "'" + actualOn + "', '" + actualOff + "'); " # execute SQL remote and local executeSQL( mySQLdbCursorObj, sqlCmd, 'write heating switching time into' ) # local #if eventID==0: return # heating control is on manual, so no event id involved # and remote writeApiEventLog( online_id, actOn=actualOn, actOff=actualOff )
class BrickletDualRelay: def __init__(self, uid, switch_id, ip, connection, logging_daemon, queue): self._bricklet = DualRelay(uid, connection) self.uid = uid self.switch_id = switch_id self.ip = ip self._logging_daemon = logging_daemon self._queue = queue self._logging_daemon.info('Tinkerforge ... DualRelay-Bricklet "%s" initialisiert' % uid) def status(self, number): _tmp = self._bricklet.get_state() return _tmp[number] def set_switch(self, switch_to, arg_a, arg_b, arg_c, arg_d): self._bricklet.set_selected_state((arg_b + 1), switch_to) self._logging_daemon.debug( 'Tinkerforge ... DualRelay-Bricklet UID "%s" , geschaltet Relais %s , SOLL = %s , IST = %s' % ( self.uid, arg_b, switch_to, self.status(arg_b))) tmp_json = json.dumps({ "usage": "switch_changed_status", "ip": self.ip, "id": self.switch_id, "value": switch_to }) for consumer in self._queue: consumer(tmp_json) self._logging_daemon.info( 'Tinkerforge ... DualRelay-Bricklet UID "%s" Relais %s , send %s -> SocketServer Warteschlange ' % ( self.uid, arg_b, self.status(arg_b)))
def __init__(self, uid, switch_id, ip, connection, logging_daemon, queue): self._bricklet = DualRelay(uid, connection) self.uid = uid self.switch_id = switch_id self.ip = ip self._logging_daemon = logging_daemon self._queue = queue self._logging_daemon.info( 'Tinkerforge ... DualRelay-Bricklet "%s" initialisiert' % uid)
def checkHeatingStatus( ipcon, UID=TF_HEATSW_UID, writeLog=False ): ''' Check if heating has been switched on at the moment ''' try: dr = DualRelay(UID, ipcon) # Create device object status = dr.get_state() except: Logger.exception("Failed to read switch status!") return (False,False) # return the current status (2 values!) as a list stat0,stat1='0','0' if status[0]: stat0='1' if status[1]: stat1='1' if writeLog: open("./Logfiles/DRstatus.log","w").write( stat0+', '+stat1+" updated "+getTmStmp() ) return ( status[0], status[1] )
def __init__(self, uid, switch_id, ip, connection, logging_daemon, queue): self._bricklet = DualRelay(uid, connection) self.uid = uid self.switch_id = switch_id self.ip = ip self._logging_daemon = logging_daemon self._queue = queue self._logging_daemon.info('Tinkerforge ... DualRelay-Bricklet "%s" initialisiert' % uid)
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "xyz" # Change to your UID import time from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_dual_relay import DualRelay if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dr = DualRelay(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Turn relays alternating on/off for 10 times with 1 second delay for i in range(10): time.sleep(1) if i % 2: dr.set_state(True, False) else: dr.set_state(False, True) raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()