JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shared.SimpleControl;
using System.Collections.Generic;
 
namespace Shared
{
[System.Serializable]
    public class MonitorInfo
    {
        static MonitorInfo ()
        {
            // 如果是第一次安装使用,就调用这个方法,初始化一些基本的数据
            if (null == Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (AllMonitorListFilesPath)))) {
                //初始化摄像头列表
                IO.FileUtils.WriteFileByBytes (AllMonitorListFilesPath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (new System.Collections.Generic.List<string> ())));
            }
        }
 
         /// <summary>
         /// 摄像头类型为URL   
        /// </summary>
        public string URL {
            get;
            set;
        }
 
        /// <summary>
        /// 摄像头类型选择  
        /// </summary>
        public string Type {
            get;
            set;
        }
 
        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName {
            get;
            set;
        }
        /// <summary>
        /// 用户密码
        /// </summary   
        public string Password {
            get;
            set;
        }
 
        /// <summary>
        /// 摄像机名
        /// </summary>
        public string Name {
            get; set;
        }
 
        /// <summary>
        /// 摄像头列表的文件名
        /// </summary>
        public static string AllMonitorListFilesPath = "MonitorList";
 
        /// <summary>
        /// 所有的摄像头的信息
        /// </summary>
        public static System.Collections.Generic.List<MonitorInfo> Lists = new System.Collections.Generic.List<MonitorInfo> ();
 
        /// <summary>
        /// 所有的摄像头列表路径
        /// </summary>
        public static System.Collections.Generic.List<string> FilePathList {
            get {
                var filePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (AllMonitorListFilesPath)));
                if (filePathList == null)
                    filePathList = new List<string> ();
                return filePathList;
                }
        }
 
        /// <summary>
        /// 根据摄像头路径恢复摄像头对象
        /// </summary>
        public static MonitorInfo GetMonitorByFilePath (string monitorFilePath)
        {
            var monitor = Newtonsoft.Json.JsonConvert.DeserializeObject<MonitorInfo> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (monitorFilePath)));
 
            if (null == monitor) {
                System.Console.WriteLine ("摄像头文件路径不对,文件路径为:" + monitorFilePath);
                return null;
            }
            return monitor;
        }
 
 
        /// <summary>
        /// 添加摄像头
        /// </summary>
        public void Add (string monitorFilePath)
        {
            if (string.IsNullOrEmpty (monitorFilePath) || IO.FileUtils.Exists (monitorFilePath)) {
                return;
            }
            var monitorFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (AllMonitorListFilesPath)));
            monitorFilePathList.Add (monitorFilePath);
            //保存摄像头列表
            IO.FileUtils.WriteFileByBytes (AllMonitorListFilesPath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (monitorFilePathList)));
            Save (monitorFilePath);
        }
 
        public void Save (string monitorFilePath)
        {
            if (string.IsNullOrEmpty (monitorFilePath)) {
                return;
            }
 
            //保存摄像头信息
            IO.FileUtils.WriteFileByBytes (monitorFilePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (this)));
        }
 
        /// <summary>
        /// 删除摄像头
        /// </summary>
        public static void Remove (string monitorFilePath)
        {
            var monitor = MonitorInfo.GetMonitorByFilePath (monitorFilePath);
            if (null == monitor) {
                return;
            }
            var monitorFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (AllMonitorListFilesPath)));
            monitorFilePathList.Remove (monitorFilePath);
            IO.FileUtils.WriteFileByBytes (AllMonitorListFilesPath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (monitorFilePathList)));
            IO.FileUtils.DeleteFile (monitorFilePath);
 
        }
 
          /// <summary>
         /// 摄像头重新编辑  
        /// </summary> 
        public void ReEdit (string oldMonitorFilePath, string newMonitorFilePath)
        {
            if (string.IsNullOrEmpty (newMonitorFilePath)) {
                return;
            }
            //信息已经更改
            if (oldMonitorFilePath != newMonitorFilePath && 2 <= oldMonitorFilePath.Split ('_').Length) {
 
                var monitorFilePathList = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (AllMonitorListFilesPath))); 
                for (int i = 0; i < monitorFilePathList.Count; i++) {
                    var monitorFilePath = monitorFilePathList [i];
                    if (monitorFilePath == oldMonitorFilePath) {
                        monitorFilePathList [i] = newMonitorFilePath;
                        break;
                    }
                }
                 IO.FileUtils.WriteFileByBytes (AllMonitorListFilesPath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (monitorFilePathList)));
                 var oldRoomName = oldMonitorFilePath.Split ('_') [1];
                IO.FileUtils.DeleteFile (oldMonitorFilePath);
             }
 
            Save (newMonitorFilePath);
        }
 
 
    }
}