def fetchMeters(f): query = models.mMaster.query.all() meters = [] details = {} dm = [] res = [] if query is not None: for meter in query: info = models.mAssociations.query.filter_by( meter=meter.as_dict()["meter"]).first() if info is not None and info.as_dict()["active"]: meters.append(meter.as_dict()) if len(meters) > 0: for meter in meters: m = meter info = models.mAssociations.query.filter_by( meter=m["meter"]).first() m["active"] = int(bool(gf.Map( info.as_dict()).active)) if info is not None else 0 m["name"] = gf.Map(info.as_dict()).name if info is not None else "" m["location"] = gf.Map( info.as_dict()).location if info is not None else "" m["ReadData"], m["kilos"], m["lastread"] = getMeterDetails(m, f) dm.append(m) return dm
def getMeters(): user_ = auth.Adminuser if user_ and user_["isAuthenticated"]: form = gf.Map(request.form.to_dict()) meters_ = api.fetchMeters(form)#fetching info from db # Call the callApi function to create a usable # object named apiObject from the API request URL. # Put the API request URL in the call # cnt_offset = f.cnt # meters_v3 = api.callApi("http://io.ekmpush.com/readMeter?ver=v3&key=Mzg3MzcyOjg2NDM5MQ&fmt=json&cnt="+cnt_offset+"&tz=America~Jamaica") # meters_v4 = api.callApi("http://io.ekmpush.com/readMeter?ver=v4&key=Mzg3MzcyOjg2NDM5MQ&fmt=json&cnt="+cnt_offset+"&tz=America~Jamaica") # meters = meters_v3 # v4_data = meters_v4["readMeter"]["ReadSet"] # for om in v4_data: # meters["readMeter"]["ReadSet"].append(om) return json.dumps({"meters":meters_},default=str) else: return json.dumps({"meters":"reload"})
def updateMeterInfo(info): meter = models.mAssociations.query.filter_by(meter=info.meter).first() error = "" result = None if str(info.set) == "name": meter.name = info.val elif str(info.set) == "location": meter.location = info.val elif str(info.set) == "active": meter.active = info.val updateMeteriod(gf.Map({"active": info.val, "meter": info.meter})) db_sess = db.session #open database session try: db_sess.commit() #commit changes except sqlalchemy.exc.SQLAlchemyError as e: db_sess.rollback() db_sess.flush() # for resetting non-commited .add() finally: db_sess.close() if error: result = None #[data] #prepare visual data else: assoc = models.mAssociations.query.filter_by(meter=info.meter).first() result = {"info": assoc.as_dict()} return result
def getMeterDetails(m, qry): details = [] data = None meter = gf.Map(m) m_ = meter res = [] lastread = 0 kilos = { "kilo_then": 0, "kilo_now": 0, "kilo_fd": 0, "kilo_td": 0 } #store kilo_total time period db_sess = db.session if meter.protocol == "v3": ld = db_sess.query(func.max(models.mDetailsv3.date)).scalar() lastread = ld data = models.mDetailsv3.query.filter( models.mDetailsv3.date == ld).order_by( models.mDetailsv3.time.desc()).filter_by(meter=m_.meter).all() elif meter.protocol == "v4": ld = db_sess.query(func.max(models.mDetailsv4.Date)).scalar() lastread = ld data = models.mDetailsv4.query.filter( models.mDetailsv4.Date == ld).order_by( models.mDetailsv4.Time.desc()).filter_by(meter=m_.meter).all() if data is not None: for detail in data: details.append(detail.as_dict()) res = details if len(res) > 0: db_sess.close() if len(res) > 0: fdd = str(res[-1:][0]["date"]) if "date" in res[-1:][0] else str( res[-1:][0]["Date"]) fdt = str(res[-1:][0]["time"]) if "date" in res[-1:][0] else str( res[-1:][0]["Time"]) fd_ = fdd + " " + fdt #from date tdd = str(res[0:1][0]["date"]) if "date" in res[0:1][0] else str( res[0:1][0]["Date"]) tdt = str(res[0:1][0]["time"]) if "date" in res[0:1][0] else str( res[0:1][0]["Time"]) td_ = tdd + " " + tdt #to date kilos["kilo_then"] = res[-1:][0]["kWh_Tot"] if res[-1:][0][ "kWh_Tot"] is not None else 0 kilos["kilo_fd"] = gf.parseStrDate(fd_) kilos["kilo_now"] = res[0:1][0]["kWh_Tot"] if res[0:1][0][ "kWh_Tot"] is not None else 0 kilos["kilo_td"] = gf.parseStrDate(td_) lastread = td_ return [res[0:int(qry.cnt)], kilos, lastread]
def addMeterInfo(): user_ = auth.Adminuser if user_ and user_["isAuthenticated"]: form = gf.Map(request.form.to_dict()) info_ = api.setMeterInfo(form)#fetching info from db return json.dumps({"detail":info_}) else: return json.dumps({"meters":"reload"})
def getMeterLastReadData(): user_ = auth.Adminuser if user_ and user_["isAuthenticated"]: form = gf.Map(request.form.to_dict()) data_ = api.fetchMeterLastReadData(form)#fetching info from db return json.dumps({"meters":data_},default=str) else: return json.dumps({"meters":"reload"})
def getKilos(): user_ = auth.Adminuser if user_ and user_["isAuthenticated"]: form = gf.Map(request.form.to_dict()) kilos_ = api.fetchMeterKilos(form)#fetching info from db return json.dumps({"meters":kilos_},default=str) else: return json.dumps({"meters":"reload"})
def login(): form = gf.Map(request.form.to_dict()) user_ = auth.authenticate(form.username,form.password) if user_ and user_["isAuthenticated"]: if auth.Adminuser["session"] is None: auth.Adminuser["session"] = api.createSession(user_) return render_template("index.html",title="Meteriod",year=gf.year(),user=user_) return render_template("login.html",title="Meteriod | Login",form=[1],user=user_) return render_template("login.html",title="Meteriod | Login",form=[1],user=user_)
def createSession(auth_user): now = gf.now() user = gf.Map(auth_user) session_ = gf.randomString(16) user_id = user.id session_exist, old_session = sessionExist(user, session_) error = "" if session_exist: result = old_session.session_ #prepare visual data else: session = models.Session(session_=str(session_), created_date=now, active=1, user_id=user.id) db_sess = db.session #open database session try: db_sess.add(session) #add prepared statment to opened session db_sess.commit() #commit changes except sqlalchemy.exc.SQLAlchemyError as e: db_sess.rollback() db_sess.flush() # for resetting non-commited .add() error = {"error": e, "session": session_} finally: db_sess.close() db_sess.flush() # for resetting non-commited .add() if error: result = error #[data] #prepare visual data else: new_session = models.Session.query.filter_by(id=session.id).first() result = new_session.session_ #prepare visual data return result
def adminAction(): form = gf.Map(request.form.to_dict()) meters = api.meteriodAction(form) users = api.getMeterUsers() #to implement user registration from admin action... #deffered implemention - unecessary return render_template("admin.html",meters=meters,users=users)
def admin(): form = gf.Map(request.form.to_dict()) meters = api.fetchActiveMeters() users = api.getMeterUsers() return render_template("admin.html",meters=meters,users=users)
def flagMeter(): form = gf.Map(request.form.to_dict())
def silentMeters(): form = gf.Map(request.form.to_dict()) meter = api.silenceMeter(form.meter)#fetching info from db return render_template("meter_silenced.html",meter=form.meter)
def fetchMeterKilos(m): details = [] fdates = [] #store data for the first date in the month that has with data data = None meter = gf.Map(m) res = 0 end_of_ld = 1 #used to seek detail (kwh_tot) for end of day for last date in res # print(meter) kilos = { "kilo_then": 0, "kilo_now": 0, "kilo_fd": 0, "kilo_td": 0 } #store kilo_total time period if meter.protocol == "v3": data = models.mDetailsv3.query.filter( models.mDetailsv3.date.between(meter.sd, meter.ed), models.mDetailsv3.kWh_Tot.isnot(None)).order_by( models.mDetailsv3.date.desc(), models.mDetailsv3.time.desc()).filter_by( meter=meter.meter).all() elif meter.protocol == "v4": data = models.mDetailsv4.query.filter( models.mDetailsv4.Date.between(meter.sd, meter.ed), models.mDetailsv4.kWh_Tot.isnot(None)).order_by( models.mDetailsv4.Date.desc(), models.mDetailsv4.Time.desc()).filter_by( meter=meter.meter).all() if data is not None: for detail in data: details.append(detail.as_dict()) res = details if len(res) > 0: fdd = str(res[-1:][0]["date"]) if "date" in res[-1:][0] else str( res[-1:][0]["Date"] ) #seek last date with kilowatt data which would be the first good read of that day. ftr = str(res[-1:][0]["time"]) if "date" in res[-1:][0] else str( res[-1:][0]["Time"]) fdr_ = fdd + " " + ftr for detail in res: fdate = detail["date"] if "date" in detail else detail["Date"] ftime = detail["time"] if "time" in detail else detail["Time"] if str(fdate) == str(fdd) and detail["kWh_Tot"] != 0: fdates.append( fdate ) #push all times read good for that day to get last good read for that day if len(fdates) > 0: end_of_ld = len(fdates) fdt = str(res[-end_of_ld:][0] ["time"]) if "date" in res[-end_of_ld:][0] else str( res[-end_of_ld:][0]["Time"]) fd_ = fdd + " " + fdt tdd = str(res[0:1][0]["date"]) if "date" in res[0:1][0] else str( res[0:1][0]["Date"]) tdt = str(res[0:1][0]["time"]) if "time" in res[0:1][0] else str( res[0:1][0]["Time"]) td_ = tdd + " " + tdt kilos["firstread"] = fdr_ kilos["kilo_then"] = res[-end_of_ld:][0]["kWh_Tot"] if res[ -end_of_ld:][0]["kWh_Tot"] is not None else 0 kilos["kilo_fd"] = gf.parseStrDate(fd_) if str(fdd) == str( tdd ): #if last date and first date are the same then pull data from earliest datetime to last kilos["kilo_then"] = res[-1:][0]["kWh_Tot"] if res[-1:][0][ "kWh_Tot"] is not None else 0 kilos["kilo_fd"] = gf.parseStrDate(fdr_) kilos["kilo_now"] = res[0:1][0]["kWh_Tot"] if res[0:1][0][ "kWh_Tot"] is not None else 0 kilos["kilo_td"] = gf.parseStrDate(td_) return kilos