Aliyun IoT .NET Core Client | CZGL.AliIoTClient: 4.1 Reporting Location Information

2019年12月15日 2895点热度 0人点赞 0条评论
内容目录

Table of Contents:


 

The location service of Alibaba Cloud IoT is not a completely independent function. The location information includes two-dimensional and three-dimensional data, which sources the location data from attribute uploads.

1) Add Two-Dimensional Location Data

Open Data Analysis -> Spatial Data Visualization -> Two-Dimensional Data -> Add to add location for the devices demonstrated above, with a refresh time of 1 second. In the product functions, open Function Definition, and add the function in Standard Functions.
Select Other Types, search for Location in the list that appears and choose one (any of the previous ones are acceptable).
The author has chosen:

Identifier: GeoLocation Applicable Category: CuttingMachine

Information to be set for location upload:

Please note, if the standard properties selected do not match the type definition in the above image, manual modifications are required. The location attributes should be modified according to the above image; if there is a discrepancy in any part, the upload will fail. Of course, you can also manually create it as shown in the picture from the beginning.


2) Basic Code

To upload location data, no significant operations are needed; simply follow the method for uploading attributes.

Model code reference:

```csharp
public class TestModel
{
    public string id { get { return DateTime.Now.Ticks.ToString(); } set { } }
    public string version { get { return "1.0"; } set { } }
    public Params @params { get; set; }

    public TestModel()
    {
        @params = new Params();
    }
    public class Params
    {
        public geoLocation GeoLocation { get; set; }

        public class geoLocation
        {
            public Value value { get; set; }
            public long time { get { return AliIoTClientJson.GetUnixTime(); } set { } }
            public geoLocation()
            {
                value = new Value();
            }
            public class Value
            {
                public double Longitude { get; set; }
                public double Latitude { get; set; }
                public double Altitude { get; set; }
                public int CoordinateSystem { get; set; }
            }
        }

        public Params()
        {
            GeoLocation = new geoLocation();
        }
    }
    public string method { get { return "thing.event.property.post"; } set { } }
}

Overall code reference: Define location model -> Set location data -> Upload location data

class Program
{
    static AliIoTClientJson client;
    static void Main(string[] args)
    {
        // Create client
        client = new AliIoTClientJson(new DeviceOptions
        {
            ProductKey = "a1A6VVt72pD",
            DeviceName = "json",
            DeviceSecret = "7QrjTptQYCdepjbQvSoqkuygic2051zM",
            RegionId = "cn-shanghai"
        });

        client.OpenPropertyDownPost();
        // Set the topic to subscribe to and the topic for receiving content
        string[] topics = new string[] { client.CombineHeadTopic("get") };
        // Use the default event
        client.UseDefaultEventHandler();

        // Connect to the server
        client.ConnectIoT(topics, null, 60);
        while (true)
        {
            ToServer();
            Thread.Sleep(1000);
        }
        Console.ReadKey();
    }

    public static void ToServer()
    {
        // Instantiate model
        TestModel model = new TestModel();

        // Set property values

        // Longitude
        model.@params.GeoLocation.value.Longitude = 113.952981;
        // Latitude
        model.@params.GeoLocation.value.Latitude = 22.539843;
        // Altitude
        model.@params.GeoLocation.value.Altitude = 56;
        // Coordinate system type
        model.@params.GeoLocation.value.CoordinateSystem = 2;

        // Upload property data
        client.Thing_Property_Post<TestModel>(model, false);
    }

    public class TestModel
    {
        public string id { get { return DateTime.Now.Ticks.ToString(); } set { } }
        public string version { get { return "1.0"; } set { } }
        public Params @params { get; set; }

        public TestModel()
        {
            @params = new Params();
        }
        public class Params
        {
            public geoLocation GeoLocation { get; set; }

            public class geoLocation
            {
                public Value value { get; set; }
                public long time { get { return AliIoTClientJson.GetUnixTime(); } set { } }
                public geoLocation()
                {
                    value = new Value();
                }
                public class Value
                {
                    public double Longitude { get; set; }
                    public double Latitude { get; set; }
                    public double Altitude { get; set; }
                    public int CoordinateSystem { get; set; }
                }
            }

            public Params()
            {
                GeoLocation = new geoLocation();
            }
        }
        public string method { get { return "thing.event.property.post"; } set { } }
    }
}

<br />

```markdown
The above code uses simulated location data. Please set the location data according to the actual situation.

Open the Alibaba Cloud IoT console -> Data Analysis -> Spatial Data Visualization -> 2D Data -> Demo Product

You will see the location pinpointed at Alibaba Cloud Tower in Shenzhen (near Gaoxin Garden Metro Station)~~~

痴者工良

高级程序员劝退师

文章评论