Beispiel #1
0
        public static void LocalToShareEx(Dictionary <string, Gateway> gts, bool bRemoveOffline)
        {
            if (!SharedData.IsProducer())
            {
                return;
            }
            if (SharedData.semaphoreInSnergy == null)
            {
                return;
            }
            if (gts.Count <= 0)
            {
                lock (SharedData.thisSharedLock)
                {
                    if (SharedData.semaphoreInSnergy != null)
                    {
                        SharedData.semaphoreInSnergy.WaitOne();
                        SharedData.WriteShareLong(32L, 0L);
                        SharedData.WriteShareLong(48L, 0L);
                        SharedData.semaphoreInSnergy.Release();
                    }
                }
                return;
            }
            byte[] array = new byte[SharedData.MAX_SHARE_DATA_SIZE + 1024L];
            Array.Clear(array, 0, array.Length);
            long num  = 0L;
            long num2 = 0L;
            long num3 = 0L;
            long num4 = 0L;
            long num5 = 0L;

            foreach (KeyValuePair <string, Gateway> current in gts)
            {
                byte[]   bytes  = Encoding.ASCII.GetBytes(current.Key);
                string[] array2 = current.Value.status.gatewayIP.Split(new char[]
                {
                    ':'
                });
                DateTime tUptime = current.Value.status.tUptime;
                if (ApplicationHandler.IsTopologyNotEmpty(current.Key) && (!bRemoveOffline || current.Value.status.gatewayIP.IndexOf(":0") < 0))
                {
                    num5 += 1L;
                    if (num2 >= SharedData.MAX_SHARE_DATA_SIZE || num3 > 0L)
                    {
                        num3 = 1L;
                    }
                    else
                    {
                        Buffer.BlockCopy(bytes, 0, array, (int)num2, 20);
                        num2 += 20L;
                        byte[] array3 = new byte[6];
                        Array.Clear(array3, 0, array3.Length);
                        if (array2.Length == 2)
                        {
                            string[] array4 = array2[0].Split(new char[]
                            {
                                '.'
                            });
                            if (array4.Length == 4)
                            {
                                array3[0] = Convert.ToByte(array4[0]);
                                array3[1] = Convert.ToByte(array4[1]);
                                array3[2] = Convert.ToByte(array4[2]);
                                array3[3] = Convert.ToByte(array4[3]);
                                array3[4] = (byte)(Convert.ToInt32(array2[1]) & 255);
                                array3[5] = (byte)(Convert.ToInt32(array2[1]) >> 8);
                            }
                        }
                        Buffer.BlockCopy(array3, 0, array, (int)num2, 6);
                        num2 += 6L;
                        long   value  = ((long)tUptime.Year << 40) + ((long)tUptime.Month << 32) + ((long)tUptime.Day << 24) + (long)((long)tUptime.Hour << 16) + (long)((long)tUptime.Minute << 8) + (long)tUptime.Second;
                        byte[] bytes2 = BitConverter.GetBytes(value);
                        Buffer.BlockCopy(bytes2, 0, array, (int)num2, 8);
                        num2 += 8L;
                        Array.Clear(array3, 0, array3.Length);
                        array3[0] = Convert.ToByte(current.Value.listDevice.Count);
                        Buffer.BlockCopy(array3, 0, array, (int)num2, 1);
                        num2 += 1L;
                        foreach (KeyValuePair <string, Device> current2 in current.Value.listDevice)
                        {
                            if (num2 >= SharedData.MAX_SHARE_DATA_SIZE)
                            {
                                num3 = 1L;
                                InSnergyService.PostLog("OutOfMem #2");
                                break;
                            }
                            byte[] bytes3 = Encoding.ASCII.GetBytes(current2.Value.sDID);
                            Buffer.BlockCopy(bytes3, 0, array, (int)num2, 20);
                            num2 += 20L;
                            long num6 = num2;
                            Array.Clear(array3, 0, array3.Length);
                            array3[0] = Convert.ToByte(current2.Value.listChannel.Count);
                            Buffer.BlockCopy(array3, 0, array, (int)num2, 1);
                            num2 += 1L;
                            long num7 = 0L;
                            foreach (KeyValuePair <string, Channel> current3 in current2.Value.listChannel)
                            {
                                if (num2 >= SharedData.MAX_SHARE_DATA_SIZE)
                                {
                                    num3 = 1L;
                                    InSnergyService.PostLog("OutOfMem #3");
                                    break;
                                }
                                int num8 = Convert.ToInt32(current3.Value.sCID);
                                if (num8 >= 1 && num8 <= 12)
                                {
                                    int num9 = current2.Value.mapChannel[num8 - 1];
                                    if (num9 != 0)
                                    {
                                        string text = num8.ToString();
                                        while (text.Length < 2)
                                        {
                                            text = "0" + text;
                                        }
                                        for (int i = 0; i < 3; i++)
                                        {
                                            string str   = "0" + (i + 1).ToString();
                                            string text2 = (num9 >> 8 * (2 - i) & 255).ToString();
                                            while (text2.Length < 2)
                                            {
                                                text2 = "0" + text2;
                                            }
                                            if (!(text2 == "00"))
                                            {
                                                byte[] bytes4 = Encoding.ASCII.GetBytes(text + str + text2);
                                                Buffer.BlockCopy(bytes4, 0, array, (int)num2, 6);
                                                num2 += 6L;
                                                long num10 = num2;
                                                Array.Clear(array3, 0, array3.Length);
                                                array3[0] = Convert.ToByte(current3.Value.measurePair.Count);
                                                Buffer.BlockCopy(array3, 0, array, (int)num2, 1);
                                                num2 += 1L;
                                                long num11 = 0L;
                                                foreach (KeyValuePair <string, Param> current4 in current3.Value.measurePair)
                                                {
                                                    int num12 = Convert.ToInt32(current4.Value.aID);
                                                    if ((SharedData.listParamEnabled == null || SharedData.listParamEnabled.Contains(num12)) && num12 > 1000 && num12 / 1000 == i + 1)
                                                    {
                                                        if (num2 >= SharedData.MAX_SHARE_DATA_SIZE)
                                                        {
                                                            num3 = 1L;
                                                            InSnergyService.PostLog("OutOfMem #4");
                                                            break;
                                                        }
                                                        byte[] bytes5 = BitConverter.GetBytes(num12);
                                                        Buffer.BlockCopy(bytes5, 0, array, (int)num2, 4);
                                                        num2 += 4L;
                                                        byte[] bytes6 = BitConverter.GetBytes(current4.Value.dvalue);
                                                        Buffer.BlockCopy(bytes6, 0, array, (int)num2, 8);
                                                        num2  += 8L;
                                                        value  = ((long)current4.Value.time.Year << 40) + ((long)current4.Value.time.Month << 32) + ((long)current4.Value.time.Day << 24) + (long)((long)current4.Value.time.Hour << 16) + (long)((long)current4.Value.time.Minute << 8) + (long)current4.Value.time.Second;
                                                        bytes2 = BitConverter.GetBytes(value);
                                                        Buffer.BlockCopy(bytes2, 0, array, (int)num2, 8);
                                                        num2  += 8L;
                                                        num11 += 1L;
                                                    }
                                                }
                                                if (num3 > 0L)
                                                {
                                                    break;
                                                }
                                                Array.Clear(array3, 0, array3.Length);
                                                array3[0] = Convert.ToByte(num11);
                                                Buffer.BlockCopy(array3, 0, array, (int)num10, 1);
                                                num7 += 1L;
                                            }
                                        }
                                    }
                                }
                            }
                            if (num3 > 0L)
                            {
                                break;
                            }
                            Array.Clear(array3, 0, array3.Length);
                            array3[0] = Convert.ToByte(num7);
                            Buffer.BlockCopy(array3, 0, array, (int)num6, 1);
                        }
                        if (num3 <= 0L)
                        {
                            num += 1L;
                            num4 = num2;
                        }
                    }
                }
            }
            Array.Resize <byte>(ref array, (int)num4);
            lock (SharedData.thisSharedLock)
            {
                if (SharedData.semaphoreInSnergy != null)
                {
                    num += num5 << 32;
                    SharedData.semaphoreInSnergy.WaitOne();
                    SharedData.WriteShareLong(32L, num);
                    SharedData.WriteShareLong(48L, num4);
                    SharedData.WriteShareArray(8280L, array);
                    long num13 = SharedData.ReadShareLong(56L);
                    num13 |= 8L;
                    SharedData.WriteShareLong(56L, num13);
                    SharedData.semaphoreInSnergy.Release();
                }
            }
            if (num3 > 0L)
            {
                if (num3 != SharedData.nLastShareMemoryErrorCode)
                {
                    InSnergyService.PostLog("Out of share-memory, data truncated");
                }
                SharedData.WriteStatus(2L, true);
            }
            else
            {
                SharedData.WriteStatus(2L, false);
            }
            SharedData.nLastShareMemoryErrorCode = num3;
        }
Beispiel #2
0
        public static long ShareToLocalEx(Dictionary <string, IGateway> gts)
        {
            List <string> list = new List <string>();

            foreach (KeyValuePair <string, IGateway> current in gts)
            {
                list.Add(current.Key);
            }
            long result = 0L;

            byte[] array = null;
            long   num;

            lock (SharedData.thisSharedLock)
            {
                if (SharedData.semaphoreInSnergy == null)
                {
                    return(result);
                }
                SharedData.semaphoreInSnergy.WaitOne();
                num    = SharedData.ReadShareLong(32L);
                result = num >> 32;
                num   &= (long)((ulong)-1);
                long nSize = SharedData.ReadShareLong(48L);
                array = SharedData.ReadShareArray(8280L, nSize);
                long num2 = SharedData.ReadShareLong(56L);
                num2 &= -9L;
                SharedData.WriteShareLong(56L, num2);
                SharedData.semaphoreInSnergy.Release();
            }
            long num3 = 0L;

            for (long num4 = 0L; num4 < num; num4 += 1L)
            {
                string @string = Encoding.ASCII.GetString(array, (int)num3, 20);
                num3 += 20L;
                long   num5 = (long)((int)array[(int)checked ((IntPtr) unchecked (num3 + 4L))] + ((int)array[(int)checked ((IntPtr) unchecked (num3 + 5L))] << 8));
                string ip   = string.Concat(checked (new object[]
                {
                    array[(int)((IntPtr)num3)].ToString(),
                    ".",
                    array[(int)((IntPtr) unchecked (num3 + 1L))].ToString(),
                    ".",
                    array[(int)((IntPtr) unchecked (num3 + 2L))].ToString(),
                    ".",
                    array[(int)((IntPtr) unchecked (num3 + 3L))].ToString(),
                    ":",
                    num5
                }));
                num3 += 6L;
                long     num6 = BitConverter.ToInt64(array, (int)num3);
                DateTime t    = new DateTime((int)(num6 >> 40), (int)(num6 >> 32 & 255L), (int)(num6 >> 24 & 255L), (int)(num6 >> 16 & 255L), (int)(num6 >> 8 & 255L), (int)(num6 & 255L));
                num3 += 8L;
                IGateway gateway = new IGateway(@string, ip, t);
                long     num7    = (long)((ulong)array[(int)checked ((IntPtr)num3)]);
                num3 += 1L;
                for (long num8 = 0L; num8 < num7; num8 += 1L)
                {
                    string string2 = Encoding.ASCII.GetString(array, (int)num3, 20);
                    num3 += 20L;
                    IBranch branch = new IBranch(string2);
                    long    num9   = (long)((ulong)array[(int)checked ((IntPtr)num3)]);
                    num3 += 1L;
                    int num10 = 0;
                    while ((long)num10 < num9)
                    {
                        string string3 = Encoding.ASCII.GetString(array, (int)num3, 6);
                        num3 += 6L;
                        IMeter meter = new IMeter(string3);
                        long   num11 = (long)((ulong)array[(int)checked ((IntPtr)num3)]);
                        num3 += 1L;
                        int num12 = 0;
                        while ((long)num12 < num11)
                        {
                            int num13 = BitConverter.ToInt32(array, (int)num3);
                            if (num13 > 1000)
                            {
                                num13 %= 100;
                            }
                            num3 += 4L;
                            double d = BitConverter.ToDouble(array, (int)num3);
                            num3 += 8L;
                            num6  = BitConverter.ToInt64(array, (int)num3);
                            t     = new DateTime((int)(num6 >> 40), (int)(num6 >> 32 & 255L), (int)(num6 >> 24 & 255L), (int)(num6 >> 16 & 255L), (int)(num6 >> 8 & 255L), (int)(num6 & 255L));
                            IParam value = new IParam(num13, d, t);
                            num3 += 8L;
                            meter.listParam.Add(num13, value);
                            num12++;
                        }
                        branch.listChannel.Add(string3, meter);
                        num10++;
                    }
                    gateway.listDevice.Add(string2, branch);
                }
                if (gts.ContainsKey(@string))
                {
                    gts[@string].Update(gateway);
                }
                else
                {
                    gts.Add(@string, gateway);
                }
                if (list.Contains(@string))
                {
                    list.Remove(@string);
                }
            }
            foreach (string current2 in list)
            {
                gts.Remove(current2);
            }
            return(result);
        }
Beispiel #3
0
        private static bool CreateShareMemory(long maxGateways, long maxDevices, long maxChannels, long maxAttributes)
        {
            if (SharedData.semaphoreInSnergy == null)
            {
                return(false);
            }
            SharedData.nMaxGateways   = maxGateways;
            SharedData.nMaxDevices    = maxDevices;
            SharedData.nMaxChannels   = maxChannels;
            SharedData.nMaxAttributes = maxAttributes;
            int num = 0;

            num += (int)(SharedData.nMaxGateways * 35L);
            num += (int)(SharedData.nMaxDevices * 21L);
            num += (int)(SharedData.nMaxChannels * (7L + SharedData.nMaxAttributes * 20L));
            SharedData.MAX_SHARE_DATA_SIZE = (long)num;
            InSnergyService.PostLog(string.Concat(new object[]
            {
                "Header + data = ",
                8280L,
                "+",
                num,
                "=",
                (long)num + 8280L
            }));
            num += 8280;
            SharedData.semaphoreInSnergy.WaitOne();
            int num2 = SharedData.SetupShareMemoryWithSercurity(num);

            if (num2 < 0)
            {
                InSnergyService.PostLog("SetupShareMemory failed");
                SharedData.semaphoreInSnergy.Release();
                return(false);
            }
            if (num2 > 0)
            {
                SharedData.nMaxGateways   = SharedData.ReadShareLong(0L);
                SharedData.nMaxDevices    = SharedData.ReadShareLong(8L);
                SharedData.nMaxChannels   = SharedData.ReadShareLong(16L);
                SharedData.nMaxAttributes = SharedData.ReadShareLong(24L);
                num  = (int)(SharedData.nMaxGateways * 35L);
                num += (int)(SharedData.nMaxDevices * 21L);
                num += (int)(SharedData.nMaxChannels * (7L + SharedData.nMaxAttributes * 20L));
                SharedData.MAX_SHARE_DATA_SIZE = (long)num;
            }
            else
            {
                SharedData.WriteShareLong(0L, SharedData.nMaxGateways);
                SharedData.WriteShareLong(8L, SharedData.nMaxDevices);
                SharedData.WriteShareLong(16L, SharedData.nMaxChannels);
                SharedData.WriteShareLong(24L, SharedData.nMaxAttributes);
                SharedData.WriteShareLong(64L, 1024L);
                SharedData.WriteShareLong(72L, 0L);
                SharedData.WriteShareLong(80L, 0L);
                SharedData.WriteShareLong(32L, 0L);
                SharedData.WriteShareLong(40L, 0L);
                SharedData.WriteShareLong(56L, 0L);
            }
            SharedData.semaphoreInSnergy.Release();
            return(true);
        }