Ejemplo n.º 1
0
        public void AddMessageRealTime(string st)
        {
            if (RealTimeSwitcher == true)
            {
                Invoke(new Action(() =>
                {
                    string[] temp;

                    if (SmartHomeController._functionController._functionManager.ValidJson(st))
                    {
                        temp = SmartHomeController._functionController._functionManager.DecripeJson(st);
                    }
                    else
                    {
                        MessageBox.Show("Input is not JSON.");
                        return;
                    }
                    try
                    {
                        string[] tempdevice = temp[1].Split(',');
                        tempdevice          = tempdevice[0].Split(' ');

                        //Temperature: 25.0,Humility: 40.0
                        temp = temp[2].Split(',');
                        string[] values;
                        values         = temp[0].Split(':');
                        double nowtime = trans.MiniTrans(DateTime.Now);
                        if (nowtime < 0.2)
                        {
                            realtime.Clear();
                            if (tempdevice[0] == "sensor" && DeviceGraph == false)
                            {
                                pp.FormClean("Temperature");
                                pp.FormClean("Humility");
                                pp.FormClean("Light");
                                pp.FormClean("Motion");
                            }
                            else if (tempdevice[0] == CurrentDevice && DeviceGraph == true)
                            {
                                pp.FormClean("Status");
                                pp.FormClean("Power");
                            }
                        }
                        realtime.Add(nowtime);
                        if (realtime[0] < (nowtime - timeinter))
                        {
                            realtime.RemoveAt(0);
                        }
                        if (tempdevice[0] == "sensor" && DeviceGraph == false)
                        {
                            pp.AddSinglePointRealTime("Temperature", nowtime, Convert.ToSingle(values[1]));
                            values = temp[1].Split(':');
                            pp.AddSinglePointRealTime("Humility", nowtime, Convert.ToSingle(values[1]));
                            values = temp[2].Split(':');

                            //Invoke the light
                            float lightValue = Convert.ToSingle(values[1]);
                            if (maxlight < lightValue)
                            {
                                maxlight = Convert.ToInt32(lightValue);
                            }
                            if (minlight > lightValue)
                            {
                                minlight = Convert.ToInt32(lightValue);
                            }
                            pp.AddSinglePointRealTime("Light", nowtime, Convert.ToSingle(lightValue));

                            int interval = ((int)Math.Round((maxlight - minlight) / 10.0)) * 10;
                            if (interval >= 400)
                            {
                                interval = 50;
                            }
                            else if (interval >= 50)
                            {
                                interval = 5;
                            }
                            else
                            {
                                interval = 1;
                            }
                            pp.adjustY2AxisRealTime(interval / 20, ((int)((minlight - 5) / 10)) * 10, ((int)((maxlight + 10) / 10)) * 10);


                            values = temp[3].Split(':');
                            pp.AddSinglePointRealTime("Motion", nowtime, Convert.ToSingle(values[1]) * 25);
                        }
                        else if (tempdevice[0] == CurrentDevice && DeviceGraph == true)
                        {
                            int sta = 0;
                            if (values[1] == "1")
                            {
                                sta = 1;
                            }
                            pp.AddSinglePointRealTime("Status", nowtime, Convert.ToSingle(sta));
                            values       = temp[1].Split(':');
                            int interval = 0;
                            if (maxpower[CurrentDevice] >= 400)
                            {
                                interval = 50;
                            }
                            else if (maxpower[CurrentDevice] >= 50)
                            {
                                interval = 5;
                            }
                            else
                            {
                                interval = 1;
                            }
                            pp.AddSinglePointRealTime("Power", nowtime, Convert.ToSingle(values[1]));
                            pp.adjustYAxisRealTime(interval, 0, ((int)((maxpower[CurrentDevice] + 10) / 10)) * 10);
                        }
                        pp.adjustXAxisRealTime(1, Convert.ToInt32(realtime.ToArray()[0] - 0.5), Convert.ToInt32(realtime.ToArray()[realtime.ToArray().Length - 1] + 2));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Put the daymodel, realtimeswitch, timeinter, list,datas in
        /// </summary>
        /// <param name="day"></param>
        /// <param name="real"></param>
        /// <param name="tinter"></param>
        /// <param name="list"></param>
        /// <param name="datas"></param>
        /// <param name="pp"></param>
        public List <double> GraphSensor(bool day, bool real, int tinter, List <double> list, List <Data> datas, ref Graph pp, ref int minlight, ref int maxlight)
        {
            dayvmodel        = day;
            RealTimeSwitcher = real;
            timeinter        = tinter;
            realtime         = list;
            string[]        values;
            string[]        value;
            string          line;
            List <DateTime> datetime = new List <DateTime>();

            int maxtemp = 0;
            int maxhumi = 0;
            int mintemp = 100;
            int minhumi = 100;

            float         motionValue = 0;
            float         lightValue  = 0;
            float         tempValue   = 0;
            float         humiValue   = 0;
            List <double> time        = new List <double>();

            pp.Text = "Temperature, Humidity and Light Sensor";
            pp.AddSeries("Temperature", true, 3, "line", "red");
            pp.AddSeries("Humility", true, 3, "line", "green");//  add points
            pp.AddSeries("Motion", true, 3, "stepline", "orange");
            pp.AddSeries("Light", false, 3, "line", "blue");

            foreach (Data d in datas)
            {
                // { "Device": "sensor 1", "Data": "Temperature:77.0,Humility:44.0,Light:95.0,Motion:0.0", "ID": "xuebo"}

                d.getDataByYear(out line);
                values = line.Split(',');
                for (int i = 0; i < values.Length; i++)
                {
                    value = values[i].Split(':');
                    switch (value[0])
                    {
                    case "Temperature":
                        if (value[0] != "Unknown")
                        {
                            tempValue = Convert.ToSingle(value[1]);
                            if (maxtemp < tempValue)
                            {
                                maxtemp = Convert.ToInt32(tempValue);
                            }
                            if (mintemp > tempValue)
                            {
                                mintemp = Convert.ToInt32(tempValue);
                            }
                        }
                        break;

                    case "Humility":
                        if (value[0] != "Unknown")
                        {
                            humiValue = Convert.ToSingle(value[1]);
                            if (maxhumi < humiValue)
                            {
                                maxtemp = Convert.ToInt32(maxhumi);
                            }
                            if (minhumi > humiValue)
                            {
                                minhumi = Convert.ToInt32(humiValue);
                            }
                        }
                        break;

                    case "Motion":
                        if (value[0] != "Unknown")
                        {
                            motionValue = Convert.ToSingle(value[1]) * 25;
                        }
                        break;

                    case "Light":
                        if (value[0] != "Unknown")
                        {
                            lightValue = Convert.ToSingle(value[1]);
                            if (maxlight < lightValue)
                            {
                                maxlight = Convert.ToInt32(lightValue);
                            }
                            if (minlight > lightValue)
                            {
                                minlight = Convert.ToInt32(lightValue);
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                if (dayvmodel == true)
                {
                    double temptime = 0;
                    if (RealTimeSwitcher == true && timeinter != 0)
                    {
                        temptime = trans.MiniTrans(d.getDateTime());
                    }
                    else
                    {
                        temptime = trans.HourTrans(d.getDateTime());
                    }
                    if (time.Count == 0)
                    {
                        if (RealTimeSwitcher)
                        {
                            realtime.Add(temptime);
                        }
                        time.Add(temptime);
                        pp.AddPoints("Temperature", "Humility", temptime, tempValue, humiValue);
                        pp.AddPoints("Motion", temptime, motionValue);
                        pp.AddPoints("Light", temptime, lightValue);
                    }
                    else if (temptime >= Convert.ToSingle(time[time.Count - 1]))
                    {
                        if (RealTimeSwitcher)
                        {
                            realtime.Add(temptime);
                        }
                        time.Add(temptime);
                        pp.AddPoints("Temperature", "Humility", temptime, tempValue, humiValue);
                        pp.AddPoints("Motion", temptime, motionValue);
                        pp.AddPoints("Light", temptime, lightValue);
                    }
                }
                else
                {
                    pp.AddPoints("Temperature", d.getDateTime(), tempValue);
                    pp.AddPoints("Humility", d.getDateTime(), humiValue);
                    pp.AddPoints("Motion", d.getDateTime(), motionValue);
                    pp.AddPoints("Light", d.getDateTime(), lightValue);
                    datetime.Add(d.getDateTime());
                }
            }
            pp.adjustYAxis(10, 0, 100, "Temperature(F) & Humility(%)");
            pp.adjustY2Axis(20, ((int)((minlight - 5) / 10)) * 10, ((int)((maxlight + 10) / 10)) * 10, "Lumen(lm)");
            if (time.Count == 0 && datetime.Count == 0)
            {
                MessageBox.Show("There is no data at that days.");
            }
            else
            {
                if (RealTimeSwitcher)
                {
                    pp.adjustXAxis(1, Convert.ToInt32(time.ToArray()[0]), Convert.ToInt32(time.ToArray()[time.ToArray().Length - 1] + 5), "Times(Hour)");
                }
                else if (dayvmodel == true)
                {
                    pp.adjustXAxis(0.25, Convert.ToInt32(time.ToArray()[0] - 0.5), Convert.ToInt32(time.ToArray()[time.ToArray().Length - 1] + 0.5), "Times(Hour)");
                }
            }
            return(realtime);
        }