You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

373 lines
13 KiB

using IOTContainer.Common;
using IOTContainer.Communication;
using IOTContainer.Model;
using IOTContainer.MvvmBase;
using IOTContainer.View;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace IOTContainer.ViewModel
{
public class ServiceInfoViewModel : ViewModelBase
{
#region 字段
private string _projectCode { get; set; }
private string _projectName { get; set; }
private ObservableCollection<BuildingItem> _buildingList;
public ObservableCollection<BuildingItem> BuildingList
{
get { return _buildingList; }
set
{
base.SetProperty(ref _buildingList, value, "BuildingList");
if (!string.IsNullOrEmpty(ComParameters.Parameters.buildingName) && value.Count > 1)
{
SelectedBuilding = value.Where(p => p.name == ComParameters.Parameters.buildingName).FirstOrDefault();
}
else
{
SelectedBuilding = value[0];
}
}
}
private ObservableCollection<FloorItem> _floorList;
public ObservableCollection<FloorItem> FloorList
{
get { return _floorList; }
set
{
base.SetProperty(ref _floorList, value, "FloorList");
if (!string.IsNullOrEmpty(ComParameters.Parameters.floorName) && value.Count > 1)
{
SelectedFloor = value.Where(p => p.name == ComParameters.Parameters.floorName).FirstOrDefault();
}
else
{
SelectedFloor = value[0];
}
}
}
private ObservableCollection<MachineItem> _machineList;
public ObservableCollection<MachineItem> MachineList
{
get { return _machineList; }
set
{
base.SetProperty(ref _machineList, value, "MachineList");
if (!string.IsNullOrEmpty(ComParameters.Parameters.devCode) && value.Count > 1)
{
SelectedMachine = value.Where(p => p.code == ComParameters.Parameters.devCode).FirstOrDefault();
}
else
{
SelectedMachine = value[0];
}
}
}
private BuildingItem _selectedBuilding;
public BuildingItem SelectedBuilding
{
get { return _selectedBuilding; }
set
{
base.SetProperty(ref _selectedBuilding, value, "SelectedBuilding");
FloorList = value.floor;
}
}
private FloorItem _selectedFloor;
public FloorItem SelectedFloor
{
get { return _selectedFloor; }
set
{
base.SetProperty(ref _selectedFloor, value, "SelectedFloor");
MachineList = value.machine;
}
}
private MachineItem _selectedMachine;
public MachineItem SelectedMachine
{
get { return _selectedMachine; }
set
{
base.SetProperty(ref _selectedMachine, value, "SelectedMachine");
if (value != null)
{
SelectedType = value.machineTypeName;
if (ComParameters.Parameters.machineAttrDic.Keys.Contains(value.attribute.ToString()))
{
SelectedAttr = ComParameters.Parameters.machineAttrDic[value.attribute.ToString()];
}
}
}
}
private string _mac;
public string MAC
{
get
{
return SystemManage.GetMac();
}
set { base.SetProperty(ref _mac, value, "MAC"); }
}
private string _ip;
public string IP
{
get
{
return SystemManage.GetLocalIp();
}
set { base.SetProperty(ref _ip, value, "IP"); }
}
private string _selectedType;
public string SelectedType
{
get { return _selectedType; }
set { base.SetProperty(ref _selectedType, value, "SelectedType"); }
}
private string _selectedAttr;
public string SelectedAttr
{
get { return _selectedAttr; }
set { base.SetProperty(ref _selectedAttr, value, "SelectedAttr"); }
}
#endregion 字段
#region 构造函数
public ServiceInfoViewModel()
{
Activate = new CommandBase(async p => await ActivateDev());
PrePageCommand = new CommandBase(async p => await PrePage());
Task.Run(() =>
{
Query();
});
}
#endregion 构造函数
#region Command命令
public CommandBase Activate { get; set; }
/// <summary>
/// 激活
/// </summary>
public async Task ActivateDev()
{
try
{
if (!Check())
{
return;
}
//主版本
ComParameters.Parameters.containerVersion = ComParameters.Parameters.PreSetContainerVersion;
//ComParameters.Parameters.webVersion = ComParameters.Parameters.PreSetWebVersion;
#region 配置数据
await Task.Run(() =>
{
//设置主屏版本
LocalStorage.InsertPipTable("infos", ConfigKey.containerVersion, ComParameters.Parameters.PreSetContainerVersion);
//LocalStorage.InsertPipTable("infos", ConfigKey.webVersion, ComParameters.Parameters.PreSetWebVersion);
LocalStorage.InsertPipTable("infos", ConfigKey.projectCode, _projectCode);
LocalStorage.InsertPipTable("infos", ConfigKey.projectName, _projectName);
//LocalStorage.InsertPipTable("infos", ConfigKey.buildingCode, _selectedBuilding.code.ToString());
LocalStorage.InsertPipTable("infos", ConfigKey.buildingName, _selectedBuilding.name);
//LocalStorage.InsertPipTable("infos", ConfigKey.floorCode, _selectedFloor.code.ToString());
LocalStorage.InsertPipTable("infos", ConfigKey.floorName, _selectedFloor.name);
LocalStorage.InsertPipTable("infos", ConfigKey.devCode, _selectedMachine.code);
LocalStorage.InsertPipTable("infos", ConfigKey.devName, _selectedMachine.name);
LocalStorage.InsertPipTable("infos", ConfigKey.devType, _selectedType);
LocalStorage.InsertPipTable("infos", ConfigKey.devAttr, _selectedAttr);
});
ComParameters.Parameters.projectCode = _projectCode;
ComParameters.Parameters.projectName = _projectName;
//ComParameters.Parameters.buildingCode = _selectedBuilding.code.ToString();
ComParameters.Parameters.buildingName = _selectedBuilding.name;
//ComParameters.Parameters.floorCode = _selectedFloor.code.ToString();
ComParameters.Parameters.floorName = _selectedFloor.name;
ComParameters.Parameters.devCode = _selectedMachine.code;
ComParameters.Parameters.devName = _selectedMachine.name;
ComParameters.Parameters.devType = _selectedType;
ComParameters.Parameters.devAttr = _selectedAttr;
#endregion
ComParameters.Parameters.MainViewModel.MainUserControl = new InitialView(true);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("设备激活失败:" + ex);
}
}
public CommandBase PrePageCommand { get; set; }
/// <summary>
/// 上一页
/// </summary>
/// <returns></returns>
public async Task PrePage()
{
ComParameters.Parameters.MainViewModel.MainUserControl = new RegisterView();
}
#endregion
#region 方法
/// <summary>
/// 获取初始数据
/// </summary>
internal void Query()
{
try
{
var result = HttpComm.Http.Register();
if (!string.IsNullOrEmpty(result) && result.Contains("\"code\""))
{
var jo = JObject.Parse(result);
string code = jo.Value<string>("code");
if (code != "200")
{
ComParameters.Parameters.MainWindow.Dispatcher.Invoke(() =>
{
ComParameters.Parameters.MainViewModel.MainUserControl = new RegisterView();
var mess = new MessBoxWindow("获取待注册设备信息失败:" + result, false)
{
//Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
mess.ShowDialog();
});
}
else
{
if (result.Contains("projectCode"))
{
var content = jo.Value<JObject>("data");
var project = content.ToObject<Project>();
if (project != null)
{
_projectCode = project.projectCode;
_projectName = project.projectName;
if (project.building.Any())
{
BuildingList = new ObservableCollection<BuildingItem>(project.building);
}
}
}
else
{
var content = jo.Value<string>("data");
content = SecurityHelper.AesDecrypt(content);
var jsons = JObject.Parse(content);
//JObject jsons = (JObject)JsonConvert.DeserializeObject(content);
var project = jsons.ToObject<Project>();
if (project != null)
{
_projectCode = project.projectCode;
_projectName = project.projectName;
if (project.building.Any())
{
BuildingList = new ObservableCollection<BuildingItem>(project.building);
}
}
}
}
}
else
{
Log.MyLog.WriteLogFile("获取待注册设备信息失败:" + result);
ComParameters.Parameters.MainWindow.Dispatcher.Invoke(() =>
{
ComParameters.Parameters.MainViewModel.MainUserControl = new RegisterView();
var mess = new MessBoxWindow("获取待注册设备信息失败:" + result, false)
{
//Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
mess.ShowDialog();
});
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile($"获取初始化资源出错:{ex}");
}
}
/// <summary>
/// 必填检查
/// </summary>
public bool Check()
{
var res = true;
var msg = "";
if (_selectedBuilding == null)
{
msg = "请选择楼栋!";
}
else if (_selectedFloor == null)
{
msg = "请选择楼层!";
}
else if (_selectedMachine == null)
{
msg = "请选择设备!";
}
else if (_selectedType == null)
{
msg = "请选择设备类型!";
}
else if (_selectedAttr == null)
{
msg = "请选择设备属性!";
}
if (!string.IsNullOrEmpty(msg))
{
var mess = new MessBoxWindow(msg, false)
{
Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
mess.ShowDialog();
res = false;
}
return res;
}
#endregion 方法
}
}