Beispiel #1
0
def sensorreading(sensorname, MinutesOfAverage, operation):
    isok = False
    # operation "average", "min" , "max" , "sum"
    if sensorname:
        timelist = hardwaremod.gettimedata(sensorname)
        theinterval = timelist[1]  # minutes
        if theinterval > 0:
            samplesnumber = int(MinutesOfAverage / theinterval + 1)
        else:
            samplesnumber = 1
            theinterval = 15
        quantity = 0
        MinutesOfAverage = samplesnumber * theinterval
        if samplesnumber > 0:
            sensordata = []
            sensordbmod.getsensordbdatasamplesN(sensorname, sensordata,
                                                samplesnumber)
            datenow = datetime.now()  # dont put UTC here !!!
            starttimecalc = datenow - timedelta(
                minutes=(MinutesOfAverage + theinterval)
            )  #if Minutes of average is zero, it allows to have at least one sample
            isok, quantitylist = sensordbmod.EvaluateDataPeriod(
                sensordata, starttimecalc, datenow)
            quantity = quantitylist[operation]
            # sensor reading value
            logger.info('Sensor reading <%s>=<%s>', sensorname, str(quantity))
            print "sensor Reading ", sensorname, "=", quantity
    return isok, quantity
Beispiel #2
0
def sensorreading(sensorname,MinutesOfAverage,operation):
	isok=False	
	# operation "average", "min" , "max" , "sum"
	if sensorname:
		timelist=hardwaremod.gettimedata(sensorname)	
		theinterval=timelist[1] # minutes
		if theinterval>0:
			samplesnumber=int(MinutesOfAverage/theinterval+1)
		else:
			samplesnumber=1
			theinterval=15
		quantity=0
		MinutesOfAverage=samplesnumber*theinterval
		if samplesnumber>0:
			sensordata=[]		
			sensordbmod.getsensordbdatasamplesN(sensorname,sensordata,samplesnumber)
			starttimecalc=datetime.now()-timedelta(minutes=(MinutesOfAverage+theinterval)) #if Minutes of average is zero, it allows to have at least one sample
			quantity=sensordbmod.EvaluateDataPeriod(sensordata,starttimecalc,datetime.now())[operation]	
			isok=True	
	return isok , quantity
Beispiel #3
0
def sensorreading(sensorname):
	MinutesOfAverage=70 #about one hour, 4 samples at 15min samples rate
	if sensorname:
		# old
		#sensordata=[]		
		#sensordbmod.getsensordbdata(sensorname,sensordata)
		# get number of samples 
		timelist=hardwaremod.gettimedata(sensorname)	
		theinterval=timelist[1] # minutes
		if theinterval>0:
			samplesnumber=int(MinutesOfAverage/theinterval+1)
		else:
			samplesnumber=1	
		# new procedure should be faster on database reading for large amount of data
		sensordata=[]
		sensordbmod.getsensordbdatasamplesN(sensorname,sensordata,samplesnumber)
		# still necessary to filter the sample based on timestamp, due to the possibility of missing samples
		starttimecalc=datetime.now()-timedelta(minutes=int(MinutesOfAverage))
		quantity=sensordbmod.EvaluateDataPeriod(sensordata,starttimecalc,datetime.now())["max"]	
	return 	quantity
Beispiel #4
0
def sensorreading(sensorname):
    MinutesOfAverage = 70  #about one hour, 4 samples at 15min samples rate
    if sensorname:
        # old
        #sensordata=[]
        #sensordbmod.getsensordbdata(sensorname,sensordata)
        # get number of samples
        timelist = hardwaremod.gettimedata(sensorname)
        theinterval = timelist[1]  # minutes
        if theinterval > 0:
            samplesnumber = int(MinutesOfAverage / theinterval + 1)
        else:
            samplesnumber = 1
        # new procedure should be faster on database reading for large amount of data
        sensordata = []
        sensordbmod.getsensordbdatasamplesN(sensorname, sensordata,
                                            samplesnumber)
        # still necessary to filter the sample based on timestamp, due to the possibility of missing samples
        starttimecalc = datetime.now() - timedelta(
            minutes=int(MinutesOfAverage))
        quantity = sensordbmod.EvaluateDataPeriod(sensordata, starttimecalc,
                                                  datetime.now())["max"]
    return quantity
Beispiel #5
0
def sensorreading(sensorname, MinutesOfAverage, operation):
    isok = False
    # operation "average", "min" , "max" , "sum"
    if sensorname:
        timelist = hardwaremod.gettimedata(sensorname)
        theinterval = timelist[1]  # minutes
        if theinterval > 0:
            samplesnumber = int(MinutesOfAverage / theinterval + 1)
        else:
            samplesnumber = 1
            theinterval = 15
        quantity = 0
        MinutesOfAverage = samplesnumber * theinterval
        if samplesnumber > 0:
            sensordata = []
            sensordbmod.getsensordbdatasamplesN(sensorname, sensordata,
                                                samplesnumber)
            starttimecalc = datetime.now() - timedelta(
                minutes=(MinutesOfAverage + theinterval)
            )  #if Minutes of average is zero, it allows to have at least one sample
            quantity = sensordbmod.EvaluateDataPeriod(
                sensordata, starttimecalc, datetime.now())[operation]
            isok = True
    return isok, quantity
Beispiel #6
0
def startpump(target, activationseconds, MinAveragetemp, MaxAverageHumid):

    logger.info('WateringPlan Startpump evaluation: %s', target)
    #workmode=autowateringmod.checkworkmode(target)
    #if workmode=="Full Auto":
    if target in autowateringmod.allowwateringplan:
        if not autowateringmod.allowwateringplan[target]:
            logger.info(
                'WateringPlan: %s pump activation blocked by automation',
                target)
            pumpit = False
            return False

    duration = hardwaremod.toint(activationseconds, 0)
    print(target, " ", duration, " ", datetime.now())

    # evaluate parameters
    #MinAverageLight=500 not used now
    MinutesOfAverage = 120  #minutes in which the average data is calculated from sensor sampling

    print("waterpump check")
    logger.info('execute water pump check %s',
                datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

    # then check the temperature and Humidity

    print("Check Humidity and Temperature")

    MinAveragetempnum = hardwaremod.tonumber(MinAveragetemp, "NA")
    MaxAverageHumidnum = hardwaremod.tonumber(MaxAverageHumid, "NA")

    # all the below conditions should be verified to start the PUMP
    pumpit = True

    hsensornamelist = hardwaremod.getsensornamebymeasure(
        hardwaremod.MEASURELIST[1])
    if hsensornamelist:
        sensordata = []
        hsensorname = hsensornamelist[0]  # get first found sensor in the list
        sensordbmod.getsensordbdata(hsensorname, sensordata)
        starttimecalc = datetime.now() - timedelta(
            minutes=int(MinutesOfAverage))
        isok, quantitylist = sensordbmod.EvaluateDataPeriod(
            sensordata, starttimecalc, datetime.now())
        humquantity = quantitylist["average"]

        logger.info(
            'Waterpump Check parameter if humquantity=%s < MaxAverageHumid=%s ',
            str(humquantity), str(MaxAverageHumid))
        print('Waterpump Check parameter if humquantity=', humquantity,
              ' < MaxAverageHumid=', MaxAverageHumid)

        if (MaxAverageHumidnum != "NA"):
            if (humquantity < MaxAverageHumidnum):
                logger.info(
                    'Humidity check PASSED, humquantity=%s < MaxAverageHumid=%s ',
                    str(humquantity), str(MaxAverageHumid))
            else:
                logger.info('Humidity check FAILED')
                print('Humidity check FAILED')
                pumpit = False

    tsensornamelist = hardwaremod.getsensornamebymeasure(
        hardwaremod.MEASURELIST[0])
    if tsensornamelist:
        sensordata = []
        tsensorname = tsensornamelist[0]  # get first found sensor in the list
        sensordbmod.getsensordbdata(tsensorname, sensordata)
        starttimecalc = datetime.now() - timedelta(
            minutes=int(MinutesOfAverage))
        isok, quantitylist = sensordbmod.EvaluateDataPeriod(
            sensordata, starttimecalc, datetime.now())
        tempquantity = quantitylist["average"]
        logger.info(
            'Waterpump Check parameter if tempquantity=%s > MinAveragetemp=%s ',
            str(tempquantity), str(MinAveragetemp))
        print('Waterpump Check parameter if tempquantity=', tempquantity,
              ' > MinAveragetemp=', MinAveragetemp)

        if (MinAveragetempnum != "NA"):
            if (tempquantity > MinAveragetempnum):
                logger.info(
                    'Temperature check PASSED, tempquantity=%s > MinAveragetemp=%s ',
                    str(tempquantity), str(MinAveragetemp))
            else:
                logger.info('Temperature check FAILED')
                print('Temperature check FAILED')
                pumpit = False

    # weather Forecast
    sensorname = weatherAPImod.DefaultCounterName()
    sensorlist = sensordbmod.gettablelist()
    if sensorname in sensorlist:
        ActiveActuatorList = weatherAPImod.ActiveActuatorList()
        if target in ActiveActuatorList:
            sensordata = []
            samplesnumber = 1
            sensordbmod.getsensordbdatasamplesN(sensorname, sensordata,
                                                samplesnumber)
            MaxPastMinutes = 1200
            starttimecalc = datetime.now() - timedelta(minutes=MaxPastMinutes)
            isok, quantitylist = sensordbmod.EvaluateDataPeriod(
                sensordata, starttimecalc, datetime.now())
            if isok:
                RainMultipier = quantitylist["average"]
                logger.info('Waterpump weather multiplier =%s ',
                            str(RainMultipier))
            else:
                logger.warning(
                    'Waterpump weather multiplier NOT found within 20 Hours apply 100 '
                )
                RainMultipier = 100
            duration = int(duration * RainMultipier / 100)
    else:
        logger.warning('Weather Sensor not found, no multpilier applied ')

    if pumpit:
        # activation of the doser before the pump
        doseron = autofertilizermod.checkactivate(target, duration)
        # watering
        #hardwaremod.makepulse(target,duration)
        activateandregister(target, duration)
        # salva su database
        #logger.info('Switch Pump %s ON, optional time for sec = %s', target, duration)
        #print 'Pump ON, optional time for sec =', duration
        #actuatordbmod.insertdataintable(target,duration)

    return True
Beispiel #7
0
def startpump(target,activationseconds,ThesholdOFFON,ThesholdONOFF):

	logger.info('WateringPlan Startpump evaluation: %s', target)	
	#workmode=autowateringmod.checkworkmode(target)
	#if workmode=="Full Auto":
	if target in autowateringmod.allowwateringplan:
		if not autowateringmod.allowwateringplan[target]:
			logger.info('WateringPlan: %s pump activation blocked by automation', target)
			pumpit=False
			return False
	
	duration=hardwaremod.toint(activationseconds,0)
	print(target, " ",duration, " " , datetime.now()) 

	print("waterpump check")
	logger.info('execute water pump check %s', datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

	# evaluate parameters
	MinutesOfAverage=hardwaremod.toint(wateringplansensordbmod.getselduration(),120) #minutes in which the average data is calculated from sensor sampling

	tsensornamelist, hsensornamelist=wateringplansensordbmod.getactivesensor()		
	
	valid1, passed1 = checksensorcondition(tsensornamelist,ThesholdOFFON, MinutesOfAverage, False)
	valid2, passed2 = checksensorcondition(hsensornamelist,ThesholdONOFF, MinutesOfAverage, True)
	
	# apply AND or OR condition
	condition=wateringplansensordbmod.getselcondition()  # "AND" , "OR"
	
	pumpit=True	
	
	
	if valid1 and valid2:
		if condition=="AND":
			pumpit=passed1 and passed2
		elif condition=="OR":
			pumpit=passed1 or passed2			
			
	else:
		if valid1:
			pumpit=passed1
		if valid2:
			pumpit=passed2
		
	print('valid1= ' , valid1 , 'valid2= ' , valid2)
	print('passed1= ' , passed1 , 'passed2= ' , passed2)
	print('pumpit= ' , pumpit, 'condition= ' , condition)
	logger.info('Condition for activation =%s ', pumpit)
				
	# weather Forecast
	sensorname=weatherAPImod.DefaultCounterName()
	sensorlist=sensordbmod.gettablelist()
	if sensorname in sensorlist:
		ActiveActuatorList=weatherAPImod.ActiveActuatorList()
		if target in ActiveActuatorList:
			sensordata=[]
			samplesnumber=1
			sensordbmod.getsensordbdatasamplesN(sensorname,sensordata,samplesnumber)
			MaxPastMinutes=1200
			starttimecalc=datetime.now()-timedelta(minutes=MaxPastMinutes)
			isok , quantitylist=sensordbmod.EvaluateDataPeriod(sensordata,starttimecalc,datetime.now())
			if isok:
				RainMultipier=quantitylist["average"]
				logger.info('Waterpump weather multiplier =%s ', str(RainMultipier))
			else:
				logger.warning('Waterpump weather multiplier NOT found within 20 Hours apply 100 ')
				RainMultipier=100
			duration=int(duration*RainMultipier/100)
	else:
		logger.warning('Weather Sensor not found, no multpilier applied ')
		

	if pumpit:	
		# activation of the doser before the pump
		doseron=autofertilizermod.checkactivate(target,duration)	
		# watering
		#hardwaremod.makepulse(target,duration)
		activateandregister(target,duration)
		# salva su database
		#logger.info('Switch Pump %s ON, optional time for sec = %s', target, duration)
		#print 'Pump ON, optional time for sec =', duration
		#actuatordbmod.insertdataintable(target,duration)
		
	return True