You are on page 1of 1

def precal_table(tagid,date,kWh):

#---------------Monthly------------------#
month="'"+str(system.date.format(date,"yyyy-MM-01 00:00:00"))+"'"
shared.sgg_ems.Ems_precal_table.precal_tableUpdate(tagid,month,'Monthly',kWh)
day="'"+str(system.date.format(date,"yyyy-MM-dd 00:00:00"))+"'"
shared.sgg_ems.Ems_precal_table.precal_tableUpdate(tagid,day,'daily',kWh)
hour ="'"+str(system.date.format(date,"yyyy-MM-dd HH:00:00"))+"'"
shared.sgg_ems.Ems_precal_table.precal_tableUpdate(tagid,hour,'hourly',kWh)

#update consolidate table


def precal_tableUpdate(tagid,TimeFieldValue,tableName,kWh):
readquery="select Time,Value, Maximum, Minimum from "+ tableName +" where
tagid=? and time= " +TimeFieldValue
val=system.db.runPrepQuery(readquery,[tagid],"sgg_EMS")
row_count=val.getRowCount()
if row_count==0:
insertquery=" insert into "+ tableName + "(tagid, time,
Value,Maximum,Minimum,Average,StdDev) values( " +str(tagid)+" , "+ TimeFieldValue +
", "+ str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " + str(kWh) + ", " +
str(0)+" )"
system.db.runUpdateQuery(insertquery,"sgg_EMS")
else:
newvalue=val.getValueAt(0,1)+kWh
Max=val.getValueAt(0,2)
Min=val.getValueAt(0,3)
if Max is None:
Max=0
if Min is None:
Min=0
if kWh>Max:
Max=kWh
if kWh<Min:
Min=kWh
n=system.date.minutesBetween(val.getValueAt(0,0),date)/5
# if n==0:
# n=1
Avg=newvalue/(n+1)
updatequery="update " + tableName + " set Value = "+str(newvalue)+",
Maximum = "+str(Max)+", Minimum = "+str(Min)+", Average = "+str(Avg)+" where tagid=
"+str(tagid)+" and time= "+ TimeFieldValue
system.db.runUpdateQuery(updatequery,"sgg_EMS")

You might also like