Ejemplo n.º 1
0
        public void read()
        {
            Monitor.Enter(mLock);
            String jsonString;

            try
            {
                jsonString = File.ReadAllText(mHWInfoFileName);
            }
            catch
            {
                Monitor.Exit(mLock);
                this.write();
                return;
            }

            try
            {
                var rootObject = JObject.Parse(jsonString);

                // categoryList
                if (rootObject.ContainsKey("list") == true)
                {
                    var categoryList = rootObject.Value <JArray>("list");
                    for (int i = 0; i < categoryList.Count; i++)
                    {
                        var categoryObject = (JObject)categoryList[i];

                        if (categoryObject.ContainsKey("id") == false ||
                            categoryObject.ContainsKey("name") == false ||
                            categoryObject.ContainsKey("list") == false)
                        {
                            continue;
                        }

                        string id         = categoryObject.Value <string>("id");
                        string name       = categoryObject.Value <string>("name");
                        var    deviceList = categoryObject.Value <JArray>("list");

                        var category = new HWInfoCategory(id, name);
                        mHWInfoCategoryMap.Add(id, category);
                        mHWInfoCategoryList.Add(category);

                        for (int j = 0; j < deviceList.Count; j++)
                        {
                            var deviceObject = (JObject)deviceList[j];

                            if (deviceObject.ContainsKey("id") == false ||
                                deviceObject.ContainsKey("name") == false ||
                                deviceObject.ContainsKey("unit") == false)
                            {
                                continue;
                            }

                            string deviceID   = deviceObject.Value <string>("id");
                            string deviceName = deviceObject.Value <string>("name");
                            string deviceUnit = deviceObject.Value <string>("unit");

                            category.update(deviceID, deviceName, 0.0, deviceUnit);
                        }
                    }
                }
            }
            catch
            {
                mHWInfoCategoryMap.Clear();
                mHWInfoCategoryList.Clear();
            }
            Monitor.Exit(mLock);
        }
Ejemplo n.º 2
0
        private void processData(List <byte> recvList)
        {
            try
            {
                int  index   = 60;
                bool isValue = false;

                var tempCategoryList = new List <HWInfoCategory>();
                while (index < recvList.Count)
                {
                    if (isValue == false)
                    {
                        // category value1
                        var  datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3] };
                        uint id1   = BitConverter.ToUInt32(datas, 0);
                        if (id1 < 100)
                        {
                            isValue = true;
                            continue;
                        }
                        index = index + 4;

                        // category value2
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3] };
                        uint id2 = BitConverter.ToUInt32(datas, 0);
                        index = index + 4;

                        // category name
                        var tempList = new List <byte>();
                        for (int i = 0; i < 128; i++)
                        {
                            if (recvList[index + i] == 0x00)
                            {
                                break;
                            }
                            tempList.Add(recvList[index + i]);
                        }
                        string name = Encoding.Default.GetString(tempList.ToArray());
                        index = index + 128;

                        // category name (repeat)
                        index = index + 128;

                        Monitor.Enter(mLock);
                        string         categoryID = string.Format("{0}/{1}/{2}/{3}", mIDPrefix, id1, id2, name);
                        HWInfoCategory category   = null;
                        if (mHWInfoCategoryMap.ContainsKey(categoryID) == false)
                        {
                            category = new HWInfoCategory(categoryID, name);
                            mHWInfoCategoryMap.Add(categoryID, category);
                            mHWInfoCategoryList.Add(category);
                        }
                        else
                        {
                            category = mHWInfoCategoryMap[categoryID];
                        }
                        tempCategoryList.Add(category);
                        Monitor.Exit(mLock);
                        //Console.WriteLine("HWInfo.processData() : {0}, {1}, {2}", id1, id2, name);
                    }
                    else
                    {
                        // device value1
                        var  datas        = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3] };
                        uint deviceValue1 = BitConverter.ToUInt32(datas, 0);
                        index = index + 4;

                        // category intex (uint)
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3] };
                        int categoryIndex = BitConverter.ToInt32(datas, 0);
                        index = index + 4;

                        // device value2
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3] };
                        uint deviceValue2 = BitConverter.ToUInt32(datas, 0);
                        index = index + 4;

                        // device name
                        var tempList = new List <byte>();
                        for (int i = 0; i < 128; i++)
                        {
                            if (recvList[index + i] == 0x00)
                            {
                                break;
                            }
                            tempList.Add(recvList[index + i]);
                        }
                        string name = Encoding.Default.GetString(tempList.ToArray());
                        index = index + 128;

                        // device name (repeat)
                        index = index + 128;

                        // unit
                        tempList = new List <byte>();
                        for (int i = 0; i < 16; i++)
                        {
                            if (recvList[index + i] == 0x00)
                            {
                                break;
                            }
                            tempList.Add(recvList[index + i]);
                        }
                        string unit = Encoding.Default.GetString(tempList.ToArray());
                        index = index + 16;

                        // current
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3], recvList[index + 4], recvList[index + 5], recvList[index + 6], recvList[index + 7] };
                        double current = BitConverter.ToDouble(datas, 0);
                        index = index + 8;

                        // minimum
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3], recvList[index + 4], recvList[index + 5], recvList[index + 6], recvList[index + 7] };
                        double minimum = BitConverter.ToDouble(datas, 0);
                        index = index + 8;

                        // maximum
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3], recvList[index + 4], recvList[index + 5], recvList[index + 6], recvList[index + 7] };
                        double maximum = BitConverter.ToDouble(datas, 0);
                        index = index + 8;

                        // average
                        datas = new byte[] { recvList[index], recvList[index + 1], recvList[index + 2], recvList[index + 3], recvList[index + 4], recvList[index + 5], recvList[index + 6], recvList[index + 7] };
                        double average = BitConverter.ToDouble(datas, 0);
                        index = index + 8;

                        try
                        {
                            if (categoryIndex < tempCategoryList.Count && name.Length > 0)
                            {
                                var    category = tempCategoryList[categoryIndex];
                                string deviceID = string.Format("{0}/{1}/{2}/{3}", category.ID, deviceValue1, deviceValue2, name);
                                category.update(deviceID, name, current, unit);
                            }
                        }
                        catch { }

                        //Console.WriteLine("HWInfo.processData() : name({0}), value({1}, {2}, {3}), cur({4}{5})", name, deviceValue1, categoryIndex, deviceValue2, current, unit);
                    }
                }

                if (mIsFirstRecv == false)
                {
                    mIsFirstRecv = true;
                    this.write();
                }
            }
            catch { }
        }