using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using HXX.Scanner.Biz.Socket;
using HXX.Scanner.Common;
namespace HXX.Scanner.Biz
{
///
/// 版本升级相关
///
public class biz_version
{
///
/// 检测是否需要升级
///
///
public async static Task check()
{
return await Task.Run(() =>
{
try
{
return get_version_info();
}
catch (Exception ee)
{
LogManager.WriteLog(ee);
return null;
}
});
}
///
/// 向服务器发起升级询问
///
///
private static response_http_updateInfo get_version_info()
{
//var url = "http://client.test.jkydata.cn/api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
//var url = config_manager.Get("apiUrlBase") + "api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
var url= config_environment.url_base + "api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
var result = http_helper.Get(url);
var entity = JsonConvert.DeserializeObject(result);
return entity;
}
///
/// 启动启动器,由启动器来复制和重启主程序
///
///
public static void start_starter(string fileName)
{
Process proc = new Process();
//proc.StartInfo.Arguments = strCommandline;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = fileName;
//proc.StartInfo.RedirectStandardInput = true;
//proc.StartInfo.RedirectStandardOutput = true;
//proc.StartInfo.RedirectStandardError = true;
proc.Start();
}
///
/// 保存临时升级信息,方便重启后更新本地版本信息,因为重启成功才表示升级真正成功
///
///
public static void write_updateInfo(response_http_updateInfo version)
{
try
{
var file = System.Windows.Forms.Application.StartupPath + @"\updateInfo.txt";
var txt = JsonConvert.SerializeObject(version);
if (File.Exists(file))
{
File.Delete(file);
}
using(StreamWriter sw=new StreamWriter(file))
{
sw.Write(txt);
}
}
catch (Exception ee)
{
LogManager.WriteLog(ee);
}
}
///
/// 读取临时升级信息,更新本地版本信息
///
///
public static bool read_updateInfo()
{
bool result = false;
try
{
var file = System.Windows.Forms.Application.StartupPath + @"\updateInfo.txt";
var txt = string.Empty;
if (File.Exists(file))
{
using (StreamReader sr = new StreamReader(file))
{
txt = sr.ReadToEnd();
}
}
if (!string.IsNullOrEmpty(txt))
{
var entity = JsonConvert.DeserializeObject(txt);
if (entity != null)
{
if (!string.IsNullOrEmpty(entity.data.appVersion))
{
ConfigManager.SetConfig("appVersion", entity.data.appVersion);
File.Delete(file);
result = true;
}
}
}
}
catch (Exception ee)
{
LogManager.WriteLog(ee);
}
return result;
}
}
}