def binary_search(bfun,  seekval, lines, low = 0, high=None):
    seekval = "%s,%.2f,%.02f\n" % (time.strftime(pconfig.dformat(), time.localtime(seekval)), 0.0, 0.0)
    high = high if high is not None else len(lines)
    
    pos = bfun(lines, seekval, low, high)
    pos = (pos if pos != high else high -1)

    return pos 
Example #2
0
def write_gpcfg(width, height, uid_fbase, cfg):

    timeformat = pconfig.dformat()
    hmin = float(cfg.get("settings", "humidity_min"))
    hmax = float(cfg.get("settings", "humidity_max"))
    tmax = float(cfg.get("settings", "temperature_max"))

    # set format x "%%d/%%m\n%%H:%%M"
    gpcfg = """
    set terminal svg size %d,%d dashed linewidth 0.7
    #set output '%s.svg'

    set bmargin 4.0
    set key right top

    set yrange [%f:%f]
    set y2range [*:%f]

    set datafile separator ","

    set timefmt '%s'
    set xdata time

    set ytics 5 nomirror tc rgb "blue"
    set ylabel 'Humidity' tc rgb "blue"

    set y2tics 1 nomirror tc rgb "red"
    set y2label 'Temperature'  tc rgb "red"

    plot '%s.tdata' \
        using 1:2 with lines lt 1 linecolor rgb "blue" title 'Humidity %%', \
    ''  using 1:3 with lines lt 1 linecolor rgb "red" title 'Degrees Celsius' axes x1y2 , \
    	%f  with lines lt 3 linecolor rgb "blue" title "Humidity limits", \
    	%f  with lines lt 3 linecolor rgb "blue" title "", \
    	%f  with lines lt 3 linecolor rgb "red" title "Temperature max" axes x1y2
    """ % (
        width,
        height,
        uid_fbase,
        hmin - 10,
        hmax + 10,
        tmax + 1.5,
        timeformat,
        uid_fbase,
        hmin,
        hmax,
        tmax,
    )

    gpcfgfn = "%s.gp" % uid_fbase
    with open(gpcfgfn, "w") as gpfo:
        gpfo.write(gpcfg)
def main(argv=None):    
    cfg = pconfig.read('rb_preserve.cfg')

    ddir = cfg.get('settings', 'data_dir')
    timeformat = pconfig.dformat();
    pwd = os.path.dirname(os.path.realpath(__file__))
    
    #sample sensor
    humidity = None
    temperature = None
    
    while humidity == None and temperature == None:
        humidity, temperature = poll()

    # see if 
    tmpfn = os.path.join(cfg.get('settings', 'tmp_dir'), "intermediary.data")

    if os.path.exists(tmpfn):
        with open(tmpfn, 'r') as pd:
            lines = pd.readlines()
    else:
        lines = []
    
    lines.append("%.2f,%.02f\n" % (humidity, temperature))
    
    if len(lines) >= int(cfg.get('settings', 'sample_period')):        
        avg_hum, avg_temp = condense(lines)
        warning_test(avg_hum, avg_temp, cfg)
        lines = []   # zerro tmp file
        
        #store in persistent file
        pfname = "%s/%s/%s.data" % (pwd, ddir, time.strftime(pconfig.dfilename_fmt()))
        pstr = "%s,%.2f,%.02f\n" % (time.strftime(timeformat), avg_hum, avg_temp)
        with open(pfname, "a") as outfile:
            outfile.write(pstr)

    #overwrite existing tmp file with current data
    with open(tmpfn, 'w') as pd:
        pd.write("".join(lines))
def _l2secs(line):
    tl = line.split(',')[0]
    return time.mktime(time.strptime(tl, pconfig.dformat()))
def webreq(form):
  cfg = pconfig.read('rb_preserve.cfg')

  #default time is past 24 hours
  firstvalue, lastvalue = plot.data_span()
  
  tend =   lastvalue
  deftimeview = 60*60*int(cfg.get('settings', 'default_view_hours'))

  timeformat = pconfig.dformat()

  getvals = {
    'begin': time.strftime(timeformat, time.localtime(tend - deftimeview)),
    'end': time.strftime(timeformat, time.localtime(tend)),
    'width': 960,
    'height' : 720,
    'origin' : ""
  }

  for k,v in getvals.iteritems():
    getvals[k] = cond_read(k, v, form)

  #try parsing string values 
  try:
    getvals['end'] = time.mktime(time.strptime(getvals['end'], timeformat))
  except:
    getvals['end'] = tend

  try:
    getvals['begin'] = time.mktime(time.strptime(getvals['begin'], timeformat)) 
  except:
    getvals['begin'] = tend - deftimeview

    
  # test that begin is before end
  if getvals['begin'] >= getvals['end']:
      if getvals['origin'] == "end":
        getvals['begin'] = getvals['end'] - deftimeview
      elif getvals['end'] >= tend:
        getvals['begin'] = tend - deftimeview
        getvals['end'] = tend
      else:
        getvals['end'] = getvals['begin'] + deftimeview

  if getvals['end'] >= tend:
    getvals['end'] = tend
  elif getvals['end'] <= firstvalue:
    getvals['end'] = firstvalue + deftimeview
    getvals['begin'] = firstvalue
    
  if getvals['begin'] < firstvalue:
    getvals['begin'] = firstvalue

  return plot.draw_svg(getvals['begin'], getvals['end'], 
                      int(getvals['width']), int(getvals['height'])).replace("</svg>","""
  <script type="text/javascript">
    top.max_secs = function(){return %f;};
    top.show_range();
    top.set_time_pickers(%f, %f);
  </script>
</svg>""" % (lastvalue *1000, getvals['begin']*1000, getvals['end']*1000))