From 41411775edf0a38a9403c7ebf4dcf7d932df3cc4 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Thu, 22 Dec 2011 18:46:19 -0700 Subject: [PATCH] first commit --- configurator.db | Bin 0 -> 4096 bytes configurator.php | 56 ++++++++++++++++++++++++ index.php | 111 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 configurator.db create mode 100644 configurator.php create mode 100644 index.php diff --git a/configurator.db b/configurator.db new file mode 100644 index 0000000000000000000000000000000000000000..4f7e552f77bf412f7009ad2f99b3219674737b8a GIT binary patch literal 4096 zcmeH}&rcIU6vyA20lY~40j4S(7DWXENxQ9WQ-4u$1>}dNRD=sjmDV(=6tM?2Trhay zWH@;++nx+3Ps+(~@`y1poIH4FVq!RX5-u8NhTXEezP~^_>89JyPG4r#qgg#9JfTFk>#AOc@H}bNF^WJ9O?;`Y8+nWVN{usokKI$2$XFz0N zsSywO+uCp`Q_9>eWUm{OL;c2t^~L&AMj~x!>BO}WL$jdPrR8U}p|P|vV5GEUYG^c( zn%0Jm=^pJ~aW0p?uceKtbdR=>&CEWr=k;ihvU8>VUzz#&LO#16rn^zB1^JGwNdw>E zYkZY2@*Gd{IPc^>w#%w)ldZD~TVW+O!v>ko+F29+z%Td#-{4Dph6S9&K0J@D@Eg9v zXLt{<;RQT}Ss7H0!^=CLDp#AJL)-^LdN>jYh674cj`RNc%JNykZ=B=Rrxi?NIL4F zswd46k`9yfCP@#uX+ZU_=_6?~Ne^07>_jg6F5ZkL_^|-^oE^ggOR#DD#+LCTd&|DE zpZJc)@E7k8dXN*kuqyOngKzO|+{P{3z%^XOMa*Fm~&oAFy8Vo3f*Y*RuZ+iJ?lYY+grV1>WPb z`J7z0xNJ0vtMK~;s>LA+#~wnfx82lQWMevF%OWc-+t|S)VlC_ywCp9J_DX81+#TMj zn{eF9SMBXa zRXh=tiqU#SYa6M9Wl}TCw7$|>OzZYv8ZW)MpEUt%66d-u&i5H{&aa5`eqEgVRkq80 z!Ux2?`(OO2KK`K$s56~*1+;^p-3IMUXct2}9?~6ENGDaHT^H@pXg5bYKiVbIj*@nt p?wr|P*PSz)(hitD9i_C3uCqO~uDn>q?Lop8b?IQxrfzQ|=pQgbmE`~c literal 0 HcmV?d00001 diff --git a/configurator.php b/configurator.php new file mode 100644 index 0000000..0ddac7d --- /dev/null +++ b/configurator.php @@ -0,0 +1,56 @@ +prepare('SELECT * FROM DEVICES WHERE READY=1 AND SENT ISNULL LIMIT 1;'); + $deviceq->execute(); + $device = $deviceq->fetchObject(); + + $updatesent = $db->prepare("UPDATE DEVICES SET SENT=1 WHERE id=:id;"); + $updatesent->bindParam(':id', $device->id); + $updatesent->execute(); + + // OUTPUT CONFIG + if(isset($device->config)){ + echo $device->config; + } + + } catch (Exception $e) { + die ($e); + } +} + +// When called with id and applied=1, mark as applied +if(strlen($id) > 0 && $applied == "1") { + try { + $updateapplied = $db->prepare("UPDATE DEVICES SET APPLIED=1 WHERE id=:id;"); + $updateapplied->bindParam(':id', $id); + $updateapplied->execute(); + } catch (Exception $e) { + die ($e); + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..5c9184b --- /dev/null +++ b/index.php @@ -0,0 +1,111 @@ + array('filter' => FILTER_SANITIZE_NUMBER_INT, 'flags' => FILTER_REQUIRE_ARRAY))); +if(isset($configdevices['configdevices'])) + $configdevices = $configdevices['configdevices']; +$applied = filter_input(INPUT_GET, 'applied', FILTER_SANITIZE_NUMBER_INT); +$download = filter_input(INPUT_GET, 'download', FILTER_SANITIZE_SPECIAL_CHARS); + +// local vars +$db; + +// init sql database if necessary +if ( ! file_exists( $dbpath ) ) + $newdb = new SQLiteDatabase( $dbpath, 0660 ); +if ( ! file_exists( $dbpath ) ) + die( "Unable to create database file at " . $dbpath ); + +// Connect to the database with PDO +try { + $db = new PDO('sqlite:'.$dbpath); +} catch (Exception $e) { + die ($e); +} + +// Create table if doesn't exist +$q = $db->query("PRAGMA table_info(DEVICES)"); +if ( $q->rowCount() == 0 ) { + $db->query( "CREATE TABLE DEVICES ( id INTEGER PRIMARY KEY, config TEXT, ready INTEGER, sent INTEGER, applied INTEGER );" ); +} + + +/********** RUNTIME SECTIONS **********/ +// Always get devices from database +try { + $devicelist = $db->prepare('SELECT * FROM DEVICES;'); + $devicelist->execute(); +} catch (Exception $e) { + die ($e); +} + +// if requested, download unconfigured devices from MyDatavation +if(strlen($download) > 0) { + // TODO: placeholder + $devices[time()] = time(); + $devices[time()+1] = time(); + $devices[time()+2] = time(); + $devices[time()+3] = time(); + + // save devices in database + try { + $stmt = $db->prepare("INSERT INTO DEVICES (id, config) VALUES (:id, :config);"); + $stmt->bindParam(':id', $i); + $stmt->bindParam(':config', $c); + + foreach($devices as $di => $dc) { + $i = $di; + $c = $dc; + $stmt->execute(); + } + } catch (Exception $e) { + die ($e); + } +} + +// if requested, begin configuring devices +if($configdevices != false && count($configdevices) > 0) { + // update device status in DB + try { + $stmt = $db->prepare("UPDATE DEVICES SET ready=1 WHERE id=:id"); + $stmt->bindParam(':id', $i); + } catch (Exception $e) { + die ($e); + } + foreach($configdevices as $d => $di) { + $i = $di; + $stmt->execute(); + } +} + +?> + + +
+ +
+ +
+

Devices

+ + + +
    +fetchObject()): ?> +
  • + + id ?> + ready=="1") { echo " ready";} ?> + sent=="1") { echo " sent";} ?> + applied=="1") { echo " applied";} ?> +
  • + +
+ +
+ +