public void Updated(ThingModel.Thing thing, string sender)
 {
     lock (_lockHashSet)
     {
         Updates.Add(thing);
     }
 }
 public void Deleted(ThingModel.Thing thing, string sender)
 {
     lock (_lockHashSet)
     {
         Updates.Remove(thing);
         Deletions.Add(thing);
     }
 }
示例#3
0
        protected ThingModel.Thing ConvertThingPublication(Thing thing, bool check, string senderId)
        {
            ThingModel.ThingType type = null;
            if (thing.string_type_name != 0)
            {
                // Here the type can be null if the string type name is not registered
                // in the LiveWarehouse (can be an error from a client)

                // So, if the type is null, it rolls back to the default type
                // It's a robust model
                type = Warehouse.GetThingType(KeyToString(thing.string_type_name));
            }

            var id = KeyToString(thing.string_id);

            var modelThing = Warehouse.GetThing(id);

            if (modelThing == null || (
                    modelThing.Type == null && type != null ||
                    type == null && modelThing.Type != null ||
                    (modelThing.Type != null && type != null && modelThing.Type.Name != type.Name)))
            {
                modelThing = new ThingModel.Thing(id, type);
            }

            foreach (var property in thing.properties)
            {
                ThingModel.Property modelProperty = null;
                string key = KeyToString(property.string_key);

                switch (property.type)
                {
                case Property.Type.LOCATION_POINT:
                    var locPoint = new Location.Point();
                    ConvertLocationProperty(locPoint, property);
                    modelProperty = new ThingModel.Property.Location.Point(key,
                                                                           locPoint);
                    break;

                case Property.Type.LOCATION_LATLNG:
                    var locLatLng = new Location.LatLng();
                    ConvertLocationProperty(locLatLng, property);
                    modelProperty = new ThingModel.Property.Location.LatLng(key,
                                                                            locLatLng);
                    break;

                case Property.Type.LOCATION_EQUATORIAL:
                    var locEquatorial = new Location.Equatorial();
                    ConvertLocationProperty(locEquatorial, property);
                    modelProperty = new ThingModel.Property.Location.Equatorial(key,
                                                                                locEquatorial);
                    break;

                case Property.Type.STRING:
                    string value;
                    if (property.string_value != null)
                    {
                        value = property.string_value.value;
                        if (String.IsNullOrEmpty(value) && property.string_value.string_value != 0)
                        {
                            value = KeyToString(property.string_value.string_value);
                        }
                    }
                    else
                    {
                        value = "undefined";
                    }
                    modelProperty = new ThingModel.Property.String(key, value);
                    break;

                case Property.Type.DOUBLE:
                    modelProperty = new ThingModel.Property.Double(key, property.double_value);
                    break;

                case Property.Type.INT:
                    modelProperty = new ThingModel.Property.Int(key, property.int_value);
                    break;

                case Property.Type.BOOLEAN:
                    modelProperty = new ThingModel.Property.Boolean(key, property.boolean_value);
                    break;

                case Property.Type.DATETIME:
                    var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

                    modelProperty = new ThingModel.Property.DateTime(key,
                                                                     dtDateTime.AddMilliseconds(property.datetime_value));
                    break;
                }

                modelThing.SetProperty(modelProperty);
            }

            if (check && type != null && !type.Check(modelThing))
            {
                Console.WriteLine("Object «" + id + "» from «" + senderId + "» is not valid, ignored");
            }
            else if (!thing.connections_change)
            {
                Warehouse.RegisterThing(modelThing, false, false, senderId);
            }

            return(modelThing);
        }
示例#4
0
        protected void ConvertThing(ThingModel.Thing thing)
        {
            var change = false;

            var publication = new Thing
            {
                string_id        = StringToKey(thing.ID),
                string_type_name = thing.Type != null?StringToKey(thing.Type.Name) : 0,
                                       connections_change = false
            };

            Thing previousThing;

            _thingsState.TryGetValue(publication.string_id, out previousThing);

            if (previousThing == null || previousThing.string_type_name != publication.string_type_name)
            {
                change = true;
            }

            IList <ThingModel.Thing> connectedThingsCache = null;

            if ((previousThing == null && thing.ConnectedThingsCount > 0) ||
                (previousThing != null && previousThing.connections.Count != thing.ConnectedThingsCount))
            {
                publication.connections_change = true;
            }
            else
            {
                connectedThingsCache = thing.ConnectedThings;
                foreach (var connectedThing in connectedThingsCache)
                {
                    var connectionKey = StringToKey(connectedThing.ID);

                    if (previousThing == null || !previousThing.connections.Contains(connectionKey))
                    {
                        publication.connections_change = true;
                        break;
                    }
                }
            }

            // If we don't have changes on the connection list,
            // it's useless to send it
            if (publication.connections_change)
            {
                change = true;
                if (connectedThingsCache == null)
                {
                    connectedThingsCache = thing.ConnectedThings;
                }

                foreach (var connectedThing in connectedThingsCache)
                {
                    var connectionKey = StringToKey(connectedThing.ID);
                    publication.connections.Add(connectionKey);
                }
            }

            foreach (var property in thing.GetProperties())
            {
                var proto = new Property
                {
                    string_key = StringToKey(property.Key)
                };

                ConvertProperty((dynamic)property, proto);

                var key = new Tuple <int, int>(publication.string_id, proto.string_key);

                if (previousThing != null)
                {
                    Property previousProto;
                    if (_propertiesState.TryGetValue(key, out previousProto))
                    {
                        // ReSharper disable CompareOfFloatsByEqualityOperator
                        // Boring but necessary ?
                        if (previousProto.type == proto.type &&
                            previousProto.boolean_value == proto.boolean_value &&
                            previousProto.datetime_value == proto.datetime_value &&
                            previousProto.double_value == proto.double_value &&
                            previousProto.int_value == proto.int_value &&
                            ((previousProto.location_value == null && proto.location_value == null) ||
                             (previousProto.location_value != null && proto.location_value != null &&
                              previousProto.location_value.x == proto.location_value.x &&
                              previousProto.location_value.y == proto.location_value.y &&
                              previousProto.location_value.z == proto.location_value.z &&
                              previousProto.location_value.z_null == proto.location_value.z_null &&
                              previousProto.location_value.string_system == proto.location_value.string_system)) &&
                            ((previousProto.string_value == null && proto.string_value == null) ||
                             (previousProto.string_value != null && proto.string_value != null && ((
                                                                                                       previousProto.string_value.string_value == proto.string_value.string_value &&
                                                                                                       previousProto.string_value.value == proto.string_value.value) ||
                                                                                                   (previousProto.string_value.value == property.ValueToString())))))
                        {
                            continue;
                        }

                        // ReSharper restore CompareOfFloatsByEqualityOperator
                    }
                }

                change = true;

                publication.properties.Add(proto);
                _propertiesState[key] = proto;
            }

            if (change)
            {
                Transaction.things_publish_list.Add(publication);
                _thingsState[publication.string_id] = publication;
            }
        }