Example #1
0
def deamon(soc=-1):
  """ Main loop, gets battery data, gets summary.py to do logging"""
  try:
    import summary
    logsummary = summary.Summary()
    summary = logsummary.summary
    printtime = time.strftime("%Y%m%d%H%M%S ", time.localtime())
    while int(printtime) <= int(summary['current']['timestamp']):
      print(printtime,summary['current']['timestamp'])
      print ("Error: Current time before last sample time")
      time.sleep(30)
      printtime = time.strftime("%Y%m%d%H%M%S", time.localtime())
    batdata = Readings()  # initialise batdata after we have valid sys time
    alarms = Alarms(batdata,summary) # initialise alarms

    print (str(printtime))
    filecopy(config['files']['summaryfile'],config['files']['summaryfile']+"R" + str(int(printtime)))
    if soc > config['battery']['capacity']:
      print ("Battery DOD must be less than Battery Capacity")

    else:
      if soc < 0:
         batdata.soc = summary['current']['ah'][0]
         batdata.socadj = summary['current']['dod'][0]
      else:
        batdata.soc = soc
        batdata.socadj = soc
        summary['current']['dod'][3] = 0
      summary['current']['dod'][3] = -100 # flag don't adjust leakage current
      prevtime = logsummary.currenttime
      prevbatvoltage = batdata.batvoltsav[numcells]
  #    logsummary.startday(summary)
  #    logsummary.starthour(summary)


      while True:
        try:
          for i in range(config['sampling']['samplesav']):
  #          printvoltage = ''
  #          for i in range(numcells+1):
  #            printvoltage = printvoltage + str(round(batdata.batvolts[i],3)).ljust(5,'0') + ' '
  #         print (printvoltage)
            batdata.getraw()

  #          if batdata.batvoltsav[numcells] >= 55.2 and prevbatvoltage < 55.2:  # reset SOC counter?
  #          print batdata.socadj/(float(summary['current']['dod'][3])*24.0)
            if batdata.batvoltsav[numcells] < config['battery']['vreset'] \
            and prevbatvoltage >= config['battery']['vreset'] \
            and summary['current']['dod'][3] != 0 \
            and -batdata.currentav[0] < config['battery']['ireset']:  # reset SOC counter?

              if summary['current']['dod'][3] <= 0 :
                socerr=0
              else:
                socerr=batdata.socadj/(float(summary['current']['dod'][3])*24.0)
                socerr=max(socerr,-0.01)
                socerr=min(socerr,0.01)
              config['battery']['ahloss']=config['battery']['ahloss']-socerr/2
              batconfigdata=SafeConfigParser()
              batconfigdata.read('battery.cfg')
              batconfigdata.set('battery','ahloss',str(config['battery']['ahloss']))
              with open('battery.cfg', 'w') as batconfig:
                batconfigdata.write(batconfig)
              batconfig.closed

              batdata.soc = 0.0
              batdata.socadj = 0.0
              summary['current']['dod'][3] = 0
            else:
              batdata.soc = batdata.soc + batdata.batah
              batdata.socadj = batdata.socadj +batdata.batahadj
            batdata.ah = batdata.ah + batdata.batah
            batdata.inahtot = batdata.inahtot + batdata.inah
            batdata.pwrbattot = batdata.pwrbattot + batdata.pwrbat
            batdata.pwrintot = batdata.pwrintot + batdata.pwrin
          prevbatvoltage = batdata.batvoltsav[numcells]
  # check alarms
          alarms.scanalarms(batdata)
  # update summaries
          logsummary.update(summary, batdata)
          if logsummary.currenttime[4] != logsummary.prevtime[4]:  # new minute
            loadconfig()
            logsummary.updatesection(summary, 'hour', 'current')
            logsummary.updatesection(summary, 'alltime','current')
            logsummary.updatesection(summary, 'currentday','current')
            logsummary.updatesection(summary, 'monthtodate', 'current')
            logsummary.updatesection(summary, 'yeartodate', 'current')
            logsummary.writesummary()
            batdata.ah = 0.0
            batdata.ahadj = 0.0
            batdata.inahtot = 0.0
            batdata.pwrbattot = 0.0
            batdata.pwrintot = 0.0
            for i in range(batdata.numiins):
              batdata.kWhin[i] = 0.0
              batdata.kWhout[i] = 0.0
            for i in range(numcells):
              batdata.baltime[i]=0


          if logsummary.currenttime[3] != logsummary.prevtime[3]:  # new hour
            logsummary.starthour(summary)

          if logsummary.currenttime[3] < logsummary.prevtime[3]: # newday
            logsummary.startday(summary)

          if logsummary.currenttime[1] != logsummary.prevtime[1]: # new month
            logsummary.startmonth(summary)

          if logsummary.currenttime[0] != logsummary.prevtime[0]: # new year
            logsummary.startyear(summary)

        except KeyboardInterrupt:
          sys.stdout.write('\n')
          logsummary.close()
          sys.exit(9)
          break
  except Exception as err:
    log.critical(err)
    raise
Example #2
0
def deamon(soc=-1):
    """ Main loop, gets battery data, gets summary.py to do logging"""

    try:
        import summary
        logsummary = summary.Summary()
        summary = logsummary.summary
        printtime = time.strftime("%Y%m%d%H%M%S ", time.localtime())
        while int(printtime) <= int(summary['current']['timestamp']):
            print(printtime, summary['current']['timestamp'])
            print("Error: Current time before last sample time")
            time.sleep(30)
            printtime = time.strftime("%Y%m%d%H%M%S", time.localtime())
        batdata = Readings()  # initialise batdata after we have valid sys time
        alarms = Alarms(batdata)  # initialise alarms

        print(str(printtime))
        filecopy(config['files']['summaryfile'],
                 config['files']['summaryfile'] + "R" + str(int(printtime)))
        if soc > config['battery']['capacity']:
            print("Battery DOD must be less than Battery Capacity")

        else:
            if soc < 0:
                batdata.soc = summary['current']['ah'][0]
                batdata.socadj = summary['current']['dod'][0]
            else:
                batdata.soc = soc
                batdata.socadj = soc
                summary['current']['dod'][3] = 0
            summary['current']['dod'][
                3] = -100  # flag don't adjust leakage current
            prevtime = logsummary.currenttime
            prevbatvoltage = batdata.batvoltsav[numcells]
            #    logsummary.startday(summary)
            #    logsummary.starthour(summary)

            while True:
                try:
                    for i in range(config['sampling']['samplesav']):
                        #          printvoltage = ''
                        #          for i in range(numcells+1):
                        #            printvoltage = printvoltage + str(round(batdata.batvolts[i],3)).ljust(5,'0') + ' '
                        #         print (printvoltage)
                        batdata.getraw()

                        #          if batdata.batvoltsav[numcells] >= 55.2 and prevbatvoltage < 55.2:  # reset SOC counter?
                        #          print batdata.socadj/(float(summary['current']['dod'][3])*24.0)
                        if batdata.batvoltsav[numcells] < config['battery']['vreset'] \
                        and prevbatvoltage >= config['battery']['vreset'] \
                        and summary['current']['dod'][3] != 0 \
                        and -batdata.currentav[0] < config['battery']['ireset']:  # reset SOC counter?

                            if summary['current']['dod'][3] <= 0:
                                socerr = 0
                            else:
                                socerr = batdata.socadj / (
                                    float(summary['current']['dod'][3]) * 24.0)
                                socerr = max(socerr, -0.01)
                                socerr = min(socerr, 0.01)
                            config['battery']['ahloss'] = config['battery'][
                                'ahloss'] - socerr / 2
                            batconfigdata = SafeConfigParser()
                            batconfigdata.read('battery.cfg')
                            batconfigdata.set('battery', 'ahloss',
                                              str(config['battery']['ahloss']))
                            with open('battery.cfg', 'w') as batconfig:
                                batconfigdata.write(batconfig)
                            batconfig.closed

                            batdata.soc = 0.0
                            batdata.socadj = 0.0
                            summary['current']['dod'][3] = 0
                        else:
                            batdata.soc = batdata.soc + batdata.batah
                            batdata.socadj = batdata.socadj + batdata.batahadj
                        batdata.ah = batdata.ah + batdata.batah
                        batdata.inahtot = batdata.inahtot + batdata.inah
                        batdata.pwrbattot = batdata.pwrbattot + batdata.pwrbat
                        batdata.pwrintot = batdata.pwrintot + batdata.pwrin
                    prevbatvoltage = batdata.batvoltsav[numcells]
                    # check alarms
                    alarms.scanalarms(batdata)
                    # update summaries
                    logsummary.update(summary, batdata)
                    if logsummary.currenttime[4] != logsummary.prevtime[
                            4]:  # new minute
                        loadconfig()
                        logsummary.updatesection(summary, 'hour', 'current')
                        logsummary.updatesection(summary, 'alltime', 'current')
                        logsummary.updatesection(summary, 'currentday',
                                                 'current')
                        logsummary.updatesection(summary, 'monthtodate',
                                                 'current')
                        logsummary.updatesection(summary, 'yeartodate',
                                                 'current')
                        logsummary.writesummary()
                        batdata.ah = 0.0
                        batdata.ahadj = 0.0
                        batdata.inahtot = 0.0
                        batdata.pwrbattot = 0.0
                        batdata.pwrintot = 0.0
                        for i in range(batdata.numiins):
                            batdata.kWhin[i] = 0.0
                            batdata.kWhout[i] = 0.0
                        for i in range(numcells):
                            batdata.baltime[i] = 0

                    if logsummary.currenttime[3] != logsummary.prevtime[
                            3]:  # new hour
                        logsummary.starthour(summary)

                    if logsummary.currenttime[3] < logsummary.prevtime[
                            3]:  # newday
                        logsummary.startday(summary)

                    if logsummary.currenttime[1] != logsummary.prevtime[
                            1]:  # new month
                        logsummary.startmonth(summary)

                    if logsummary.currenttime[0] != logsummary.prevtime[
                            0]:  # new year
                        logsummary.startyear(summary)

                except KeyboardInterrupt:
                    sys.stdout.write('\n')
                    logsummary.close()
                    sys.exit(9)
                    break
    except Exception as err:
        log.critical(err)
        raise
Example #3
0
    if currenttime[8:10] != prevtime[8:10]:  # new hour
        logsummary.starthour(summary)

    if currenttime[6:8] != prevtime[6:8]:  # newday
        logsummary.startday(summary)

    if currenttime[4:6] != prevtime[4:6]:  # new month
        logsummary.startmonth(summary)

    if currenttime[0:4] != prevtime[0:4]:  # new year
        logsummary.startyear(summary)

import summary
logsummary = summary.Summary()
batdata = Readings()  # initialise batdata
alarms = Alarms(batdata, summary)  # initialise alarms


def deamon(soc=-1):
    """Battery Management deamon to run in background"""

    numtries = 0
    while True:
        try:
            initmain(soc)
            while True:
                mainloop()
                numtries = 0
        except KeyboardInterrupt:
            numtries = maxtries