UNPKG

thingmodel

Version:

Synchronize data and models across multiple devices

3 lines 36.3 kB
var ThingModel;!function(a){var b=function(){function b(a){this.type=a}return Object.defineProperty(b,"WithoutType",{get:function(){return new b(null)},enumerable:!0,configurable:!0}),b.As=function(a){return new b(a)},b.prototype.IdentifiedBy=function(b){return new c(new a.Thing(b,this.type))},b}();a.BuildANewThing=b;var c=function(){function b(a){this.thing=a,this.ContainingA=this,this.ContainingAn=this,this.AndA=this,this.AndAn=this}return b.prototype.String=function(b,c){return this.thing.SetProperty(new a.Property.String(b,c)),this},b.prototype.Double=function(b,c){return this.thing.SetProperty(new a.Property.Double(b,c)),this},b.prototype.Int=function(b,c){return this.thing.SetProperty(new a.Property.Int(b,c)),this},b.prototype.Boolean=function(b,c){return this.thing.SetProperty(new a.Property.Boolean(b,c)),this},b.prototype.DateTime=function(b,c){return this.thing.SetProperty(new a.Property.DateTime(b,c)),this},b.prototype.Location=function(b,c){var d;if("undefined"!=typeof c?d=b:(d="location",c=b),!c)return this;switch(c.type){case"equatorial":this.thing.SetProperty(new a.Property.Location.Equatorial(d,c));break;case"latlng":this.thing.SetProperty(new a.Property.Location.LatLng(d,c));break;case"point":default:this.thing.SetProperty(new a.Property.Location.Point(d,c))}return this},b.prototype.Build=function(){return this.thing},b}();a.ThingPropertyBuilder=c}(ThingModel||(ThingModel={}));var ThingModel;!function(a){var b=function(){function b(){}return b.Named=function(b){return new c(new a.ThingType(b))},b}();a.BuildANewThingType=b;var c=function(){function b(a){this.type=a,this.ContainingA=this,this.ContainingAn=this,this.AndA=this,this.AndAn=this}return Object.defineProperty(b.prototype,"NotRequired",{get:function(){return this.nextPropertyIsNotRequired=!0,this},enumerable:!0,configurable:!0}),b.prototype.WhichIs=function(a){return this.lastProperty?this.lastProperty.Description=a:this.type.Description=a,this},b.prototype._createProperty=function(b,c,d){this.lastPropertyAdded||null==this.lastProperty||(this.type.DefineProperty(this.lastProperty),this.lastPropertyAdded=!1);var e=new a.PropertyType(b,d,!0);e.Name=c,this.nextPropertyIsNotRequired&&(this.nextPropertyIsNotRequired=!1,e.Required=!1),this.lastProperty=e},b.prototype.String=function(a,b){return this._createProperty(a,b,5),this},b.prototype.LocationPoint=function(a,b){return"undefined"==typeof a&&(a="location"),this._createProperty(a,b,2),this},b.prototype.LocationLatLng=function(a,b){return"undefined"==typeof a&&(a="location"),this._createProperty(a,b,3),this},b.prototype.LocationEquatorial=function(a,b){return"undefined"==typeof a&&(a="location"),this._createProperty(a,b,4),this},b.prototype.Double=function(a,b){return this._createProperty(a,b,6),this},b.prototype.Int=function(a,b){return this._createProperty(a,b,7),this},b.prototype.Boolean=function(a,b){return this._createProperty(a,b,8),this},b.prototype.DateTime=function(a,b){return this._createProperty(a,b,9),this},b.prototype.CopyOf=function(a){var b=this;return _.each(a.Properties,function(a){b.type.DefineProperty(a.Clone())}),this},b.prototype.Build=function(){return this.lastPropertyAdded||null==this.lastProperty||this.type.DefineProperty(this.lastProperty),this.type},b}();a.ThingTypePropertyBuilder=c}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(b){var c=function(){function b(b,c,d){this._connexionDelay=2e3,this.SenderID=b,this._path=c,this._warehouse=d,this._thingModelObserver=new a.Proto.ProtoModelObserver,d.RegisterObserver(this._thingModelObserver),this._fromProtobuf=new a.Proto.FromProtobuf(this._warehouse),this._toProtobuf=new a.Proto.ToProtobuf,this._closed=!0,this._reconnection=!1,this._observers=null,this._sendMessageWaitingList=[],this.Connect()}return b.prototype.Connect=function(){var b=this;if(this._closed){this._ws=new WebSocket(this._path),this._ws.onopen=function(){console.info("ThingModel: Open connection"),b._closed=!1,b._fromProtobuf=new a.Proto.FromProtobuf(b._warehouse),b._toProtobuf=new a.Proto.ToProtobuf,b._connexionDelay=2e3;var c=!b._reconnection;b._sendMessageWaitingList.length>0&&(_.each(b._sendMessageWaitingList,function(a){b.SendMessage(a)}),b._sendMessageWaitingList=[]),b.Send(),c&&b.NotifyObservers(function(a){return a.OnFirstOpen()}),b.NotifyObservers(function(a){return a.OnOpen()})},this._ws.onclose=function(){b._closed||(b._closed=!0,console.info("ThingModel: Connection lost"),b.NotifyObservers(function(a){return a.OnClose()})),b._reconnection=!0,setTimeout(function(){return b.Connect()},b._connexionDelay),b._connexionDelay<2e4&&(b._connexionDelay+=3500)};var c="undefined"!=typeof FileReader;this._ws.onmessage=function(a){var d=a.data;if("string"==typeof d)console.info("ThingModel has received a string: "+a);else if(c){var e=new FileReader;e.readAsArrayBuffer(d),e.onload=function(){return b.parseBuffer(e.result)}}else b.parseBuffer(d)}}},b.prototype.parseBuffer=function(a){var b=this._fromProtobuf.Convert(a);console.debug("ThingModel: message from: "+b),this._toProtobuf.ApplyThingsSuppressions(_.values(this._thingModelObserver.Deletions)),this._thingModelObserver.Reset(),this.NotifyObservers(function(a){return a.OnTransaction(b)})},b.prototype.Send=function(){if(this._closed)return void console.debug("ThingModel: Does not send, waiting for connexion");if(this._thingModelObserver.SomethingChanged){var a=this._thingModelObserver.GetTransaction(this._toProtobuf,this.SenderID,this._reconnection);this._ws.send(this._toProtobuf.ConvertTransaction(a)),this._thingModelObserver.Reset(),this._reconnection=!1,console.debug("ThingModel: transaction sent"),this.NotifyObservers(function(a){return a.OnSend()})}},b.prototype.SendMessage=function(a){this._closed?this._sendMessageWaitingList.push(a):this._ws.send(a)},b.prototype.Close=function(){this._closed||(this._ws.close(),this._closed=!0)},b.prototype.IsConnected=function(){return this._closed},b.prototype.RegisterObserver=function(a){null===this._observers?this._observers=[a]:this._observers.push(a)},b.prototype.UnregisterObserver=function(a){null!==this._observers&&this._observers.splice(_.indexOf(this._observers,a),1)},b.prototype.NotifyObservers=function(a){null!==this._observers&&_.each(this._observers,a)},b}();b.Client=c}(a.WebSockets||(a.WebSockets={}));a.WebSockets}(ThingModel||(ThingModel={}));var __extends=this.__extends||function(a,b){function c(){this.constructor=a}for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);c.prototype=b.prototype,a.prototype=new c},ThingModel;!function(a){!function(a){var b=function(a){function b(b,c,d){a.call(this,b,c,d),this._isLive=!0,this._isPaused=!1}return __extends(b,a),Object.defineProperty(b.prototype,"IsLive",{get:function(){return this._isLive},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"IsPaused",{get:function(){return this._isPaused},enumerable:!0,configurable:!0}),b.prototype.Live=function(){(!this._isLive||this._isPaused)&&(this.SendMessage("live"),this._isLive=!0,this._isPaused=!1)},b.prototype.Play=function(){this._isPaused&&(this.SendMessage("play"),this._isPaused=!1)},b.prototype.Pause=function(){this._isPaused||(this.SendMessage("pause"),this._isPaused=!0)},b.prototype.Load=function(a){this.SendMessage("load "+a.getTime()),this._isLive=!1,this._isPaused=!0},b.prototype.Send=function(){return!this._isLive||this._isPaused?void console.debug("ThingModelClientEnterpriseEdition cannot send data while paused or in a past situation"):void a.prototype.Send.call(this)},b}(a.Client);a.ClientEnterpriseEdition=b}(a.WebSockets||(a.WebSockets={}));a.WebSockets}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(b){var c=function(){function c(a){this._warehouse=a,this.StringDeclarations={}}return c.prototype.KeyToString=function(a){if(0==a)return"";var b=this.StringDeclarations[a];return"undefined"==typeof b?"undefined":b},c.prototype.Convert=function(a,c){"undefined"==typeof c&&(c=!1);var d=b.ProtoTools.Builder.Transaction.decode(a);return this.ConvertTransaction(d,c)},c.prototype.ConvertTransaction=function(a,b){var c=this;_.each(a.string_declarations,function(a){c.StringDeclarations[a.key]=a.value});var d=this.KeyToString(a.string_sender_id),e={};_.each(a.things_remove_list,function(a){var b=c.KeyToString(a),d=c._warehouse.GetThing(b);d&&(e[d.ID]=d)}),this._warehouse.RemoveCollection(e,!0,d),_.each(a.thingtypes_declaration_list,function(a){c.ConvertThingTypeDeclaration(a,d)});var f=[];return _.each(a.things_publish_list,function(a){var e=c.ConvertThingPublication(a,b,d);a.connections_change&&f.push({proto:a,model:e})}),_.each(f,function(a){a.model.DisconnectAll(),_.each(a.proto.connections,function(b){var d=c._warehouse.GetThing(c.KeyToString(b));d&&a.model.Connect(d)}),c._warehouse.RegisterThing(a.model,!1,!1,d)}),d},c.prototype.ConvertThingTypeDeclaration=function(b,c){var d=this,e=new a.ThingType(this.KeyToString(b.string_name));e.Description=this.KeyToString(b.string_description),_.each(b.properties,function(b){var c=null;switch(b.type){case 0:c=1;break;case 1:c=5;break;case 2:c=6;break;case 3:c=7;break;case 4:c=8;break;case 5:c=9}var f=new a.PropertyType(d.KeyToString(b.string_key),c,b.required);f.Description=d.KeyToString(b.string_description),f.Name=d.KeyToString(b.string_name),e.DefineProperty(f)}),this._warehouse.RegisterType(e,!0,c)},c.prototype.ConvertThingPublication=function(b,c,d){var e=this,f=null;0!=b.string_type_name&&(f=this._warehouse.GetThingType(this.KeyToString(b.string_type_name)));var g=this.KeyToString(b.string_id),h=this._warehouse.GetThing(g);return(null==h||null==h.Type&&null!=f||null==f&&null!=h.Type||null!=h.Type&&null!=f&&h.Type.Name!=f.Name)&&(h=new a.Thing(g,f)),_.each(b.properties,function(a){e.ConvertThingProperty(a,h)}),c&&null!=f&&!f.Check(h)?console.log("Object «"+g+"» from «"+d+"»not valid, ignored"):b.connections_change||this._warehouse.RegisterThing(h,!1,!1,d),h},c.prototype.ConvertLocationProperty=function(a,b){null!=b.location_value&&(a.X=b.location_value.x,a.Y=b.location_value.y,b.location_value.z_null||(a.Z=b.location_value.z))},c.prototype.ConvertThingProperty=function(b,c){var d=null,e=this.KeyToString(b.string_key);switch(b.type){case 2:var f=new a.Location.Equatorial;this.ConvertLocationProperty(f,b),d=new a.Property.Location.Equatorial(e,f);break;case 0:var g=new a.Location.Point;this.ConvertLocationProperty(g,b),d=new a.Property.Location.Point(e,g);break;case 1:var h=new a.Location.LatLng;this.ConvertLocationProperty(h,b),d=new a.Property.Location.LatLng(e,h);break;case 3:var i;null!=b.string_value?(i=b.string_value.value,i||0==b.string_value.string_value||(i=this.KeyToString(b.string_value.string_value))):i="undefined",d=new a.Property.String(e,i);break;case 4:d=new a.Property.Double(e,b.double_value);break;case 5:d=new a.Property.Int(e,b.int_value);break;case 6:d=new a.Property.Boolean(e,b.boolean_value);break;case 7:d=new a.Property.DateTime(e,new Date(b.datetime_value.toNumber()))}c.SetProperty(d)},c}();b.FromProtobuf=c}(a.Proto||(a.Proto={}));a.Proto}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(a){var b=function(){function a(){this.Updates={},this.Deletions={},this.Definitions={},this.PermanentDefinitions={},this.somethingChanged=!1}return a.prototype.Reset=function(){this.Updates={},this.Deletions={},this.Definitions={},this.somethingChanged=!0},a.prototype.New=function(a){this.Updates[a.ID]=a,this.somethingChanged=!0},a.prototype.Deleted=function(a){delete this.Updates[a.ID],this.Deletions[a.ID]=a,this.somethingChanged=!0},a.prototype.Updated=function(a){this.Updates[a.ID]=a,this.somethingChanged=!0},a.prototype.Define=function(a){this.Definitions[a.Name]=a,this.PermanentDefinitions[a.Name]=a,this.somethingChanged=!0},Object.defineProperty(a.prototype,"SomethingChanged",{get:function(){return this.somethingChanged},enumerable:!0,configurable:!0}),a.prototype.GetTransaction=function(a,b,c,d){return"undefined"==typeof c&&(c=!1),"undefined"==typeof d&&(d=!1),a.Convert(d?[]:_.values(this.Updates),_.values(this.Deletions),_.values(c?this.PermanentDefinitions:this.Definitions),b)},a}();a.ProtoModelObserver=b}(a.Proto||(a.Proto={}));a.Proto}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(a){var b=function(){function a(){}return a.Builder=dcodeIO.ProtoBuf.loadJson({"package":"ThingModel.Proto",messages:[{name:"Property",fields:[{rule:"required",type:"int32",name:"string_key",id:1,options:{}},{rule:"required",type:"Type",name:"type",id:2,options:{"default":"STRING"}},{rule:"optional",type:"Location",name:"location_value",id:3,options:{}},{rule:"optional",type:"String",name:"string_value",id:4,options:{}},{rule:"optional",type:"double",name:"double_value",id:5,options:{}},{rule:"optional",type:"sint32",name:"int_value",id:6,options:{}},{rule:"optional",type:"bool",name:"boolean_value",id:7,options:{}},{rule:"optional",type:"int64",name:"datetime_value",id:8,options:{}}],enums:[{name:"Type",values:[{name:"LOCATION_POINT",id:0},{name:"LOCATION_LATLNG",id:1},{name:"LOCATION_EQUATORIAL",id:2},{name:"STRING",id:3},{name:"DOUBLE",id:4},{name:"INT",id:5},{name:"BOOLEAN",id:6},{name:"DATETIME",id:7}],options:{}}],messages:[{name:"Location",fields:[{rule:"required",type:"double",name:"x",id:1,options:{"default":0}},{rule:"required",type:"double",name:"y",id:2,options:{"default":0}},{rule:"optional",type:"double",name:"z",id:3,options:{"default":0}},{rule:"optional",type:"int32",name:"string_system",id:4,options:{}},{rule:"optional",type:"bool",name:"z_null",id:5,options:{"default":!1}}],enums:[],messages:[],options:{}},{name:"String",fields:[{rule:"optional",type:"string",name:"value",id:1,options:{}},{rule:"optional",type:"int32",name:"string_value",id:2,options:{"default":0}}],enums:[],messages:[],options:{}}],options:{}},{name:"PropertyType",fields:[{rule:"required",type:"int32",name:"string_key",id:1,options:{}},{rule:"required",type:"Type",name:"type",id:2,options:{"default":"STRING"}},{rule:"required",type:"bool",name:"required",id:3,options:{"default":!0}},{rule:"optional",type:"int32",name:"string_name",id:4,options:{}},{rule:"optional",type:"int32",name:"string_description",id:5,options:{}}],enums:[{name:"Type",values:[{name:"LOCATION",id:0},{name:"STRING",id:1},{name:"DOUBLE",id:2},{name:"INT",id:3},{name:"BOOLEAN",id:4},{name:"DATETIME",id:5}],options:{}}],messages:[],options:{}},{name:"StringDeclaration",fields:[{rule:"required",type:"string",name:"value",id:1,options:{}},{rule:"required",type:"int32",name:"key",id:2,options:{}}],enums:[],messages:[],options:{}},{name:"Thing",fields:[{rule:"required",type:"int32",name:"string_id",id:1},{rule:"required",type:"int32",name:"string_type_name",id:2},{rule:"repeated",type:"Property",name:"properties",id:3},{rule:"repeated",type:"int32",name:"connections",id:4,options:{packed:!0}},{rule:"optional",type:"bool",name:"connections_change",id:5,options:{"default":!1}}],enums:[],messages:[]},{name:"ThingType",fields:[{rule:"required",type:"int32",name:"string_name",id:1},{rule:"optional",type:"int32",name:"string_description",id:2},{rule:"repeated",type:"PropertyType",name:"properties",id:3}],enums:[],messages:[]},{name:"Transaction",fields:[{rule:"repeated",type:"StringDeclaration",name:"string_declarations",id:1},{rule:"repeated",type:"Thing",name:"things_publish_list",id:2},{rule:"repeated",type:"int32",name:"things_remove_list",id:3,options:{packed:!0}},{rule:"repeated",type:"ThingType",name:"thingtypes_declaration_list",id:4},{rule:"required",type:"int32",name:"string_sender_id",id:5}],enums:[],messages:[]}],enums:[],imports:[],services:[]}).build("ThingModel.Proto"),a}();a.ProtoTools=b}(a.Proto||(a.Proto={}));a.Proto}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(a){var b=function(){function b(){this.StringDeclarations={},this.StringDeclarationsCpt=0,this._stringToDeclare={},this._thingsState={},this._propertiesState={}}return b.prototype.StringToKey=function(b){if(!b)return 0;var c=this.StringDeclarations[b];if(c)return c;c=++this.StringDeclarationsCpt,this.StringDeclarations[b]=c;var d=new a.ProtoTools.Builder.StringDeclaration;return d.setKey(c),d.setValue(b),this._transaction.string_declarations.push(d),c},b.prototype.Convert=function(b,c,d,e){var f=this;return this._transaction=new a.ProtoTools.Builder.Transaction,this._transaction.setStringSenderId(this.StringToKey(e)),_.each(b,function(a){f.ConvertThing(a)}),this.ConvertDeleteList(c),this.ConvertDeclarationList(d),this._transaction},b.prototype.ConvertTransaction=function(a){return a.toArrayBuffer()},b.prototype.ConvertThing=function(b){var c=this,d=!1,e=this.StringToKey(b.ID),f=new a.ProtoTools.Builder.Thing;f.setStringId(e),f.setStringTypeName(null!=b.Type?this.StringToKey(b.Type.Name):0),f.setConnectionsChange(!1);var g=this._thingsState[e];(null==g||g.getStringTypeName()!=f.getStringTypeName())&&(d=!0);var h=null;null==g&&b.ConnectedThingsCount>0||null!=g&&g.getConnections().length!=b.ConnectedThingsCount?f.setConnectionsChange(!0):(h=b.ConnectedThings,_.any(h,function(a){var b=c.StringToKey(a.ID);return null!=g&&_.contains(g.getConnections(),b)?!1:(f.setConnectionsChange(!0),!0)})),f.getConnectionsChange()&&(d=!0,h||(h=b.ConnectedThings),_.each(h,function(a){var b=c.StringToKey(a.ID);f.connections.push(b)})),_.each(b.Properties,function(b){var h=c.StringToKey(b.Key),i=new a.ProtoTools.Builder.Property;switch(i.setStringKey(h),b.Type){case 3:case 2:case 4:c.ConvertLocationProperty(b,i);break;case 5:c.ConvertStringProperty(b,i);break;case 6:i.setType(4),i.setDoubleValue(b.Value);break;case 7:i.setType(5),i.setIntValue(b.Value);break;case 8:i.setType(6),i.setBooleanValue(b.Value);break;case 9:i.setType(7),i.setDatetimeValue(b.Value.getTime())}var j=e+":"+h;if(null!=g){var k=c._propertiesState[j];if(null!=k&&k.getType()==i.getType()&&k.getBooleanValue()==i.getBooleanValue()&&k.getDatetimeValue()==i.getDatetimeValue()&&k.getDoubleValue()==i.getDoubleValue()&&k.getIntValue()==i.getIntValue()){var l=k.getLocationValue(),m=i.getLocationValue();if(null==l&&null==m||null!=l&&null!=m&&l.x==m.x&&l.y==m.y&&l.z==m.z&&l.z_null==m.z_null&&l.getStringSystem()==m.getStringSystem()){var n=k.getStringValue(),o=i.getStringValue();if(null==n&&null==o||null!=n&&null!=o&&(n.getStringValue()==o.getStringValue()&&n.getValue()==o.getValue()||b.ValueToString()==n.getValue()))return}}}d=!0,f.properties.push(i),c._propertiesState[j]=i}),d&&(this._transaction.things_publish_list.push(f),this._thingsState[e]=f)},b.prototype.ConvertDeleteList=function(a){var b=this;_.each(a,function(a){var c=b.StringToKey(a.ID);b._transaction.things_remove_list.push(c),b.ManageThingSuppression(c)})},b.prototype.ConvertDeclarationList=function(b){var c=this;_.each(b,function(b){var d=new a.ProtoTools.Builder.ThingType;d.setStringName(c.StringToKey(b.Name)),d.setStringDescription(c.StringToKey(b.Description)),_.each(b.Properties,function(b){var e=new a.ProtoTools.Builder.PropertyType;switch(e.setStringKey(c.StringToKey(b.Key)),e.setStringName(c.StringToKey(b.Name)),e.setStringDescription(c.StringToKey(b.Description)),e.setRequired(b.Required),b.Type){case 3:case 4:case 2:case 1:e.setType(0);break;case 5:e.setType(1);break;case 6:e.setType(2);break;case 7:e.setType(3);break;case 8:e.setType(4);break;case 9:e.setType(5)}d.properties.push(e)}),c._transaction.thingtypes_declaration_list.push(d)})},b.prototype.ConvertLocationProperty=function(b,c){var d=b.Value;switch(d.type){case"latlng":c.setType(1);break;case"equatorial":c.setType(2);break;case"point":default:c.setType(0)}var e=new a.ProtoTools.Builder.Property.Location;e.setX(d.X),e.setY(d.Y),e.setStringSystem(this.StringToKey(d.System)),e.setZNull(null==d.Z),null!=d.Z&&e.setZ(d.Z),c.setLocationValue(e)},b.prototype.ConvertStringProperty=function(b,c){var d=b.Value;c.setType(3);var e=new a.ProtoTools.Builder.Property.String;d&&this._stringToDeclare[d]?e.setStringValue(this.StringToKey(d)):(e.setValue(d),this._stringToDeclare[d]=!0),c.setStringValue(e)},b.prototype.ManageThingSuppression=function(a){var b=this;delete this._thingsState[a];var c=a+":";_.each(this._propertiesState,function(a,d){0===d.indexOf(c)&&delete b._propertiesState[d]})},b.prototype.ApplyThingsSuppressions=function(a){var b=this;_.each(a,function(a){var c=b.StringDeclarations[a.ID];c&&b.ManageThingSuppression(c)})},b}();a.ToProtobuf=b}(a.Proto||(a.Proto={}));a.Proto}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(a){var b=function(){function a(a,b,c){"undefined"==typeof a&&(a=0),"undefined"==typeof b&&(b=0),"undefined"==typeof c&&(c=null),this.X=a,this.Y=b,this.Z=c,this.type="point"}return a.prototype.Compare=function(a){return null!=a&&a.X===this.X&&a.Y===this.Y&&a.Z==this.Z&&a.System==this.System},a.prototype.toString=function(){var a=this.X+" - "+this.Y;return null!=this.Z&&(a+=" - "+this.Z),null!=this.System&&(a+=" -- "+this.System),a},a}();a.Point=b;var c=function(a){function b(b,c,d){"undefined"==typeof b&&(b=0),"undefined"==typeof c&&(c=0),"undefined"==typeof d&&(d=null),a.call(this,b,c,d),this.type="latlng"}return __extends(b,a),Object.defineProperty(b.prototype,"Latitude",{get:function(){return this.X},set:function(a){this.X=a},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"Longitude",{get:function(){return this.Y},set:function(a){this.Y=a},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"Altitude",{get:function(){return this.Z},set:function(a){this.Z=a},enumerable:!0,configurable:!0}),b}(b);a.LatLng=c;var d=function(a){function b(b,c,d){"undefined"==typeof b&&(b=0),"undefined"==typeof c&&(c=0),"undefined"==typeof d&&(d=0),a.call(this,b,c,d),this.type="equatorial"}return __extends(b,a),Object.defineProperty(b.prototype,"RightAscension",{get:function(){return this.X},set:function(a){this.X=a},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"Declination",{get:function(){return this.Y},set:function(a){this.Y=a},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"HourAngle",{get:function(){return this.Z},set:function(a){this.Z=a},enumerable:!0,configurable:!0}),b}(b);a.Equatorial=d}(a.Location||(a.Location={}));a.Location}(ThingModel||(ThingModel={}));var ThingModel;!function(a){var b=function(){function a(a,b){if(!a)throw"The Property key should not be null or empty";this._key=a,this._value=b}return Object.defineProperty(a.prototype,"Key",{get:function(){return this._key},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"Type",{get:function(){return 0},enumerable:!0,configurable:!0}),a.prototype.ValueToString=function(){return null!=this._value?this._value.toString():""},a.prototype.CompareValue=function(a){return null==a||null!=this._value&&null==a._value?!1:null==this._value&&null==a._value?!0:null!=this._value&&null!=a._value?this._value.Compare&&a._value.Compare?this._value.Compare(a._value):this._value===a._value:!1},a.prototype.Clone=function(){return new a(this._key,this._value)},a}();a.Property=b,function(b){!function(c){var d=function(c){function d(a,b){c.call(this,a,b)}return __extends(d,c),Object.defineProperty(d.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=a},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"Type",{get:function(){return 2},enumerable:!0,configurable:!0}),d.prototype.Clone=function(){var c=new a.Location.Point(this._value.X,this._value.Y,this._value.Z);return this._value.System&&(c.System=this._value.System),new b.Location.Point(this._key,c)},d}(b);c.Point=d;var e=function(c){function d(a,b){c.call(this,a,b)}return __extends(d,c),Object.defineProperty(d.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=a},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"Type",{get:function(){return 3},enumerable:!0,configurable:!0}),d.prototype.Clone=function(){var c=new a.Location.LatLng(this._value.X,this._value.Y,this._value.Z);return this._value.System&&(c.System=this._value.System),new b.Location.LatLng(this._key,c)},d}(b);c.LatLng=e;var f=function(c){function d(a,b){c.call(this,a,b)}return __extends(d,c),Object.defineProperty(d.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=a},enumerable:!0,configurable:!0}),Object.defineProperty(d.prototype,"Type",{get:function(){return 4},enumerable:!0,configurable:!0}),d.prototype.Clone=function(){var c=new a.Location.Equatorial(this._value.X,this._value.Y,this._value.Z);return this._value.System&&(c.System=this._value.System),new b.Location.Equatorial(this._key,c)},d}(b);c.Equatorial=f}(b.Location||(b.Location={}));var c=(b.Location,function(a){function c(b,c){a.call(this,b,c)}return __extends(c,a),Object.defineProperty(c.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=a},enumerable:!0,configurable:!0}),Object.defineProperty(c.prototype,"Type",{get:function(){return 5},enumerable:!0,configurable:!0}),c.prototype.Clone=function(){return new b.String(this._key,this._value)},c}(b));b.String=c;var d=function(a){function c(b,c){a.call(this,b,c),null==this._value&&(this._value=0)}return __extends(c,a),Object.defineProperty(c.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=null==a?0:a},enumerable:!0,configurable:!0}),Object.defineProperty(c.prototype,"Type",{get:function(){return 6},enumerable:!0,configurable:!0}),c.prototype.Clone=function(){return new b.Double(this._key,this._value)},c}(b);b.Double=d;var e=function(a){function c(b,c){a.call(this,b,c),null==this._value&&(this._value=0)}return __extends(c,a),Object.defineProperty(c.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=Math.round(a)},enumerable:!0,configurable:!0}),Object.defineProperty(c.prototype,"Type",{get:function(){return 7},enumerable:!0,configurable:!0}),c.prototype.Clone=function(){return new b.Int(this._key,this._value)},c}(b);b.Int=e;var f=function(a){function c(b,c){a.call(this,b,!!c)}return __extends(c,a),Object.defineProperty(c.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=!!a},enumerable:!0,configurable:!0}),Object.defineProperty(c.prototype,"Type",{get:function(){return 8},enumerable:!0,configurable:!0}),c.prototype.Clone=function(){return new b.Boolean(this._key,this._value)},c}(b);b.Boolean=f;var g=function(a){function c(b,c){a.call(this,b,c)}return __extends(c,a),Object.defineProperty(c.prototype,"Value",{get:function(){return this._value},set:function(a){this._value=a},enumerable:!0,configurable:!0}),Object.defineProperty(c.prototype,"Type",{get:function(){return 9},enumerable:!0,configurable:!0}),c.prototype.Clone=function(){return new b.Double(this._key,this._value?new Date(this._value.getTime()):this._value)},c}(b);b.DateTime=g}(a.Property||(a.Property={}));var b=a.Property}(ThingModel||(ThingModel={}));var ThingModel;!function(a){var b=function(){function a(a,b,c){if("undefined"==typeof c&&(c=!0),!a)throw"The PropertyType key should not be null or empty";this._key=a,this._type=b,this.Required=c}return Object.defineProperty(a.prototype,"Key",{get:function(){return this._key},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"Type",{get:function(){return this._type},enumerable:!0,configurable:!0}),a.prototype.Check=function(a){return!this.Required&&null==a||null!=a&&a.Type==this._type&&a.Key==this.Key},a.prototype.Clone=function(){var b=new a(this._key,this._type,this.Required);return b.Name=this.Name,this.Description=this.Description,b},a}();a.PropertyType=b}(ThingModel||(ThingModel={}));var ThingModel;!function(a){var b=function(){function b(a,b){if("undefined"==typeof b&&(b=null),this._type=null,!a)throw new Error("The thing ID should not be null or empty");this._id=a,this._type=b,this._properties={},this._propertiesCount=0,this._connections={},this._connectionsCount=0}return Object.defineProperty(b.prototype,"ID",{get:function(){return this._id},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"Type",{get:function(){return this._type},enumerable:!0,configurable:!0}),b.prototype.SetProperty=function(a){this.HasProperty(a.Key)||++this._propertiesCount,this._properties[a.Key]=a},b.prototype.HasProperty=function(a){return _.has(this._properties,a)},b.prototype.GetProperty=function(a,b){var c=this._properties[a];return!c||b&&c.Type!=b?null:c},b.prototype.GetString=function(a){var b=this.GetProperty(a,5);return b?b.Value:null},b.prototype.Connect=function(a){if(a){if(a===this||a._id===this._id)throw new Error("You can't connect a thing directly to itself");this.IsConnectedTo(a)||++this._connectionsCount,this._connections[a._id]=a}},b.prototype.Disconnect=function(a){return this.IsConnectedTo(a)?(--this._connectionsCount,delete this._connections[a._id],!0):!1},b.prototype.DisconnectAll=function(){this._connections={},this._connectionsCount=0},b.prototype.IsConnectedTo=function(a){return!!a&&_.has(this._connections,a._id)},Object.defineProperty(b.prototype,"ConnectedThings",{get:function(){return _.values(this._connections)},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"ConnectedThingsCount",{get:function(){return this._connectionsCount},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"Properties",{get:function(){return _.values(this._properties)},enumerable:!0,configurable:!0}),b.prototype.Compare=function(a,b,c){return"undefined"==typeof b&&(b=!0),"undefined"==typeof c&&(c=!1),this===a?!0:!a||null!=this._type&&null!=a._type&&this._type.Name!=a._type.Name||null==this._type&&null!=a._type||null!=this._type&&null==a._type?!1:b&&this._id!=a._id?!1:this._connectionsCount!==a._connectionsCount||_.any(this._connections,function(b){return!_.has(a._connections,b._id)})?!1:this._propertiesCount!==a._propertiesCount||_.any(this._properties,function(b){var c=a._properties[b.Key];return null==c||!c.CompareValue(b)})?!1:c?this.RecursiveCompare(a,{}):!0},b.prototype.RecursiveCompare=function(a,b){return _.has(b,this._id)?!0:this.Compare(a)?(b[this._id]=!0,!_.any(this._connections,function(c){var d=a._connections[c._id];return!c.RecursiveCompare(d,b)})):!1},Object.defineProperty(b.prototype,"ContainingA",{get:function(){return this._propertyBuilder||(this._propertyBuilder=new a.ThingPropertyBuilder(this)),this._propertyBuilder},enumerable:!0,configurable:!0}),Object.defineProperty(b.prototype,"ContainingAn",{get:function(){return this.ContainingA},enumerable:!0,configurable:!0}),b.prototype.String=function(b,c){if("undefined"!=typeof c)return this.SetProperty(new a.Property.String(b,c)),this;var d=this.GetProperty(b,5);return d?d.Value:null},b.prototype.Double=function(b,c){if("undefined"!=typeof c)return this.SetProperty(new a.Property.Double(b,c)),this;var d=this.GetProperty(b,6);return d?d.Value:null},b.prototype.Int=function(b,c){if("undefined"!=typeof c)return this.SetProperty(new a.Property.Int(b,c)),this;var d=this.GetProperty(b,7);return d?d.Value:null},b.prototype.Boolean=function(b,c){if("undefined"!=typeof c)return this.SetProperty(new a.Property.Boolean(b,c)),this;var d=this.GetProperty(b,8);return d?d.Value:null},b.prototype.DateTime=function(b,c){if("undefined"!=typeof c)return this.SetProperty(new a.Property.DateTime(b,c)),this;var d=this.GetProperty(b,9);return d?d.Value:null},b.prototype.LocationPoint=function(b,c){var d;if("string"==typeof b||b instanceof String?d=b:(d="location",c=b),"undefined"!=typeof c)return this.SetProperty(new a.Property.Location.Point(d,c)),this;var e=this.GetProperty(d,2);return e?e.Value:null},b.prototype.LocationLatLng=function(b,c){var d;if("string"==typeof b||b instanceof String?d=b:(d="location",c=b),"undefined"!=typeof c)return this.SetProperty(new a.Property.Location.LatLng(d,c)),this;var e=this.GetProperty(d,3);return e?e.Value:null},b.prototype.LocationEquatorial=function(b,c){var d;if("string"==typeof b||b instanceof String?d=b:(d="location",c=b),"undefined"!=typeof c)return this.SetProperty(new a.Property.Location.Equatorial(d,c)),this;var e=this.GetProperty(d,4);return e?e.Value:null },b}();a.Thing=b}(ThingModel||(ThingModel={}));var ThingModel;!function(a){var b=function(){function a(a){if(!a)throw new Error("The name should not be null or empty");this._name=a,this._properties={}}return Object.defineProperty(a.prototype,"Name",{get:function(){return this._name},enumerable:!0,configurable:!0}),a.prototype.Check=function(a){return(a.Type===this||null!==a.Type&&a.Type._name===this._name)&&_.all(this._properties,function(b){return b.Check(a.GetProperty(b.Key))})},a.prototype.DefineProperty=function(a){this._properties[a.Key]=a},a.prototype.GetPropertyDefinition=function(a){return this._properties[a]},Object.defineProperty(a.prototype,"Properties",{get:function(){return _.values(this._properties)},enumerable:!0,configurable:!0}),a}();a.ThingType=b}(ThingModel||(ThingModel={}));var ThingModel;!function(a){!function(a){a[a.Unknown=0]="Unknown",a[a.Location=1]="Location",a[a.LocationPoint=2]="LocationPoint",a[a.LocationLatLng=3]="LocationLatLng",a[a.LocationEquatorial=4]="LocationEquatorial",a[a.String=5]="String",a[a.Double=6]="Double",a[a.Int=7]="Int",a[a.Boolean=8]="Boolean",a[a.DateTime=9]="DateTime"}(a.Type||(a.Type={}));a.Type}(ThingModel||(ThingModel={}));var ThingModel;!function(a){var b=function(){function a(){this._thingTypes={},this._things={},this._observers=[]}return a.prototype.RegisterType=function(a,b,c){if("undefined"==typeof b&&(b=!0),"undefined"==typeof c&&(c=null),!a)throw new Error("The thing type information is null.");(b||!_.has(this._thingTypes,a.Name))&&(this._thingTypes[a.Name]=a,this.NotifyThingTypeDefine(a,c))},a.prototype.RegisterThing=function(a,b,c,d){var e=this;if("undefined"==typeof b&&(b=!0),"undefined"==typeof c&&(c=!0),"undefined"==typeof d&&(d=null),!a)throw new Error("A thing should not be null if it want to be allowed in the warehouse");var f=!_.has(this._things,a.ID);if(this._things[a.ID]=a,c&&a.Type&&this.RegisterType(a.Type,!1,d),b){var g={};_.each(a.ConnectedThings,function(a){e.RecursiveRegisterThing(a,c,g,d)})}f?this.NotifyThingCreation(a,d):this.NotifyThingUpdate(a,d)},a.prototype.RecursiveRegisterThing=function(a,b,c,d){var e=this;c.hasOwnProperty(a.ID)||(c[a.ID]=!0,this.RegisterThing(a,!1,b,d),_.each(a.ConnectedThings,function(a){e.RecursiveRegisterThing(a,b,c,d)}))},a.prototype.RegisterCollection=function(a,b,c){var d=this;"undefined"==typeof b&&(b=!0),"undefined"==typeof c&&(c=null);var e={};_.each(a,function(a){d.RecursiveRegisterThing(a,b,e,c)})},a.prototype.RemoveCollection=function(a,b,c){var d=this;"undefined"==typeof b&&(b=!0),"undefined"==typeof c&&(c=null);var e={};_.each(_.keys(a),function(f){var g=a[f];b&&_.each(d._things,function(a){a.IsConnectedTo(g)&&(e[a.ID]=a)}),d.RemoveThing(g,!1,c)}),b&&_.each(_.keys(e),function(b){a.hasOwnProperty(b)||d.NotifyThingUpdate(e[b])})},a.prototype.RemoveThing=function(a,b,c){var d=this;"undefined"==typeof b&&(b=!0),"undefined"==typeof c&&(c=null),a&&(_.each(this._things,function(e){e.IsConnectedTo(a)&&(e.Disconnect(a),b&&d.NotifyThingUpdate(e,c))}),_.has(this._things,a.ID)&&(delete this._things[a.ID],this.NotifyThingDeleted(a,c)))},a.prototype.RegisterObserver=function(a){this._observers.push(a)},a.prototype.UnregisterObserver=function(a){this._observers.splice(_.indexOf(this._observers,a),1)},a.prototype.NotifyThingTypeDefine=function(a,b){"undefined"==typeof b&&(b=null),_.each(this._observers,function(c){c.Define(a,b)})},a.prototype.NotifyThingUpdate=function(a,b){"undefined"==typeof b&&(b=null),_.each(this._observers,function(c){c.Updated(a,b)})},a.prototype.NotifyThingCreation=function(a,b){"undefined"==typeof b&&(b=null),_.each(this._observers,function(c){c.New(a,b)})},a.prototype.NotifyThingDeleted=function(a,b){"undefined"==typeof b&&(b=null),_.each(this._observers,function(c){c.Deleted(a,b)})},a.prototype.GetThing=function(a){return this._things[a]},a.prototype.GetThingType=function(a){return this._thingTypes[a]},Object.defineProperty(a.prototype,"Things",{get:function(){return _.values(this._things)},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"ThingsTypes",{get:function(){return _.values(this._thingTypes)},enumerable:!0,configurable:!0}),a}();a.Warehouse=b}(ThingModel||(ThingModel={})); //# sourceMappingURL=ThingModel.min.js.map