diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Util/LogWithConsole.cs b/Correlator/Util/LogWithConsole.cs index a61a659..47bb8a9 100644 --- a/Correlator/Util/LogWithConsole.cs +++ b/Correlator/Util/LogWithConsole.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Correlator.Util { @@ -13,7 +12,7 @@ public static void WriteLog(this string className, string log) { LogHelper.Info(log); - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $" {className} => " + log); + Debug.WriteLine($"{className} => " + log); } } } \ No newline at end of file diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Util/LogWithConsole.cs b/Correlator/Util/LogWithConsole.cs index a61a659..47bb8a9 100644 --- a/Correlator/Util/LogWithConsole.cs +++ b/Correlator/Util/LogWithConsole.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Correlator.Util { @@ -13,7 +12,7 @@ public static void WriteLog(this string className, string log) { LogHelper.Info(log); - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $" {className} => " + log); + Debug.WriteLine($"{className} => " + log); } } } \ No newline at end of file diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index 15307ca..b119014 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -1,10 +1,13 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Windows.Forms; +using Correlator.SensorHubTag; using HandyControl.Controls; +using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util { @@ -92,5 +95,86 @@ Growl.Success("快照已保存"); } + + public static int ToIntensity(this int value) + { + if (value >= -56) + { + return 5; + } + else if (value >= -72) + { + return 4; + } + else if (value >= -88) + { + return 3; + } + else if (value >= -104) + { + return 2; + } + else if (value >= -120) + { + return 1; + } + else + { + return 0; + } + } + + public static List GetTags(this byte[] strTags) + { + var tags = new List(); + try + { + var i = 0; + while (i < strTags.Length) + { + var oid = new byte[4]; + var len = new byte[2]; + Array.Copy(strTags, i, oid, 0, 4); + Array.Copy(strTags, i + 4, len, 0, 2); + + Array.Reverse(len); + int iLen = BitConverter.ToInt16(len, 0); + var strOid = oid.ConvertBytes2String(); + + var value = new byte[iLen]; + Array.Copy(strTags, i + 6, value, 0, iLen); + + i = i + 6 + iLen; + var tag = TagFactory.Create(strOid, iLen, value); + tags.Add(tag); + } + } + catch (Exception e) + { + LogHelper.Info("设备上传协议出错:" + e.Message); + } + + return tags; + } + + /// + /// 电量 + /// + /// + /// + public static CellTag GetCellTag(this IEnumerable tags) + { + return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); + } + + /// + /// 状态 + /// + /// + /// + public static SensorExceptionTag GetSensorExceptionTag(this IEnumerable tags) + { + return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); + } } } \ No newline at end of file diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Util/LogWithConsole.cs b/Correlator/Util/LogWithConsole.cs index a61a659..47bb8a9 100644 --- a/Correlator/Util/LogWithConsole.cs +++ b/Correlator/Util/LogWithConsole.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Correlator.Util { @@ -13,7 +12,7 @@ public static void WriteLog(this string className, string log) { LogHelper.Info(log); - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $" {className} => " + log); + Debug.WriteLine($"{className} => " + log); } } } \ No newline at end of file diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index 15307ca..b119014 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -1,10 +1,13 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Windows.Forms; +using Correlator.SensorHubTag; using HandyControl.Controls; +using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util { @@ -92,5 +95,86 @@ Growl.Success("快照已保存"); } + + public static int ToIntensity(this int value) + { + if (value >= -56) + { + return 5; + } + else if (value >= -72) + { + return 4; + } + else if (value >= -88) + { + return 3; + } + else if (value >= -104) + { + return 2; + } + else if (value >= -120) + { + return 1; + } + else + { + return 0; + } + } + + public static List GetTags(this byte[] strTags) + { + var tags = new List(); + try + { + var i = 0; + while (i < strTags.Length) + { + var oid = new byte[4]; + var len = new byte[2]; + Array.Copy(strTags, i, oid, 0, 4); + Array.Copy(strTags, i + 4, len, 0, 2); + + Array.Reverse(len); + int iLen = BitConverter.ToInt16(len, 0); + var strOid = oid.ConvertBytes2String(); + + var value = new byte[iLen]; + Array.Copy(strTags, i + 6, value, 0, iLen); + + i = i + 6 + iLen; + var tag = TagFactory.Create(strOid, iLen, value); + tags.Add(tag); + } + } + catch (Exception e) + { + LogHelper.Info("设备上传协议出错:" + e.Message); + } + + return tags; + } + + /// + /// 电量 + /// + /// + /// + public static CellTag GetCellTag(this IEnumerable tags) + { + return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); + } + + /// + /// 状态 + /// + /// + /// + public static SensorExceptionTag GetSensorExceptionTag(this IEnumerable tags) + { + return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); + } } } \ No newline at end of file diff --git a/Correlator/Util/SerialPortClient.cs b/Correlator/Util/SerialPortClient.cs new file mode 100644 index 0000000..69946ab --- /dev/null +++ b/Correlator/Util/SerialPortClient.cs @@ -0,0 +1,207 @@ +using System; +using System.IO.Ports; +using System.Linq; +using System.Windows; +using MessageBox = HandyControl.Controls.MessageBox; + +namespace Correlator.Util +{ + public class SerialPortClient + { + #region 变量 + + private string _portName; + private int _baudRate; + private int _dataBits; + private Parity _parity; + private StopBits _stopBits; + + public string PortName + { + get => _portName; + set => _portName = value; + } + + public int BaudRate + { + get => _baudRate; + set => _baudRate = value; + } + + public Parity Parity + { + get => _parity; + set => _parity = value; + } + + public int DataBits + { + get => _dataBits; + set => _dataBits = value; + } + + public StopBits StopBits + { + get => _stopBits; + set => _stopBits = value; + } + + public event Action DataReceivedAction; + public event SerialErrorReceivedEventHandler ErrorReceivedEventHandler; + + #endregion + + #region 懒汉单例模式 + + private static readonly Lazy Lazy = + new Lazy(() => new SerialPortClient()); + + public static SerialPortClient Get => Lazy.Value; + + private readonly SerialPort _serialPort = new SerialPort(); + + private SerialPortClient() + { + BoundSerialPortEvents(); + } + #endregion + + public string[] GetPorts() + { + return SerialPort.GetPortNames(); + } + + /// + /// 串口是否已打开 + /// + public bool IsOpen => _serialPort.IsOpen; + + /// + /// 打开串口 + /// + public void Open() + { + if (!_serialPort.IsOpen) + { + _serialPort.PortName = _portName; + _serialPort.BaudRate = _baudRate; + _serialPort.Parity = _parity; + _serialPort.DataBits = _dataBits; + _serialPort.StopBits = _stopBits; + + _serialPort.Open(); + } + } + + /// + /// 关闭端口 + /// + public void Close() + { + if (_serialPort.IsOpen) + { + _serialPort.Close(); + } + } + + /// + /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据 + /// + public void DiscardBuffer() + { + _serialPort.DiscardInBuffer(); + _serialPort.DiscardOutBuffer(); + } + + #region 写入数据 + + /// + /// 写入数据 + /// + /// + /// + /// + public void Write(byte[] buffer, int offset, int count) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, offset, count); + } + + /// + /// 写入数据 + /// + /// 写入端口的字节数组 + public void Write(byte[] buffer) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, 0, buffer.Length); + } + + /// + /// 写入数据 + /// + /// + public void Write(string text) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(text); + } + #endregion + + /// + /// 获取设备信号 + /// + /// + public void ShowSignal(byte devId) + { + if (!FlowStatus.CanCollectData) + { + CommandSender.SendCellAndStatusCollectCmd(_serialPort, devId); + } + } + + private void BoundSerialPortEvents() + { + _serialPort.DataReceived += SerialPort_DataReceived; + _serialPort.ErrorReceived += SerialPort_ErrorReceived; + } + + public void UnBoundSerialPortEvents() + { + _serialPort.DataReceived -= SerialPort_DataReceived; + _serialPort.ErrorReceived -= SerialPort_ErrorReceived; + } + + /// + /// 数据接收 + /// + /// + /// + void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + if (_serialPort.IsOpen) + { + var receivedData = new byte[_serialPort.BytesToRead]; + _serialPort.Read(receivedData, 0, receivedData.Length); + if (receivedData.Any() && DataReceivedAction != null) + { + DataReceivedAction(receivedData); + } + } + else + { + MessageBox.Show("串口未打开", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + + /// + /// 错误数据 + /// + /// + /// + void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) + { + ErrorReceivedEventHandler?.Invoke(sender, e); + } + } +} \ No newline at end of file diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Util/LogWithConsole.cs b/Correlator/Util/LogWithConsole.cs index a61a659..47bb8a9 100644 --- a/Correlator/Util/LogWithConsole.cs +++ b/Correlator/Util/LogWithConsole.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Correlator.Util { @@ -13,7 +12,7 @@ public static void WriteLog(this string className, string log) { LogHelper.Info(log); - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $" {className} => " + log); + Debug.WriteLine($"{className} => " + log); } } } \ No newline at end of file diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index 15307ca..b119014 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -1,10 +1,13 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Windows.Forms; +using Correlator.SensorHubTag; using HandyControl.Controls; +using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util { @@ -92,5 +95,86 @@ Growl.Success("快照已保存"); } + + public static int ToIntensity(this int value) + { + if (value >= -56) + { + return 5; + } + else if (value >= -72) + { + return 4; + } + else if (value >= -88) + { + return 3; + } + else if (value >= -104) + { + return 2; + } + else if (value >= -120) + { + return 1; + } + else + { + return 0; + } + } + + public static List GetTags(this byte[] strTags) + { + var tags = new List(); + try + { + var i = 0; + while (i < strTags.Length) + { + var oid = new byte[4]; + var len = new byte[2]; + Array.Copy(strTags, i, oid, 0, 4); + Array.Copy(strTags, i + 4, len, 0, 2); + + Array.Reverse(len); + int iLen = BitConverter.ToInt16(len, 0); + var strOid = oid.ConvertBytes2String(); + + var value = new byte[iLen]; + Array.Copy(strTags, i + 6, value, 0, iLen); + + i = i + 6 + iLen; + var tag = TagFactory.Create(strOid, iLen, value); + tags.Add(tag); + } + } + catch (Exception e) + { + LogHelper.Info("设备上传协议出错:" + e.Message); + } + + return tags; + } + + /// + /// 电量 + /// + /// + /// + public static CellTag GetCellTag(this IEnumerable tags) + { + return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); + } + + /// + /// 状态 + /// + /// + /// + public static SensorExceptionTag GetSensorExceptionTag(this IEnumerable tags) + { + return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); + } } } \ No newline at end of file diff --git a/Correlator/Util/SerialPortClient.cs b/Correlator/Util/SerialPortClient.cs new file mode 100644 index 0000000..69946ab --- /dev/null +++ b/Correlator/Util/SerialPortClient.cs @@ -0,0 +1,207 @@ +using System; +using System.IO.Ports; +using System.Linq; +using System.Windows; +using MessageBox = HandyControl.Controls.MessageBox; + +namespace Correlator.Util +{ + public class SerialPortClient + { + #region 变量 + + private string _portName; + private int _baudRate; + private int _dataBits; + private Parity _parity; + private StopBits _stopBits; + + public string PortName + { + get => _portName; + set => _portName = value; + } + + public int BaudRate + { + get => _baudRate; + set => _baudRate = value; + } + + public Parity Parity + { + get => _parity; + set => _parity = value; + } + + public int DataBits + { + get => _dataBits; + set => _dataBits = value; + } + + public StopBits StopBits + { + get => _stopBits; + set => _stopBits = value; + } + + public event Action DataReceivedAction; + public event SerialErrorReceivedEventHandler ErrorReceivedEventHandler; + + #endregion + + #region 懒汉单例模式 + + private static readonly Lazy Lazy = + new Lazy(() => new SerialPortClient()); + + public static SerialPortClient Get => Lazy.Value; + + private readonly SerialPort _serialPort = new SerialPort(); + + private SerialPortClient() + { + BoundSerialPortEvents(); + } + #endregion + + public string[] GetPorts() + { + return SerialPort.GetPortNames(); + } + + /// + /// 串口是否已打开 + /// + public bool IsOpen => _serialPort.IsOpen; + + /// + /// 打开串口 + /// + public void Open() + { + if (!_serialPort.IsOpen) + { + _serialPort.PortName = _portName; + _serialPort.BaudRate = _baudRate; + _serialPort.Parity = _parity; + _serialPort.DataBits = _dataBits; + _serialPort.StopBits = _stopBits; + + _serialPort.Open(); + } + } + + /// + /// 关闭端口 + /// + public void Close() + { + if (_serialPort.IsOpen) + { + _serialPort.Close(); + } + } + + /// + /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据 + /// + public void DiscardBuffer() + { + _serialPort.DiscardInBuffer(); + _serialPort.DiscardOutBuffer(); + } + + #region 写入数据 + + /// + /// 写入数据 + /// + /// + /// + /// + public void Write(byte[] buffer, int offset, int count) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, offset, count); + } + + /// + /// 写入数据 + /// + /// 写入端口的字节数组 + public void Write(byte[] buffer) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, 0, buffer.Length); + } + + /// + /// 写入数据 + /// + /// + public void Write(string text) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(text); + } + #endregion + + /// + /// 获取设备信号 + /// + /// + public void ShowSignal(byte devId) + { + if (!FlowStatus.CanCollectData) + { + CommandSender.SendCellAndStatusCollectCmd(_serialPort, devId); + } + } + + private void BoundSerialPortEvents() + { + _serialPort.DataReceived += SerialPort_DataReceived; + _serialPort.ErrorReceived += SerialPort_ErrorReceived; + } + + public void UnBoundSerialPortEvents() + { + _serialPort.DataReceived -= SerialPort_DataReceived; + _serialPort.ErrorReceived -= SerialPort_ErrorReceived; + } + + /// + /// 数据接收 + /// + /// + /// + void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + if (_serialPort.IsOpen) + { + var receivedData = new byte[_serialPort.BytesToRead]; + _serialPort.Read(receivedData, 0, receivedData.Length); + if (receivedData.Any() && DataReceivedAction != null) + { + DataReceivedAction(receivedData); + } + } + else + { + MessageBox.Show("串口未打开", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + + /// + /// 错误数据 + /// + /// + /// + void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) + { + ErrorReceivedEventHandler?.Invoke(sender, e); + } + } +} \ No newline at end of file diff --git a/Correlator/Util/SerialPortManager.cs b/Correlator/Util/SerialPortManager.cs index 84e6158..2b18dd9 100644 --- a/Correlator/Util/SerialPortManager.cs +++ b/Correlator/Util/SerialPortManager.cs @@ -67,7 +67,7 @@ var operateType = SensorHubClass.GetOpeTypeByPdu(pduType); - var tags = GetTags(settings); + var tags = settings.GetTags(); //根据数据长度判断数据类型 switch (receiveData.Length) @@ -178,13 +178,13 @@ /// private void UpdateDeviceSignal(string devCode, List tags) { - var cellTag = GetCellTag(tags); + var cellTag = tags.GetCellTag(); if (cellTag != null) { _devStatus.Cell = cellTag.Cell; } - var sensorExceptionTag = GetSensorExceptionTag(tags); + var sensorExceptionTag = tags.GetSensorExceptionTag(); if (sensorExceptionTag != null) { _devStatus.Status = sensorExceptionTag.State; @@ -333,59 +333,6 @@ return data; } - private static List GetTags(byte[] strTags) - { - var tags = new List(); - try - { - var i = 0; - while (i < strTags.Length) - { - var oid = new byte[4]; - var len = new byte[2]; - Array.Copy(strTags, i, oid, 0, 4); - Array.Copy(strTags, i + 4, len, 0, 2); - - Array.Reverse(len); - int iLen = BitConverter.ToInt16(len, 0); - var strOid = oid.ConvertBytes2String(); - - var value = new byte[iLen]; - Array.Copy(strTags, i + 6, value, 0, iLen); - - i = i + 6 + iLen; - var tag = TagFactory.Create(strOid, iLen, value); - tags.Add(tag); - } - } - catch (Exception e) - { - LogHelper.Info("设备上传协议出错:" + e.Message); - } - - return tags; - } - - /// - /// 电量 - /// - /// - /// - private static CellTag GetCellTag(IEnumerable tags) - { - return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); - } - - /// - /// 状态 - /// - /// - /// - private static SensorExceptionTag GetSensorExceptionTag(IEnumerable tags) - { - return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); - } - /// /// 上报的噪声 /// @@ -408,33 +355,5 @@ Sp.Write("ATA" + Environment.NewLine); } } - - public int SetSignalIntensity(int value) - { - if (value >= -56) - { - return 5; - } - else if (value >= -72) - { - return 4; - } - else if (value >= -88) - { - return 3; - } - else if (value >= -104) - { - return 2; - } - else if (value >= -120) - { - return 1; - } - else - { - return 0; - } - } } } \ No newline at end of file diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Util/LogWithConsole.cs b/Correlator/Util/LogWithConsole.cs index a61a659..47bb8a9 100644 --- a/Correlator/Util/LogWithConsole.cs +++ b/Correlator/Util/LogWithConsole.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Correlator.Util { @@ -13,7 +12,7 @@ public static void WriteLog(this string className, string log) { LogHelper.Info(log); - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $" {className} => " + log); + Debug.WriteLine($"{className} => " + log); } } } \ No newline at end of file diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index 15307ca..b119014 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -1,10 +1,13 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Windows.Forms; +using Correlator.SensorHubTag; using HandyControl.Controls; +using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util { @@ -92,5 +95,86 @@ Growl.Success("快照已保存"); } + + public static int ToIntensity(this int value) + { + if (value >= -56) + { + return 5; + } + else if (value >= -72) + { + return 4; + } + else if (value >= -88) + { + return 3; + } + else if (value >= -104) + { + return 2; + } + else if (value >= -120) + { + return 1; + } + else + { + return 0; + } + } + + public static List GetTags(this byte[] strTags) + { + var tags = new List(); + try + { + var i = 0; + while (i < strTags.Length) + { + var oid = new byte[4]; + var len = new byte[2]; + Array.Copy(strTags, i, oid, 0, 4); + Array.Copy(strTags, i + 4, len, 0, 2); + + Array.Reverse(len); + int iLen = BitConverter.ToInt16(len, 0); + var strOid = oid.ConvertBytes2String(); + + var value = new byte[iLen]; + Array.Copy(strTags, i + 6, value, 0, iLen); + + i = i + 6 + iLen; + var tag = TagFactory.Create(strOid, iLen, value); + tags.Add(tag); + } + } + catch (Exception e) + { + LogHelper.Info("设备上传协议出错:" + e.Message); + } + + return tags; + } + + /// + /// 电量 + /// + /// + /// + public static CellTag GetCellTag(this IEnumerable tags) + { + return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); + } + + /// + /// 状态 + /// + /// + /// + public static SensorExceptionTag GetSensorExceptionTag(this IEnumerable tags) + { + return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); + } } } \ No newline at end of file diff --git a/Correlator/Util/SerialPortClient.cs b/Correlator/Util/SerialPortClient.cs new file mode 100644 index 0000000..69946ab --- /dev/null +++ b/Correlator/Util/SerialPortClient.cs @@ -0,0 +1,207 @@ +using System; +using System.IO.Ports; +using System.Linq; +using System.Windows; +using MessageBox = HandyControl.Controls.MessageBox; + +namespace Correlator.Util +{ + public class SerialPortClient + { + #region 变量 + + private string _portName; + private int _baudRate; + private int _dataBits; + private Parity _parity; + private StopBits _stopBits; + + public string PortName + { + get => _portName; + set => _portName = value; + } + + public int BaudRate + { + get => _baudRate; + set => _baudRate = value; + } + + public Parity Parity + { + get => _parity; + set => _parity = value; + } + + public int DataBits + { + get => _dataBits; + set => _dataBits = value; + } + + public StopBits StopBits + { + get => _stopBits; + set => _stopBits = value; + } + + public event Action DataReceivedAction; + public event SerialErrorReceivedEventHandler ErrorReceivedEventHandler; + + #endregion + + #region 懒汉单例模式 + + private static readonly Lazy Lazy = + new Lazy(() => new SerialPortClient()); + + public static SerialPortClient Get => Lazy.Value; + + private readonly SerialPort _serialPort = new SerialPort(); + + private SerialPortClient() + { + BoundSerialPortEvents(); + } + #endregion + + public string[] GetPorts() + { + return SerialPort.GetPortNames(); + } + + /// + /// 串口是否已打开 + /// + public bool IsOpen => _serialPort.IsOpen; + + /// + /// 打开串口 + /// + public void Open() + { + if (!_serialPort.IsOpen) + { + _serialPort.PortName = _portName; + _serialPort.BaudRate = _baudRate; + _serialPort.Parity = _parity; + _serialPort.DataBits = _dataBits; + _serialPort.StopBits = _stopBits; + + _serialPort.Open(); + } + } + + /// + /// 关闭端口 + /// + public void Close() + { + if (_serialPort.IsOpen) + { + _serialPort.Close(); + } + } + + /// + /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据 + /// + public void DiscardBuffer() + { + _serialPort.DiscardInBuffer(); + _serialPort.DiscardOutBuffer(); + } + + #region 写入数据 + + /// + /// 写入数据 + /// + /// + /// + /// + public void Write(byte[] buffer, int offset, int count) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, offset, count); + } + + /// + /// 写入数据 + /// + /// 写入端口的字节数组 + public void Write(byte[] buffer) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, 0, buffer.Length); + } + + /// + /// 写入数据 + /// + /// + public void Write(string text) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(text); + } + #endregion + + /// + /// 获取设备信号 + /// + /// + public void ShowSignal(byte devId) + { + if (!FlowStatus.CanCollectData) + { + CommandSender.SendCellAndStatusCollectCmd(_serialPort, devId); + } + } + + private void BoundSerialPortEvents() + { + _serialPort.DataReceived += SerialPort_DataReceived; + _serialPort.ErrorReceived += SerialPort_ErrorReceived; + } + + public void UnBoundSerialPortEvents() + { + _serialPort.DataReceived -= SerialPort_DataReceived; + _serialPort.ErrorReceived -= SerialPort_ErrorReceived; + } + + /// + /// 数据接收 + /// + /// + /// + void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + if (_serialPort.IsOpen) + { + var receivedData = new byte[_serialPort.BytesToRead]; + _serialPort.Read(receivedData, 0, receivedData.Length); + if (receivedData.Any() && DataReceivedAction != null) + { + DataReceivedAction(receivedData); + } + } + else + { + MessageBox.Show("串口未打开", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + + /// + /// 错误数据 + /// + /// + /// + void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) + { + ErrorReceivedEventHandler?.Invoke(sender, e); + } + } +} \ No newline at end of file diff --git a/Correlator/Util/SerialPortManager.cs b/Correlator/Util/SerialPortManager.cs index 84e6158..2b18dd9 100644 --- a/Correlator/Util/SerialPortManager.cs +++ b/Correlator/Util/SerialPortManager.cs @@ -67,7 +67,7 @@ var operateType = SensorHubClass.GetOpeTypeByPdu(pduType); - var tags = GetTags(settings); + var tags = settings.GetTags(); //根据数据长度判断数据类型 switch (receiveData.Length) @@ -178,13 +178,13 @@ /// private void UpdateDeviceSignal(string devCode, List tags) { - var cellTag = GetCellTag(tags); + var cellTag = tags.GetCellTag(); if (cellTag != null) { _devStatus.Cell = cellTag.Cell; } - var sensorExceptionTag = GetSensorExceptionTag(tags); + var sensorExceptionTag = tags.GetSensorExceptionTag(); if (sensorExceptionTag != null) { _devStatus.Status = sensorExceptionTag.State; @@ -333,59 +333,6 @@ return data; } - private static List GetTags(byte[] strTags) - { - var tags = new List(); - try - { - var i = 0; - while (i < strTags.Length) - { - var oid = new byte[4]; - var len = new byte[2]; - Array.Copy(strTags, i, oid, 0, 4); - Array.Copy(strTags, i + 4, len, 0, 2); - - Array.Reverse(len); - int iLen = BitConverter.ToInt16(len, 0); - var strOid = oid.ConvertBytes2String(); - - var value = new byte[iLen]; - Array.Copy(strTags, i + 6, value, 0, iLen); - - i = i + 6 + iLen; - var tag = TagFactory.Create(strOid, iLen, value); - tags.Add(tag); - } - } - catch (Exception e) - { - LogHelper.Info("设备上传协议出错:" + e.Message); - } - - return tags; - } - - /// - /// 电量 - /// - /// - /// - private static CellTag GetCellTag(IEnumerable tags) - { - return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); - } - - /// - /// 状态 - /// - /// - /// - private static SensorExceptionTag GetSensorExceptionTag(IEnumerable tags) - { - return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); - } - /// /// 上报的噪声 /// @@ -408,33 +355,5 @@ Sp.Write("ATA" + Environment.NewLine); } } - - public int SetSignalIntensity(int value) - { - if (value >= -56) - { - return 5; - } - else if (value >= -72) - { - return 4; - } - else if (value >= -88) - { - return 3; - } - else if (value >= -104) - { - return 2; - } - else if (value >= -120) - { - return 1; - } - else - { - return 0; - } - } } } \ No newline at end of file diff --git a/Correlator/Util/SystemConfig.cs b/Correlator/Util/SystemConfig.cs new file mode 100644 index 0000000..385e00b --- /dev/null +++ b/Correlator/Util/SystemConfig.cs @@ -0,0 +1,42 @@ +namespace Correlator.Util +{ + public class SystemConfig + { + /// + /// 顺序不可更改 + /// + public struct SystemPowerStatus + { + public ACLineStatus AcLineStatus; //0 = offline, 1 = Online, 255 = UnKnown Status. + public BatteryFlag BatteryFlag; + public byte BatteryPercent; + public SystemStatusFlag StatusFlag; + public int BatteryLifeTime; + public int BatteryFullLifeTime; + } + + public enum ACLineStatus : byte + { + Offline = 0, //offline + Online = 1, //Online + UnKnownStatus = 255 // 未知 + } + + public enum BatteryFlag : byte + { + Middle = 0, // 电池未充电并且电池容量介于高电量和低电量之间 + High = 1, // 电池电量超过66% + Low = 2, // 电池电量不足33% + Critical = 4, // 电池电量不足百分之五 + Charging = 8, // 充电中 + NoSystemBattery = 128, // 无系统电池 + UnKnownStatus = 255 // 无法读取电池标志信息 + } + + public enum SystemStatusFlag : byte + { + Off = 0, // 节电功能已关闭 + On = 1 // 节电功能已打开,节省电池。尽可能节约能源 + } + } +} \ No newline at end of file diff --git a/Correlator/Correlator.csproj b/Correlator/Correlator.csproj index e44fb6a..ea05467 100644 --- a/Correlator/Correlator.csproj +++ b/Correlator/Correlator.csproj @@ -250,8 +250,10 @@ + + diff --git a/Correlator/Dialog/CheckResponseDialog.xaml.cs b/Correlator/Dialog/CheckResponseDialog.xaml.cs index f785ede..887adab 100644 --- a/Correlator/Dialog/CheckResponseDialog.xaml.cs +++ b/Correlator/Dialog/CheckResponseDialog.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; @@ -36,8 +35,7 @@ var child = RedResponsePanel.Children[i] as Ellipse; if (child?.Fill.ToString() == "#FFD3D3D3") { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + - $" CheckResponseDialog.xaml => {i}"); + Debug.WriteLine($"CheckResponseDialog.xaml => {i}"); } } }; diff --git a/Correlator/SensorHubTag/SensorExceptionTag.cs b/Correlator/SensorHubTag/SensorExceptionTag.cs index 4ba39a7..cc2bdb5 100644 --- a/Correlator/SensorHubTag/SensorExceptionTag.cs +++ b/Correlator/SensorHubTag/SensorExceptionTag.cs @@ -1,6 +1,6 @@ namespace Correlator.SensorHubTag { - internal class SensorExceptionTag : Tag + public class SensorExceptionTag : Tag { public const string SensorExceptionTagOid = "60000009"; diff --git a/Correlator/Util/CommandSender.cs b/Correlator/Util/CommandSender.cs index d20b376..26b8d79 100644 --- a/Correlator/Util/CommandSender.cs +++ b/Correlator/Util/CommandSender.cs @@ -171,13 +171,11 @@ var btVersion = byte.Parse(version, NumberStyles.HexNumber); byte[] btDevId = { 0x21, 0x17, 0x00, 0x08, 0x22, devId }; //广播模式,设备类型不解析 - //byte[] btDevId = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //广播模式,设备类型不解析 const string routeFlag = "1"; var btRouteFlag = byte.Parse(routeFlag, NumberStyles.HexNumber); byte[] btDstNode = { 0x22, devId }; - //byte[] btDstNode = { 0xFF, 0xFF }; const short pduTypeByte = 2; var operateType = (short)(pduTypeByte & 0x7F); @@ -193,7 +191,6 @@ byte[] totalLen = { 0x00, 0x13 }; - //wrap the whole data var result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + oid.Length]; result[0] = preambleByte; diff --git a/Correlator/Util/LogWithConsole.cs b/Correlator/Util/LogWithConsole.cs index a61a659..47bb8a9 100644 --- a/Correlator/Util/LogWithConsole.cs +++ b/Correlator/Util/LogWithConsole.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Correlator.Util { @@ -13,7 +12,7 @@ public static void WriteLog(this string className, string log) { LogHelper.Info(log); - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $" {className} => " + log); + Debug.WriteLine($"{className} => " + log); } } } \ No newline at end of file diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index 15307ca..b119014 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -1,10 +1,13 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Windows.Forms; +using Correlator.SensorHubTag; using HandyControl.Controls; +using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util { @@ -92,5 +95,86 @@ Growl.Success("快照已保存"); } + + public static int ToIntensity(this int value) + { + if (value >= -56) + { + return 5; + } + else if (value >= -72) + { + return 4; + } + else if (value >= -88) + { + return 3; + } + else if (value >= -104) + { + return 2; + } + else if (value >= -120) + { + return 1; + } + else + { + return 0; + } + } + + public static List GetTags(this byte[] strTags) + { + var tags = new List(); + try + { + var i = 0; + while (i < strTags.Length) + { + var oid = new byte[4]; + var len = new byte[2]; + Array.Copy(strTags, i, oid, 0, 4); + Array.Copy(strTags, i + 4, len, 0, 2); + + Array.Reverse(len); + int iLen = BitConverter.ToInt16(len, 0); + var strOid = oid.ConvertBytes2String(); + + var value = new byte[iLen]; + Array.Copy(strTags, i + 6, value, 0, iLen); + + i = i + 6 + iLen; + var tag = TagFactory.Create(strOid, iLen, value); + tags.Add(tag); + } + } + catch (Exception e) + { + LogHelper.Info("设备上传协议出错:" + e.Message); + } + + return tags; + } + + /// + /// 电量 + /// + /// + /// + public static CellTag GetCellTag(this IEnumerable tags) + { + return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); + } + + /// + /// 状态 + /// + /// + /// + public static SensorExceptionTag GetSensorExceptionTag(this IEnumerable tags) + { + return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); + } } } \ No newline at end of file diff --git a/Correlator/Util/SerialPortClient.cs b/Correlator/Util/SerialPortClient.cs new file mode 100644 index 0000000..69946ab --- /dev/null +++ b/Correlator/Util/SerialPortClient.cs @@ -0,0 +1,207 @@ +using System; +using System.IO.Ports; +using System.Linq; +using System.Windows; +using MessageBox = HandyControl.Controls.MessageBox; + +namespace Correlator.Util +{ + public class SerialPortClient + { + #region 变量 + + private string _portName; + private int _baudRate; + private int _dataBits; + private Parity _parity; + private StopBits _stopBits; + + public string PortName + { + get => _portName; + set => _portName = value; + } + + public int BaudRate + { + get => _baudRate; + set => _baudRate = value; + } + + public Parity Parity + { + get => _parity; + set => _parity = value; + } + + public int DataBits + { + get => _dataBits; + set => _dataBits = value; + } + + public StopBits StopBits + { + get => _stopBits; + set => _stopBits = value; + } + + public event Action DataReceivedAction; + public event SerialErrorReceivedEventHandler ErrorReceivedEventHandler; + + #endregion + + #region 懒汉单例模式 + + private static readonly Lazy Lazy = + new Lazy(() => new SerialPortClient()); + + public static SerialPortClient Get => Lazy.Value; + + private readonly SerialPort _serialPort = new SerialPort(); + + private SerialPortClient() + { + BoundSerialPortEvents(); + } + #endregion + + public string[] GetPorts() + { + return SerialPort.GetPortNames(); + } + + /// + /// 串口是否已打开 + /// + public bool IsOpen => _serialPort.IsOpen; + + /// + /// 打开串口 + /// + public void Open() + { + if (!_serialPort.IsOpen) + { + _serialPort.PortName = _portName; + _serialPort.BaudRate = _baudRate; + _serialPort.Parity = _parity; + _serialPort.DataBits = _dataBits; + _serialPort.StopBits = _stopBits; + + _serialPort.Open(); + } + } + + /// + /// 关闭端口 + /// + public void Close() + { + if (_serialPort.IsOpen) + { + _serialPort.Close(); + } + } + + /// + /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据 + /// + public void DiscardBuffer() + { + _serialPort.DiscardInBuffer(); + _serialPort.DiscardOutBuffer(); + } + + #region 写入数据 + + /// + /// 写入数据 + /// + /// + /// + /// + public void Write(byte[] buffer, int offset, int count) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, offset, count); + } + + /// + /// 写入数据 + /// + /// 写入端口的字节数组 + public void Write(byte[] buffer) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(buffer, 0, buffer.Length); + } + + /// + /// 写入数据 + /// + /// + public void Write(string text) + { + if (!_serialPort.IsOpen) _serialPort.Open(); + _serialPort.Write(text); + } + #endregion + + /// + /// 获取设备信号 + /// + /// + public void ShowSignal(byte devId) + { + if (!FlowStatus.CanCollectData) + { + CommandSender.SendCellAndStatusCollectCmd(_serialPort, devId); + } + } + + private void BoundSerialPortEvents() + { + _serialPort.DataReceived += SerialPort_DataReceived; + _serialPort.ErrorReceived += SerialPort_ErrorReceived; + } + + public void UnBoundSerialPortEvents() + { + _serialPort.DataReceived -= SerialPort_DataReceived; + _serialPort.ErrorReceived -= SerialPort_ErrorReceived; + } + + /// + /// 数据接收 + /// + /// + /// + void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + if (_serialPort.IsOpen) + { + var receivedData = new byte[_serialPort.BytesToRead]; + _serialPort.Read(receivedData, 0, receivedData.Length); + if (receivedData.Any() && DataReceivedAction != null) + { + DataReceivedAction(receivedData); + } + } + else + { + MessageBox.Show("串口未打开", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + + /// + /// 错误数据 + /// + /// + /// + void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) + { + ErrorReceivedEventHandler?.Invoke(sender, e); + } + } +} \ No newline at end of file diff --git a/Correlator/Util/SerialPortManager.cs b/Correlator/Util/SerialPortManager.cs index 84e6158..2b18dd9 100644 --- a/Correlator/Util/SerialPortManager.cs +++ b/Correlator/Util/SerialPortManager.cs @@ -67,7 +67,7 @@ var operateType = SensorHubClass.GetOpeTypeByPdu(pduType); - var tags = GetTags(settings); + var tags = settings.GetTags(); //根据数据长度判断数据类型 switch (receiveData.Length) @@ -178,13 +178,13 @@ /// private void UpdateDeviceSignal(string devCode, List tags) { - var cellTag = GetCellTag(tags); + var cellTag = tags.GetCellTag(); if (cellTag != null) { _devStatus.Cell = cellTag.Cell; } - var sensorExceptionTag = GetSensorExceptionTag(tags); + var sensorExceptionTag = tags.GetSensorExceptionTag(); if (sensorExceptionTag != null) { _devStatus.Status = sensorExceptionTag.State; @@ -333,59 +333,6 @@ return data; } - private static List GetTags(byte[] strTags) - { - var tags = new List(); - try - { - var i = 0; - while (i < strTags.Length) - { - var oid = new byte[4]; - var len = new byte[2]; - Array.Copy(strTags, i, oid, 0, 4); - Array.Copy(strTags, i + 4, len, 0, 2); - - Array.Reverse(len); - int iLen = BitConverter.ToInt16(len, 0); - var strOid = oid.ConvertBytes2String(); - - var value = new byte[iLen]; - Array.Copy(strTags, i + 6, value, 0, iLen); - - i = i + 6 + iLen; - var tag = TagFactory.Create(strOid, iLen, value); - tags.Add(tag); - } - } - catch (Exception e) - { - LogHelper.Info("设备上传协议出错:" + e.Message); - } - - return tags; - } - - /// - /// 电量 - /// - /// - /// - private static CellTag GetCellTag(IEnumerable tags) - { - return tags.Where(tag => tag is CellTag).Cast().FirstOrDefault(); - } - - /// - /// 状态 - /// - /// - /// - private static SensorExceptionTag GetSensorExceptionTag(IEnumerable tags) - { - return tags.Where(tag => tag is SensorExceptionTag).Cast().FirstOrDefault(); - } - /// /// 上报的噪声 /// @@ -408,33 +355,5 @@ Sp.Write("ATA" + Environment.NewLine); } } - - public int SetSignalIntensity(int value) - { - if (value >= -56) - { - return 5; - } - else if (value >= -72) - { - return 4; - } - else if (value >= -88) - { - return 3; - } - else if (value >= -104) - { - return 2; - } - else if (value >= -120) - { - return 1; - } - else - { - return 0; - } - } } } \ No newline at end of file diff --git a/Correlator/Util/SystemConfig.cs b/Correlator/Util/SystemConfig.cs new file mode 100644 index 0000000..385e00b --- /dev/null +++ b/Correlator/Util/SystemConfig.cs @@ -0,0 +1,42 @@ +namespace Correlator.Util +{ + public class SystemConfig + { + /// + /// 顺序不可更改 + /// + public struct SystemPowerStatus + { + public ACLineStatus AcLineStatus; //0 = offline, 1 = Online, 255 = UnKnown Status. + public BatteryFlag BatteryFlag; + public byte BatteryPercent; + public SystemStatusFlag StatusFlag; + public int BatteryLifeTime; + public int BatteryFullLifeTime; + } + + public enum ACLineStatus : byte + { + Offline = 0, //offline + Online = 1, //Online + UnKnownStatus = 255 // 未知 + } + + public enum BatteryFlag : byte + { + Middle = 0, // 电池未充电并且电池容量介于高电量和低电量之间 + High = 1, // 电池电量超过66% + Low = 2, // 电池电量不足33% + Critical = 4, // 电池电量不足百分之五 + Charging = 8, // 充电中 + NoSystemBattery = 128, // 无系统电池 + UnKnownStatus = 255 // 无法读取电池标志信息 + } + + public enum SystemStatusFlag : byte + { + Off = 0, // 节电功能已关闭 + On = 1 // 节电功能已打开,节省电池。尽可能节约能源 + } + } +} \ No newline at end of file diff --git a/Correlator/ViewModel/HomePageViewModel.cs b/Correlator/ViewModel/HomePageViewModel.cs index b0dbd50..0891f88 100644 --- a/Correlator/ViewModel/HomePageViewModel.cs +++ b/Correlator/ViewModel/HomePageViewModel.cs @@ -26,11 +26,11 @@ { #region RelayCommand - public RelayCommand WindowLoadedCommand { get; } - public RelayCommand AudioCommand { get; } - public RelayCommand SaveAudioCommand { get; } - public RelayCommand ResetParamCommand { get; } - public RelayCommand StartCalculateCommand { get; } + public RelayCommand WindowLoadedCommand { set; get; } + public RelayCommand AudioCommand { private set; get; } + public RelayCommand SaveAudioCommand { private set; get; } + public RelayCommand ResetParamCommand { private set; get; } + public RelayCommand StartCalculateCommand { private set; get; } #endregion @@ -368,49 +368,22 @@ #region PC电量状态 [DllImport("kernel32.dll", EntryPoint = "GetSystemPowerStatus")] - private static extern void GetSystemPowerStatus(ref SystemPowerStatus lpSystemPowerStatus); - - /// - /// 顺序不可更改 - /// - private struct SystemPowerStatus - { - public ACLineStatus AcLineStatus; //0 = offline, 1 = Online, 255 = UnKnown Status. - public BatteryFlag BatteryFlag; - public byte BatteryPercent; - public SystemStatusFlag StatusFlag; - public int BatteryLifeTime; - public int BatteryFullLifeTime; - } - - private enum ACLineStatus : byte - { - Offline = 0, //offline - Online = 1, //Online - UnKnownStatus = 255 // 未知 - } - - private enum BatteryFlag : byte - { - Middle = 0, // 电池未充电并且电池容量介于高电量和低电量之间 - High = 1, // 电池电量超过66% - Low = 2, // 电池电量不足33% - Critical = 4, // 电池电量不足百分之五 - Charging = 8, // 充电中 - NoSystemBattery = 128, // 无系统电池 - UnKnownStatus = 255 // 无法读取电池标志信息 - } - - private enum SystemStatusFlag : byte - { - Off = 0, // 节电功能已关闭 - On = 1 // 节电功能已打开,节省电池。尽可能节约能源 - } + private static extern void GetSystemPowerStatus(ref SystemConfig.SystemPowerStatus lpSystemPowerStatus); #endregion private HomePageWindow _homePageWindow; + private readonly DispatcherTimer _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + private readonly DispatcherTimer _batteryTimer = new DispatcherTimer + { + Interval = TimeSpan.FromMinutes(5) + }; + //计算时间 private int _runningSeconds; @@ -429,13 +402,13 @@ //加速度计下发指令Timer private readonly DispatcherTimer _accelerometerCommandTimer = new DispatcherTimer { - Interval = new TimeSpan(0, 0, 10) + Interval = TimeSpan.FromSeconds(10) }; //水听器下发指令Timer private readonly DispatcherTimer _hydrophoneCommandTimer = new DispatcherTimer { - Interval = new TimeSpan(0, 0, 10) + Interval = TimeSpan.FromSeconds(10) }; private string _locateDataDir; @@ -447,10 +420,6 @@ public HomePageViewModel() { - DisplayCurrentTime(); - - DisplayBattery(); - #region 默认值 StartState = false; @@ -483,141 +452,9 @@ RegisterMessage(); - WindowLoadedCommand = new RelayCommand(it => - { - Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " HomePageViewModel => 加载"); - _homePageWindow = it; - }); + InitCommand(); - AudioCommand = new RelayCommand(delegate - { - if (!SerialPortManager.Get.Sp.IsOpen) - { - MessageBox.Show("串口状态异常,无法操作", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); - return; - } - - var auditionWindow = new AuditionWindow - { - Owner = Window.GetWindow(_homePageWindow) - }; - auditionWindow.ShowDialog(); - }); - - SaveAudioCommand = new RelayCommand(() => - { - if (FlowStatus.CorrelatorData != null) - { - SaveAudio(); - } - else - { - MessageBox.Show("还未开始采集数据,无法保存音频", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); - } - }); - - ResetParamCommand = new RelayCommand(ResetParam); - - StartCalculateCommand = new RelayCommand(delegate - { - if (!SerialPortManager.Get.Sp.IsOpen) - { - MessageBox.Show("串口状态异常,无法操作", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); - return; - } - - //每次计算之前都需要判断是否已经完成之前的计算,然后清空上一次的计算数据 - ResetParam(); - - var materialDialog = new SelectMaterialDialog - { - Owner = Window.GetWindow(_homePageWindow) - }; - materialDialog.ShowDialog(); - }); - - //计算时间Timer - _runningTimer.Tick += delegate - { - _runningSeconds++; - PassTime = _runningSeconds.ToString(); - if (_runningSeconds >= 90) - { - _runningTimer.Stop(); - MessageBox.Show("接收不到数据,请点击复位并重新开始采集数据", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); - } - }; - - //自动截屏Timer - _snapShotTimer.Tick += delegate - { - _snapShotTimer.Stop(); - if (!string.IsNullOrEmpty(_snapShotPath)) - { - _snapShotPath.SnapShot(); - "HomePageViewModel".WriteLog("截屏路径:" + _snapShotPath); - } - }; - - //加速度计下发指令Timer - _accelerometerCommandTimer.Tick += delegate - { - if (FlowStatus.CanCollectData) - { - CommandSender.SendCorrelatorWakeUpCmd(SerialPortManager.Get.Sp); - } - }; - - //水听器下发指令Timer - _hydrophoneCommandTimer.Tick += delegate - { - if (FlowStatus.CanCollectData) - { - CommandSender.SendHydrophoneWakeUpCmd(SerialPortManager.Get.Sp); - } - }; - } - - /// - /// 实时显示时间 - /// - private void DisplayCurrentTime() - { - var timer = new DispatcherTimer - { - Interval = TimeSpan.FromSeconds(1) - }; - timer.Tick += delegate - { - var dateTime = DateTime.Now; - var strDateTime = dateTime.ToString("yyyy年MM月dd日") + - dateTime.ToString("\0 \0 \0 dddd \0 \0 \0", new CultureInfo("zh-cn")) + - dateTime.ToString("HH:mm:ss"); - CurrentTime = strDateTime; - }; - timer.Start(); - } - - /// - /// 获取电池 - /// - private void DisplayBattery() - { - var timerBattery = new DispatcherTimer - { - Interval = TimeSpan.FromSeconds(300) - }; - - var sysPower = new SystemPowerStatus(); - GetSystemPowerStatus(ref sysPower); - Battery = sysPower.BatteryPercent == 255 ? 100 : sysPower.BatteryPercent; - - timerBattery.Tick += delegate - { - GetSystemPowerStatus(ref sysPower); - Battery = sysPower.BatteryPercent == 255 ? 100 : sysPower.BatteryPercent; - }; - timerBattery.Start(); + TimerTick(); } /// @@ -630,7 +467,7 @@ "HomePageViewModel".WriteLog("SensorA收到消息:" + JsonConvert.SerializeObject(dev)); TransmitterABattery = Convert.ToDouble(dev.Cell); - var signalIntensity = SerialPortManager.Get.SetSignalIntensity(Convert.ToInt32(dev.Signal)); + var signalIntensity = Convert.ToInt32(dev.Signal).ToIntensity(); TransmitterASignalIntensity = signalIntensity; var sensorStatus = dev.Status == "1"; TransmitterAFault = sensorStatus; @@ -644,7 +481,7 @@ "HomePageViewModel".WriteLog("SensorB收到消息:" + JsonConvert.SerializeObject(dev)); TransmitterBBattery = Convert.ToDouble(dev.Cell); - var signalIntensity = SerialPortManager.Get.SetSignalIntensity(Convert.ToInt32(dev.Signal)); + var signalIntensity = Convert.ToInt32(dev.Signal).ToIntensity(); TransmitterBSignalIntensity = signalIntensity; var sensorStatus = dev.Status == "1"; TransmitterBFault = sensorStatus; @@ -760,6 +597,62 @@ }); } + private void InitCommand() + { + WindowLoadedCommand = new RelayCommand(it => + { + Debug.WriteLine("HomePageViewModel => 加载"); + _homePageWindow = it; + }); + + AudioCommand = new RelayCommand(delegate + { + if (!SerialPortManager.Get.Sp.IsOpen) + { + MessageBox.Show("串口状态异常,无法操作", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); + return; + } + + var auditionWindow = new AuditionWindow + { + Owner = Window.GetWindow(_homePageWindow) + }; + auditionWindow.ShowDialog(); + }); + + SaveAudioCommand = new RelayCommand(() => + { + if (FlowStatus.CorrelatorData != null) + { + SaveAudio(); + } + else + { + MessageBox.Show("还未开始采集数据,无法保存音频", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); + } + }); + + ResetParamCommand = new RelayCommand(ResetParam); + + StartCalculateCommand = new RelayCommand(delegate + { + if (!SerialPortManager.Get.Sp.IsOpen) + { + MessageBox.Show("串口状态异常,无法操作", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); + return; + } + + //每次计算之前都需要判断是否已经完成之前的计算,然后清空上一次的计算数据 + ResetParam(); + + var materialDialog = new SelectMaterialDialog + { + Owner = Window.GetWindow(_homePageWindow) + }; + materialDialog.ShowDialog(); + }); + } + /// /// 重置参数 /// @@ -861,5 +754,75 @@ DialogHub.Get.DismissLoadingDialog(); return true; } + + /// + /// 各种定时器 + /// + private void TimerTick() + { + //实时显示时间 + _timer.Tick += delegate + { + var dateTime = DateTime.Now; + var strDateTime = dateTime.ToString("yyyy年MM月dd日") + + dateTime.ToString("\0 \0 \0 dddd \0 \0 \0", new CultureInfo("zh-cn")) + + dateTime.ToString("HH:mm:ss"); + CurrentTime = strDateTime; + }; + _timer.Start(); + + //获取电池电量 + var sysPower = new SystemConfig.SystemPowerStatus(); + GetSystemPowerStatus(ref sysPower); + Battery = sysPower.BatteryPercent == 255 ? 100 : sysPower.BatteryPercent; + + _batteryTimer.Tick += delegate + { + GetSystemPowerStatus(ref sysPower); + Battery = sysPower.BatteryPercent == 255 ? 100 : sysPower.BatteryPercent; + }; + _batteryTimer.Start(); + + //计算时间Timer + _runningTimer.Tick += delegate + { + _runningSeconds++; + PassTime = _runningSeconds.ToString(); + if (_runningSeconds >= 90) + { + _runningTimer.Stop(); + MessageBox.Show("接收不到数据,请点击复位并重新开始采集数据", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); + } + }; + + //自动截屏Timer + _snapShotTimer.Tick += delegate + { + _snapShotTimer.Stop(); + if (!string.IsNullOrEmpty(_snapShotPath)) + { + _snapShotPath.SnapShot(); + "HomePageViewModel".WriteLog("截屏路径:" + _snapShotPath); + } + }; + + //加速度计下发指令Timer + _accelerometerCommandTimer.Tick += delegate + { + if (FlowStatus.CanCollectData) + { + CommandSender.SendCorrelatorWakeUpCmd(SerialPortManager.Get.Sp); + } + }; + + //水听器下发指令Timer + _hydrophoneCommandTimer.Tick += delegate + { + if (FlowStatus.CanCollectData) + { + CommandSender.SendHydrophoneWakeUpCmd(SerialPortManager.Get.Sp); + } + }; + } } } \ No newline at end of file