Пример #1
0
        //服务开启
        private void btn_Start_Click(object sender, EventArgs e)
        {
            try
            {
                if (test_connection() == false)
                {
                    listBox1.Items.Add("连接数据库失败");
                    listBox1.Items.Add("请确保数据库服务已开启,并且数据库连接填写正确");
                    return;
                }

                ServerSocketHelper.Init();
                ServerSocketHelper.StartListen(txt_ServerIp.Text.Trim(), txt_Port_SWS.Text.Trim());


                GPRSControl.DTU_NetServer.Init();
                // GPRSControl.GPRSControlNetServer.StartListen("192.168.1.8","5678");
                GPRSControl.DTU_NetServer.StartListen(txt_ServerIp.Text.Trim(), txt_port_DTU.Text.Trim());

                V88CommunicationThread.getInstance().Start();
                UdpV88Server.getInstance().StartListen(txt_ServerIp.Text.Trim(), txt_port_udp.Text.Trim());

                //心跳线程开启
                HeartbeatThread.Start();
                //主动发指令的线程开启
                ControlCommandThread.Start();
                //定时发指令的线程开启
                AutoCollectionThread.Start();

                save();

                btn_Start.Enabled = false;
                btn_Stop.Enabled  = true;

                ///关闭自动启动服务的定时器
                if (AutoStartTimer != null)
                {
                    AutoStartTimer.Stop();
                }
                listBox1.Items.Add("服务已成功开启");
            }
            catch (Exception ex)
            {
                ServerSocketHelper.Close();
                //  DTU_NetServer.Close();

                LogMg.AddError(ex);
                listBox1.Items.Add(ex.Message);
                //  MessageBox.Show(ex.Message);
            }
        }
        private void handlerV88Msg(string data)
        {
            LogMg.AddDebug("udp协议接收的数据=============     " + data);
            List <String> datas = CaiBao(data);
            DateTime      date  = DateTime.Now;
            List <Dictionary <string, string> > listDir = parseDataV88(datas, ref date);

            foreach (Dictionary <string, string> item in listDir)
            {
                try
                {   //找出设备编号
                    String stationNo = getStationNo(item);
                    if (String.IsNullOrEmpty(stationNo))
                    {
                        continue;
                    }
                    //找出编号对应的站点
                    SWS_DB.guangdai_station_link link = V88StationLink.SingleOrDefault(c => c.wsid == stationNo);
                    if (link == null)
                    {
                        continue;
                    }
                    SWSDataContext sws = new SWSDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, link.db_name, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword));
                    updateStationLine(sws, (int)link.station_id);
                    foreach (string key in item.Keys)
                    {
                        //数据
                        KeyValuePair <string, string> data1 = item.Single(c => c.Key == key);
                        try
                        {
                            KeyValuePair <string, int>?keyAndValue = getRegisterByKey(key);
                            if (keyAndValue == null)
                            {
                                continue;
                            }
                            List <gong_kuang_config> gks = sws.gong_kuang_config.Where(c => c.station_id == link.station_id && c.read_register == ((KeyValuePair <string, int>)keyAndValue).Value.ToString()).ToList();
                            foreach (gong_kuang_config gk in gks)
                            {
                                if (gk == null || gk.testid == null || gk.testid == 0)
                                {
                                    continue;
                                }
                                AutoCollectionThread.updateStationOnlineInfo(sws, (int)link.station_id);
                                //保存数据
                                double multiple  = gk.Multiple == null ? 1 : (double)gk.Multiple;
                                double addNumber = gk.AddNumber == null ? 0 : (double)gk.AddNumber;
                                String strValue  = data1.Value;
                                if (key.StartsWith("k") || key.StartsWith("K"))
                                {
                                    if (strValue == "1")
                                    {
                                        strValue = "0";
                                    }
                                    else
                                    {
                                        strValue = "1";
                                    }
                                }
                                double value = Double.Parse(strValue) * multiple + addNumber;
                                gk.read_value = value.ToString();
                                realrec rec = new realrec();
                                rec.testid   = gk.testid;
                                rec.value    = (decimal)value;
                                rec.testtime = date;
                                rec.remark   = "V88协议的设备数据";
                                sws.realrec.InsertOnSubmit(rec);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogMg.AddError(ex);
                        }
                    }
                    sws.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex);
                }
            }
        }