class PlugwiseOperator():

    def __init__(self, port,mac):
        stick = Stick(port)
        self.socket = Circle(mac, stick)
        self.socketActive = True
        self.socketUsage = 0

    def setStatus(self,status):
        if status == "true":
            self.socket.switch_on()
        else:
            self.socket.switch_off()
    def getStatus(self):
        if self.socketActive:
            try:
                pwUsage = round(self.socket.get_power_usage(), 2)
            except:
                print "socket not active"
                pwUsage = 0
                self.socketActive = False
        pwIsOn = "OFF"        
        if pwUsage > 0.1:
            pwIsOn = "ON"
            self.socketUsage = pwUsage
        else:
            pwUsage = self.socketUsage
        return pwIsOn
        
    def getData(self):
        if self.socketActive:
            try:
                pwUsage = round(self.socket.get_power_usage(), 2)
            except:
                print "socket not active"
                pwUsage = 0
                self.socketActive = False
        else:
            pwUsage = 0

        pwIsOn = "OFF"

        if pwUsage > 0.1:
            pwIsOn = "ON"
            self.socketUsage = pwUsage
        else:
            pwUsage = self.socketUsage
        return pwUsage
Exemple #2
0
def read_plugs(port):
    l = open(logfile, 'a')
    PS = Stick(port)
    try:
        PS = Stick(port)
        add_to_blacklist = []
        db_update = []
        db_device = []
    except serial.SerialException as e:
        l.write(human_time + "\t Serial exception " + str(e)+"\n")
    except OSError as er:
        raise Exception(human_time + " Port " + port + " does not exist")
    except Exception:
        import traceback
        l.write(human_time + "\t Couldn`t open serial port, Exception: " + traceback.format_exc()+"\n")
    for i in circles['circle_mac']:
        smart_plug = Circle(i, PS)
        try:
           if is_blacklisted(script_start, i):
                db_entry.append("-1")
           else:
                a = smart_plug.get_power_usage()
                db_entry.append(a)
                db_update.append(a)
                db_device.append(i)
        except ValueError:
            db_entry.append("-2")
            l.write(human_time + " : "+str(smart_plugs[i])+" = "+ str(i) + ": Wrong value exception thrown, can`t read the power\n")
        except TimeoutException:
            db_entry.append("-3")
            l.write(human_time + " : "+str(smart_plugs[i]) +" = "+ str(i) + ": Timeout / Added to banlist for "
                    + str(blacklist_duration)+" seconds \n")
            add_to_blacklist.append(i)
    if add_to_blacklist:
        blacklist(add_to_blacklist)
    if db_update:
        update_db_device_table(db_device, db_update)
    l.close()