define.class("$server/service", function (require) {
	this.__thingmodel = {}

	this.attributes = {
		things: Config({type: Array, value: [], flow:"out"}),
		connected: Config({type: Boolean, value: false, persist: true, flow:"out"})
	}

	this.update = function(thingid, state, value) {
		for (var i = 0; i < this.__things.length; i++) {
			var thing = this.__things[i];
			// console.log('thing', thing)
			var meta = thing.state('meta');
			var id = meta['iot:thing-id'];
			if (id === thingid) {
				// found the thing, set its state
				thing.set(':' + state, value);
				return;
				// console.log('found id', thingid, state, value)
			}
		}
		// we should have returned above
		if (! thing) console.warn('missing thing', thingid);
	}

	this.updateAll = function(state, value) {
		// set on all things
		this.__things.set(':' + state, value);
	}

	this.__updateModel = function(thing) {
		var id = thing.thing_id();
		var meta = thing.state("meta");
		var model = thing.state("model");
		var states = thing.state("istate")
		var facets = meta['iot:facet'];
		if (facets && facets.map) {
			facets = facets.map(function(facet) {
				return facet.split(':')[1]
			})
		}

		var attributemeta = model['iot:attribute']
		// console.log('thing metadata', id, meta, facets)
		// console.log('thing model', attributemeta);

		// rewrite state to include metadata about type, readonly
		for (var i = 0; i < attributemeta.length; i++) {
			var attrmodel = attributemeta[i];
			var key  = attrmodel['schema:name'];
			states[key] = {
				value: states[key],
				type: attrmodel['iot:type'].split('.')[1],
				readonly: ! attrmodel['iot:write']
			}
		}

		// copy over fields
		this.__thingmodel[id] = {
			state: states,
			id: meta['iot:thing-id'],
			name: meta['schema:name'],
			reachable: meta['iot:reachable'],
			manufacturer: meta['schema:manufacturer'],
			model: meta['schema:model'] || meta['iot:model-id'],
			facets: facets
		};

		var keys = Object.keys(this.__thingmodel).sort();
		var things = [];
		for (var i = 0; i < keys.length; i++) {
			var key = keys[i];
			things[i] = this.__thingmodel[key];
		}

		// shouldn't this be enough to update the attribute in the browser over RPC?
		this.things = things;
		// console.log("updated state\n", JSON.stringify(things));
	}

	this.init = function() {
		// allow connection logic to be overridden
		this.__things = this.connect(require("iotdb"));
		// console.log('THINGS: ', this.__things);

		// listen for new things
		this.__things.on("thing", function(thing) {
			//console.log('new THING: ', thing)
			this.__updateModel(thing);
			// register for changes to each thing
			thing.on("istate", function(thing_inner) {
				// console.log('new state on THING: ', thing)
				// update to reflect changes
				this.__updateModel(thing_inner);
			}.bind(this));
		}.bind(this));
	}

	// override to change connection
	this.connect = function(iotdb) {
		if (this.connected) return;
		this.connected = true;
		return iotdb.connect('HueLight', {poll: 1}).connect();
	}
});

@@ -51,52 +51,540 @@
  %7B%0A%09
-this.__thingmodel = %7B%7D%0A%0A%09this.attributes = %7B
+// The hub class makes it very easy to connect to a wide variety of devices including%0A%09// SmartThings, Philips Hue and many more.%0A%09//%0A%09// IMPORTANT: see /examples/components/iot/README.md for setup instructions.%0A%09this.__thingmodel = %7B%7D%0A%0A%09this.attributes = %7B%0A%09%09// A list of things connected to the hub, automatically updated as new devices are discovered and their state changes.%0A%09%09// Each thing consists of an object containing an id, name, and a state object representing its current state's value type and if it's readonly or not.
 %0A%09%09t
@@ -636,16 +636,47 @@
 out%22%7D),%0A
+%09%09// If true, we are connected%0A
 %09%09connec
@@ -745,24 +745,76 @@
 %22out%22%7D)%0A%09%7D%0A%0A
+%09// Updates a specific thing's state to a new value%0A
 %09this.update
@@ -1260,16 +1260,21 @@
  thing) 
+%7B%0A%09%09%09
 console.
@@ -1305,19 +1305,61 @@
 ingid);%0A
+%09
 %09%7D%0A
+%09%7D%0A%0A%09// Updates all things to a new value
 %0A%09this.u
@@ -2732,87 +2732,8 @@
 %09%7D%0A%0A
-%09%09// shouldn't this be enough to update the attribute in the browser over RPC?%0A
 %09%09th
@@ -3376,17 +3376,17 @@
 %09%7D%0A%0A%09// 
-o
+O
 verride 
@@ -3395,26 +3395,77 @@
  change 
-connection
+what gets connected. Currently attemts to connect everything.
 %0A%09this.c
@@ -3525,33 +3525,8 @@
 rn;%0A
-%09%09this.connected = true;%0A
 %09%09re
@@ -3578,15 +3578,40 @@
 nect();%0A
+%09%09this.connected = true;%0A
 %09%7D%0A%7D);%0A

Add documentation for IOT hub