sub readXML{ # create object $xml = new XML::Simple (KeyAttr=>[]); # read XML file $data = $xml->XMLin($config_path . "sensors.xml"); } sub writeValueXML{ while(! (-x $config_path . "sensors.xml")){} #wait until other process finish writing the file system("sudo chmod 644 $config_path" . "sensors.xml"); #set permission for root to rw only (exclusive write) my $output = new IO::File(">" . $config_path . "sensors.xml"); my $writer = new XML::Writer(OUTPUT => $output); my $flag = 0;; $count = 0; $writer->startTag("Sensors"); while($count < $DATANUM){ #write xml values $e = $data->{$sensor_type[$count]}; $writer->startTag("$sensor_type[$count]"); $writer->startTag("value"); $writer->characters($vars[$count]); $writer->endTag("value"); $writer->startTag("threshold"); $writer->characters($e->{threshold}); $writer->endTag("threshold"); $writer->endTag("$sensor_type[$count]"); if(($vars[$count]*1.0) > ($e->{threshold}*1.0)){ #*1 to force it to float type if (-e $config_path . "SensorAlert.txt" && !$flag){ while(! (-x $config_path . "SensorAlert.txt")){} #wait until other process finish writing the file unlink($config_path . "SensorAlert.txt"); #set permission for root to rw only (exclusive write) } open WRITEFILE, "+>>", $config_path . "SensorAlert.txt"; print WRITEFILE "$count:$vars[$count]\n"; close(WRITEFILE); $flag++; } $count++; } $e = $data->{Weather_Data_Interval}; $writer->startTag("Weather_Data_Interval"); $writer->startTag("value"); $writer->characters($e->{value}); $writer->endTag("value"); $writer->endTag("Weather_Data_Interval"); $writer->endTag("Sensors"); $writer->end(); $output->close(); if($flag){ read_config_file; if ($config_data[10] ne "Sensors:disabled\n"){ $config_data[10] = "Sensors:true\n"; write_config_file; } system("sudo chmod 777 $config_path" . "SensorAlert.txt"); } system("sudo chmod 777 $config_path" . "sensors.xml"); } sub appendLoggedFile{ my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(); $year = 1900 + $yearOffset; my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year"; open WRITEFILE, "+>>", $data_path . "$months[$month]_$dayOfMonth" . "_SensorLog.txt" or die $!; print WRITEFILE $values . ",\t\t" . $theTime . "\n"; close(WRITEFILE); }