Vincent's Perl Code

Submitted by novazembla on Mon, 2007-11-12 21:31.

Vincent's Perl Code

#place linux perl header here

print "TEST DATA\n";
print "#################################################\n";
 
#first get actual time converted into the number of seconds from 00:00
my $timeInSeconds = getTimeInSeconds();
print "TIME IN SECONDS:" . $timeInSeconds . "\n";

print "\n#################################################\n";

# test urinal function. the function returns true if urinal flushes.
print "urinal: ";

if (urinal($timeInSeconds)) {
	# it's true (1) so it flushes
	print "flushes!\n";
} else {
	# it's false (0) so no action
	print "inactive!\n";
}

print "\n#################################################\n";

# test the power consumption function.
print "Power Consumption: " . powerUsage($timeInSeconds) . "\n";

print "\n#################################################\n";

# test the movement detector functions.
print "Movement detector indicates: ";

if (movementDetector()) {
	# it's true (1) so it flushes
	print "activity!\n";
} else {
	# it's false (0) so no action
	print "no activity!\n";
}

print "\n#################################################\n";


# this method returns the number of seconds passed already today and is just 
# a helper function.
sub getTimeInSeconds {
	# retrieve time information
	my ($sec,$min,$hour) = localtime(time);
	# calculate and return number of seconds. 
	return ($hour * 60 * 60) + ($min * 60) + $sec;
}

# this method simulates the flushing interval of the urinal 
sub urinal {
	# get the first argument it's the actual timestamp
	my $timestamp = shift;

	# the urinal flushes every x minutes in seconds
	my $interval = 900;
	# flush lenght in second;
	my $flushLength = 60; 
	
	# check if urinal flushes
	# % is a modulo operator and returns the remainder of a devision (e.g.: 24/5 = 4) --> 24 % 5 = 4
	if ($timestamp % $interval >= 0 && $timestamp % $interval <= $flushLength) {
		# apperantly the urinal flushes right now. 
		return 1;
	} else {
		#no flushing
		return 0;
	}
}

# this function returns an estimation of the actual level of power consumption of the LGB
sub powerUsage() {
	# get the first argument it's the actual timestamp
	my $timestamp = shift;

	# this is a representation of the power usage of the LGB in form of a mathematical function
	# based on an estimated sample and estimated with a polynominal regression analysis. 
	# calculate and return the current power usage
	return 	8.5303654e-93*($timestamp**20)
			+1.0676401e-86*($timestamp**19)
			-4.6480158e-81*($timestamp**18)
			+4.4422658e-76*($timestamp**17)
			+5.0305359e-71*($timestamp**16)
			-1.2326941e-65*($timestamp**15)
			+5.9061408e-61*($timestamp**14)
			+4.1528408e-56*($timestamp**13)
			-5.285163e-51*($timestamp**12)
			+1.6155833e-46*($timestamp**11)
			-5.8320484e-42*($timestamp**10)
			+1.4573385e-36*($timestamp**9)
			-1.4989895e-31*($timestamp**8)
			+7.8930969e-27*($timestamp**7)
			-2.474806e-22*($timestamp**6)
			+4.8413829e-18*($timestamp**5)
			-5.8897369e-14*($timestamp**4)
			+4.2691101e-10*($timestamp**3)
			-1.6333479e-06*($timestamp**2)
			+0.0021441309*$timestamp
			+5.9974494;
}

# this method simulates one of the movement detector 
sub movementDetector {
	# the sensor detects activity in x percent
	my $activity = 5; 
	
	# get random value and compare it with activity percentage 
	if (rand() <= $activity/100) {
		# apperantly someone walks in front of the activity sensor
		return 1;
	} else {
		# no activity
		return 0;
	}
}

back