| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace DeviceCenter.model
- {
- class Config
- {
- private static readonly object _lock = new object();
- private static Config _Instance;
- public string url { get; set; }
- public string username { get; set; }
- public string password { get; set; }
- public string token { get; set; }
- public List<Device> devices { get; set; }
- public void save()
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + "config.json";
- File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
- }
- public static Config getInstance()
- {
- if (null == _Instance)
- {
- lock (_lock)
- {
- if (null == _Instance)
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + "config.json";
- if (File.Exists(path))
- {
- Config config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
- for (int i = 0; i < config.devices.Count; i++)
- {
- Device device = new Device();
- if (config.devices[i].type == DeviceType.ACS)
- {
- device = new AcsDevice();
- }
- else if (config.devices[i].type == DeviceType.CAR_CAM)
- {
- device = new CarCamDevice();
- }
- device.ip = config.devices[i].ip;
- device.port = config.devices[i].port;
- device.username = config.devices[i].username;
- device.password = config.devices[i].password;
- device.name = config.devices[i].name;
- device.status = DeviceStatus.IDLE;
- device.areaId = config.devices[i].areaId;
- device.buildingId = config.devices[i].buildingId;
- device.floorId = config.devices[i].floorId;
- device.direction = config.devices[i].direction;
- config.devices[i] = device;
- }
- _Instance = config;
- }
- else
- {
- Config config = new Config();
- config.devices = new List<Device>();
- config.username = "";
- config.password = "";
- config.save();
- _Instance = config;
- }
- }
- }
- }
- return _Instance;
- }
- }
- }
|