Exemplo n.º 1
0
        private double CalculateStrain(BgkStrainConfig ssv)
        {
            double Digit        = ssv.frequency * ssv.frequency / 1000;
            double currentValue = ssv.G * (Digit - ssv.R0) + ssv.K * (ssv.temperature - ssv.T0);

            return(Math.Round(currentValue, 3));
        }
Exemplo n.º 2
0
        private void LoadBgkStrainConfig(SqlConnection connection)
        {
            string     strainStatement = "select SensorId,G_C,R0,K,T0,Description from BgkStrainConfig";
            SqlCommand strainCommand   = new SqlCommand(strainStatement, connection);

            using (SqlDataReader reader = strainCommand.ExecuteReader())
            {
                Dictionary <string, BgkStrainConfig> configCollection = new Dictionary <string, BgkStrainConfig>();

                while (reader.Read())
                {
                    string sensorId = reader.GetString(0);
                    double g        = reader.GetDouble(1);
                    double r0       = reader.GetDouble(2);
                    double k        = reader.GetDouble(3);
                    double t0       = reader.GetDouble(4);
                    object desc     = reader.GetValue(5);

                    BgkStrainConfig ssv = new BgkStrainConfig(sensorId, g, r0, k, t0);
                    configCollection.Add(sensorId, ssv);

                    string[]     viewItem = { sensorId, sensorId, desc.ToString() };
                    ListViewItem listItem = new ListViewItem(viewItem);
                    this.listView1.Items.Add(listItem);
                }
                BgkStrainSolve actSolver = new BgkStrainSolve(configCollection, 300, redis, redisDbIndex, textBoxLog, dataQueue);
                solverCollection.Add(actSolver);
            }
        }
Exemplo n.º 3
0
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgWorker = sender as BackgroundWorker;
            string           stamp    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string           str      = "";

            str += stamp + " ";
            if (redis.IsConnected)
            {
            }
            else
            {
                str += "redis server is not connected";
                //lthis.AppendLog(str);
                return;
            }

            Dictionary <RedisKey, RedisValue> pair = new Dictionary <RedisKey, RedisValue>();

            //string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                IDatabase db_raw    = this.redis.GetDatabase(0);
                IDatabase db_result = this.redis.GetDatabase(redisDbIndex);

                //RedisKey[] keys = list.Keys.Select(key => (RedisKey)key).ToArray();
                List <RedisKey> keyCollection = new List <RedisKey>();
                foreach (string k in list.Keys)
                {
                    keyCollection.Add(k);
                    //keyCollection.Add(k + "-005");
                    //keyCollection.Add(k + "-012");
                }

                RedisKey[] keys = keyCollection.ToArray();

                RedisValue[] vals = db_raw.StringGet(keys);

                Dictionary <string, DataValue> dvs = new Dictionary <string, DataValue>();

                foreach (RedisValue rv in vals)
                {
                    if (!rv.IsNull)
                    {
                        Bgk_Micro_40A_Data dv   = JsonConvert.DeserializeObject <Bgk_Micro_40A_Data>((string)rv);
                        string             key  = dv.SensorId;
                        double             temp = ResistanceToTemperature(dv.Value2);

                        BgkStrainConfig ptv = list[key];
                        if (ptv.Stamp != dv.TimeStamp)
                        {
                            ptv.isUpdated   = true;
                            ptv.temperature = temp;
                            ptv.frequency   = dv.Value1;
                            ptv.Stamp       = dv.TimeStamp;
                        }
                    }
                }

                foreach (string key in list.Keys)
                {
                    //string str = "";

                    BgkStrainConfig ptv = list[key];

                    if (ptv.isUpdated)
                    {
                        if (ptv.temperature == 0 || ptv.frequency == 0)
                        {
                            this.AppendLog(stamp + " " + ptv.SensorId + "This Channel is broken");
                            continue;
                        }
                        string strainNormalKey = ptv.SensorId + "-009";

                        double strain = CalculateStrain(ptv);

                        DataValue strainDv = new DataValue();
                        strainDv.SensorId  = ptv.SensorId;
                        strainDv.TimeStamp = ptv.Stamp;
                        strainDv.ValueType = "009";
                        strainDv.Value     = strain;

                        string result = JsonConvert.SerializeObject(strainDv);
                        pair[strainNormalKey] = result;

                        strainDv.ValueType = "005";
                        strainDv.Value     = ptv.temperature;
                        string tempKey = ptv.SensorId + "-005";
                        result        = JsonConvert.SerializeObject(strainDv);
                        pair[tempKey] = result;

                        Strain_Data sd = new Strain_Data();
                        sd.SensorId    = ptv.SensorId;
                        sd.TimeStamp   = ptv.Stamp;
                        sd.Frequency   = ptv.frequency;
                        sd.Strain      = strain;
                        sd.Temperature = ptv.temperature;

                        string    mq_string = JsonConvert.SerializeObject(sd);
                        RabbitMsg msg       = new RabbitMsg();
                        msg.RouteKey = ptv.SensorId;
                        msg.Body     = mq_string;
                        dataQueue.Enqueue(msg);

                        ptv.isUpdated = false;

                        str += ptv.SensorId + "\r\n";
                        str += "R0: " + ptv.R0.ToString() + " T0: " + ptv.T0 + " G: " + ptv.G + " K: " + ptv.K + "\r\n";
                        str += "frequency: " + ptv.frequency + " temperature: " + ptv.temperature + "\r\n";
                        str += "Strain: " + strain.ToString() + "\r\n";
                        ////lthis.AppendLog(str);

                        if (bgWorker.CancellationPending == true)
                        {
                            if (pair.Count > 0)
                            {
                                db_result.StringSet(pair.ToArray());
                                pair.Clear();
                            }
                            e.Cancel = true;
                            break;
                        }
                    }
                    else
                    {
                        //this.AppendLog(stamp + key + "is not updated");
                        continue;
                    }
                }
                if (pair.Count > 0)
                {
                    db_result.StringSet(pair.ToArray());
                    pair.Clear();
                }
                //lthis.AppendLog(str);
                //foreach (string key in list.Keys)
                //{
                //    StaticStrainValue ptv = list[key];
                //    ptv.IsUpdated = false;
                //}
            }
            catch (Exception ex)
            {
                this.AppendLog(ex.Message);
                using (StreamWriter sw = new StreamWriter(@"ErrLog.txt", true))
                {
                    sw.WriteLine(stamp + " " + ex.Message + " \r\n" + ex.StackTrace.ToString());
                    sw.WriteLine("---------------------------------------------------------");
                    sw.Close();
                }
            }
        }