ENEN DEDE
home
Station ladebereit

Alter Code (100% Prozessorlast)

/usr/local/bin/emob/impsim:

#!/usr/bin/env python

import subprocess
import smtplib
import socket

from email.mime.text import MIMEText

import time
import MySQLdb as mdb
import sys

import RPi.GPIO as GPIO, time, os
from sys import stdout 

from datetime import datetime

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

gread = 0
hcount = 0
load = 0 
lch = 0
imp = 0
lcheck = 0

now = datetime.now()
last = now
lastlast = last
delta = now - last

pstart = 1

def RCtime (RCpin):
	reading = 0
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        GPIO.setup(RCpin, GPIO.IN)
        while (GPIO.input(RCpin) == GPIO.LOW):    
	    reading = 1
            time.sleep(0.1)
        return reading

def setload ():
    con = None
    try:
        con = mdb.connect('localhost', 'db_user', 'db_pass', 'emob');
        cur = con.cursor()
	cur.execute("UPDATE emob.settings SET loading='1'")
    	con.commit()
    except mdb.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        sys.exit(1)
    finally:        
        if con:    
            con.close()

def unsetload ():
    con = None
    try:
        con = mdb.connect('localhost', 'db_user', 'db_pass', 'emob');
        cur = con.cursor()
	cur.execute("UPDATE emob.settings SET loading='0'")
    	con.commit()
    except mdb.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        sys.exit(1)
    finally:        
        if con:    
            con.close()

def writedb (akw,fcount):
    loading = 0
    con = None
    try:
        con = mdb.connect('localhost', 'db_user', 'db_pass', 'emob');
        cur = con.cursor()
	cur.execute("SELECT loading FROM emob.settings")
	ldata = cur.fetchone()
	loading = ldata[0]
	if loading == 1:
	    cur.execute("SELECT total FROM emob.countings")
	    data = cur.fetchone()
	    tkw = data[0]
	    if fcount == 100:    
		tkw += 1
	    sql = "UPDATE emob.countings SET actual=%s, total=%s"
	    cur.execute(sql, (akw,tkw))
    	con.commit()
    except mdb.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        sys.exit(1)
    finally:        
        if con:    
            con.close()
    return loading

def writerem (rload):
    con = None
    try:
        con = mdb.connect('remote_host', 'db_user', 'db_pass', 'ips');
        cur = con.cursor()
	sql = "UPDATE ips.station SET loading=%s"
	cur.execute(sql, (rload))
    	con.commit()
    except mdb.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        sys.exit(1)
    finally:        
        if con:    
            con.close()

def postit(pload, pkw):
    Empfaenger = 'your@mail.here'
    Absender = 'your@mail.here'
    Passwort = 'password'
    smtpserver = smtplib.SMTP_SSL('mailserver_hostname',465)
    smtpserver.ehlo()

    # In Account einloggen
    smtpserver.login(Absender, Passwort)

    # Text
    if pload == 1:
	Wert = 'Ladevrogang gestartet'
    if pload == 0:
	Wert = 'Ladevorgang beendet: %s kW' % pkw
    msg = MIMEText(Wert)

    # Betreff
    msg['Subject'] = 'Nachricht von der Ladestation'

    # Absender
    msg['From'] = 'ladestation'

    #Empfaenger
    msg['To'] = Empfaenger

    # E-Mail abschicken
    smtpserver.sendmail(Absender, [Empfaenger], msg.as_string())
    smtpserver.quit()

unsetload()
writedb(0,0)

while True: 
	imp = RCtime(2)
	now = datetime.now()
	delta = now - last
	if imp == 1:
	    pstart = 0
	    lastlast = last
	    last = now
	    delta = last - lastlast
	if delta.seconds < 30 and pstart != 1:
	    if lcheck == 1 and imp == 1:
		hcount += 1
		if hcount > 100:
		    hcount = 1	    
		gread += 0.00125
    		writedb(gread,hcount)
	    if lcheck == 0 and imp == 1:
		setload()
		lcheck = 1
		writedb(gread,hcount)
		writerem(1)
		postit(1, 0)
	else:
	    if lcheck == 1:
		unsetload()
		lcheck = 0
		writerem(0)
		postit(0, gread)
		hcount = 0
		gread = 0