示例#1
0
        static private void SolarSync()
        {
            var readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");

            if (modbusReader == null)
            {
                modbusReader = new ModBus();
            }
            do
            {
                try
                {
                    _apiKey       = System.Configuration.ConfigurationManager.AppSettings["APIKey"];
                    _societyIDKey = System.Configuration.ConfigurationManager.AppSettings["SocietyKey"];

                    if (_apiKey != null && _apiKey.Length > 0 && _societyIDKey != null && _societyIDKey.Length > 0)
                    {
                        if (readDataClient == null)
                        {
                            readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                            WriteToLog(DateTime.Now + "New Object Initialized For GetHouseDetails");
                        }
                        if (readDataClient != null)
                        {
                            DataTable objHouseDetailsdataTable = new DataTable();
                            if (readDataClient == null)
                            {
                                readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                                WriteToLog(DateTime.Now + "New Object Initialized For GetHouseDetails");
                            }
                            objHouseDetailsdataTable = readDataClient.GetHouseDetails(_societyIDKey, _apiKey);

                            if (objHouseDetailsdataTable != null && objHouseDetailsdataTable.Rows.Count > 0)
                            {
                                foreach (DataRow datarowItem in objHouseDetailsdataTable.Rows)
                                {
                                    var _soceityID     = datarowItem.Field <string>("SiD");
                                    var _houseID       = datarowItem.Field <string>("HiD");
                                    var _houseNo       = datarowItem.Field <string>("House No");
                                    var _meterID       = datarowItem.Field <string>("MiD");
                                    var _meterType     = Convert.ToInt16(datarowItem.Field <Int16>("PiD"));
                                    var _meterSettings = datarowItem.Field <string>("metersetting");
                                    var _ipAddress     = datarowItem.Field <string>("IPAddress");
                                    var _port          = Convert.ToInt32(datarowItem.Field <string>("Port"));
                                    if (_meterType == 3)
                                    {
                                        Modbus itembus = Newtonsoft.Json.JsonConvert.DeserializeObject <Modbus>(_meterSettings);
                                        if (itembus != null && itembus.RiD.Length > 0 && itembus.Address.Length > 0)
                                        {
                                            var _regType      = Convert.ToInt32(itembus.RiD);
                                            var _startAddress = Convert.ToInt32(itembus.Address);
                                            var _qty          = itembus.Quantity;
                                            var _deviceID     = itembus.DeviceID;
                                            //int[] readHoldingRegisters = ModbusReading.ReadRegisterWithDeviceIDs(_ipAddress, _port, _startAddress, _regType, _qty, _deviceID);
                                            //var byteresult = GetMSB(readHoldingRegisters);
                                            bool _response = modbusReader.OpenProtocol(_ipAddress, _port);
                                            if (_response == true)
                                            {
                                                var _reading = modbusReader.ReadHoldingregister(Convert.ToString(_deviceID), Convert.ToString(_startAddress), Convert.ToString(_qty));//("1", "3204", "5");
                                                if (_reading != null && _reading.Length > 0)
                                                {
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                    }
                }
                catch (Exception _exception)
                {
                    if (_exception.InnerException != null)
                    {
                        WriteToLog(_exception.InnerException.ToString());
                    }
                    else
                    {
                        WriteToLog(_exception.Message.ToString());
                    }
                }

                System.Threading.Thread.Sleep(syncInterval);
            }while (true);
        }
        static private void SolarSync()
        {
            var readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");

            do
            {
                try
                {
                    _apiKey       = "35e63cb9-d1b8-49cd-b5c1-39212d33377t"; //System.Configuration.ConfigurationManager.AppSettings["APIKey"];
                    _societyIDKey = "02c7f630-3cca-479f-b36d-7a58807ecc4c"; //"8c634931-122a-4318-b5f7-f63a0d3135b3";//System.Configuration.ConfigurationManager.AppSettings["SocietyKey"];

                    if (_apiKey != null && _apiKey.Length > 0 && _societyIDKey != null && _societyIDKey.Length > 0)
                    {
                        if (readDataClient == null)
                        {
                            readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                            WriteToLog(DateTime.Now + "New Object Initialized For GetHouseDetails");
                        }
                        if (readDataClient != null)
                        {
                            DataTable objHouseDetailsdataTable = new DataTable();
                            if (readDataClient == null)
                            {
                                readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                                WriteToLog(DateTime.Now + "New Object Initialized For GetHouseDetails");
                            }
                            objHouseDetailsdataTable = readDataClient.GetModbusDetails(_societyIDKey, _apiKey);

                            if (objHouseDetailsdataTable != null && objHouseDetailsdataTable.Rows.Count > 0)
                            {
                                foreach (DataRow datarowItem in objHouseDetailsdataTable.Rows)
                                {
                                    var _soceityID = datarowItem.Field <string>("SiD");
                                    //var _houseID = datarowItem.Field<string>("HiD");
                                    var _siteName      = datarowItem.Field <string>("Site Name");//House No
                                    var _meterID       = datarowItem.Field <string>("SMiD");
                                    var _meterType     = Convert.ToInt16(datarowItem.Field <Int16>("PiD"));
                                    var _meterSettings = datarowItem.Field <string>("MeterSetting");
                                    var _ipAddress     = datarowItem.Field <string>("IPAddress");
                                    var _port          = Convert.ToInt32(datarowItem.Field <string>("Port"));
                                    if (_meterType == 3)
                                    {
                                        Modbus itembus = Newtonsoft.Json.JsonConvert.DeserializeObject <Modbus>(_meterSettings);
                                        if (itembus != null && itembus.RiD.Length > 0 && itembus.Address.Length > 0)
                                        {
                                            var _regType      = Convert.ToInt32(itembus.RiD);
                                            var _startAddress = Convert.ToInt32(itembus.DEAddress);
                                            var _qty          = itembus.DEQuantity;
                                            var _deviceID     = itembus.DeviceID;
                                            var _protocalType = itembus.ProtocolType;
                                            if (modbusReader == null)
                                            {
                                                modbusReader = new ModBus();
                                            }
                                            bool _response = modbusReader.OpenProtocol(_ipAddress, _port, _protocalType);
                                            if (_response == true)
                                            {
                                                var _reading = modbusReader.ReadHoldingregister(Convert.ToString(_deviceID), Convert.ToString(_startAddress), Convert.ToString(_qty));//("1", "3204", "5");
                                                if (_reading != null && _reading.Length > 0)
                                                {
                                                    try
                                                    {
                                                        var id        = _reading[0]; //4655;
                                                        var hexid     = $"{id:X}";
                                                        var id1       = _reading[1]; //31213;
                                                        var hexid1    = $"{id1:X}";
                                                        var resulthex = hexid + hexid1;
                                                        int value     = Convert.ToInt32(resulthex, 16);

                                                        if (readDataClient == null)
                                                        {
                                                            readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                                                            WriteToLog(DateTime.Now + "New Object Initialized");
                                                        }
                                                        readDataClient.WriteSolarDetails(_soceityID, _siteName, _meterID, _ipAddress, Convert.ToString(_port), "0.00", Convert.ToString(value), 3);
                                                        _stats = "SUCCESS";
                                                        readDataClient.WriteModbusErrorLog(_soceityID, _meterID, _ipAddress, Convert.ToString(_port), _stats, "0", "Success", DateTime.Now);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        WriteToLog(DateTime.Now + e.InnerException.ToString());
                                                    }
                                                }
                                                else
                                                {
                                                    _stats = "FAULT";
                                                    readDataClient.WriteModbusErrorLog(_soceityID, _meterID, _ipAddress, Convert.ToString(_port), _stats, "2", "Meter not reachable", DateTime.Now);
                                                }
                                            }
                                            else
                                            {
                                                _stats = "FAULT";
                                                readDataClient.WriteModbusErrorLog(_soceityID, _meterID, _ipAddress, Convert.ToString(_port), _stats, "1", "IP not reachable", DateTime.Now);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                    }
                }
                catch (Exception _exception)
                {
                    if (_exception.InnerException != null)
                    {
                        WriteToLog(_exception.InnerException.ToString());
                    }
                    else
                    {
                        WriteToLog(_exception.Message.ToString());
                    }
                }

                System.Threading.Thread.Sleep(syncInterval);
            }while (true);
        }