new cleanup and updates
[home-automation.git] / includes.php
index f55dd8b..a4ef0cc 100644 (file)
@@ -13,32 +13,37 @@ function get_data($item) {
 }
 
 function summon($person,$status) {
-  if ($person == "russ") {
-    $myFile = "russ.txt";
-    $fh = fopen($myFile, 'w') or die("can't open file");
-    fwrite($fh, $status);
+  $myFile = "summon.txt";
+  $list = file("summon.txt");
+  $fh = fopen($myFile, 'w') or die("can't open file");
+  $matched=0;
+  foreach ($list as $summoned) {
+    list($item,$flag) = split("=",trim($summoned));
+    if ($person == $item) {
+      fwrite($fh, "$person=$status\n");
+      $matched=1; 
+    } else {
+      if (strlen($item) > 0) {
+        fwrite($fh, "$item=$flag\n");
+      }
+    }
+  }
+  fclose($fh);
+  if ($matched==0) {
+    $fh = fopen($myFile, 'a') or die("can't open file");
+    fwrite($fh, "$person=$status\n");
     fclose($fh);
   }
-  if ($person == "beth") {
-    $myFile = "beth.txt";
-    $fh = fopen($myFile, 'w') or die("can't open file");
-    fwrite($fh, $status);
-    fclose($fh); 
-  } 
 }
 
 function summon_status($person) {
-  if ($person == "russ") {
-    $myFile = "russ.txt";
-    $fh = fopen($myFile, 'r') or die("can't open file");
-    $status = fread($fh, 5);
-    fclose($fh);
-  }
-  if ($person == "beth") {
-    $myFile = "beth.txt";
-    $fh = fopen($myFile, 'r') or die("can't open file");
-    $status = fread($fh, 5);
-    fclose($fh);
+  $myFile = "summon.txt";
+  $list = file("summon.txt");
+  foreach ($list as $summoned) {
+    list($item,$flag) = split("=",$summoned);
+    if ($person == $item) {
+      return($flag);
+    }    
   }
   return($status);
 }