Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
168
169
170
171
using IStation.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
 
namespace IStation.DAL
{
    /// <summary>
    /// 监测月数据集
    /// </summary>    
    public partial class StationSignalRecordPacket
    {
        /// <summary>
        ///  文件信息类
        /// </summary>
        private class FileInfo
        {
            public string FullName { get; set; }
            public int Year { get; set; }
            public int Month { get; set; }
            public int TimeStep { get; set; }
            public int RecordCount { get; set; }
        }
 
        #region FolderInfo
 
        /// <summary>
        /// 查询年月
        /// </summary> 
        private bool GetYearMonth(string folderPath, out int year, out int month, out int timeStep, out int recordCount)
        {
            year = month = timeStep = recordCount = 0;
            if (!File.Exists(folderPath))
                return false;
 
            var name = Path.GetFileNameWithoutExtension(folderPath);
            var strList = name.Split(Settings.File.FileNameSpacer);
            if (strList.Length != 4)
                return false;
            if (!int.TryParse(strList[0], out year))
            {
                return false;
            }
            if (!int.TryParse(strList[1], out month))
            {
                return false;
            }
            if (!int.TryParse(strList[2], out recordCount))
            {
                return false;
            }
            if (!int.TryParse(strList[3], out timeStep))
            {
                return false;
            }
 
            return true;
        }
 
        /// <summary>
        /// 查询文件信息列表
        /// </summary> 
        private List<FileInfo> GetStationSignalRecordPacketFileInfoList(string stationFolder)
        {
            if (!Directory.Exists(stationFolder))
                return default;
            var files = Directory.GetFiles(stationFolder);
            if (files == null || files.Count() < 1)
                return default;
 
            var list = new List<FileInfo>(files.Count());
            foreach (var file in files)
            {
                if (!GetYearMonth(file, out int year, out int month, out int timeStep, out int recordCount))
                    continue;
                var info = new FileInfo
                {
                    FullName = file,
                    Year = year,
                    Month = month,
                    TimeStep = timeStep,
                    RecordCount = recordCount
                };
                list.Add(info);
            }
            if (list.Count > 0)
            {
                list = list.OrderBy(x => x.Year).ThenBy(x => x.Month).ToList();
            }
            return list;
        }
 
        #endregion
 
 
        #region CsvConvert
        //To
        private string ToDsString(Model.StationSignalRecord stationSignalRecord)
        {
            var line = $"{stationSignalRecord.Time:yyyy-MM-dd HH:mm:ss},{stationSignalRecord.PumpRunCount}";
            for (int i = 0; i < stationSignalRecord.PumpSignalRecords.Count; i++)
            {
                var pumpSignalRecord = stationSignalRecord.PumpSignalRecords[i];
                line += $",{pumpSignalRecord.EnginePumpID}" +
                        $",{pumpSignalRecord.RunStatus}" +
                        $",{pumpSignalRecord.Rpm}" +
                        $",{pumpSignalRecord.Frequency}" +
                        $",{pumpSignalRecord.FlowRate}" +
                        $",{pumpSignalRecord.TotalFlow}" +
                        $",{pumpSignalRecord.InletPressure}" +
                        $",{pumpSignalRecord.OutletPressure}" +
                        $",{pumpSignalRecord.InstantaneousPower}" +
                        $",{pumpSignalRecord.TotalActiveEnergy}";
            }
            return line;
        }
 
        //from
        public Model.StationSignalRecord FromDsString(string dsString)
        {
            var strList = dsString.Split(',');
            if (strList.Length < 2)
                return default;
            var model = new Model.StationSignalRecord
            {
                Time = DateTime.Parse(strList[0]),
                PumpRunCount = int.Parse(strList[1]),
                PumpSignalRecords = new List<PumpSignalRecord>()
            };
            for (int i = 0; i < model.PumpRunCount; i++)
            {
                var pumpSignalRecord = new Model.PumpSignalRecord();
                var startIndex = i * 10 + 2;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.EnginePumpID = long.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.RunStatus = int.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.Rpm = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.Frequency = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.FlowRate = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.TotalFlow = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.InletPressure = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.OutletPressure = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.InstantaneousPower = double.Parse(strList[startIndex]);
                startIndex++;
                if (!string.IsNullOrEmpty(strList[startIndex]))
                    pumpSignalRecord.TotalActiveEnergy = double.Parse(strList[startIndex]);
                model.PumpSignalRecords.Add(pumpSignalRecord);
            }
            return model;
        }
        #endregion
    }
}