Example #1
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);
        }
Example #2
0
 protected void ConvertProperty(ThingModel.Property.Double property, Property proto)
 {
     proto.type         = Property.Type.DOUBLE;
     proto.double_value = property.Value;
 }