#! /usr/bin/env python
#  Routine to collect from the AM2315 Temp/Humidity Sensor
#  and pass along to the network for ultimately data display
#
#  borrowed various online routines for AM2315 library
#   31 May 2017   s.poulos
#
import os
import sys
import time
import signal
from AM2315 import *

sensor=AM2315()

def slowtimer(signum,junk):
#	print('Signal handler called with signal',signum)
	exit()

signal.signal(signal.SIGALRM,slowtimer)  # setup to send alarm to program timer 
signal.alarm(5)   # enable the alarm for 5 secs

# default unit number (or which lab unit)  to print out
unit = 1
if len(sys.argv) > 1:
        unit = sys.argv[1]

#while 1:
humid = 999
while humid > 100:
	humid = sensor.humidity()
	time.sleep(.001)   # sleep 10msec,delay
temp = 999
while temp > 100:
	temp =sensor.temperature()
	time.sleep(.001)   # sleep 10msec,delay
today = time.localtime()

new_rec = "%4d %03d %02d %02d %02d 000 TmpC_Hum_0%c %s %s " % (today.tm_year, today.tm_yday, today.tm_hour, today.tm_min, today.tm_sec,unit, temp, humid)

print(new_rec)
signal.alarm(0)     # Disable the timer alarm
#time.sleep(2.0)
