Esempio n. 1
0
File: Sql.cs Progetto: wpmyj/csharp
        private static bool InsertToDensitySheet(DensityPacket Packet)
        {
            string sCommand = "";

            try
            {
                Packet.StationId = ASearchBInSheet(Configs.SqlStationInforSheet, "StationNumber", Packet.StationNumber);
                sCommand         = "INSERT INTO " + Configs.SqlDensitySheet + " VALUES("
                                   + Packet.StationId + ","
                                   + Packet.StationNumber + ","
                                   + AddHead(Packet.RecordDataTime).ToString() + ","
                                   + Packet.RecordWaterLevel + ","
                                   + Packet.RecordRainFall + ","
                                   + Packet.RecordDimension + ","
                                   + Packet.RecordLongitude + ","
                                   + Packet.RecordTemperature + ","
                                   + Packet.RecordVoltage + ","
                                   + AddHead(Packet.RecordOther).ToString() + ","
                                   + AddHead(Packet.Note).ToString() + ")";
                TryOpen();
                SqlCommand objSqlCommand = new SqlCommand(sCommand, Sql.SqlConn);
                objSqlCommand.ExecuteNonQuery();
                TryClose();
            }
            catch (Exception E)
            {
                CreateInfor.WriteLogs(E.Message + "->导致【" + sCommand + "】入库失败");
                Console.WriteLine(sCommand);
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
File: Sql.cs Progetto: wpmyj/csharp
 public static bool MainSql()
 {
     try
     {
         SqlConn = new SqlConnection(Read_User_Str());
         SqlConn.Open();
         if (SqlConn.State == ConnectionState.Open)
         {
             SysFlag.SqlConnectOk = true;
         }
         else
         {
             return(false);
         }
         RealCollectThread = new Thread(new ThreadStart(RealCollect));
         RealCollectThread.Start();
     }
     catch (Exception E)
     {
         CreateInfor.WriteLogs(E.Message + "导致【数据库连接失败】请手动修改当前目录下Configs配置文件");
         SysFlag.SqlConnectOk = false;
         return(false);
     }
     return(true);
 }
Esempio n. 3
0
        public static bool InsertToRealTimeSheet(RealTimePacket Packet)
        {
            string sCommand = "";

            try
            {
                int.Parse(Packet.RecordExplain);
            }
            catch { Packet.RecordExplain = "0"; }
            try
            {
                Packet.RecordDataTime = DateTime.Parse(Packet.RecordDataTime).AddSeconds(double.Parse(Packet.RecordExplain)).ToString();
                sCommand = "INSERT INTO " + SheetName.ST_GRW_R + "(STCD,TM,GWBD,GWBDRMK) VALUES("
                           + Tools.GetSqlString(Packet.StationNumber) + ","
                           + "to_date(" + Tools.GetSqlString(Packet.RecordDataTime) + ",'yyyy-mm-dd hh24:mi:ss'),"
                           + Tools.GetSqlString(Packet.RecordWaterLevel) + ","
                           + Tools.GetSqlString(Packet.RecordExplain) + ")";
                TryOpen();
                OracleCommand objSqlCommand = new OracleCommand(sCommand, OracleConn);
                objSqlCommand.ExecuteNonQuery();
                TryClose();
            }
            catch (Exception E)
            {
                CreateInfor.WriteLogs(E.Message + "->导致【" + sCommand + "】入库失败或数据被更新!");
                return(false);
            }
            return(true);
        }
Esempio n. 4
0
File: Sql.cs Progetto: wpmyj/csharp
        private static string ASearchBInSheet(string table, string Source, string Target)
        {
            string temp = "select * from ";

            temp += table + " where ";
            temp += Source + "=";
            temp += Target;
            try
            {
                TryOpen();
                SqlCommand    objSqlCommand = new SqlCommand(temp, Sql.SqlConn);
                SqlDataReader Reader        = objSqlCommand.ExecuteReader();
                temp = "NULL";
                while (Reader.Read())
                {
                    temp = Reader.GetValue(0).ToString();
                    if (temp != null)
                    {
                        break;
                    }
                }
                Reader.Close();
                TryClose();
            }
            catch (Exception E)
            {
                CreateInfor.WriteLogs(E.Message + "->导致【" + temp + "】执行失败!");
            }
            return(temp);
        }
Esempio n. 5
0
        public static bool CreateServiceThread()
        {
            int Count = 0;

            while (SysFlag.ServiceStart && Count++ < 1000)
            {
                try
                {
                    Configs.ListenSock.Bind(Tools.Get_IpPoint(Configs.ServicePoint));//绑定
                    break;
                }
                catch { System.Threading.Thread.Sleep(100); }
            }
            Configs.ListenSock.Listen(10);//监听
            Configs.ListenSock.ReceiveTimeout = 5000;
            Configs.ListenSock.SendTimeout    = 5000;
            SysFlag.ListenOk = true;
            if (Count > 990)
            {
                CreateInfor.WriteLogs("【申请用户交互端口失败】请释放或更改端口!");
                SysFlag.ListenOk     = false;
                SysFlag.ServiceStart = false;//服务启动失败
                return(false);
            }
            CreateInfor.WriteLogs("第【" + Count.ToString() + "】次申请侦听端口才成功!");
            Thread ServiceThread = new Thread(new ThreadStart(ServiceMainThread));

            ServiceThread.Start();
            CreateInfor.WriteLogs("侦听主线程创建成功!使用通道【" + Tools.Get_IpPoint(Configs.ServicePoint).ToString() + "】");
            return(true);
        }
Esempio n. 6
0
        public static bool UpdataToRealTimeSheet(RealTimePacket Packet)
        {
            string sCommand = "";

            try
            {
                int.Parse(Packet.RecordExplain);
            }
            catch { Packet.RecordExplain = "0"; }
            try
            {
                Packet.RecordDataTime = DateTime.Parse(Packet.RecordDataTime).AddSeconds(double.Parse(Packet.RecordExplain)).ToString();
                sCommand = "UPDATE " + SheetName.ST_GRW_R + " SET " +
                           " STCD=" + Tools.GetSqlString(Packet.StationNumber) + "," +
                           " TM=" + "to_date(" + Tools.GetSqlString(Packet.RecordDataTime) + ",'yyyy-mm-dd hh24:mi:ss')," +
                           " GWBD=" + Tools.GetSqlString(Packet.RecordWaterLevel) + "," +
                           " GWBDRMK=" + Tools.GetSqlString(Packet.RecordExplain) +
                           " WHERE STCD=" + Tools.GetSqlString(Packet.StationNumber) +
                           " AND TM=to_date(" + Tools.GetSqlString(Packet.RecordDataTime) + ",'yyyy-mm-dd hh24:mi:ss')";
                TryOpen();
                OracleCommand objSqlCommand = new OracleCommand(sCommand, OracleConn);
                objSqlCommand.ExecuteNonQuery();
                TryClose();
            }
            catch (Exception E)
            {
                CreateInfor.WriteLogs(E.Message + "->导致【" + sCommand + "】更新失败!");
                return(false);
            }
            return(true);
        }
Esempio n. 7
0
 public static void MainThread()
 {
     while (SysFlag.ServiceStart)
     {
         try
         {
             Socket       client    = Configs.ListenSock.Accept();//当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
             IPEndPoint   clientip  = (IPEndPoint)client.RemoteEndPoint;
             ClientThread newclient = new ClientThread(client);
             Thread       newthread = new Thread(new ThreadStart(newclient.ClientService));
             newthread.Start();
             SysFlag.ListenOk = true;
             if (!SysFlag.ServiceStart)
             {
                 client.Close(); return;
             }
         }
         catch (Exception E)
         {
             if (SysFlag.ListenOk)
             {
                 CreateInfor.WriteLogs(E.Message + "导致【RTU服务主线程抛锚】!");
             }
             SysFlag.ListenOk = false;
         }
     }
     Configs.ListenSock.Close();
     CreateInfor.WriteLogs("侦听通道关闭!");
 }
Esempio n. 8
0
 public void ClientService()
 {
     byte[] bytes     = new byte[Configs.DataByteSize];
     byte[] sendbytes = new byte[Configs.DataByteSize];
     if (service != null)
     {
         SysFlag.ClientCount++;
     }
     try
     {
         NetworkStream NewStream = new NetworkStream(service);
         NewStream.ReadTimeout = Configs.ReviceTimeOut;
         while (SysFlag.ServiceStart)
         {
             try
             {
                 //int RxLength = NewStream.Read(bytes, 0, Configs.DataByteSize);
                 int RxLength = service.Receive(bytes);
                 if (RxLength > Configs.AgreementAAAMinLength)
                 {
                     int  TxLength = 0;
                     bool ok       = DataCollection.ExpendRecord(bytes, RxLength);
                     if (ok)
                     {
                         TxLength = DataCollection.CreateReturnInfor(ref sendbytes, "OK");
                     }
                     else
                     {
                         TxLength = DataCollection.CreateReturnInfor(ref sendbytes, "ER");
                     }
                     //NewStream.Write(sendbytes, 0, TxLength);
                     service.Send(sendbytes);
                 }
                 else
                 {
                     break;
                 }
             }
             catch (Exception E)
             {
                 CreateInfor.WriteLogs(E.Message + "导致【RTU服务子线程抛锚】!");
                 break;
             }
         }
         service.Close();
     }
     catch { }
     if (SysFlag.ClientCount > 0)
     {
         SysFlag.ClientCount--;
     }
 }
Esempio n. 9
0
        public void ClientService()
        {
            byte[] RxBuffer  = new byte[Configs.DataByteSize];
            byte[] sendbytes = new byte[Configs.DataByteSize];
            int    RxLength;
            bool   LoginOk = false;

            if (service != null)
            {
                SysFlag.UserCount++;
            }

            try
            {
                NetworkStream NewStream = new NetworkStream(service);
                NewStream.ReadTimeout = Configs.UserTimeOut;
                //  if (!ReadLogin(NewStream)) return;
                while (SysFlag.ServiceStart)
                {
                    try
                    {
                        RxLength = NewStream.Read(RxBuffer, 0, Configs.DataByteSize);
                        if (RxLength > 0)
                        {
                            string UFA = UserFaceAnswered(RxBuffer, RxLength, ref LoginOk);
                            if (UFA != "")
                            {
                                sendbytes = Encoding.GetEncoding("gb2312").GetBytes(UFA);
                                NewStream.Write(sendbytes, 0, sendbytes.Length);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    catch (TimeoutException E) { }
                    catch {; break; }
                }
                service.Close();
            }
            catch { CreateInfor.WriteLogs("程序关闭延迟导致【用户接口读取连接用户数据失败】"); }
            SysFlag.UserCount--;
        }
Esempio n. 10
0
 public static bool LinkOracle()
 {
     try
     {
         OracleConn = new OracleConnection(Read_User_Str());
         OracleConn.Open();
         if (OracleConn.State == ConnectionState.Open)
         {
             return(true);
         }
     }
     catch (Exception E)
     {
         CreateInfor.WriteLogs(E.Message + "导致【数据库连接失败】请手动修改当前目录下Configs配置文件");
         SysFlag.SqlConnectOk = false;
         return(false);
     }
     return(true);
 }
Esempio n. 11
0
        public static bool ReSetConfigS(ref string D, string Nod, string S)
        {
            string SS = S;

            try
            {
                S = ReadVal(RootStr, Nod, S);
            }
            catch (Exception E)
            {
                CreateInfor.WriteLogs(E.Message + "没有找到对象:" + S);
            }
            if (S == "")
            {
                CreateInfor.WriteLogs("配置项为空,已加载默认值,请检查!" + SS); CreateXmlDoc(); return(false);
            }
            D = S;
            return(true);
        }
Esempio n. 12
0
 static void Main(string[] args)
 {
     Tools.IfNotFileCreat(Configs.Path);
     CreateInfor.WriteLogs("================================================Start=======================================================");
     if (XML.LoadConfig())
     {
         CreateInfor.WriteLogs("读取配置成功!");
     }
     else
     {
         CreateInfor.WriteLogs("读取配置失败,已重置配置,服务结束!"); return;
     }
     if (Sql.MainSql())
     {
         CreateInfor.WriteLogs("SqlService数据库连接成功!");
     }
     else
     {
         CreateInfor.WriteLogs("SqlService数据库连接失败,服务结束!"); return;
     }
     if (Oracle.LinkOracle())
     {
         CreateInfor.WriteLogs("Oracle数据库连接成功!");
     }
     else
     {
         CreateInfor.WriteLogs("Oracle数据库连接失败,服务结束!"); return;
     }
     //  if (UserFaceThread.CreateUser()) CreateInfor.WriteLogs("用户访问通道建立!");
     // else { CreateInfor.WriteLogs("用户访问通道创建失败,服务结束!"); return; }
     if (ClientThread.CreateServiceThread())
     {
         CreateInfor.WriteLogs("侦听通道建立!");
     }
     else
     {
         CreateInfor.WriteLogs("侦听通道通道创建失败,服务结束!"); return;
     }
     CreateInfor.WriteLogs("服务完全进入运行状态");
 }
Esempio n. 13
0
 public static void MainThread()
 {
     while (SysFlag.ServiceStart)
     {
         try
         {
             Socket   client    = Configs.UserSock.Accept();//当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
             UserFace newclient = new UserFace(client);
             Thread   newthread = new Thread(new ThreadStart(newclient.ClientService));
             newthread.Start();
             if (!SysFlag.ServiceStart)
             {
                 client.Close(); return;
             }
         }
         catch (Exception E)
         {
             CreateInfor.WriteLogs(E.Message + "导致【错误处于客户端侦听循环】");
         }
     }
     Configs.UserSock.Close();
     CreateInfor.WriteLogs("用户访问通道关闭!");
 }
Esempio n. 14
0
        public string  UserFaceAnswered(byte[] RxBuffer, int RxLength, ref bool LoginOk)
        {
            string      Msg = Encoding.GetEncoding("gb2312").GetString(RxBuffer, 0, RxLength);
            ManageOrder MO  = new ManageOrder(Msg);

            if (MO.Oder1 == "login")
            {
                if (XML.ReadLogin(MO.Oder2, MO.Oder3))
                {
                    CreateInfor.WriteLogs("用户【" + MO.Oder2 + "】登录!");
                    LoginOk = true;
                    return("LoginOk");
                }
            }
            if (!LoginOk)
            {
                return("LoginError");
            }
            if (MO.Oder1 == "begin")
            {
                switch (MO.Oder2)
                {
                case "get": switch (MO.Oder3)
                    {
                    case "end": break;

                    case "clientcount": return(SysFlag.ClientCount.ToString());

                    case "usercount": return(SysFlag.UserCount.ToString());

                    case "sqlconnectok": return(SysFlag.SqlConnectOk.ToString());

                    case "sqlpassword": return(Configs.SqlPassWord);

                    case "sqlname": return(Configs.SqlName);

                    case "sqldatabase": return(Configs.SqlDataBase);

                    case "sqldatasource": return(Configs.SqlDataSource);

                    case "sqldensitysheet": return(Configs.SqlDensitySheet);

                    case "sqlrealtimesheet": return(Configs.SqlRealTimeSheet);

                    case "sqlstationinfor": return(Configs.SqlStationInforSheet);

                    case "configspath": return(Configs.Path);

                    case "densityqueuesize": return(Configs.DensityPacketQueue.Count.ToString());

                    case "realtimequeuesize": return(Configs.RealTimePacketQueue.Count.ToString());

                    case "listenok": return(SysFlag.ListenOk.ToString());

                    case "runpath": return(Directory.GetCurrentDirectory());

                    case "realtimepacketcount": return(SysFlag.RealTimePacketCount.ToString());

                    case "densitypacketcount": return(SysFlag.DensityPacketCount.ToString());
                    }
                    break;

                case "set": switch (MO.Oder3)
                    {
                    case "sqlname": return(XML.SetConfigS(ref Configs.SqlName, MO.Oder4).ToString());

                    case "sqlpassword": return(XML.SetConfigS(ref Configs.SqlPassWord, MO.Oder4).ToString());

                    case "sqldatasource": return(XML.SetConfigS(ref Configs.SqlDataSource, MO.Oder4).ToString());

                    case "sqldatabase": return(XML.SetConfigS(ref Configs.SqlDataBase, MO.Oder4).ToString());

                    case "sqldensitysheet": return(XML.SetConfigS(ref Configs.SqlDensitySheet, MO.Oder4).ToString());

                    case "sqlrealtimesheet": return(XML.SetConfigS(ref Configs.SqlRealTimeSheet, MO.Oder4).ToString());

                    case "servicepoint": return(XML.SetConfigD(ref Configs.ServicePoint, MO.Oder4).ToString());

                    case "sqlstationinforsheet": return(XML.SetConfigS(ref Configs.SqlStationInforSheet, MO.Oder4).ToString());

                    case "userrootpassword": return(XML.SetConfigS(ref Configs.UserRootPassWord, MO.Oder4).ToString());
                    }
                    break;

                case "open": switch (MO.Oder3)
                    {
                    case "end": break;
                    }
                    break;

                case "restart": switch (MO.Oder3)
                    {
                    case "end": break;
                    }
                    break;

                default: break;
                }
            }
            return("False");
        }
Esempio n. 15
0
 protected override void OnStop()
 {
     CreateInfor.WriteLogs("================================================Stop========================================================\r\n");
     SysFlag.ServiceStart = false;
     System.GC.Collect();
 }