All pastes #962352 Raw Edit

Something

public text v1 · immutable
#962352 ·published 2008-03-29 18:29 UTC
rendered paste body
#!/usr/bin/python
# readtags.py		read tags for 10 seconds and report results
#
# \file readtags.py
# \author Rehmi Post <rehmi@thingmagic.com>
# \ingroup ReferenceCodE
# Copyright (c) 2004 ThingMagic LLC. All Rights Reserved.
#
# readtags.py
#
# read tags for 10 seconds and report results
#

import sys, time, telnetlib, socket

default_sqlport = 8080
default_host = '10.0.0.101'
default_runtime = 10
default_proto = 'EPC0'
default_antenna = 1
default_timeout = 600

def multirange(host, port=8080, proto=None, runtime=10,
               antenna_id=None, timeout=None):
  tn = telnetlib.Telnet()

  q  = "RESET; DECLARE q CURSOR FOR"
  q += " SELECT read_count, protocol_id, antenna_id, id, dspmicros, timestamp"
  q += " FROM tag_id"
  if proto != None:
    q += " WHERE protocol_id=\'%s\'" % (proto)
  if antenna_id != None:
    q += " AND antenna_id=%d" % (antenna_id)
  if timeout != None:
    q += " SET time_out=%d" % (timeout)
  q += ";"
  q += " SET auto q=ON, repeat=0;"

  print '-- Constructed query "%s"' % (q)

  print "-- Connecting to %s..."%host
  try:
    tn.open( host, port )
  except socket.error:
    raise "Error 2: "+host+" sql server is down"
  print "-- connected."

  print '-- Sending query'
  tn.write (q)

  start = time.time()
  count = 0

  # result set - key: (tag id, antenna_id),  value: read_count
  results = {}

  # Process search results as they come in
  while time.time()-start <= runtime:
    output = tn.read_until("\n",1)

    # Provide feedback
#    print '%s' % (output[:-1])

    # Process line
    if output and output != "\n":
      try:
	read_count_str, protocol_id_str, antenna_id_str, tag_id, dspmicros_str, timestamp_str = output.split('|');

	read_count = int(read_count_str)
	protocol_id = int(protocol_id_str)
	antenna_id = int(antenna_id_str)
	dspmicros = int(dspmicros_str)
	timestamp = float(timestamp_str)

	key = (antenna_id, tag_id)
	if results.has_key(key):
	  cum_read_count = results[key]
	else:
	  cum_read_count = 0
	cum_read_count += read_count
	results[key] = cum_read_count

      except (ValueError):
	print "Can't parse line \"%s\""%output

  # Stop search
  tn.write("SET auto=OFF;")
#  time.sleep( 0.5 )  # give command time to go through
  tn.close()
  print "-- Connection closed, done!"

  return results



def report(results):
  # Sort by tag_id, antenna_id
  resultlist = []
  for key in results.keys():
    antenna_id, tag_id = key
    read_count = results[key]
    resultlist.append([antenna_id, read_count, tag_id])

  all_tags = []
  for tag in resultlist:
    id = tag[2]
    if not (id in all_tags):
      all_tags.append(id)

  tag_results = [0]*len(all_tags)

  for tag in resultlist:
    count = tag[1]
    id = tag[2]
    if id in all_tags:
      tag_results[all_tags.index(id)] = count

  # print out a report
  total_ant = {}
  total = 0
  for record in resultlist:
    ant, count, tag = record
    print "%5d reads of %s on antenna %d speaking protocol %s" % (count,tag,ant, default_proto)
    if ant in total_ant.keys():
      total_ant[ant] += count
    else:
      total_ant[ant] = count
    total += count

  for ant in total_ant.keys():
    print "%5d total reads on antenna %d" % (total_ant[ant], ant)
  print "%5d total reads" % (total)



def run ():
  print "-- Initiating read at %s" % time.ctime()
  results = multirange(host=default_host,runtime=default_runtime,proto=default_proto, antenna_id=default_antenna, timeout=default_timeout)
  print "-- Read complete at %s" % time.ctime()
  report(results)


if __name__ == '__main__':
  run()