#!/usr/bin/env python

# Mike Zelazny
# Example on how to use KlystronList.py with aidaget

import subprocess
import KlystronList
import string

KLYS = KlystronList.KlystronList()
for klystron in KLYS:
   try:
      args = [ 'aidaget',  klystron + '//HSTA' ]
      process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      hsta_list = process.communicate()
      nHSTA = 0
      for each_hsta in string.split(hsta_list[0]):
          nHSTA += 1
          if 1 == nHSTA:
              hsta = string.atoi(each_hsta,16)
      if (hsta & 1):
         print(klystron + ' is online')
      else:
         print(klystron + ' is offline')
   except ValueError as e:
      print('aidaget ' + klystron + '//HSTA failed!')
