天天看點

阿裡雲物聯網 .NET Core 用戶端 | CZGL.AliIoTClient:4.1 上報位置資訊

阿裡雲物聯網的位置服務,并不是完全獨立的功能。位置資訊包含 二維、三維,位置資料來源于屬性的上傳。

1)添加二維位置資料

打開 資料分析 -> 空間資料可視化 -> 二維資料 -> 添加,為上面示範的裝置添加位置,重新整理時間為1秒。

在産品功能中,打開功能定義 ,在 标準功能 裡,添加功能。

選擇 其它類型 ,裡面搜尋

位置

,在出現的清單中選一個(前面那幾個都可以)。

筆者選擇的是:

辨別符:GeoLocation 适用類别:CuttingMachine           

位置上傳要設定的資訊:

阿裡雲物聯網 .NET Core 用戶端 | CZGL.AliIoTClient:4.1 上報位置資訊

注意注意,如果選擇的标準屬性跟上圖的類型定義不一樣,需要手動修改。要把上面的位置屬性按上圖來改,有一個地方不同,都會失敗。

當然,也可以一開始就按圖手動建立。

阿裡雲物聯網 .NET Core 用戶端 | CZGL.AliIoTClient:4.1 上報位置資訊

2)基礎代碼

上傳位置資料,不需要做什麼大操作,按照屬性的上傳方法上傳即可。模型代碼參考:

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 { } }
        }           

整體代碼參考:

定義位置模型 -> 設定位置資料 -> 上傳位置資料

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

            client.OpenPropertyDownPost();
            // 設定要訂閱的Topic、運作接收内容的Topic
            string[] topics = new string[] { client.CombineHeadTopic("get") };
            // 使用預設事件
            client.UseDefaultEventHandler();

            // 連接配接伺服器
            client.ConnectIoT(topics, null, 60);
            while (true)
            {
                ToServer();
                Thread.Sleep(1000);
            }
            Console.ReadKey();
        }

        public static void ToServer()
        {
            // 執行個體化模型
            TestModel model = new TestModel();

            // 設定屬性值

            // 經度
            [email protected] = 113.952981;
            // 緯度
            [email protected] = 22.539843;
            // 海拔
            [email protected] = 56;
            // 坐标系類型
            [email protected] = 2;

            // 上傳屬性資料
            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 { } }
        }           

上面使用的是模拟位置資料,請實際情況設定位置資料。

打開阿裡雲物聯網控制台 -> 資料分析 -> 空間資料可視化 -> 二維資料 -> 示範産品

會看到定位到了深圳阿裡雲大廈(高新園地鐵站附近)~~~