def valores(): '''Retorna valores''' rand_pot = lambda : "%.2f Kw" % (randrange(1,250)/10.) rand_pot_r = lambda : "%.2f KVa" % (randrange(1,250)/10.) rand_current = lambda : "%.2f KVa" % (randrange(1000,5000)/10.) rand_volt = lambda : "%.2f V" % (randrange(10000, 14000)/100.) from models import Profile, COMaster, VarSys, IED, AI, DI comaster = Profile.by_name('default').comaster_set.get(address='192.168.1.97') ied1 = IED.get(co_master=comaster, addr_485_IED=1) ied2 = IED.get(co_master=comaster, addr_485_IED=2) ied3 = IED.get(co_master=comaster, addr_485_IED=3) potencia_2 = AI.get(id=2).value potencia_2 = ("%s W" % (potencia_2 * 1.09)) if potencia_2 != 0x4000 else '0 KW' potencia_r_2 = AI.get(id=3).value potencia_r_2 = ("%s VA" % (potencia_r_2 * 1.09)) if potencia_r_2 != 0x4000 else '0 KW' return jsonify({ # Potencia 'potencia-1': rand_pot(), #'potencia-2': AI.get(ied=ied2, offset=0).value, # FIXME: Indirección correcta 'potencia-2': potencia_2, 'potencia-3': rand_pot(), 'potencia-4': rand_pot(), # Potencias reactivas 'potencia-r-1': rand_pot_r(), #'potencia-r-2': AI.get(ied=ied2, offset=1).|value, 'potencia-r-2': potencia_r_2, 'potencia-r-3': rand_pot_r(), 'potencia-r-4': rand_pot_r(), # Corrientes 'corriente-1': rand_current(), 'corriente-2': rand_current(), 'corriente-3': rand_current(), 'corriente-4': rand_current(), #'tension-1': "%.2f Kv" % AI.get(ied=ied1, offset=0).value, 'tension-1': "%.2f Kv" % (AI.get(id=1).value /float(100)), 'interruptor-1': DI.get(ied=ied1, port=1, bit=0).value, 'interruptor-2': DI.get(ied=ied1, port=1, bit=1).value, })
def iterdis(): # Iterar ieds for ied in self.factory.comaster.ied_set.order_by('offset'): # Ordenar por puerto y por bit for di in DI.filter(ied=ied).order_by(('port', 'asc'), ('bit', 'asc')): yield di
def saveInDatabase(self): print "Acutalizando DB" # print self.input from models import DI, AI, VarSys, Energy, Event payload = self.input.payload_10 # Iterar de a bit def iterdis(): # Iterar ieds for ied in self.factory.comaster.ied_set.order_by('offset'): # Ordenar por puerto y por bit for di in DI.filter(ied=ied).order_by(('port', 'asc'), ('bit', 'asc')): yield di def iterais(): # Iterar ieds for ied in self.factory.comaster.ied_set.order_by('offset'): # Itera por ais for ai in AI.filter(ied=ied).order_by('offset'): yield ai def itervarsys(): # Iterar ieds for ied in self.factory.comaster.ied_set.order_by('offset'): for varsys in VarSys.filter(ied=ied).order_by('offset'): yield varsys #======================================================================= # Guardando... #======================================================================= for varsys, value in zip(itervarsys(), payload.varsys): varsys.value = value varsys.save() for di, value in zip(iterdis(), iterbits(payload.dis)): di.value = value di.save() for ai, value in zip(iterais(), payload.ais): ai.value = value ai.save() if self.save_events: for ev in payload.event: if ev.evtype == "DIGITAL": ied = self.factory.comaster.ied_set.get(addr_485_IED=ev.addr485) di = DI.get(ied=ied, port=ev.port, bit=ev.bit) # di = comaster.dis.get(port = ev.port, bit = ev.bit) timestamp = datetime( ev.year + 2000, ev.month, ev.day, ev.hour, ev.minute, ev.second, int(ev.subsec * 100000)) Event(di=di, timestamp=timestamp, subsec=ev.subsec, q=0, value=ev.status).save() elif ev.evtype == "ENERGY": timestamp = datetime(ev.year + 2000, ev.month, ev.day, ev.hour, ev.minute, ev.second) ied = self.factory.comaster.ied_set.get(addr_485_IED=ev.addr485) Energy(ied=ied, q=ev.value.q, timestamp=timestamp, address=ev.addr485, channel=ev.channel, value=ev.value.val,).save() print ("Guardados %d eventos" % len(payload.event))