Jumat, 26 Juni 2020

Python Code DB to HDW

APP_LAMPU slides
In [5]:
import os
import MySQLdb
import time
from time import gmtime, strftime
In [6]:
def updateDB(cursor,db,idx):
    sql = "UPDATE `tb_lampu`  set `tag`='1' where id_lampu='%s'" % (idx)
    #print(sql)
    v=0
    try:
        cursor.execute(sql)
        db.commit()
        v=1
    except:
        db.rollback()
        return v
    
def lastDB(cursor,db,idx):
    cursor.execute("SELECT `id_lampu` FROM `tb_lampu` where `tag`='0' order by id_lampu desc limit 0,1")
    v=0
    for row in cursor.fetchall():
        v=row[0]

    return v
In [ ]:
loadcell=100
auto=True
    
while (True):
    ada=0
    db = MySQLdb.connect("localhost","root","","db_isport")
    cursor = db.cursor()
    
    cursor.execute("SELECT `id_lampu`,`status`,`request` FROM `tb_lampu` where `tag`='0' order by `id_lampu` asc limit 0,1")
    for row in cursor.fetchall():
        ada=1
        id_lampu=row[0]
        status=row[1]
        request=row[2]
        print(str(id_lampu)+"#"+str(status)+"#"+str(request))
        updateDB(cursor,db,id_lampu)
        
        
    if ada>0:
        if status=="AUTO":
            print("AUTOMATIC ON")
            auto=True
        elif status=="MANUAL":
            print("MANUAL ON")
            auto=False
            
            
        if auto==True:
            if request=="Badminton" and  loadcell >=1000:
                print("Badminton ON")
            elif request=="Badminton" and  loadcell <1000:
                print("Badminton OFF")
            elif request=="Volley" and  loadcell <1000:
                print("Volley OFF")
            elif request=="Volley" and  loadcell <1000:
                print("Volley OFF")
            elif request=="Futsal" and  loadcell <1000:
                print("Futsal OFF")
            elif request=="Futsal" and  loadcell <1000:
                print("Futsal OFF")
                
       
        if status=="ON":
            if auto==True:
                print("GAGAL NYALA KAN-[ST AUTO]:"+ request)
            else:
                print("SUKSES NYALA-[ST MANUAL]:"+ request)
                #GPIO ON
                
        elif status=="OFF":
            if auto==True:
                print("GAGAL PADAM KAN-[ST AUTO]:"+ request)
            else:
                print("SUKSES PADAM-[ST MANUAL]:"+ request)
                #GPIO OFF
            
        
            
                
    time.sleep(1)
    db.close()
1#ON#Badminton
UPDATE `tb_lampu`  set `tag`='1' where id_lampu='1'
Badminton OFF
GAGAL NYALA KAN-[ST AUTO]:Badminton
1#ON#Badminton
UPDATE `tb_lampu`  set `tag`='1' where id_lampu='1'
Badminton OFF
GAGAL NYALA KAN-[ST AUTO]:Badminton
1#MANUAL#Badminton
UPDATE `tb_lampu`  set `tag`='1' where id_lampu='1'
MANUAL ON
1#ON#Badminton
UPDATE `tb_lampu`  set `tag`='1' where id_lampu='1'
SUKSES NYALA-[ST MANUAL]:Badminton
In [ ]:
 

Tidak ada komentar:

Posting Komentar