コード例 #1
0
        private List <Model.Object> ParseObjects(ObjectDefinition objectDefinition, int requestContentType, Response response)
        {
            List <Model.Object> result = new List <Model.Object>();
            int contentType;

            if (response.ContentType == -1)
            {
                contentType = requestContentType;
            }
            else
            {
                contentType = response.ContentType;
            }
            if (contentType == TlvConstant.CONTENT_TYPE_TLV)
            {
                TlvReader reader = new TlvReader(response.Payload);
                while (reader.Read())
                {
                    if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ObjectInstance)
                    {
                        TlvReader    objectReader = new TlvReader(reader.TlvRecord.Value);
                        Model.Object item         = ObjectUtils.ParseObject(objectDefinition, objectReader);
                        if (item != null)
                        {
                            item.InstanceID = reader.TlvRecord.Identifier.ToString();
                            result.Add(item);
                        }
                    }
                }
            }
            return(result);
        }
コード例 #2
0
        private Model.Object ParseObject(ObjectDefinition objectDefinition, int requestContentType, Response response)
        {
            Model.Object result = null;
            int          contentType;

            if (response.ContentType == -1)
            {
                contentType = requestContentType;
            }
            else
            {
                contentType = response.ContentType;
            }
            if (contentType == TlvConstant.CONTENT_TYPE_TLV)
            {
                TlvReader reader = new TlvReader(response.Payload);
                result = ObjectUtils.ParseObject(objectDefinition, reader);
            }
            else if (contentType == MediaType.ApplicationJson)
            {
                JsonReader reader = new JsonReader(new MemoryStream(response.Payload));
                result = ObjectUtils.ParseObject(objectDefinition, reader);
            }
            return(result);
        }
コード例 #3
0
        private Property ParseProperty(ObjectDefinition objectDefinition, PropertyDefinition propertyDefinition, int requestContentType, Response response)
        {
            Property result = null;
            int      contentType;

            if (response.ContentType == -1)
            {
                contentType = requestContentType;
            }
            else
            {
                contentType = response.ContentType;
            }
            if (contentType == TlvConstant.CONTENT_TYPE_TLV)
            {
                TlvReader    reader      = new TlvReader(response.Payload);
                Model.Object lwm2mObject = ObjectUtils.ParseObject(objectDefinition, reader);
                if ((lwm2mObject != null) && (lwm2mObject.Properties.Count > 0))
                {
                    foreach (Property item in lwm2mObject.Properties)
                    {
                        if (item.PropertyDefinitionID == propertyDefinition.PropertyDefinitionID)
                        {
                            result = item;
                            break;
                        }
                    }
                }
            }
            else if ((contentType == MediaType.TextPlain) || (contentType == TlvConstant.CONTENT_TYPE_PLAIN))
            {
                string text = Encoding.UTF8.GetString(response.Payload);
                result = new Property();
                result.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID;
                result.PropertyID           = propertyDefinition.PropertyID;
                result.Value = new PropertyValue(ObjectUtils.GetValue(text, propertyDefinition));
            }
            else if ((contentType == MediaType.ApplicationJson) || (contentType == TlvConstant.CONTENT_TYPE_JSON))
            {
                JsonReader reader = new JsonReader(new MemoryStream(response.Payload));
                //LWM2MObject lwm2mObject = ObjectUtils.ParseObject(objectDefinition, reader);
                //if ((lwm2mObject != null) && (lwm2mObject.Properties.Count > 0))
                //{
                //    foreach (LWM2MProperty item in lwm2mObject.Properties)
                //    {
                //        if (item.PropertyDefinitionID == propertyDefinition.PropertyDefinitionID)
                //        {
                //            result = item;
                //            break;
                //        }
                //    }
                //}
                result = ObjectUtils.ParseProperty(propertyDefinition, reader);
            }
            return(result);
        }
コード例 #4
0
ファイル: Events.cs プロジェクト: tuga1975/DeviceServer
 public void ObservationNotify(LWM2MClient client, Model.Object lwm2mObject)
 {
     if (client.OrganisationID != 0)
     {
         ServiceEventMessage message = new ServiceEventMessage();
         FillDeviceParameters(client, message);
         message.Parameters.Add("Object", lwm2mObject);
         BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.OBSERVATION_NOTIFICATION, message, TMessagePublishMode.Confirms);
     }
 }
コード例 #5
0
 private byte[] SerialiseObject(ObjectDefinition objectDefinition, Model.Object item, ushort objectInstanceID)
 {
     byte[] result = null;
     using (MemoryStream steam = new MemoryStream())
     {
         TlvWriter writer    = new TlvWriter(steam);
         byte[]    objectTLV = SerialiseObject(objectDefinition, item);
         int       length    = objectTLV.Length;
         writer.WriteType(TTlvTypeIdentifier.ObjectInstance, objectInstanceID, length);
         steam.Write(objectTLV, 0, length);
         result = steam.ToArray();
     }
     return(result);
 }
コード例 #6
0
            internal override Model ReadEntryBB(BinaryReaderEx br)
            {
                ModelType type = br.GetEnum32 <ModelType>(br.Position + 8);

                switch (type)
                {
                case ModelType.MapPiece:
                    var mapPiece = new Model.MapPiece(br, MSBVersion.MSBVersionBB);
                    MapPieces.Add(mapPiece);
                    return(mapPiece);

                case ModelType.Object:
                    var obj = new Model.Object(br, MSBVersion.MSBVersionBB);
                    Objects.Add(obj);
                    return(obj);

                case ModelType.Enemy:
                    var enemy = new Model.Enemy(br, MSBVersion.MSBVersionBB);
                    Enemies.Add(enemy);
                    return(enemy);

                case ModelType.Item:
                    var enemy2 = new Model.Enemy(br);
                    Enemies.Add(enemy2);
                    return(enemy2);

                case ModelType.Player:
                    var player = new Model.Player(br, MSBVersion.MSBVersionBB);
                    Players.Add(player);
                    return(player);

                case ModelType.Collision:
                    var collision = new Model.Collision(br, MSBVersion.MSBVersionBB);
                    Collisions.Add(collision);
                    return(collision);

                /*
                 * case ModelType.Other:
                 * var other = new Model.Other(br);
                 * Others.Add(other);
                 * return other;
                 */
                default:
                    return(null);

                    //throw new NotImplementedException($"Unsupported model type: {type}");
                }
            }
コード例 #7
0
        private byte[] SerialiseObject(ObjectDefinition objectDefinition, Model.Object item)
        {
            byte[]       result      = null;
            TlvWriter    arrayWriter = null;
            MemoryStream arraystream = null;

            using (MemoryStream steam = new MemoryStream())
            {
                TlvWriter writer = new TlvWriter(steam);
                foreach (Property property in item.Properties)
                {
                    PropertyDefinition propertyDefinition = objectDefinition.GetProperty(property.PropertyID);
                    if (propertyDefinition != null)
                    {
                        if (propertyDefinition.IsCollection)
                        {
                            if (property.Values != null)
                            {
                                ushort identifier;
                                if (ushort.TryParse(propertyDefinition.PropertyID, out identifier))
                                {
                                    if (arrayWriter == null)
                                    {
                                        arraystream = new MemoryStream();
                                        arrayWriter = new TlvWriter(arraystream);
                                    }
                                    arraystream.SetLength(0);
                                    foreach (PropertyValue propertyValue in property.Values)
                                    {
                                        WriteValue(arrayWriter, TTlvTypeIdentifier.ResourceInstance, propertyDefinition.DataType, propertyValue.PropertyValueID, propertyValue.Value);
                                    }
                                    byte[] arrayItems = arraystream.ToArray();
                                    writer.Write(TTlvTypeIdentifier.MultipleResources, identifier, arrayItems);
                                }
                            }
                        }
                        else if (property.Value != null)
                        {
                            WriteValue(writer, TTlvTypeIdentifier.ResourceWithValue, propertyDefinition.DataType, propertyDefinition.PropertyID, property.Value.Value);
                        }
                    }
                }
                result = steam.ToArray();
            }
            return(result);
        }
コード例 #8
0
 public Model.Object GetObject(Guid clientID, Guid objectDefinitionID, string instanceID)
 {
     Model.Object result = null;
     try
     {
         LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID);
         if (client != null)
         {
             ObjectDefinitionLookups lookups          = BusinessLogicFactory.Clients.GetLookups();
             ObjectDefinition        objectDefinition = lookups.GetObjectDefinition(objectDefinitionID);
             if (objectDefinition != null)
             {
                 int objectID;
                 if (int.TryParse(objectDefinition.ObjectID, out objectID))
                 {
                     Model.ObjectType objectType = client.SupportedTypes.GetObjectType(objectID);
                     if (objectType != null)
                     {
                         Request  request  = client.NewGetRequest(objectType, instanceID);
                         Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT);
                         if (response != null && response.StatusCode == StatusCode.Content)
                         {
                             BusinessLogicFactory.Clients.UpdateClientActivity(client);
                             result = ParseObject(objectDefinition, request.Accept, response);
                             if (result != null)
                             {
                                 result.InstanceID = instanceID;
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
     }
     return(result);
 }
コード例 #9
0
ファイル: ServerAPI.cs プロジェクト: CreatorDev/DeviceServer
 public void SaveObjectProperty(Guid clientID, Guid objectDefinitionID, string instanceID, Property property, TObjectState state)
 {
     LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID);
     if (client == null)
     {
         ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Client not found ", clientID.ToString()));
         throw new NoLongerAvailableException("Device not connected");
     }
     else
     {
         ObjectDefinitionLookups lookups = BusinessLogicFactory.Clients.GetLookups();
         ObjectDefinition objectDefinition = lookups.GetObjectDefinition(objectDefinitionID);
         if (objectDefinition != null)
         {
             int objectID;
             if (int.TryParse(objectDefinition.ObjectID, out objectID))
             {
                 Model.ObjectType objectType = client.SupportedTypes.GetObjectType(objectID);
                 if (objectType != null)
                 {
                     PropertyDefinition propertyDefinition = objectDefinition.GetProperty(property.PropertyDefinitionID);
                     if (propertyDefinition != null)
                     {
                         byte[] payload = null;
                         int contentType = TlvConstant.CONTENT_TYPE_TLV;
                         if (state != TObjectState.Delete)
                         {
                             if ((property.Value != null) || (property.Values != null))
                             {
                                 if ((property.Value != null) && (LWM2MClient.DataFormat == MediaType.TextPlain))
                                 {
                                     contentType = LWM2MClient.DataFormat;
                                     //contentType = TlvConstant.CONTENT_TYPE_PLAIN;
                                     string text = SerialiseProperty(propertyDefinition, property);
                                     if (text != null)
                                         payload = Encoding.UTF8.GetBytes(text);
                                 }
                                 else
                                 {
                                     Model.Object lwm2mObject = new Model.Object();
                                     lwm2mObject.Properties.Add(property);
                                     payload = SerialiseObject(objectDefinition, lwm2mObject);
                                 }
                             }
                         }
                         Request request = null;
                         switch (state)
                         {
                             case TObjectState.NotChanged:
                                 break;
                             case TObjectState.Add:
                                 request = client.NewPostRequest(objectType, instanceID, propertyDefinition.PropertyID, contentType, payload);
                                 break;
                             case TObjectState.Update:
                                 request = client.NewPutRequest(objectType, instanceID, propertyDefinition.PropertyID, contentType, payload);
                                 break;
                             case TObjectState.Delete:
                                 request = client.NewDeleteRequest(objectType, instanceID, propertyDefinition.PropertyID);
                                 break;
                             default:
                                 break;
                         }
                         Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT);
                         if (response == null)
                         {
                             throw new TimeoutException();
                         }
                         else
                         {
                             BusinessLogicFactory.Clients.UpdateClientActivity(client);
                             if (response.StatusCode != StatusCode.Changed)
                             {
                                 throw new BadRequestException();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #10
0
 public ObjectInstance(Model.ObjectDefinition objectDefinition, Model.Object resource)
 {
     ObjectDefinition = objectDefinition;
     _Resource = resource;
 }
コード例 #11
0
 private void Deserialise(XmlReader reader)
 {
     while (reader.Read())
     {
         if (reader.NodeType == XmlNodeType.Element)
         {
             if (_Resource == null)
             {
                 if (string.Compare(ObjectDefinition.SerialisationName, reader.Name, true) == 0)
                 {
                     _Resource = new Model.Object();
                     _Resource.ObjectDefinitionID = ObjectDefinition.ObjectDefinitionID;
                     _Resource.ObjectID = ObjectDefinition.ObjectID;
                 }
             }
             else
             {
                 if (reader.Name.Equals("Links"))
                 {
                     Links = new List<Link>();
                     Links.Deserialise(reader);
                 }
                 else
                 {
                     Model.PropertyDefinition propertyDefinition = ObjectDefinition.GetPropertyBySerialisationName(reader.Name);
                     if (propertyDefinition == null)
                     {
                         if (string.Compare(reader.Name, "InstanceID", true) == 0)
                         {
                             reader.Read();
                             _Resource.InstanceID = reader.Value;
                         }
                     }
                     else
                     {
                         Model.Property property = new Model.Property();
                         property.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID;
                         property.PropertyID = propertyDefinition.PropertyID;
                         if (propertyDefinition.IsCollection)
                         {
                             XmlReader collectionReader = reader.ReadSubtree();
                             DeserialiseItems(propertyDefinition, property, collectionReader);
                         }
                         else
                         {
                             property.Value = new Model.PropertyValue();
                             property.Value.Value = GetValue(propertyDefinition.DataType, reader.ReadInnerXml());
                         }
                         _Resource.Properties.Add(property);
                     }
                 }
             }
         }
         else if (reader.NodeType == XmlNodeType.EndElement && string.Compare(ObjectDefinition.SerialisationName, reader.Name, true) == 0)
         {
             break;
         }
     }
 }
コード例 #12
0
 private void Deserialise(JsonReader reader)
 {
     Model.PropertyDefinition propertyDefinition = null;
     Model.Property property = null;
     bool isID = false;
     while (reader.Read())
     {
         switch (reader.State)
         {
             case TJsonReaderState.NotSet:
                 break;
             case TJsonReaderState.Array:
                 break;
             case TJsonReaderState.BOF:
                 break;
             case TJsonReaderState.Boolean:
                 if (propertyDefinition != null)
                 {
                     Model.PropertyValue propertyValue = new Model.PropertyValue(reader.AsBoolean.ToString());
                     if (propertyDefinition.IsCollection)
                     {
                         if (property.Values == null)
                             property.Values = new List<Model.PropertyValue>();
                         property.Values.Add(propertyValue);
                     }
                     else
                     {
                         property.Value = new Model.PropertyValue(propertyValue.Value);
                     }
                 }
                 break;
             case TJsonReaderState.EndArray:
                 propertyDefinition = null;
                 break;
             case TJsonReaderState.EndObject:
                 break;
             case TJsonReaderState.EOF:
                 break;
             case TJsonReaderState.Member:
                 if (reader.Text.Equals("Links"))
                 {
                     Links = new List<Link>();
                     Links.Deserialise(reader);
                 }
                 else
                 {
                     propertyDefinition = ObjectDefinition.GetPropertyBySerialisationName(reader.Text);
                     if (propertyDefinition == null)
                     {
                         isID = string.Compare(reader.Text, "InstanceID", true) == 0;
                     }
                     else
                     {
                         isID = false;
                         property = new Model.Property();
                         property.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID;
                         property.PropertyID = propertyDefinition.PropertyID;
                         if (_Resource != null)
                             _Resource.Properties.Add(property);
                     }
                 }
                 break;
             case TJsonReaderState.Null:
                 break;
             case TJsonReaderState.Number:
                 if (propertyDefinition != null)
                 {
                     Model.PropertyValue propertyValue = new Model.PropertyValue(reader.Text);
                     if (propertyDefinition.IsCollection)
                     {
                         if (property.Values == null)
                             property.Values = new List<Model.PropertyValue>();
                         property.Values.Add(propertyValue);
                     }
                     else
                     {
                         property.Value = new Model.PropertyValue(propertyValue.Value);
                     }
                 }
                 break;
             case TJsonReaderState.Object:
                 if (_Resource == null)
                 {
                     _Resource = new Model.Object();
                     _Resource.ObjectDefinitionID = ObjectDefinition.ObjectDefinitionID;
                     _Resource.ObjectID = ObjectDefinition.ObjectID;
                 }
                 break;
             case TJsonReaderState.String:
                 if (propertyDefinition == null)
                 {
                     if (isID)
                         _Resource.InstanceID = reader.Text;
                 }
                 else
                 {
                     string text;
                     if (propertyDefinition.DataType == Model.TPropertyDataType.DateTime)
                     {
                         text = GetDateTimeString(reader.Text);
                     }
                     else
                     {
                         text = reader.Text;
                     }
                     Model.PropertyValue propertyValue = new Model.PropertyValue(text);
                     if (propertyDefinition.IsCollection)
                     {
                         if (property.Values == null)
                             property.Values = new List<Model.PropertyValue>();
                         property.Values.Add(propertyValue);
                     }
                     else
                     {
                         property.Value = new Model.PropertyValue(propertyValue.Value);
                     }
                 }
                 break;
             default:
                 break;
         }
     }
 }
コード例 #13
0
 public static Model.Object ParseObject(ObjectDefinition objectDefinition, JsonReader reader)
 {
     Model.Object result = null;
     while (reader.Read())
     {
         if ((reader.State == TJsonReaderState.Member) && (string.Compare(reader.Text,"e") == 0))
         {
             if (result == null)
             {
                 result = new Model.Object();
                 result.ObjectID = objectDefinition.ObjectID;
                 result.ObjectDefinitionID = objectDefinition.ObjectDefinitionID;
             }
             if (reader.Read())
             {
                 if (reader.State == TJsonReaderState.Array)
                 {
                     while (reader.Read())
                     {
                         if (reader.State == TJsonReaderState.Object)
                         {
                             Property property = ParseProperty(objectDefinition, reader);
                             if (property != null)
                             {
                                 bool found = false;
                                 foreach (Property item in result.Properties)
                                 {
                                     if (item.PropertyDefinitionID == property.PropertyDefinitionID)
                                     {
                                         if ((item.Values != null) && (property.Values != null))
                                         {
                                             item.Values.Add(property.Values[0]);
                                         }
                                         found = true;
                                         break;
                                     }
                                 }
                                 if (!found)
                                     result.Properties.Add(property);
                             }
                         }
                         if (reader.State == TJsonReaderState.EndArray)
                             break;
                     }
                 }
             }
         }
     }
     return result;
 }
コード例 #14
0
 public static Model.Object ParseObject(ObjectDefinition objectDefinition, TlvReader reader)
 {
     Model.Object result = null;
     while (reader.Read())
     {
         if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ObjectInstance)
         {
             TlvReader objectReader = new TlvReader(reader.TlvRecord.Value);
             result = ParseObject(objectDefinition, objectReader);
             if (result != null)
             {
                 result.InstanceID = reader.TlvRecord.Identifier.ToString();
             }
             break;
         }
         if ((reader.TlvRecord.TypeIdentifier != TTlvTypeIdentifier.ObjectInstance) && (reader.TlvRecord.TypeIdentifier != TTlvTypeIdentifier.NotSet))
         {
             if (result == null)
             {
                 result = new Model.Object();
                 result.ObjectID = objectDefinition.ObjectID;
                 result.ObjectDefinitionID = objectDefinition.ObjectDefinitionID;
             }
             if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ResourceWithValue)
             {
                 string propertyID = reader.TlvRecord.Identifier.ToString();
                 PropertyDefinition property = objectDefinition.GetProperty(propertyID);
                 if (property != null)
                 {
                     Property lwm2mProperty = new Property();
                     lwm2mProperty.PropertyDefinitionID = property.PropertyDefinitionID;
                     lwm2mProperty.PropertyID = property.PropertyID;
                     lwm2mProperty.Value = new PropertyValue(GetValue(reader, property));
                     result.Properties.Add(lwm2mProperty);
                 }
             }
             else if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.MultipleResources)
             {
                 string propertyID = reader.TlvRecord.Identifier.ToString();
                 PropertyDefinition property = objectDefinition.GetProperty(propertyID);
                 if (property != null)
                 {
                     Property lwm2mProperty = new Property();
                     lwm2mProperty.PropertyDefinitionID = property.PropertyDefinitionID;
                     lwm2mProperty.PropertyID = property.PropertyID;
                     result.Properties.Add(lwm2mProperty);
                     TlvReader arrayReader = new TlvReader(reader.TlvRecord.Value);
                     while (arrayReader.Read())
                     {
                         if (arrayReader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ResourceInstance)
                         {
                             string value = GetValue(arrayReader, property);
                             if (value != null)
                             {
                                 if (lwm2mProperty.Values == null)
                                     lwm2mProperty.Values = new List<PropertyValue>();
                                 PropertyValue propertyValue = new PropertyValue();
                                 propertyValue.PropertyValueID = arrayReader.TlvRecord.Identifier.ToString();
                                 propertyValue.Value = value;
                                 lwm2mProperty.Values.Add(propertyValue);
                             }
                         }
                     }
                 }
             }
         }
     }
     return result;
 }
コード例 #15
0
 public InfoVideo(Home frm)
 {
     InitializeComponent();
     Child     = frm;
     videoInfo = new Model.Object();
 }