예제 #1
0
        /// <summary>
        /// 查找新版本
        /// </summary>
        public static void SearchUpdate(string more, Action <UpdateModel> FindNewVersion = null)
        {
            Action action = new Action(() =>
            {
                try
                {
                    //1分钟后执行
                    System.Threading.Thread.Sleep(1000 * 60 * 1);
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    string version = assembly.GetName().Version.ToString();//获取主版本号

                    //获取硬件号
                    string uid = Res.Server.OperatesService.GetOperates().ServiceUID();
                    //
                    //定义webClient对象
                    CookieAwareWebClient webClient = new CookieAwareWebClient();
                    //定义通信地址 和 JSON数据
                    string URL = "https://www.oybab.net/software/update/";
                    //string URL = "http://192.168.1.100/OyBabNet/software/update/";

                    //组装数据
                    NameValueCollection postValues = new NameValueCollection();
                    //postValues.Add("json_data", jsonString);
                    postValues.Add("version", version);
                    postValues.Add("uid", uid);
                    postValues.Add("lang", Resources.GetRes().GetLangByLangIndex(Resources.GetRes().CurrentLangIndex).Culture.Name);
                    postValues.Add("app", Resources.GetRes().SOFT_SERVICE_NAME);
                    postValues.Add("more", more);
                    postValues.Add("name", Resources.GetRes().KEY_NAME_0);
                    postValues.Add("os", OSCheck.GetOS());
                    postValues.Add("ostype", OSCheck.GetOSType());

                    //向服务器发送POST数据
                    byte[] responseArray = webClient.UploadValues(URL, postValues);
                    string data          = Encoding.UTF8.GetString(responseArray);

                    //如果空
                    if (string.IsNullOrWhiteSpace(data))
                    {
                         #if DEBUG
                        ExceptionPro.ExpInfoLog("Response empty!");
                        #endif
                        return;
                    }

                    UpdateModel model = UpdateModel.FromJsonTo <UpdateModel>(data);
                    if (null != model)
                    {
                        //如果成功
                        if (model.Code == "1")
                        {
                            if (null != FindNewVersion)
                            {
                                FindNewVersion(model);
                            }
                        }
                        //如果需要锁住
                        else if (model.Code == "-4")
                        {
                            if (Res.Server.OperatesService.GetOperates().ServiceLock())
                            {
                                postValues.Add("ConfirmLock", "1");
                                postValues.Add("TId", model.TId);
                                webClient.UploadValues(URL, postValues);
                                //ExceptionPro.ExpInfoLog("Lock Key Success!");
                                //#if DEBUG
                                ExceptionPro.ExpErrorLog("LS");
                                //#endif
                            }
                            else
                            {
                                postValues.Add("ConfirmLock", "0");
                                postValues.Add("TId", model.TId);
                                webClient.UploadValues(URL, postValues);
                                //ExceptionPro.ExpErrorLog("Lock Key Faild!");
                                //#if DEBUG
                                ExceptionPro.ExpErrorLog("LF");
                                //#endif
                            }
                            return;
                        }
                        //如果其他或失败
                        else
                        {
                            #if DEBUG
                            if (!string.IsNullOrWhiteSpace(model.ErrorMsg))
                            {
                                throw new OybabException(model.ErrorMsg, true);
                            }
                            else
                            {
                                throw new OybabException(string.Format(Res.Resources.GetRes().GetString("UpdateUnableError"), model.Code), true);
                            }
                            #endif
                        }
                    }
                    //数据返回空或解析失败
                    else
                    {
                        #if DEBUG
                        throw new OybabException(Res.Resources.GetRes().GetString("UpdateNonReturnOrCantRead"), true);
                        #endif
                    }
                }
                catch
#if DEBUG
                (Exception ex)
#endif
                {
#if DEBUG
                    ExceptionPro.ExpLog(ex, null, true);
#endif
                }
            });

            action.BeginInvoke(null, null);
        }
예제 #2
0
        /// <summary>
        /// 查找新版本
        /// </summary>
        public static void SearchUpdate(string more, Action <UpdateModel> FindNewVersion = null)
        {
            Task.Run(async() =>
            {
                try
                {
                    //1分钟后执行
                    await ExtX.Sleep(1000 * 60 * 1);

                    string version = OperatesService.Instance.AllVersion;

                    //获取硬件号
                    string uid = await OperatesService.Instance.ServiceUID();

                    CookieContainer CookieContainer = new CookieContainer();
                    //
                    //定义webClient对象
                    var cookieContainer = new CookieContainer();
                    using (var handler = new HttpClientHandler()
                    {
                        CookieContainer = cookieContainer, UseCookies = true
                    })
                    {
                        //定义通信地址 和 JSON数据
                        string URL = "https://www.oybab.net/software/update/";
                        //string URL = "http://localhost:3282/software/update/";

                        using (var client = new HttpClient(handler)
                        {
                            BaseAddress = new Uri(URL)
                        })
                        {
                            var values = new Dictionary <string, string>();
                            values.Add("version", version);
                            values.Add("uid", uid);
                            values.Add("lang", TradingSystemX.Res.Instance.GetLangByLangIndex(TradingSystemX.Res.Instance.CurrentLangIndex).Culture.Name);
                            values.Add("app", Resources.Instance.SOFT_SERVICE_NAME);
                            values.Add("more", more);
                            values.Add("name", Resources.Instance.KEY_NAME_0);
                            values.Add("os", OperatesService.Instance.GetOS());
                            values.Add("ostype", DeviceInfo.VersionString);

                            var content = new FormUrlEncodedContent(values);



                            //向服务器发送POST数据
                            var response = await client.PostAsync(URL, content);
                            string data  = Encoding.UTF8.GetString(await response.Content.ReadAsByteArrayAsync());

                            //如果空
                            if (string.IsNullOrWhiteSpace(data))
                            {
#if DEBUG
                                ExceptionPro.ExpInfoLog("Response empty!");
#endif
                                return;
                            }

                            UpdateModel model = data.DeserializeObject <UpdateModel>();
                            if (null != model)
                            {
                                //如果成功
                                if (model.Code == "1")
                                {
                                    if (null != FindNewVersion)
                                    {
                                        FindNewVersion(model);
                                    }
                                }
                                //如果需要锁住
                                else if (model.Code == "-4")
                                {
                                    if (await OperatesService.Instance.ServiceLock())
                                    {
                                        values.Add("ConfirmLock", "1");
                                        values.Add("TId", model.TId);

                                        var content2 = new FormUrlEncodedContent(values);


                                        await client.PostAsync(URL, content2);

                                        ExceptionPro.ExpErrorLog("LS");
                                    }
                                    else
                                    {
                                        values.Add("ConfirmLock", "0");
                                        values.Add("TId", model.TId);

                                        var content2 = new FormUrlEncodedContent(values);

                                        await client.PostAsync(URL, content2);

                                        ExceptionPro.ExpErrorLog("LF");
                                    }
                                    return;
                                }
                                //如果其他或失败
                                else
                                {
#if DEBUG
                                    if (!string.IsNullOrWhiteSpace(model.ErrorMsg))
                                    {
                                        throw new OybabException(model.ErrorMsg, true);
                                    }
                                    else
                                    {
                                        throw new OybabException(string.Format(TradingSystemX.Res.Instance.GetString("UpdateUnableError"), model.Code), true);
                                    }
#endif
                                }
                            }
                            //数据返回空或解析失败
                            else
                            {
#if DEBUG
                                throw new OybabException(TradingSystemX.Res.Instance.GetString("UpdateNonReturnOrCantRead"), true);
#endif
                            }
                        }
                    }
                }
                catch
#if DEBUG
                (Exception ex)
#endif
                {
#if DEBUG
                    ExceptionPro.ExpLog(ex, null, true);
#endif
                }
            });
        }