Ejemplo n.º 1
0
 void Data2mssql()
 {
     if (error)
     {
         Console.WriteLine("Something error happened");
         connect();
         return;
     }
     try
     {
         //从电表读取数据,并解析到对象
         SmartMeterData.getActive_Energy(MeterDataAccessor.read_active_Energy());
         SmartMeterData.getActive_Power(MeterDataAccessor.read_active_Power());
         SmartMeterData.getReactive_Power(MeterDataAccessor.read_Reactive_Power());
         DateTime            datetime         = DateTime.Now;
         CSmartMeterDataInfo datainfo         = SmartMeterData.smartMeterData;
         UserfulMeterData    userfulMeterData = new UserfulMeterData(datainfo.Total_Active_Power_65, datainfo.Reactive_Power_Total_67, datainfo.Active_Energy_Import_Tariff_1_801, datetime);
         publicSQL.DataOperate(userfulMeterData, PublicSQL.MODE_INSERT);
         string tempstr = "---GetData:" + "\nTime:" +
                          userfulMeterData.DateTime + "\nActivePower:" +
                          String.Format("{0:F}", userfulMeterData.ActivePower) +
                          "W\tReactivePower:" +
                          String.Format("{0:F}", userfulMeterData.ReactivePower) +
                          "Var\nEnergy:" + String.Format("{0:F}", userfulMeterData.ActiveEnergy) + "\n";
         Console.WriteLine(tempstr);
     }
     catch (Exception e)
     {
         Console.WriteLine("{0}", e.ToString());
         connect();
     }
 }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        ///   public async void ThreadProcAcqSmartMeterData()
        public async void ThreadProcAcqSmartMeterData()
        {
            byte[] bholdregs1 = null;

            TCPSGInterface  meter1       = new TCPSGInterface(Constants.METER_SERVER_IP, context);
            TCPAmmeterData  meterData1   = new TCPAmmeterData(meter1);
            List <LoadData> loadDatalist = new List <LoadData>();

            List <LoadIDResult> loadIDResultListForDisBug = null;
            int currentCount = 0;

            while (meteracqflag)
            {
                try
                {
                    CSmartMeterData csmartMeterData1 = new CSmartMeterData();
                    csmartMeterData1.getActive_Power(meterData1.read_active_Power());
                    csmartMeterData1.getReactive_Power(meterData1.read_Reactive_Power());
                    csmartMeterData1.getCurrent(meterData1.read_current());
                    csmartMeterData1.getVoltage_Vph_n(meterData1.read_voltage_Vph_n());
                    csmartMeterData1.getActive_Energy(meterData1.read_active_Energy());

                    float  activePower1   = csmartMeterData1.smartMeterData.Total_Active_Power_65;
                    float  reactivePower1 = csmartMeterData1.smartMeterData.Reactive_Power_Total_67;
                    float  current1       = csmartMeterData1.smartMeterData.Current_a_13;
                    float  voltage1       = csmartMeterData1.smartMeterData.Voltage_Va_n_1;
                    double activeEnergy1  = csmartMeterData1.smartMeterData.Active_Energy_Import_Tariff_1_801;

                    DateTime dt = DateTime.Now;
                    loadDatalist.Add(new LoadData(activePower1, reactivePower1, dt));
                    currentCount++;
                    LoadIDResult itemtest = new LoadIDResult();
                    if (currentCount == 10)
                    {
                        loadIDResultList = LoadIdentification.handleHisLogDBMethodWindow(loadDatalist);

                        currentCount = 0;

                        loadDatalist = new List <LoadData>();
                    }

                    context.Post((s) =>
                    {
                        //可以在此访问UI线程中的对象,因为代理本身是在UI线程的上下文中执行的
                        TxtAP.Text = String.Format("{0:F}", activePower1) + "W";
                        TxtRP.Text = String.Format("{0:F}", reactivePower1) + "Var";
                        if (currentCount == 0)
                        {
                            if (loadIDResultList.Count > 0)
                            {
                                foreach (LoadIDResult item in loadIDResultList)
                                {
                                    if (currentLoadList.Count > 0)
                                    {
                                        try
                                        {
                                            foreach (LoadIDResult curitem in currentLoadList)
                                            {
                                                if (curitem.Name.Equals(item.Name))
                                                {
                                                    if (item.Event_type != curitem.Event_type)
                                                    {
                                                        currentLoadList.Remove(curitem);
                                                    }
                                                }
                                            }
                                        }
                                        catch
                                        {
                                        }
                                    }

                                    if (item.Event_type == EVENT_TYPE.ON)
                                    {
                                        currentLoadList.Add(item);
                                    }
                                }
                            }
                        }
                    }
                                 , null);
                }
                catch
                {
                }
                await Task.Delay(500);
            }
        }