コード例 #1
0
        static private void DLMXsync()
        {
            var readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");

            do
            {
                try
                {
                    _apiKey       = _txtAPIKey.ToString();    //System.Configuration.ConfigurationManager.AppSettings["APIKey"];
                    _societyIDKey = _txtSocietyID.ToString(); //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 = null; //ModbusReading.ReadingRegister(_ipAddress, _port, _startAddress, _regType, _qty);
                                        //    //ReadModbus(_ipAddress, _port, _startAddress, _regType);

                                        //    if (readHoldingRegisters != null && readHoldingRegisters.Length > 0)
                                        //    {
                                        //        //objdataTableWriteDLMX.Rows.Add(_soceityID, _houseID, _meterID, _ipAddress, _port, readHoldingRegisters);
                                        //    }
                                        //}
                                    }
                                    else
                                    {
                                        Model.DLMX itemdLMX = Newtonsoft.Json.JsonConvert.DeserializeObject <Model.DLMX>(_meterSettings);

                                        if (itemdLMX != null && itemdLMX.Manufacturer.Length > 0 && itemdLMX.Model.Length > 0)
                                        {
                                            var      manufacture  = itemdLMX.Manufacturer;
                                            var      model        = itemdLMX.Model;
                                            var      importExport = Convert.ToInt16(itemdLMX.ImportExport);
                                            string[] val          = dLMX.DLMXRead(_ipAddress, _port, importExport);
                                            if (val != null && val.Length > 0)
                                            {
                                                try
                                                {
                                                    if (readDataClient == null)
                                                    {
                                                        readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                                                        WriteToLog(DateTime.Now + "New Object Initialized");
                                                    }

                                                    readDataClient.WriteDLMXDetails(_soceityID, _houseNo, _meterID, _ipAddress, Convert.ToString(_port), val[0], val[1], importExport);

                                                    if (val[2] == "1" || val[2] == "2" || val[2] == "0")
                                                    {
                                                        readDataClient.WriteErrorLog(_soceityID, _houseID, _meterID, _ipAddress, Convert.ToString(_port), "FAULT", val[2], val[3], DateTime.Now);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    WriteToLog(DateTime.Now + e.InnerException.ToString());
                                                }
                                            }
                                            else
                                            {
                                                readDataClient.WriteDLMXDetails(_soceityID, _houseID, _meterID, _ipAddress, Convert.ToString(_port), null, null, 0);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                    }
                }
                #region
                //catch (CommunicationException _communicationException)
                //{
                //    Console.ForegroundColor = ConsoleColor.Red;
                //    Console.WriteLine(_communicationException.InnerException.ToString());
                //    Console.WriteLine("Got {0}", _communicationException.GetType());
                //    WriteToLog(DateTime.Now + _communicationException.InnerException.ToString() + " : Communication Out Exception");
                //    Console.ResetColor();
                //    //readDataClient.Abort();
                //}
                //catch (TimeoutException _timeoutException)
                //{
                //    Console.ForegroundColor = ConsoleColor.Red;
                //    Console.WriteLine(_timeoutException.InnerException.ToString());
                //    Console.WriteLine("Time Out Exception {0}", _timeoutException.GetType());
                //    WriteToLog(DateTime.Now + _timeoutException.InnerException.ToString() + " : Time Out Exception");
                //    Console.ResetColor();
                //    //readDataClient.Abort();
                //}
                #endregion
                catch (Exception _exception)
                {
                    if (_exception.InnerException != null)
                    {
                        WriteToLog(_exception.InnerException.ToString());
                    }
                    else
                    {
                        WriteToLog(_exception.Message.ToString());
                    }
                }

                System.Threading.Thread.Sleep(syncInterval);
            }while (true);
        }
コード例 #2
0
        static private void DLMXsync()
        {
            string[] value          = new string[2];
            var      readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");

            do
            {
                try
                {
                    //_apiKey = _txtAPIKey.ToString(); //System.Configuration.ConfigurationManager.AppSettings["APIKey"];
                    //_societyIDKey = _txtSocietyID.ToString(); //System.Configuration.ConfigurationManager.AppSettings["SocietyKey"];
                    _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.GetDMLSDetails(_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 = null; //ModbusReading.ReadingRegister(_ipAddress, _port, _startAddress, _regType, _qty);
                                        //    //ReadModbus(_ipAddress, _port, _startAddress, _regType);

                                        //    if (readHoldingRegisters != null && readHoldingRegisters.Length > 0)
                                        //    {
                                        //        //objdataTableWriteDLMX.Rows.Add(_soceityID, _houseID, _meterID, _ipAddress, _port, readHoldingRegisters);
                                        //    }
                                        //}
                                    }
                                    else
                                    {
                                        Model.DLMX itemdLMX = Newtonsoft.Json.JsonConvert.DeserializeObject <Model.DLMX>(_meterSettings);

                                        if (itemdLMX != null && itemdLMX.Manufacturer.Length > 0 && itemdLMX.Model.Length > 0)
                                        {
                                            var manufacture     = itemdLMX.Manufacturer;
                                            var model           = itemdLMX.Model;
                                            var importExport    = Convert.ToInt16(itemdLMX.ImportExport);
                                            var cientAddress    = itemdLMX.ClientAdd;
                                            var _name           = itemdLMX.Name;
                                            var _authentication = itemdLMX.Authentication;
                                            var _password       = itemdLMX.Password;
                                            var _SObisValue     = itemdLMX.SObisValue;
                                            var _RObisValue     = itemdLMX.RObisValue;
                                            var _serialNoConfig = itemdLMX.SerialNo;
                                            if (reader == null)
                                            {
                                                reader = new Reader();
                                            }
                                            try
                                            {
                                                reader = new Reader(_ipAddress, _port, cientAddress, _name, _authentication, _password, _SObisValue, _RObisValue);//
                                                value  = reader.DLMSImport();
                                            }
                                            catch (Exception _exception)
                                            {
                                                if (_exception.InnerException != null)
                                                {
                                                    WriteToLog(_exception.InnerException.ToString());
                                                }
                                                else
                                                {
                                                    WriteToLog(_exception.InnerException.ToString());
                                                }
                                                #region
                                                //reader = new Reader(_ipAddress, _port, cientAddress, _name, _authentication, _password, _SObisValue, _RObisValue);
                                                //value = reader.Read();

                                                //else
                                                //{
                                                //    try
                                                //    {
                                                //        WriteToLog(_exception.Message.ToString());
                                                //        reader = new Reader(_ipAddress, _port, cientAddress, _name, _authentication, _password, _SObisValue, _RObisValue);
                                                //        value = reader.Read();
                                                //    }
                                                //    catch (Exception _exceptions)
                                                //    {
                                                //        if (_exception.Message != null)
                                                //            WriteToLog(_exception.Message.ToString());
                                                //        else
                                                //            WriteToLog(_exceptions.InnerException.ToString());
                                                //        DataTable objLatestMeterreadingdataTable = new DataTable();
                                                //        objLatestMeterreadingdataTable = readDataClient.GetLatestMeterreading(_soceityID, _ipAddress, importExport);
                                                //        if (objLatestMeterreadingdataTable.Rows.Count > 0)
                                                //        {
                                                //            DataRow _datarowLatestMeterreading = objLatestMeterreadingdataTable.Rows[0];
                                                //            var _readings = _datarowLatestMeterreading.Field<double>("Reading");
                                                //            var _serialNo = _datarowLatestMeterreading.Field<Int64>("SerialNo");
                                                //            readDataClient.WriteDLMXDetails(_soceityID, _houseNo, _meterID, _ipAddress, Convert.ToString(_port), Convert.ToString(_serialNo), Convert.ToString(_readings), importExport);
                                                //            _stats = "FAULT";
                                                //            value[2] = "1";
                                                //            value[3] = "IP not reachable";
                                                //            readDataClient.WriteErrorLog(_soceityID, _houseID, _meterID, _ipAddress, Convert.ToString(_port), _stats, value[2], value[3], DateTime.Now);
                                                //        }
                                                //    }

                                                //}
                                                #endregion
                                            }

                                            if (value != null && value.Length > 0)
                                            {
                                                try
                                                {
                                                    if (readDataClient == null)
                                                    {
                                                        readDataClient = new ReadingServiceReference.UberServiceClient("BasicHttpBinding_IUberService");
                                                        WriteToLog(DateTime.Now + "New Object Initialized");
                                                    }
                                                    //value[0]->Serial Number
                                                    //value[1]->reading
                                                    //readDataClient.WriteDLMXDetails(_soceityID, _houseNo, _meterID, _ipAddress, Convert.ToString(_port), value[0], value[1], importExport);

                                                    if (value[2] == "0" || value[2] == "1" || value[2] == "2")
                                                    {
                                                        if (value[2] == "1" || value[2] == "2")
                                                        {
                                                            DataTable objLatestMeterreadingdataTable = new DataTable();
                                                            objLatestMeterreadingdataTable = readDataClient.GetLatestMeterreading(_soceityID, _ipAddress, importExport);
                                                            if (objLatestMeterreadingdataTable.Rows.Count > 0)
                                                            {
                                                                DataRow _datarowLatestMeterreading = objLatestMeterreadingdataTable.Rows[0];
                                                                var     _readings = _datarowLatestMeterreading.Field <double>("Reading");
                                                                var     _serialNo = _datarowLatestMeterreading.Field <Int64>("SerialNo");
                                                                readDataClient.WriteDLMXDetails(_soceityID, _houseNo, _meterID, _ipAddress, Convert.ToString(_port), Convert.ToString(_serialNo), Convert.ToString(_readings), importExport);
                                                                _stats = "FAULT";
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (_serialNoConfig.ToString() == value[0].ToString())
                                                            {
                                                                readDataClient.WriteDLMXDetails(_soceityID, _houseNo, _meterID, _ipAddress, Convert.ToString(_port), value[0], value[1], importExport);
                                                                _stats = "SUCCESS";
                                                            }
                                                            else
                                                            {
                                                                DataTable objLatestMeterreadingdataTable = new DataTable();
                                                                objLatestMeterreadingdataTable = readDataClient.GetLatestMeterreading(_soceityID, _ipAddress, importExport);
                                                                if (objLatestMeterreadingdataTable.Rows.Count > 0)
                                                                {
                                                                    DataRow _datarowLatestMeterreading = objLatestMeterreadingdataTable.Rows[0];
                                                                    var     _readings = _datarowLatestMeterreading.Field <double>("Reading");
                                                                    var     _serialNo = _datarowLatestMeterreading.Field <Int64>("SerialNo");
                                                                    readDataClient.WriteDLMXDetails(_soceityID, _houseNo, _meterID, _ipAddress, Convert.ToString(_port), Convert.ToString(_serialNo), Convert.ToString(_readings), importExport);
                                                                    _stats   = "FAULT";
                                                                    value[2] = "3";
                                                                    value[3] = "Serial No Mismatch";
                                                                }
                                                            }
                                                        }
                                                        readDataClient.WriteErrorLog(_soceityID, _houseID, _meterID, _ipAddress, Convert.ToString(_port), _stats, value[2], value[3], DateTime.Now);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    WriteToLog(DateTime.Now + e.InnerException.ToString());
                                                }
                                            }
                                            else
                                            {
                                                readDataClient.WriteDLMXDetails(_soceityID, _houseID, _meterID, _ipAddress, Convert.ToString(_port), null, null, 0);//asdsadfg
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                    }
                }
                #region
                //catch (CommunicationException _communicationException)
                //{
                //    Console.ForegroundColor = ConsoleColor.Red;
                //    Console.WriteLine(_communicationException.InnerException.ToString());
                //    Console.WriteLine("Got {0}", _communicationException.GetType());
                //    WriteToLog(DateTime.Now + _communicationException.InnerException.ToString() + " : Communication Out Exception");
                //    Console.ResetColor();
                //    //readDataClient.Abort();
                //}
                //catch (TimeoutException _timeoutException)
                //{
                //    Console.ForegroundColor = ConsoleColor.Red;
                //    Console.WriteLine(_timeoutException.InnerException.ToString());
                //    Console.WriteLine("Time Out Exception {0}", _timeoutException.GetType());
                //    WriteToLog(DateTime.Now + _timeoutException.InnerException.ToString() + " : Time Out Exception");
                //    Console.ResetColor();
                //    //readDataClient.Abort();
                //}
                #endregion
                catch (Exception _exception)
                {
                    if (_exception.InnerException != null)
                    {
                        WriteToLog(_exception.InnerException.ToString());
                    }
                    else
                    {
                        WriteToLog(_exception.Message.ToString());
                    }
                }

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