ENEN DEDE
home
Station ladebereit

Code ohne Leaf-Korrektur

#!/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)
GPIO.setup(2, GPIO.IN)

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

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

pstart = 1

Empf = 'your(at)mail(dot)here'
Abs = 'your(at)mail(dot)here'
Passw = 'password'
smtpserv = smtplib.SMTP_SSL('mailserver_hostname',465)
smtpserv.ehlo()

# In Account einloggen
smtpserv.login(Abs, Passw)

Wtxt = 'Ladestation neu gestartet'
cmsg = MIMEText(Wtxt)
cmsg['Subject'] = 'Nachricht von der Ladestation'

cmsg['From'] = 'ladestation'
cmsg['To'] = Empf

smtpserv.sendmail(Abs, [Empf], cmsg.as_string())
smtpserv.quit()

def RCtime (channel):
	global last
	global lcheck
	global gread
	global hcount
	global imp
	imp = 1
	now = datetime.now()
	delta = now - last
	lastlast = last
	last = now
	delta = last - lastlast
	if delta.seconds < 40:
	    if lcheck == 1:
		hcount += 1
		if hcount > 100:
		    hcount = 1	    
		gread += 0.00125
    		writedb(gread,hcount)
	    else:
		setload()
		lcheck = 1
		writedb(gread,hcount)
		writerem(1)
		postit(1, 0)

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(at)mail(dot)here'
    Absender = 'your(at)mail(dot)here'
    Passwort = 'password'
    smtpserver = smtplib.SMTP_SSL('mailserver_hostname',465)
    smtpserver.ehlo()

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

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

    # Betreff + Datum
    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)

GPIO.add_event_detect(2, GPIO.RISING, callback = RCtime, bouncetime = 200)

try:
    while True:
	imp = 0
        time.sleep(60)
	if imp == 0 and lcheck == 1:
		unsetload()
		lcheck = 0
		writerem(0)
		postit(0, gread)
		hcount = 0
		gread = 0

except KeyboardInterrupt:
    GPIO.remove_event_detect(2)