Exemplo n.º 1
0
        /// <summary>
        /// 将指定客户端Mac地址提交上的CityInfo插入到数据库中并返回成功插入的城市的列表
        /// </summary>
        /// <param name="cityInfoList">客户端服务提交上来的城市数据列表</param>
        /// <param name="clientMac">客户端MAC地址</param>
        /// <returns></returns>
        public string[] InsertCityListInfo(List <AirDataInfo> cityInfoList, string clientMac)
        {
            LockTable(new string[] { "citylistinfo", "serviceairinfotable" }, SqlLockType.write);
            List <string> resultList = new List <string>();

            string[] noFreashCityArr = GetNoFreashCityListByClientMac(clientMac);
            if (noFreashCityArr.Length > 0)
            {
                foreach (string cityName in noFreashCityArr)
                {
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = conn;
                    IEnumerable <AirDataInfo> Enumer  = cityInfoList.Where(t => t.CityName == cityName);
                    IEnumerator <AirDataInfo> itertor = Enumer.GetEnumerator();
                    bool haveThisCity = itertor.MoveNext();
                    //未刷新城市在当前List中存在
                    if (haveThisCity)
                    {
                        AirDataInfo airInfo = itertor.Current;
                        cmd.CommandText = "INSERT INTO serviceairinfotable(AQI,UpdateTime,PM25,PM10,PolText,FL,FS,Pressure,Text,cityName,Temp,ShiDu,Week) VALUES(?aqi,?updatetime,?pm25,?pm10,?pollutionindex,?fl,?fs,?pressure,?nowweathertext,?cityname,?temperature,?shidu,?week)";
                        MySqlParameter[] param = new MySqlParameter[]
                        {
                            new MySqlParameter("?aqi", airInfo.AQI),
                            new MySqlParameter("?updatetime", airInfo.UpdateTime),
                            new MySqlParameter("?pm25", airInfo.PM25),
                            new MySqlParameter("?pm10", airInfo.PM10),
                            new MySqlParameter("?pollutionindex", airInfo.PollutionIndex),
                            new MySqlParameter("?fl", airInfo.FL),
                            new MySqlParameter("?fs", airInfo.FS),
                            new MySqlParameter("?pressure", airInfo.Pressure),
                            new MySqlParameter("?nowweathertext", airInfo.NowWeatherText),
                            new MySqlParameter("?cityname", airInfo.CityName),
                            new MySqlParameter("?temperature", airInfo.Temp),
                            new MySqlParameter("?shidu", airInfo.Hum),
                            new MySqlParameter("?week", airInfo.Week)
                        };
                        cmd.Parameters.AddRange(param);
                        int resultNum = 0;
                        try
                        {
                            resultNum = cmd.ExecuteNonQuery();
                        }
                        catch (Exception exp)
                        {
                            LogInfoHelper.WriteRunTimeMessage("插入城市数据信息异常" + exp.Message + "\r\n" + exp.StackTrace, RunTimeMessageType.Exception, true);
                        }

                        if (resultNum == 1)
                        {
                            cmd.CommandText = string.Format("update citylistinfo set LastFreashTime='{1}',AssignMachineMAC='' where CityName='{0}'", cityName, airInfo.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                            if (cmd.ExecuteNonQuery() != 1)
                            {
                                LogInfoHelper.WriteRunTimeMessage("更新LastFreashTime信息异常", RunTimeMessageType.Exception, true);
                            }
                            resultList.Add(cityName);
                        }
                    }
                    else
                    {
                        LogInfoHelper.WriteRunTimeMessage(string.Format("client :{0}提交上的城市列表信息中有未包含的城市信息  城市名称为{1}", clientMac, cityName), RunTimeMessageType.Exception, true);
                        continue;
                    }
                }
            }
            else
            {
                LogInfoHelper.WriteRunTimeMessage(string.Format("client {0} 被分配的城市列表全部被覆盖~_~!!!", clientMac), RunTimeMessageType.Log, true);
            }
            UnLockTable();
            DisconnectDatabase();
            return(resultList.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将form中的城市信息解析为城市天气信息的实例集合
        /// </summary>
        /// <param name="formDate">客户端服务提交上来的额form信息</param>
        /// <returns></returns>
        private List <AirDataInfo> AnayFormData(string formDate)
        {
            List <AirDataInfo> resultList = new List <AirDataInfo>();

            try
            {
                //form中数据格式为 data = {[cityInfo],[cityInfo],[cityInfo].....}   [cityInfo]格式 [key=value,key=value,key=value........]
                formDate = formDate.Trim('{');
                formDate = formDate.Trim('}');
                string[] singleCityInfoArr = formDate.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                //singleCityInfoArr中 格式应为 1. [cityInfo]    2. [cityInfo]  、、、、
                foreach (string singleCityInfoStr in singleCityInfoArr)
                {
                    string   cityInfo    = singleCityInfoStr.TrimStart('[').TrimEnd(']');
                    string[] keyValueArr = cityInfo.Split(',');
                    //[cityInfo]中单个属性的格式为  [key=value,key=value,key=value、、、、]
                    //keyValueArr 中格式应为 key=value,key=value,key=value、、、、、
                    Dictionary <string, string> airInfoDic = new Dictionary <string, string>();
                    foreach (string keyValueInfo in keyValueArr)
                    {
                        string key   = keyValueInfo.Split('=')[0];
                        string value = keyValueInfo.Split('=')[1];
                        if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                        {
                            airInfoDic.Add(key, value);
                        }
                    }
                    AirDataInfo    newCityInfo = new AirDataInfo();
                    Type           airInfoType = newCityInfo.GetType();
                    PropertyInfo[] properties  = airInfoType.GetProperties();
                    //反射的方式循环设置相应属性的值
                    foreach (PropertyInfo property in properties)
                    {
                        try
                        {
                            //该属性为int类型
                            if (property.PropertyType == typeof(int))
                            {
                                int value = int.Parse(airInfoDic[property.Name]);
                                property.SetValue(newCityInfo, value, null);
                            }
                            else if (property.PropertyType == typeof(DateTime))
                            {
                                DateTime value = DateTime.Parse(airInfoDic[property.Name]);
                                property.SetValue(newCityInfo, value, null);
                            }
                            //该属性为string类型
                            else
                            {
                                property.SetValue(newCityInfo, airInfoDic[property.Name], null);
                            }
                        }
                        catch (Exception exp)
                        {
                            LogInfoHelper.WriteRunTimeMessage(string.Format("反射设置对象属性值时发生异常 属性名为{0}  dic中value为{1}   exp.Messge 为{2}", property.Name, airInfoDic[property.Name], exp.Message), RunTimeMessageType.Exception, true);
                        }
                    }
                    resultList.Add(newCityInfo);
                }//第一层Foreach
            }
            catch (Exception exp)
            {
                string message = string.Format("解析客户端提交上来的form数据异常 form数据为{0}  \r\n 异常信息为 {1}", formDate, exp.Message);
                LogInfoHelper.WriteRunTimeMessage(message, RunTimeMessageType.Exception, true);
            }
            return(resultList);
        }