Esempio n. 1
0
        private void DeviceControlCallBack(Socket socket, string json)
        {
            C_To_S_Data <CSDataStandard.Transfer.DeviceControl> receiveObj = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.DeviceControl> >(json);

            //获取所有Org
            Clazz.Config.XML_Org org = SysConfig.orgConfig.GetOrgByOrgId(receiveObj.OrgId);
            if (org == null)  //判断Org是否存在
            {
                string msg = "OrgId:" + receiveObj.OrgId + "不存在";
                LogMg.AddError(msg);
                lb_msg.Items.Add(msg);
            }
            else
            {
                try
                {
                    SWSDataContext  db          = new SWSDataContext(ServerSocketHelper.GetConnection(org.DBName));
                    string          stationName = "未知的客户端";
                    country_station station     = db.country_station.SingleOrDefault(c => c.id == receiveObj.StationId);
                    if (station != null)
                    {
                        stationName = station.name;
                    }

                    saveData(db, receiveObj, stationName);

                    sendData(db, socket, stationName, receiveObj.StationId);
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex);
                    DEBUG.MsgBox(ex.ToString());
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 保存客户端的IP地址
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="c_to_s_data"></param>
        public void SaveClientIp(Socket socket, C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data)
        {
            IPEndPoint ipEndPoint = (IPEndPoint)socket.RemoteEndPoint;
            string     ip         = ipEndPoint.Address.ToString();

            Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId);

            if (_org == null)
            {
                //将信息写入到日志文件中    orgid为***的污水厂不存在
                LogMg.AddError(String.Format("OrgId:{0}  不存在", c_to_s_data.OrgId));
                //isSuccess = false;
            }
            else
            {
                try
                {
                    SWSDataContext  SWS     = new SWSDataContext(ServerSocketHelper.GetConnection(_org.DBName)); //建立一个分厂数据源提供程序实例
                    country_station station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId);
                    if (station == null)
                    {
                        LogMg.AddError("StationId: " + c_to_s_data.StationId + " 不存在");
                    }
                    else
                    {
                        station.ip = ip;     //保存客户端IP地址
                        SWS.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex);
                }
            }
        }
Esempio n. 3
0
        public void Save(Socket socket, string json)
        {
            C_To_S_Data <object> obj = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <object> >(json);

            Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(obj.OrgId);

            if (_org == null)
            {
                //将信息写入到日志文件中    orgid为***的污水厂不存在
                LogMg.AddError("OrgId:\"{0}\"不存在");
                //isSuccess = false;
            }
            else
            {
                try
                {
                    SWSDataContext  SWS     = new SWSDataContext(ServerSocketHelper.GetConnection(_org.DBName)); //建立一个分厂数据源提供程序实例
                    country_station station = SWS.country_station.SingleOrDefault(c => c.id == obj.StationId);
                    if (station == null)
                    {
                        LogMg.AddError("StationId: " + obj.StationId + " 不存在");
                    }
                    else
                    {
                        station.ip = obj.Data[0].ToString();     //保存客户端IP地址
                        SWS.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex);
                }
            }
        }
Esempio n. 4
0
        private void sendData(SWSDataContext db, Socket socket, string stationName, int stationId)
        {
            //发送设备控制信息到客户端
            C_To_S_Data <CSDataStandard.Transfer.DeviceControl> sendObj = new C_To_S_Data <CSDataStandard.Transfer.DeviceControl>();

            sendObj.Flag = HandleFlag.DeviceControl;
            sendObj.Data = new List <CSDataStandard.Transfer.DeviceControl>();


            // List<device_control> listDeviceControl = db.device_control.Where(c => c.state == "0" && c.station_id == stationId).ToList();   //0表示未发送
            //把那时不需要发送到客户端的记录的发送状态都置为2
            db.ExecuteCommand("UPDATE  dbo.device_control SET state=2 WHERE station_id=" + stationId + " AND state=0 AND id NOT IN (SELECT MAX(id) AS datetime FROM dbo.device_control WHERE station_id=" + stationId + " AND state=0 GROUP BY testid,gong_kuang_id)");
            //找出需要发送的几行数据
            List <device_control> listDeviceControl = db.ExecuteQuery <device_control>("SELECT * FROM dbo.device_control WHERE id IN (SELECT MAX(id) AS datetime FROM dbo.device_control WHERE station_id=" + stationId + " AND state=0 GROUP BY testid,gong_kuang_id)").ToList();

            //device_control device_control = listDeviceControl.OrderByDescending(c => c.datetime).FirstOrDefault();
            foreach (device_control device_control in listDeviceControl)
            {
                try
                {
                    gong_kuang_config gongKuang = db.gong_kuang_config.SingleOrDefault(c => c.id == device_control.gong_kuang_id);
                    CSDataStandard.Transfer.DeviceControl transferDeviceControl = new CSDataStandard.Transfer.DeviceControl();
                    transferDeviceControl.Value           = device_control.control_code; //控制的值
                    transferDeviceControl.Address         = (int)gongKuang.address;
                    transferDeviceControl.Register        = gongKuang.write_register;
                    transferDeviceControl.DeviceControlId = device_control.id;   //设备控制表的id
                    //transferDeviceControl.DeviceNumber = device_control.device_number;
                    //transferDeviceControl.Datetime = Convert.ToDateTime(device_control.datetime);
                    //transferDeviceControl.ControlCode = device_control.control_code;
                    sendObj.Data.Add(transferDeviceControl);
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex);
                }
            }
            //更改设备控制表中的数据为已发送状态
            foreach (device_control item in listDeviceControl)
            {
                item.state = "1";
            }
            db.SubmitChanges();    //保存

            //把对象转换为json字符串
            string sendJson = Utility.JsonHelper.JsonSerializer <C_To_S_Data <CSDataStandard.Transfer.DeviceControl> >(sendObj);

            Byte[] msgSend = Encoding.Unicode.GetBytes(sendJson);
            socket.Send(msgSend, msgSend.Length, SocketFlags.None);
            //显示到UI界面
            // string msg = string.Format("发送{0}行数据到【{1}】客户端", sendObj.Data.Count, stationName);
            //  lb_msg.BeginInvoke(new Action<string>(printMsg), msg);
        }
Esempio n. 5
0
 /// <summary>
 /// 保存客户端发来的设备控制信息    ,把状态字段更新到数据库
 /// </summary>
 /// <param name="db"></param>
 /// <param name="receiveObj"></param>
 private void saveData(SWSDataContext db, C_To_S_Data <CSDataStandard.Transfer.DeviceControl> receiveObj, string stationName)
 {
     try
     {
         //接收客户端数据
         foreach (CSDataStandard.Transfer.DeviceControl item in receiveObj.Data)
         {
             device_control device_control = db.device_control.Where(c => c.id == item.DeviceControlId).SingleOrDefault();
             if (device_control != null)
             {
                 device_control.execute_result = item.ExecuteResult;
             }
         }
         db.SubmitChanges();
         //输出消息
         // string msg = string.Format("保存【{0}】客户端发送来的{1}行设备控制执行结果", stationName, receiveObj.Data.Count);
         // lb_msg.BeginInvoke(new Action<string>(printMsg), msg);
     }
     catch (Exception ex)
     {
         LogMg.AddError(ex);
     }
 }
Esempio n. 6
0
        /// <summary>
        /// 将json字符串转换成对象, 再把对象保存到数据库中
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private bool Save(string json, Socket socket)
        {
            bool   isSuccess   = true;
            string stationName = "";   //分厂名称
            C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json);

            Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId);

            if (_org == null)
            {
                //将信息写入到日志文件中    orgid为***的污水厂不存在
                LogMg.AddError(string.Format("OrgId:{0}不存在", c_to_s_data.OrgId));

                isSuccess = false;
            }
            else
            {
                try
                {
                    SWSDataContext SWS = new SWSDataContext(Util.ServerSocketHelper.GetConnection(_org.DBName));     //建立一个分厂数据源提供程序实例

                    //查找站点名称
                    country_station _station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId);

                    //更新站点的IP
                    _station.ip = ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString();

                    if (_station != null)
                    {
                        stationName = _station.name;    //站点名称
                    }

                    //遍历数据   并把数据添加到数据库中
                    List <realrec> listrealrec = new List <realrec>();
                    List <testrec> listtestrec = new List <testrec>();
                    foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data)
                    {
                        test test = SWS.test.SingleOrDefault(c => c.testid == item.TestId);
                        if (test == null)
                        {
                            LogMg.AddError(string.Format("testid为 {0} 的检测点不存在", item.TestId));    //记录日志
                            isSuccess = false;
                        }
                        else
                        {
                            if (test.means.Trim() == "屏幕取词" || test.means.Trim() == "自动获取")
                            {
                                realrec _realrec = new realrec();
                                _realrec.testid   = item.TestId;
                                _realrec.testtime = item.TestTime;
                                _realrec.value    = (decimal)item.Value;
                                listrealrec.Add(_realrec);
                            }
                            if (test.means.Trim() == "检测录入")
                            {
                                testrec _testrec = SWS.testrec.SingleOrDefault(c => c.testid == item.TestId && c.testtime == item.TestTime);
                                //判断检测当前数据在数据库中是否已经存在
                                if (_testrec == null)
                                {
                                    _testrec          = new testrec();
                                    _testrec.testid   = item.TestId;
                                    _testrec.testtime = item.TestTime;
                                    _testrec.value    = (decimal)item.Value;
                                    listtestrec.Add(_testrec);
                                }
                                else
                                {
                                    _testrec.testid   = item.TestId;
                                    _testrec.testtime = item.TestTime;
                                    _testrec.value    = (decimal)item.Value;
                                }
                            }
                        }
                    }

                    SWS.realrec.InsertAllOnSubmit(listrealrec);
                    SWS.testrec.InsertAllOnSubmit(listtestrec);

                    SWS.SubmitChanges();
                    isSuccess = true;
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    //把错误信息输出到日志文件中
                    LogMg.AddError(ex.ToString());
                    DEBUG.ThrowException(ex);
                }
            }

            //lock (lb_msg)
            //{
            //    if (lb_msg.Items.Count > 200)
            //       lb_msg.Items.Clear();
            //   this.lb_msg.Items.Add(string.Format("时间:{0}     客户端:{1}     数据行数{2}      保存{3}", DateTime.Now.ToString(), stationName, c_to_s_data.Data.Count, isSuccess ? "成功" : "失败"));
            // }

            SaveClientIp(socket, c_to_s_data);

            return(isSuccess);
        }
        //接收到客户端发来的数据,并向客户端返回消息
        private static void RecieveCallBack(IAsyncResult ar)
        {
            Socket RSocket = null;
            string json    = string.Empty;

            try
            {
                RSocket = (Socket)ar.AsyncState;
                int REnd = RSocket.EndReceive(ar);
                json = Encoding.Unicode.GetString(MsgBuffer, 0, REnd);

                if (string.IsNullOrEmpty(json))
                {
                    LogMg.AddError("服务器接收到的数据为空");
                    return;
                }

                C_To_S_Data <object> obj = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <object> >(json);

                AddClient(obj.OrgId, obj.StationId);   //添加或更新客户端信息

                if (obj.Flag == HandleFlag.Country)
                {
                    if (CountryCallBack != null)
                    {
                        CountryCallBack(RSocket, json);
                    }
                }
                else if (obj.Flag == HandleFlag.DeviceControl)
                {
                    if (DeviceControlCallBack != null)
                    {
                        DeviceControlCallBack(RSocket, json);
                    }
                }
                else if (obj.Flag == HandleFlag.PMQC)
                {
                    if (PMQCCallBack != null)
                    {
                        PMQCCallBack(RSocket, json);
                    }
                }
                else if (obj.Flag == HandleFlag.MobileDetection)
                {
                    if (MobileDetectionCallBack != null)
                    {
                        MobileDetectionCallBack(RSocket, json);
                    }
                }
                else if (obj.Flag == HandleFlag.ClientPublicIp)
                {
                    if (ClientPublicIpCallBack != null)
                    {
                        ClientPublicIpCallBack(RSocket, json);
                    }
                }
                else if (obj.Flag == HandleFlag.DownLoadConfig)
                {
                    DownLoadConfig.DownLoad(RSocket, json);
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError("json字符串:" + json + System.Environment.NewLine + ex.ToString());
                LogMg.AddError(ex.ToString());
                DEBUG.ThrowException(ex);
            }
        }
Esempio n. 8
0
        public static void DownLoad(Socket socket, string json)
        {
            S_To_C_Data <ClientConfig> sendObj = new S_To_C_Data <ClientConfig>();
            ClientConfig _clientConfig         = new ClientConfig();

            try
            {
                C_To_S_Data <ClientConfig>            obj      = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <ClientConfig> >(json);
                Clazz.Config.ClientConfig.XML_Station _station = SysConfig.clientConfig.AllStation.SingleOrDefault(c => c.TransferCode == obj.TransferCode);

                if (_station == null)
                {
                    LogMg.AddDebug("TransferCode:" + obj.TransferCode + " 不存在");
                }
                else
                {
                    _clientConfig.OrgId           = SysConfig.clientConfig.GetOrgIdByTransferCode(obj.TransferCode);
                    _clientConfig.StationId       = SysConfig.clientConfig.GetStationIdByTransferCode(obj.TransferCode);
                    _clientConfig.ListCountryTest = (from c in SysConfig.clientConfig.AllCountryTest
                                                     where c.StationUniqueId == _station.UniqueId
                                                     select new CSDataStandard.Config.CountryTest
                    {
                        NodeId = c.NodeId,
                        TestId = c.TestId,
                        Multiple = c.Multiple
                    }).ToList();
                    _clientConfig.ListDevice = new List <DeviceControl>();
                    //(from c in SysConfig.clientConfig.AllDevice
                    //                        where c.StationUniqueId == _station.UniqueId
                    //                        select new CSDataStandard.Config.DeviceControl
                    //                        {
                    //                            Number = c.Number,
                    //                            DeviceId = c.DeviceId
                    //                        }).ToList();
                    _clientConfig.ListPMQCTest = (from c in SysConfig.clientConfig.AllPMQCTest
                                                  where c.StationUniqueId == _station.UniqueId
                                                  select new CSDataStandard.Config.PMQCTest
                    {
                        X = c.X,
                        Y = c.Y,
                        TestId = c.TestId,
                        Id = c.Id,
                        Name = c.Name
                    }).ToList();
                    _clientConfig.ListMobileDetection = (from c in SysConfig.clientConfig.AllMobileDetection
                                                         where c.StationUniqueId == _station.UniqueId
                                                         select new CSDataStandard.Config.MobileDetection
                    {
                        TestId = c.TestId,
                        TestTarger = c.TestTarger
                    }).ToList();
                    _clientConfig.ListMcgsTest = (from c in SysConfig.clientConfig.AllMCGSTest
                                                  where c.StationUniqueId == _station.UniqueId
                                                  select new CSDataStandard.Config.MCGSTest
                    {
                        TestId = c.TestId,
                        ColumnName = c.ColumnName,
                        TestName = c.TestName
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
            }

            sendObj.Data = new List <ClientConfig>();
            sendObj.Data.Add(_clientConfig);                                 //添加数据
            sendObj.Flag    = CSDataStandard.Enum.HandleFlag.DownLoadConfig; //类型为  下载配置文件
            sendObj.Success = true;

            string sendJSON = Utility.JsonHelper.JsonSerializer(sendObj);

            socket.Send(Encoding.Unicode.GetBytes(sendJSON));
        }
Esempio n. 9
0
        private bool Save(string json, Socket RSocket)
        {
            bool   isSuccess   = true;
            string stationName = "";   //站点名称
            C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json);



            Clazz.Config.XML_Org _org = SysConfig.orgConfig.GetOrgByOrgId(c_to_s_data.OrgId);

            if (_org == null)
            {
                //将信息写入到日志文件中    orgid为***的污水厂不存在
                LogMg.AddError("OrgId:\"{0}\"不存在");
                isSuccess = false;
            }
            else
            {
                try
                {
                    SWSDataContext SWS = new SWSDataContext(Util.ServerSocketHelper.GetConnection(_org.DBName));     //建立一个分厂数据源提供程序实例

                    //查找站点名称
                    country_station _station = SWS.country_station.SingleOrDefault(c => c.id == c_to_s_data.StationId);
                    if (_station != null)
                    {
                        stationName = _station.name;    //站点名称
                    }

                    //遍历数据   并把数据添加到数据库中
                    List <testrec> list     = new List <testrec>();
                    List <test>    listTest = new List <test>();
                    foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data)
                    {
                        //判断检测当前数据在数据库中是否已经存在
                        if (SWS.realrec.SingleOrDefault(c => c.testid == item.TestId && c.testtime == item.TestTime) == null)
                        {
                            testrec _testrec = new testrec();
                            _testrec.testid   = item.TestId;
                            _testrec.testtime = item.TestTime;
                            _testrec.value    = (decimal)item.Value;
                            list.Add(_testrec);
                        }

                        //修改test表
                        SWS.ExecuteCommand(string.Format("update test set  [value]={0} where testid={1}", item.Value, item.TestId));
                    }

                    SWS.testrec.InsertAllOnSubmit(list);
                    SWS.SubmitChanges();
                    isSuccess = true;
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    //把错误信息输出到日志文件中
                    LogMg.AddError(ex.ToString());
                    DEBUG.ThrowException(ex);
                }
            }

            lock (lb_msg)
            {
                if (lb_msg.Items.Count > 200)
                {
                    lb_msg.Items.Clear();
                }
                this.lb_msg.Items.Add(string.Format("时间:{0}     客户端:{1}     数据行数{2}      保存{3}", DateTime.Now.ToString(), stationName, c_to_s_data.Data.Count, isSuccess ? "成功" : "失败"));
            }

            return(isSuccess);
        }
Esempio n. 10
0
        /// <summary>
        /// 序列化json  并保存到数据库
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private bool Save(string json)
        {
            C_To_S_Data <CSDataStandard.Transfer.RealRec> c_to_s_data = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <CSDataStandard.Transfer.RealRec> >(json);

            lb_msg.Items.Add(string.Format("污水厂编号:{0}    接收数据行数:{1}", c_to_s_data.OrgId, c_to_s_data.Data.Count));

            try
            {
                SWSDataContext SWS = new SWSDataContext(getConByOrgId(c_to_s_data.OrgId));
                //循环从客户端接收到的数据
                foreach (CSDataStandard.Transfer.RealRec item in c_to_s_data.Data)
                {
                    //根据testid找出test记录信息
                    test _test = SWS.test.SingleOrDefault(c => c.testid == item.TestId);
                    if (_test != null)
                    {
                        //把数据转换成数值型
                        double top1 = 0, top2 = 0, btm1 = 0, btm2 = 0;
                        try
                        {
                            top1 = Convert.ToDouble(_test.toplimit1);
                            top2 = Convert.ToDouble(_test.toplimit2);
                            btm1 = Convert.ToDouble(_test.btmlimit1);
                            btm2 = Convert.ToDouble(_test.btmlimit2);
                        }
                        catch (Exception ex)
                        {
                            LogMg.AddError(ex);
                            //throw ex;
                        }

                        char status = '1';
                        //拿检测到的值和 提示上限 提示下限 警报上限 警报下限做比较
                        if (item.Value >= btm1 && item.Value <= top1)
                        {
                            status = '1';
                        }
                        else if ((item.Value > top1 && item.Value < top2) || (item.Value > btm2 && item.Value < btm1))
                        {
                            status = '2';
                        }
                        else
                        {
                            status = '3';
                        }
                        //修改test表
                        SWS.ExecuteCommand(string.Format("update test set testtime='{0}' , [value]={1} , status='{2}' , means='{3}' where testid={4}", item.TestTime, item.Value, status, "屏幕取词", item.TestId));
                        //插入数据到realrec
                        SWS.ExecuteCommand(string.Format("insert into realrec (testid,testtime,[value],status) values({0},'{1}',{2},'{3}')", item.TestId, item.TestTime, item.Value, status));
                    }
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
                DEBUG.ThrowException(ex);
                return(false);
            }

            return(true);
        }