commit 8057c763db2757353f73654b0de80d2c8d9efad5 Author: henry Date: Tue Feb 21 11:47:47 2017 +0800 no message diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc0c7f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +Beetle.JT808/Beetle.JT808.ClientApp/bin/ +Beetle.JT808/Beetle.JT808.ClientApp/obj/ +Beetle.JT808/Beetle.JT808.UnitTestProject1/bin/ +Beetle.JT808/Beetle.JT808.UnitTestProject1/obj/ +Beetle.JT808/Beetle.JT808/bin/ +Beetle.JT808/Beetle.JT808/obj/ +Beetle.JT808/Beetle.TJ808.BaseServer/bin/ +Beetle.JT808/Beetle.TJ808.BaseServer/obj/ diff --git a/Beetle.JT808/.vs/Beetle.JT808/v14/.suo b/Beetle.JT808/.vs/Beetle.JT808/v14/.suo new file mode 100644 index 0000000..006a4db Binary files /dev/null and b/Beetle.JT808/.vs/Beetle.JT808/v14/.suo differ diff --git a/Beetle.JT808/AssemblyDependencies1.dgml b/Beetle.JT808/AssemblyDependencies1.dgml new file mode 100644 index 0000000..4a6e3be --- /dev/null +++ b/Beetle.JT808/AssemblyDependencies1.dgml @@ -0,0 +1,677 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/App.config b/Beetle.JT808/Beetle.JT808.ClientApp/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Beetle.JT808.ClientApp.csproj b/Beetle.JT808/Beetle.JT808.ClientApp/Beetle.JT808.ClientApp.csproj new file mode 100644 index 0000000..9deb593 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Beetle.JT808.ClientApp.csproj @@ -0,0 +1,94 @@ + + + + + Debug + AnyCPU + {88C57185-5E00-454E-91E9-6E8AAD0E99FC} + WinExe + Properties + Beetle.JT808.ClientApp + Beetle.JT808.ClientApp + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + Form + + + FrmMain.cs + + + + + FrmMain.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + {ad6a9116-9faf-475d-8451-4316f2df910f} + Beetle.JT808 + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.Designer.cs b/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.Designer.cs new file mode 100644 index 0000000..4bffdbb --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.Designer.cs @@ -0,0 +1,159 @@ +namespace Beetle.JT808.ClientApp +{ + partial class FrmMain + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.txtConnectionStatus = new System.Windows.Forms.ToolStripStatusLabel(); + this.txtStatus = new System.Windows.Forms.ToolStripStatusLabel(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.groupBox3 = new System.Windows.Forms.GroupBox(); + this.label1 = new System.Windows.Forms.Label(); + this.textBox1 = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.statusStrip1.SuspendLayout(); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // statusStrip1 + // + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.txtConnectionStatus, + this.txtStatus}); + this.statusStrip1.Location = new System.Drawing.Point(3, 712); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(767, 22); + this.statusStrip1.TabIndex = 1; + this.statusStrip1.Text = "statusStrip1"; + // + // txtConnectionStatus + // + this.txtConnectionStatus.Name = "txtConnectionStatus"; + this.txtConnectionStatus.Size = new System.Drawing.Size(0, 17); + // + // txtStatus + // + this.txtStatus.Name = "txtStatus"; + this.txtStatus.Size = new System.Drawing.Size(0, 17); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.label2); + this.groupBox1.Controls.Add(this.textBox1); + this.groupBox1.Controls.Add(this.label1); + this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top; + this.groupBox1.Location = new System.Drawing.Point(3, 3); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(767, 63); + this.groupBox1.TabIndex = 2; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "服务器信息"; + // + // groupBox2 + // + this.groupBox2.Dock = System.Windows.Forms.DockStyle.Top; + this.groupBox2.Location = new System.Drawing.Point(3, 66); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(767, 105); + this.groupBox2.TabIndex = 3; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "注册信息"; + // + // groupBox3 + // + this.groupBox3.Dock = System.Windows.Forms.DockStyle.Top; + this.groupBox3.Location = new System.Drawing.Point(3, 171); + this.groupBox3.Name = "groupBox3"; + this.groupBox3.Size = new System.Drawing.Size(767, 580); + this.groupBox3.TabIndex = 4; + this.groupBox3.TabStop = false; + this.groupBox3.Text = "行驶信息设置"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(18, 29); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(59, 12); + this.label1.TabIndex = 0; + this.label1.Text = "服务地址:"; + // + // textBox1 + // + this.textBox1.Location = new System.Drawing.Point(89, 26); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(134, 21); + this.textBox1.TabIndex = 1; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(258, 29); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(59, 12); + this.label2.TabIndex = 2; + this.label2.Text = "服务端口:"; + // + // FrmMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(773, 737); + this.Controls.Add(this.groupBox3); + this.Controls.Add(this.groupBox2); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.statusStrip1); + this.MaximizeBox = false; + this.Name = "FrmMain"; + this.Padding = new System.Windows.Forms.Padding(3); + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "TJ/T 808 emulator"; + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel txtConnectionStatus; + private System.Windows.Forms.ToolStripStatusLabel txtStatus; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.GroupBox groupBox2; + private System.Windows.Forms.GroupBox groupBox3; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label1; + } +} + diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.cs b/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.cs new file mode 100644 index 0000000..50c8fc8 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Beetle.JT808.ClientApp +{ + public partial class FrmMain : Form + { + public FrmMain() + { + InitializeComponent(); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.resx b/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.resx new file mode 100644 index 0000000..dafba2f --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/FrmMain.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 127, 17 + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Program.cs b/Beetle.JT808/Beetle.JT808.ClientApp/Program.cs new file mode 100644 index 0000000..d1cd7b7 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Beetle.JT808.ClientApp +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FrmMain()); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Properties/AssemblyInfo.cs b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..8918a16 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的常规信息通过以下 +// 特性集控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Beetle.JT808.ClientApp")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Beetle.JT808.ClientApp")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 使此程序集中的类型 +// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, +// 则将该类型上的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("c5e82d67-3f37-41c8-a58f-3950864f734f")] + +// 程序集的版本信息由下面四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Resources.Designer.cs b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Resources.Designer.cs new file mode 100644 index 0000000..1953f04 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace Beetle.JT808.ClientApp.Properties +{ + + + /// + /// 一个强类型的资源类,用于查找本地化的字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的、缓存的 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Beetle.JT808.ClientApp.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 为所有资源查找重写当前线程的 CurrentUICulture 属性, + /// 方法是使用此强类型资源类。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Resources.resx b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Settings.Designer.cs b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Settings.Designer.cs new file mode 100644 index 0000000..4265f1c --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Beetle.JT808.ClientApp.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Settings.settings b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.ClientApp/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Beetle.JT808/Beetle.JT808.UnitTestProject1/Beetle.JT808.UnitTestProject1.csproj b/Beetle.JT808/Beetle.JT808.UnitTestProject1/Beetle.JT808.UnitTestProject1.csproj new file mode 100644 index 0000000..adeb5e2 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.UnitTestProject1/Beetle.JT808.UnitTestProject1.csproj @@ -0,0 +1,92 @@ + + + + Debug + AnyCPU + {5CDBD16D-5D06-4868-A5E8-DB2B62EF2182} + Library + Properties + Beetle.JT808.UnitTestProject1 + Beetle.JT808.UnitTestProject1 + v4.5 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\Lib\Beetle.Express.dll + + + + + + + + + + + + + + + + + + + + + + {ad6a9116-9faf-475d-8451-4316f2df910f} + Beetle.JT808 + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808.UnitTestProject1/Properties/AssemblyInfo.cs b/Beetle.JT808/Beetle.JT808.UnitTestProject1/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e578372 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.UnitTestProject1/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的常规信息通过以下特性集 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Beetle.JT808.UnitTestProject1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Beetle.JT808.UnitTestProject1")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, +// 请将该类型上的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("6cb0dd38-40da-49b5-83ce-877c30bfb7ef")] + +// 程序集的版本信息由以下四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Beetle.JT808/Beetle.JT808.UnitTestProject1/UnitTest1.cs b/Beetle.JT808/Beetle.JT808.UnitTestProject1/UnitTest1.cs new file mode 100644 index 0000000..e1b407f --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.UnitTestProject1/UnitTest1.cs @@ -0,0 +1,190 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Beetle.JT808.UnitTestProject1 +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + + //Beetle.Express.Clients.SyncTcpClient client = new Express.Clients.SyncTcpClient("192.168.1.184", 5555); + //DateTime dt = new DateTime(2016, 12, 23, 13, 50, 50); + //IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + //{ + // b.Direction = 4; + // b.Height = 5; + // b.Latitude = 56; + // b.Longitude = 100; + // b.Speed = 100; + // b.Status.ACC = true; + // b.Time = dt; + // b.WarningMark.DisplayTheFault = true; + //}); + //client.Send(buffer.Array, 0, buffer.Length); + Console.WriteLine("OK"); + } + + private const UInt16 BUSINESS_NO = 1; + + private const string SIM = "13660170908"; + + [TestMethod] + public void TestClientResponse() + { + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + b.BussinessNO = 12; + b.ResultID = 5; + b.Result = Messages.ResultType.Success; + }); + buffer.Postion = 0; + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.ClientResponse response = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + Assert.AreEqual(response.BussinessNO, 12); + Assert.AreEqual(response.Result, Messages.ResultType.Success); + Assert.AreEqual(response.ResultID, 5); + } + + [TestMethod] + public void TestClientPing() + { + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + + }); + buffer.Postion = 0; + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.ClientPing response = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + } + + [TestMethod] + public void TestCenterResponse() + { + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + b.BussinessNO = 12; + b.ResultID = 5; + b.Result = Messages.ResultType.Success; + }); + buffer.Postion = 0; + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.CenterResponse response = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + Assert.AreEqual(response.BussinessNO, 12); + Assert.AreEqual(response.Result, Messages.ResultType.Success); + Assert.AreEqual(response.ResultID, 5); + } + + [TestMethod] + public void TestClientRegister() + { + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + b.City = 5; + b.Color = 4; + b.DeviceID = "abc"; + b.DeviceNumber = "1002"; + b.PlateNumber = "粤A4XB38"; + b.Provider = "gdgz"; + b.Province = 10; + }); + buffer.Postion = 0; + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.ClientRegister register = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + Assert.AreEqual(register.City, 5); + Assert.AreEqual(register.Color, 4); + Assert.AreEqual(register.DeviceID, "abc"); + Assert.AreEqual(register.DeviceNumber, "1002"); + Assert.AreEqual(register.PlateNumber, "粤A4XB38"); + Assert.AreEqual(register.Provider, "gdgz"); + Assert.AreEqual(register.Province, 10); + } + + [TestMethod] + public void TestClientRegisterResponse() + { + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + b.BusinessNO = 6; + b.Result = 10; + b.Signature = "bbqabc"; + }); + buffer.Postion = 0; + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.ClientRegisterResponse response = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + Assert.AreEqual(response.BusinessNO, 6); + Assert.AreEqual(response.Result, 10); + Assert.AreEqual(response.Signature, "bbqabc"); + } + + [TestMethod] + public void TestClientRegisterCancel() + { + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + + }); + buffer.Postion = 0; + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.ClientRegisterCancel response = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + } + + [TestMethod] + public void TestClientSignature() + { + } + + [TestMethod] + public void TestClientPostion() + { + + DateTime dt = new DateTime(2016, 12, 23, 13, 50, 50); + IProtocolBuffer buffer = MessageFactory.MessateToBuffer(BUSINESS_NO, SIM, (m, b) => + { + b.Direction = 4; + b.Height = 5; + b.Latitude = 56; + b.Longitude = 100; + b.Speed = 100; + b.Status.ACC = true; + b.Time = dt; + b.WarningMark.DisplayTheFault = true; + }); + buffer.Postion = 0; + Console.WriteLine(buffer.ToString()); + IMessage msg = MessageFactory.MessageFromBuffer(buffer); + Messages.ClientPostion postion = msg.GetBody(); + Assert.AreEqual(msg.ID, MessageFactory.GetMessageID()); + Assert.AreEqual(msg.SIM, SIM); + Assert.AreEqual(msg.BussinessNO, BUSINESS_NO); + Assert.AreEqual(postion.Direction, 4); + Assert.AreEqual(postion.Height, 5); + Assert.AreEqual(postion.Latitude, (uint)56); + Assert.AreEqual(postion.Longitude, (uint)100); + Assert.AreEqual(postion.Speed, 100); + Assert.AreEqual(postion.Status.ACC, true); + Assert.AreEqual(postion.Time, dt); + Assert.AreEqual(postion.WarningMark.DisplayTheFault, true); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808.sln b/Beetle.JT808/Beetle.JT808.sln new file mode 100644 index 0000000..6590b5b --- /dev/null +++ b/Beetle.JT808/Beetle.JT808.sln @@ -0,0 +1,38 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Beetle.JT808", "Beetle.JT808\Beetle.JT808.csproj", "{AD6A9116-9FAF-475D-8451-4316F2DF910F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Beetle.JT808.ClientApp", "Beetle.JT808.ClientApp\Beetle.JT808.ClientApp.csproj", "{88C57185-5E00-454E-91E9-6E8AAD0E99FC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Beetle.JT808.UnitTestProject1", "Beetle.JT808.UnitTestProject1\Beetle.JT808.UnitTestProject1.csproj", "{5CDBD16D-5D06-4868-A5E8-DB2B62EF2182}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Beetle.TJ808.BaseServer", "Beetle.TJ808.BaseServer\Beetle.TJ808.BaseServer.csproj", "{03EB1725-EC1F-4933-B0E9-E7F48A5B414D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AD6A9116-9FAF-475D-8451-4316F2DF910F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD6A9116-9FAF-475D-8451-4316F2DF910F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD6A9116-9FAF-475D-8451-4316F2DF910F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD6A9116-9FAF-475D-8451-4316F2DF910F}.Release|Any CPU.Build.0 = Release|Any CPU + {88C57185-5E00-454E-91E9-6E8AAD0E99FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {88C57185-5E00-454E-91E9-6E8AAD0E99FC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {88C57185-5E00-454E-91E9-6E8AAD0E99FC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {88C57185-5E00-454E-91E9-6E8AAD0E99FC}.Release|Any CPU.Build.0 = Release|Any CPU + {5CDBD16D-5D06-4868-A5E8-DB2B62EF2182}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CDBD16D-5D06-4868-A5E8-DB2B62EF2182}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CDBD16D-5D06-4868-A5E8-DB2B62EF2182}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CDBD16D-5D06-4868-A5E8-DB2B62EF2182}.Release|Any CPU.Build.0 = Release|Any CPU + {03EB1725-EC1F-4933-B0E9-E7F48A5B414D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03EB1725-EC1F-4933-B0E9-E7F48A5B414D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03EB1725-EC1F-4933-B0E9-E7F48A5B414D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03EB1725-EC1F-4933-B0E9-E7F48A5B414D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Beetle.JT808/Beetle.JT808.v11.suo b/Beetle.JT808/Beetle.JT808.v11.suo new file mode 100644 index 0000000..e1000de Binary files /dev/null and b/Beetle.JT808/Beetle.JT808.v11.suo differ diff --git a/Beetle.JT808/Beetle.JT808/Beetle.JT808.csproj b/Beetle.JT808/Beetle.JT808/Beetle.JT808.csproj new file mode 100644 index 0000000..b8d32f1 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Beetle.JT808.csproj @@ -0,0 +1,80 @@ + + + + + Debug + AnyCPU + {AD6A9116-9FAF-475D-8451-4316F2DF910F} + Library + Properties + Beetle.JT808 + Beetle.JT808 + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808/Beetle.JT808.csproj.user b/Beetle.JT808/Beetle.JT808/Beetle.JT808.csproj.user new file mode 100644 index 0000000..07dbd5b --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Beetle.JT808.csproj.user @@ -0,0 +1,6 @@ + + + + ProjectFiles + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808/ClassDiagram1.cd b/Beetle.JT808/Beetle.JT808/ClassDiagram1.cd new file mode 100644 index 0000000..f65ba6f --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/ClassDiagram1.cd @@ -0,0 +1,344 @@ + + + + + + AAgAAAAAAABACAAAACAAAAAAAD4IAAAAAAAAQAAAAAg= + Core.cs + + + + + + AAAAgJAAAAAAgAABAAABAAAAAACAAAAAAAAAAQBAACA= + Implement\Message.cs + + + + + + + AAAAAAABCAAA7AGMIgARgARgAAgAAKACAgABwREAAAA= + Implement\ProtocolBuffer.cs + + + + + + + AAAABAAAAAAAAAAAAAAAIAAAAACAAABAAAAAABAACAA= + Implement\ProtocolBuffer.cs + + + + + + IUAAgIAAAIAhAAAAgAAAAABgAgCAAAAgAAgAAAAAAAA= + MessageBodyAttributes.cs + + + + + + AAAAgIAAAAAAAAAAAAAAAAQAAAAAAAAAAAEAAAAAAAA= + MessageBodyPacket.cs + + + + + + AAAAAAAAAAAAAAAEAAAAAAAACAAAAgAAAAAAAAAAAAA= + MessageFactory.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + ProtocolProcessError.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAABAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAABAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAABAgAAAAAAAAAAAAAAEAAAA= + Serializes\IReadWriteHandler.cs + + + + + + AAAAAAAAAAAAgAAAAABAAAAAAAAAAAAAAAAAAAAAAAA= + Serializes\MessageTypeAttribute.cs + + + + + + AAAAAAAAAAAAIAABAAAAAAQAAAAAAAAAAAAAAAAAAAA= + Messages\CenterResponse.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + Messages\ClientPing.cs + + + + + + AAAAAAAEBEAAAAAAACAIAAABAAAAAAAAAAAAAAEAAQA= + Messages\ClientPostion.cs + + + + + + EQACgIAAAAUIAAIFAAACIgAAAEAAQAAJBCAEUECACAI= + Messages\ClientPostion.cs + + + + + + + AIAwgIAAAEAIBAJBAAIIAAAAAAAAAAIAAAAAgCAACgA= + Messages\ClientPostion.cs + + + + + + + AAAAAAAAAAAMAAAIAAAAAAAAAQAAAAAAAAAAkgAAAAA= + Messages\ClientRegister.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + Messages\ClientRegisterCancel.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAgAAE= + Messages\ClientRegisterResponse.cs + + + + + + AAAAAAAAAAAAIAABAAAAAAQAAAAAAAAAAAAAAAAAAAA= + Messages\ClientResponse.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE= + Messages\ClientSignature.cs + + + + + + AAAAAgAAIAEAAgAAAAAAAAAAAAAAAAAAAAAAAAAAgAA= + Serializes\EmitHandler.cs + + + + + + ACQAAgAAIAAABABAIAAAAAAAAAAAAAAAAAAAAQAAAAA= + Serializes\EmitHandler.cs + + + + + + AACAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAIEAAAAA= + Serializes\EmitHandler.cs + + + + + + EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAA= + Serializes\EmitHandler.cs + + + + + + AAAAAVAAAAAEBKIAEAABAEQAAAAAAEABABAgAAgAAAA= + Serializes\EmitHandler.cs + + + + + + AAAAAAAAAAAIAAAAAABAAAAgAAAAAAAAAAAAAAEAAAA= + Serializes\PropertySerializeHandler.cs + + + + + + AAAAACAAAAAEAAAAQAAAAAAAACBAAgAAAAACAAAAAAA= + Serializes\Serializer.cs + + + + + + AAADBAgAAAAAAABAAAAAAAAAAAABAAAAAAACAAAAAAA= + Serializes\Serializer.cs + + + + + + AAAAgJAAAAAAgAABAAABAAAAAACAAAAAAAAAAQBAACA= + IMessage.cs + + + + + + AAAAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + IMessageBody.cs + + + + + + AAAAAAABCAAAzACAIgABAARgAAgAAIAAAAABQQEAAAA= + IProtocolBuffer.cs + + + + + + AAAAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + Serializes\IBitType.cs + + + + + + AAAAAAAAAAAAAQAAAAAAAAAAAgAAAACAAAAAAAAAEAA= + Messages\ClientResponse.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAA= + Serializes\EmitHandler.cs + + + + + + AAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + Serializes\EmitHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAgA= + Serializes\EmitHandler.cs + + + + + + AAAAAAAAAAAAAAAAAAAAAAgQAAAAAAAAAAAAAAAAAAA= + Serializes\EmitHandler.cs + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.JT808/Core.cs b/Beetle.JT808/Beetle.JT808/Core.cs new file mode 100644 index 0000000..1b8f34f --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Core.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + class Core + { + + public byte[] Encode(string value) + { + return Encoding.GetEncoding("GBK").GetBytes(value); + } + + public string Decode(byte[] data, int offset, int count) + { + return Encoding.GetEncoding("GBK").GetString(data, offset, count); + } + + public static short SwapInt16(short v) + { + return (short)(((v & 0xff) << 8) | ((v >> 8) & 0xff)); + } + public static ushort SwapUInt16(ushort v) + { + return (ushort)(((v & 0xff) << 8) | ((v >> 8) & 0xff)); + } + public static int SwapInt32(int v) + { + return (int)(((SwapInt16((short)v) & 0xffff) << 0x10) | + (SwapInt16((short)(v >> 0x10)) & 0xffff)); + } + public static uint SwapUInt32(uint v) + { + return (uint)(((SwapUInt16((ushort)v) & 0xffff) << 0x10) | + (SwapUInt16((ushort)(v >> 0x10)) & 0xffff)); + } + public static long SwapInt64(long v) + { + return (long)(((SwapInt32((int)v) & 0xffffffffL) << 0x20) | + (SwapInt32((int)(v >> 0x20)) & 0xffffffffL)); + } + public static ulong SwapUInt64(ulong v) + { + return (ulong)(((SwapUInt32((uint)v) & 0xffffffffL) << 0x20) | + (SwapUInt32((uint)(v >> 0x20)) & 0xffffffffL)); + } + + public static byte GetCRC(byte[] array, int offset, int count) + { + //write crc + byte crc = 1; + for (int i = offset; i < offset + count; i++) + { + if (i == 1) + crc = array[i]; + else + crc = (byte)(crc ^ array[i]); + } + return crc; + } + + public static bool GetBitValue(uint value, int index) + { + uint tag = 1; + tag = tag << (index); + return (value & tag) > 0; + } + + public static byte GetByteBitValue(params bool[] values) + { + byte result = 0; + for (int i = 0; i < values.Length; i++) + result = (byte)(result | ((values[i] ? 1 : 0) << i)); + return result; + } + + public static ushort GetUShortBitValue(params bool[] values) + { + ushort result = 0; + for (int i = 0; i < values.Length; i++) + result = (ushort)(result | ((values[i] ? 1 : 0) << i)); + return result; + } + + public static uint GetUIntBitValue(params bool[] values) + { + uint result = 0; + for (int i = 0; i < values.Length; i++) + result = (uint)(result | ((values[i] ? (uint)1 : (uint)0) << i)); + return result; + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/IMessage.cs b/Beetle.JT808/Beetle.JT808/IMessage.cs new file mode 100644 index 0000000..9627104 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/IMessage.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public interface IMessage + { + ushort ID { get; set; } + MessageBodyAttributes Property { get; set; } + string SIM { get; set; } + ushort BussinessNO { get; set; } + PacketInfo Packet { get; set; } + void Save(IProtocolBuffer buffer); + void Load(IProtocolBuffer buffer); + object Body { get; set; } + byte CRC { get; set; } + T GetBody(); + + } +} diff --git a/Beetle.JT808/Beetle.JT808/IMessageBody.cs b/Beetle.JT808/Beetle.JT808/IMessageBody.cs new file mode 100644 index 0000000..9d6255c --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/IMessageBody.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public interface IMessageBody + { + void Save(IProtocolBuffer buffer); + void Load(IProtocolBuffer buffer); + } +} diff --git a/Beetle.JT808/Beetle.JT808/IProtocolBuffer.cs b/Beetle.JT808/Beetle.JT808/IProtocolBuffer.cs new file mode 100644 index 0000000..837f39c --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/IProtocolBuffer.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public interface IProtocolBuffer + { + bool Import(byte value); + int Import(byte[] data, int offset, int count); + + void Reset(); + int Length { get; } + void SetLength(int length); + int Postion { get; set; } + byte[] Array { get; } + + void Write(byte[] data); + void Write(byte data); + void Write(ushort value); + void Write(uint value); + void WriteBCD(string value); + void WriteASCII(string value, int length); + int WriteGBK(string value); + void WriteSubBuffer(IProtocolBuffer buffer); + void WriteTag(); + + byte Read(); + byte[] Read(int length); + ushort ReadUInt16(); + uint ReadUInt(); + string ReadBCD(int length); + string ReadASCII(int length); + string ReadGBK(int length = -1); + void ReadSubBuffer(IProtocolBuffer buffer, int count); + } +} diff --git a/Beetle.JT808/Beetle.JT808/Implement/Message.cs b/Beetle.JT808/Beetle.JT808/Implement/Message.cs new file mode 100644 index 0000000..9b0db3f --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Implement/Message.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Beetle.JT808.Serializes; +namespace Beetle.JT808 +{ + public class Message : IMessage + { + public Message() + { + Property = new JT808.MessageBodyAttributes(); + } + + public object Body + { + get; + set; + } + + public ushort ID + { + get; + set; + } + + public MessageBodyAttributes Property + { + get; + set; + } + + public string SIM + { + get; + set; + } + + public ushort BussinessNO + { + get; + set; + } + + public PacketInfo Packet + { + get; + set; + } + + public byte CRC + { + get; + set; + } + + public void Load(IProtocolBuffer buffer) + { + byte crc = Core.GetCRC(buffer.Array, 1, buffer.Length - 3); + this.CRC = buffer.Array[buffer.Length - 2]; + if (this.CRC != crc) + throw new ProtocolProcessError("message check CRC error!"); + buffer.Read(); //read start + ID = buffer.ReadUInt16(); //read id + Property.Load(buffer); //read property + SIM = buffer.ReadBCD(6); //read sim + BussinessNO = buffer.ReadUInt16(); //read no + if (Property.IsPacket) //read packet + { + Packet = new PacketInfo(); + Packet.Load(buffer); + } + if (Property.BodyLength > 0) //read body + { + IProtocolBuffer bodybuffer = ProtocolBufferPool.Default.Pop(); + try + { + + buffer.ReadSubBuffer(bodybuffer, Property.BodyLength); + Serializer serializer = SerializerFactory.Defalut.Get(ID); + Body = serializer.CreateObject(); + serializer.Deserialize(Body, bodybuffer); + } + finally + { + ProtocolBufferPool.Default.Push(bodybuffer); + } + } + this.CRC = buffer.Read(); //read crc + buffer.Read(); //read end + } + + public void Save(IProtocolBuffer buffer) + { + IProtocolBuffer bodybuffer = null; + try + { + if (Packet != null) + Property.IsPacket = true; + if (Body != null) + { + Serializer serializer = SerializerFactory.Defalut.Get(Body.GetType()); + if (serializer == null) + throw new ProtocolProcessError(string.Format("{0} serializer not found!", Body)); + ID = serializer.MessageType.ID; + if (!serializer.MessageType.NoBody) + { + + bodybuffer = ProtocolBufferPool.Default.Pop(); + serializer.Serialize(Body, bodybuffer); + if (bodybuffer.Length > MessageBodyAttributes.BODY_LENGTH) + throw new ProtocolProcessError("message body to long!"); + Property.BodyLength = (ushort)bodybuffer.Length; + + } + } + buffer.WriteTag(); //write start + buffer.Write(ID); //write id + Property.Save(buffer); //write body property + buffer.WriteBCD(SIM); //write sim + buffer.Write(BussinessNO); //write no + if (Packet != null) //write packet + Packet.Save(buffer); + if (bodybuffer != null) //write body + buffer.WriteSubBuffer(bodybuffer); + byte crc = Core.GetCRC(buffer.Array, 1, buffer.Length - 1); + buffer.Write(crc); //write crc + buffer.WriteTag(); //write end + } + finally + { + if (bodybuffer != null) + ProtocolBufferPool.Default.Push(bodybuffer); + } + } + + public T GetBody() + { + return (T)Body; + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Implement/ProtocolBuffer.cs b/Beetle.JT808/Beetle.JT808/Implement/ProtocolBuffer.cs new file mode 100644 index 0000000..e20f3ce --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Implement/ProtocolBuffer.cs @@ -0,0 +1,364 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public class ProtocolBuffer : IProtocolBuffer + { + private byte[] mArray = new byte[1024]; + + private int mPostion; + + private int mLength; + + private bool mProtocolStart = false; + + public const byte PROTOBUF_TAG = 0x7e; + + public const byte REPLACE_TAG = 0x7d; + + public bool Import(byte value) + { + if (value == PROTOBUF_TAG) + { + OnWrite(value); + if (!mProtocolStart) + { + mProtocolStart = true; + } + else + { + mPostion = 0; + return true; + } + } + else + { + if (mProtocolStart) + { + OnWrite(value); + } + } + return false; + } + + public int Import(byte[] data, int offset, int count) + { + int result = 0; + for (int i = 0; i < count; i++) + { + result++; + byte value = data[offset + i]; + if (Import(value)) + return result; + } + return -1; + } + + private byte OnRead() + { + byte value = mArray[mPostion]; + mPostion++; + return value; + } + + public byte Read() + { + byte value = OnRead(); + if (value == REPLACE_TAG) + { + value = Read(); + if (value == 0x01) + { + return REPLACE_TAG; + } + else if (value == 0x02) + { + return PROTOBUF_TAG; + } + } + return value; + } + + public byte[] Read(int length) + { + byte[] result = new byte[length]; + for (int i = 0; i < length; i++) + { + result[i] = Read(); + } + return result; + } + + private ProtocolBuffer OnWrite(byte value) + { + mArray[mPostion] = value; + mPostion++; + mLength++; + return this; + } + + public void WriteTag() + { + OnWrite(PROTOBUF_TAG); + } + + public void Write(byte data) + { + if (data == PROTOBUF_TAG) + { + OnWrite(REPLACE_TAG).OnWrite(0x02); + } + else if (data == REPLACE_TAG) + { + OnWrite(REPLACE_TAG).OnWrite(0x01); + } + else + { + OnWrite(data); + } + } + + public void Write(byte[] data) + { + for (int i = 0; i < data.Length; i++) + { + Write(data[i]); + } + } + + public int Length { get { return mLength; } } + + public int Postion { get { return mPostion; } set { mPostion = value; } } + + public byte[] Array { get { return mArray; } } + + public void ReadSubBuffer(IProtocolBuffer buffer, int count) + { + Buffer.BlockCopy(mArray, mPostion, buffer.Array, 0, count); + mPostion += count; + buffer.SetLength(count); + buffer.Postion = 0; + } + + public void WriteSubBuffer(IProtocolBuffer buffer) + { + Buffer.BlockCopy(buffer.Array, 0, mArray, mPostion, buffer.Length); + mPostion += buffer.Length; + mLength += buffer.Length; + } + + public void SetLength(int length) + { + mLength = length; + } + + public void Reset() + { + mPostion = 0; + mLength = 0; + mProtocolStart = false; + } + + public void Write(ushort value) + { + value = Core.SwapUInt16(value); + byte[] data = BitConverter.GetBytes(value); + Write(data); + } + + public void Write(uint value) + { + value = Core.SwapUInt32(value); + byte[] data = BitConverter.GetBytes(value); + Write(data); + } + + public void WriteBCD(string value) + { + byte[] data = str2Bcd(value); + Write(data); + } + + public ushort ReadUInt16() + { + byte[] data = Read(2); + ushort result = BitConverter.ToUInt16(data, 0); + return Core.SwapUInt16(result); + } + + public uint ReadUInt() + { + byte[] data = Read(4); + uint result = BitConverter.ToUInt32(data, 0); + return Core.SwapUInt32(result); + } + + public string ReadBCD(int length) + { + byte[] data = Read(length); + return bcd2Str(data); + } + + public byte[] str2Bcd(String asc) + { + int len = asc.Length; + int mod = len % 2; + if (mod != 0) + { + asc = "0" + asc; + len = asc.Length; + } + byte[] abt = new byte[len]; + if (len >= 2) + { + len = len / 2; + } + byte[] bbt = new byte[len]; + abt = System.Text.Encoding.ASCII.GetBytes(asc); + int j, k; + for (int p = 0; p < asc.Length / 2; p++) + { + if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) + { + j = abt[2 * p] - '0'; + } + else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) + { + j = abt[2 * p] - 'a' + 0x0a; + } + else + { + j = abt[2 * p] - 'A' + 0x0a; + } + if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) + { + k = abt[2 * p + 1] - '0'; + } + else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) + { + k = abt[2 * p + 1] - 'a' + 0x0a; + } + else + { + k = abt[2 * p + 1] - 'A' + 0x0a; + } + int a = (j << 4) + k; + byte b = (byte)a; + bbt[p] = b; + } + return bbt; + + } + + public string bcd2Str(byte[] bytes) + { + StringBuilder temp = new StringBuilder(bytes.Length * 2); + for (int i = 0; i < bytes.Length; i++) + { + temp.Append((byte)((bytes[i] & 0xf0) >> 4)); + temp.Append((byte)(bytes[i] & 0x0f)); + } + return temp.ToString().Substring(0, 1).Equals("0") ? temp.ToString().Substring(1) : temp.ToString(); + + } + + public void WriteASCII(string value, int length) + { + if (value.Length > length) + { + value = value.Substring(0, length); + } + else + { + for (int i = value.Length; i < length; i++) + { + value = " " + value; + } + } + byte[] data = Encoding.ASCII.GetBytes(value); + Write(data); + } + + public string ReadASCII(int length) + { + byte[] data = Read(length); + return Encoding.ASCII.GetString(data); + } + + public int WriteGBK(string value) + { + int postion = mPostion; + byte[] data = Encoding.GetEncoding("GBK").GetBytes(value); + Write(data); + return mPostion - postion; + } + + public string ReadGBK(int length = -1) + { + if (length == -1) + return Encoding.GetEncoding("GBK").GetString(Array, mPostion, mLength - mPostion); + byte[] data = Read(length); + return Encoding.GetEncoding("GBK").GetString(data); + } + + public override string ToString() + { + string hex = BitConverter.ToString(Array, 0, Length).Replace("-", string.Empty); + return hex; + } + } + + + public class ProtocolBufferPool + { + private System.Collections.Concurrent.ConcurrentStack + mPool = new System.Collections.Concurrent.ConcurrentStack(); + + public ProtocolBufferPool() + { + for (int i = 0; i < 1000; i++) + { + mPool.Push(CreateBuffer()); + } + } + + public IProtocolBuffer Pop() + { + IProtocolBuffer result = null; + if (!mPool.TryPop(out result)) + { + result = CreateBuffer(); + } + + result.Reset(); + return result; + } + + private IProtocolBuffer CreateBuffer() + { + ProtocolBuffer buffer = new ProtocolBuffer(); + return buffer; + } + + public void Push(IProtocolBuffer buffer) + { + mPool.Push(buffer); + } + + private static ProtocolBufferPool mDefault; + + public static ProtocolBufferPool Default + { + get + { + if (mDefault == null) + mDefault = new ProtocolBufferPool(); + return mDefault; + } + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/MessageBodyAttributes.cs b/Beetle.JT808/Beetle.JT808/MessageBodyAttributes.cs new file mode 100644 index 0000000..fdcefae --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/MessageBodyAttributes.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public class MessageBodyAttributes + { + + #region consts + public const ushort CUSTOM_HEIGHT = 0x8000; + + public const ushort CUSTOM_LOW = 0x4000; + + public const ushort IS_PACKET = 0x2000; + + public const ushort ENCRYPT_HEIGHT = 0x1000; + + public const ushort ENCRYPT_MIDDLE = 0x400; + + public const ushort ENCRYPT_LOW = 0x200; + + public const ushort BODY_LENGTH = 0x1FF; + #endregion + + //保留位15 + public bool CustomHigh { get; set; } + + //保留位14 + public bool CustomLow { get; set; } + + //分包位13 + public bool IsPacket { get; set; } + + //加密位12 + public bool EncryptHigh { get; set; } + + //加密位11 + public bool EncryptMiddle { get; set; } + + //加密位10 + public bool EncryptLow { get; set; } + + //消息长度9-0 + public ushort BodyLength { get; set; } + + public void Save(IProtocolBuffer buffer) + { + ushort value = (ushort)(BodyLength & BODY_LENGTH); + if (CustomHigh) + value |= CUSTOM_HEIGHT; + if (CustomLow) + value |= CUSTOM_LOW; + if (IsPacket) + value |= IS_PACKET; + if (EncryptHigh) + value |= ENCRYPT_HEIGHT; + if (EncryptMiddle) + value |= ENCRYPT_MIDDLE; + if (EncryptLow) + value |= ENCRYPT_LOW; + buffer.Write(value); + } + + public void Load(IProtocolBuffer buffer) + { + + ushort value = buffer.ReadUInt16(); + CustomHigh = (CUSTOM_HEIGHT & value) > 0; + CustomLow = (CUSTOM_LOW & value) > 0; + IsPacket = (IS_PACKET & value) > 0; + EncryptHigh = (ENCRYPT_HEIGHT & value) > 0; + EncryptMiddle = (ENCRYPT_MIDDLE & value) > 0; + EncryptLow = (ENCRYPT_LOW & value) > 0; + BodyLength = (ushort)(BODY_LENGTH & value); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/MessageBodyPacket.cs b/Beetle.JT808/Beetle.JT808/MessageBodyPacket.cs new file mode 100644 index 0000000..bd30229 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/MessageBodyPacket.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public class PacketInfo + { + public ushort Count { get; set; } + + public ushort Index { get; set; } + + public void Save(IProtocolBuffer buffer) + { + Count = Core.SwapUInt16(Count); + Index = Core.SwapUInt16(Index); + } + + public void Load(IProtocolBuffer buffer) + { + byte[] data = buffer.Read(2); + Count = BitConverter.ToUInt16(data, 0); + Count = Core.SwapUInt16(Count); + + data = buffer.Read(2); + Index = BitConverter.ToUInt16(data, 0); + Index = Core.SwapUInt16(Count); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/MessageFactory.cs b/Beetle.JT808/Beetle.JT808/MessageFactory.cs new file mode 100644 index 0000000..50bf713 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/MessageFactory.cs @@ -0,0 +1,44 @@ + +using Beetle.JT808.Messages; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public class MessageFactory + { + + public static IProtocolBuffer MessateToBuffer(ushort businessNO, string sim, Action handler) where T : new() + { + IProtocolBuffer buffer = ProtocolBufferPool.Default.Pop(); + Message msg = new Message(); + msg.BussinessNO = businessNO; + msg.SIM = sim; + T body = new T(); + msg.Body = body; + if (handler != null) + handler(msg, body); + msg.Save(buffer); + return buffer; + } + + public static ushort GetMessageID() + { + Type type = typeof(T); + Serializes.Serializer serializer = Serializes.SerializerFactory.Defalut.Get(type); + if (serializer == null) + throw new ProtocolProcessError(string.Format("{0} serializer not found!", type)); + return serializer.MessageType.ID; + } + + public static Message MessageFromBuffer(IProtocolBuffer buffer) + { + Message msg = new Message(); + msg.Load(buffer); + return msg; + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/CenterResponse.cs b/Beetle.JT808/Beetle.JT808/Messages/CenterResponse.cs new file mode 100644 index 0000000..9bf3e10 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/CenterResponse.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + + [MessageType(ID = 0x0008)] + public class CenterResponse + { + [UInt16Handler] + public ushort BussinessNO { get; set; } + [UInt16Handler] + public ushort ResultID { get; set; } + [ByteHandler] + public ResultType Result { get; set; } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientPing.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientPing.cs new file mode 100644 index 0000000..072bdc4 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientPing.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + [MessageType(NoBody = true, ID = 0x0002)] + public class ClientPing + { + + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientPostion.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientPostion.cs new file mode 100644 index 0000000..9947952 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientPostion.cs @@ -0,0 +1,342 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + [MessageType(ID = 0x0200)] + public class ClientPostion + { + public ClientPostion() + { + Status = new ClientPostionStatus(); + WarningMark = new ClientPostionWarningMark(); + } + + [UIntBitHandler(typeof(ClientPostionWarningMark))] + public ClientPostionWarningMark WarningMark { get; set; } + + [UIntBitHandler(typeof(ClientPostionStatus))] + public ClientPostionStatus Status { get; set; } + + [UIntHandler] + public uint Longitude { get; set; } + + [UIntHandler] + public uint Latitude { get; set; } + + [UInt16Handler] + public ushort Height { get; set; } + + [UInt16Handler] + public ushort Speed { get; set; } + + [UInt16Handler] + public ushort Direction { get; set; } + + [TimeBCD] + public DateTime Time { get; set; } + + + } + + + + + public class ClientPostionWarningMark : IBitCustomType + { + /// + /// 紧急报瞥触动报警开关后触发 + /// + public bool TouchAlarmSwitch { get; set; } + + /// + /// 超速报警 + /// + public bool SpeedLimit { get; set; } + + /// + /// 疲劳驾驶 + /// + public bool Fatigue { get; set; } + + /// + /// 预警 + /// + public bool Alert { get; set; } + + /// + /// GNSS模块发生故障 + /// + public bool GNSSModule { get; set; } + + /// + /// GNSS天线未接或被剪断 + /// + public bool GNSSCutAntenna { get; set; } + + /// + /// GNSS天线短路 + /// + public bool GNSSShortCircuit { get; set; } + + /// + /// 终端主电源欠压 + /// + public bool MainPowerVoltage { get; set; } + + /// + /// 终端主电源掉电 + /// + public bool MainPowerOff { get; set; } + + /// + /// 终端LCD或显示器故障 + /// + public bool DisplayTheFault { get; set; } + + /// + /// TTS模块故障 + /// + public bool TTSModuleFailure { get; set; } + + /// + /// 摄像头故障 + /// + public bool CameraMalfunction { get; set; } + + public bool Keep12 { get; set; } + + public bool Keep13 { get; set; } + + public bool Keep14 { get; set; } + + public bool Keep15 { get; set; } + + public bool Keep16 { get; set; } + + public bool Keep17 { get; set; } + + /// + /// 驾驶超时 + /// + public bool DrivingTimeoutOfDay { get; set; } + + /// + /// 超时停车 + /// + public bool StopTimeout { get; set; } + + /// + /// 进出区域 + /// + public bool InOutArea { get; set; } + + /// + /// 进出路线 + /// + public bool InOutLine { get; set; } + + /// + /// 路段行驶时间 + /// + public bool BritainsTime { get; set; } + + /// + /// 路线偏离报警 + /// + public bool LaneDeparture { get; set; } + + /// + /// VSS故障 + /// + public bool VSSFault { get; set; } + + /// + /// 油量异常 + /// + public bool OilFault { get; set; } + + /// + /// 被盗 + /// + public bool Stolen { get; set; } + + /// + /// 非法点火 + /// + public bool IllegalIgnition { get; set; } + + /// + /// 车辆非法位移 + /// + public bool IllegalDisplacement { get; set; } + + public bool Keep29 { get; set; } + + public bool Keep30 { get; set; } + + public bool Keep31 { get; set; } + + public void Load(object value) + { + uint data = (uint)value; + TouchAlarmSwitch = Core.GetBitValue(data, 0); + SpeedLimit = Core.GetBitValue(data, 1); + Fatigue = Core.GetBitValue(data, 2); + Alert = Core.GetBitValue(data, 3); + GNSSModule = Core.GetBitValue(data, 4); + GNSSCutAntenna = Core.GetBitValue(data, 5); + GNSSShortCircuit = Core.GetBitValue(data, 6); + MainPowerVoltage = Core.GetBitValue(data, 7); + MainPowerOff = Core.GetBitValue(data, 8); + DisplayTheFault = Core.GetBitValue(data, 9); + TTSModuleFailure = Core.GetBitValue(data, 10); + CameraMalfunction = Core.GetBitValue(data, 11); + Keep12 = Core.GetBitValue(data, 12); + Keep13 = Core.GetBitValue(data, 13); + Keep14 = Core.GetBitValue(data, 14); + Keep15 = Core.GetBitValue(data, 15); + Keep16 = Core.GetBitValue(data, 16); + Keep17 = Core.GetBitValue(data, 17); + DrivingTimeoutOfDay = Core.GetBitValue(data, 18); + StopTimeout = Core.GetBitValue(data, 19); + InOutArea = Core.GetBitValue(data, 20); + InOutLine = Core.GetBitValue(data, 21); + BritainsTime = Core.GetBitValue(data, 22); + LaneDeparture = Core.GetBitValue(data, 23); + VSSFault = Core.GetBitValue(data, 24); + OilFault = Core.GetBitValue(data, 25); + Stolen = Core.GetBitValue(data, 26); + IllegalIgnition = Core.GetBitValue(data, 27); + IllegalDisplacement = Core.GetBitValue(data, 28); + Keep29 = Core.GetBitValue(data, 29); + Keep30 = Core.GetBitValue(data, 30); + Keep31 = Core.GetBitValue(data, 31); + + } + + public object Save() + { + return Core.GetUIntBitValue(TouchAlarmSwitch, SpeedLimit, Fatigue, Alert, GNSSModule, GNSSCutAntenna, GNSSShortCircuit, + MainPowerVoltage, MainPowerOff, DisplayTheFault, TTSModuleFailure, CameraMalfunction, Keep12, + Keep13, Keep14, Keep15, Keep16, Keep17, DrivingTimeoutOfDay, StopTimeout, InOutArea, InOutLine, + BritainsTime, LaneDeparture, VSSFault, OilFault, Stolen, IllegalIgnition, IllegalDisplacement, + Keep29, Keep30, Keep31); + } + } + + public class ClientPostionStatus : IBitCustomType + { + public bool ACC { get; set; } + + public bool Location { get; set; } + + public bool Latitude { get; set; } + + public bool Longitude { get; set; } + + public bool Operate { get; set; } + + public bool Encryption { get; set; } + + public bool Keep6 { get; set; } + + public bool Keep7 { get; set; } + + public bool Keep8 { get; set; } + + public bool Keep9 { get; set; } + + public bool OilRoad { get; set; } + + public bool ElectricityRoad { get; set; } + + public bool DoorLock { get; set; } + + public bool Keep13 { get; set; } + + public bool Keep14 { get; set; } + + public bool Keep15 { get; set; } + + public bool Keep16 { get; set; } + + public bool Keep17 { get; set; } + + public bool Keep18 { get; set; } + + public bool Keep19 { get; set; } + + public bool Keep20 { get; set; } + + public bool Keep21 { get; set; } + + public bool Keep22 { get; set; } + + public bool Keep23 { get; set; } + + public bool Keep24 { get; set; } + + public bool Keep25 { get; set; } + + public bool Keep26 { get; set; } + + public bool Keep27 { get; set; } + + public bool Keep28 { get; set; } + + public bool Keep29 { get; set; } + + public bool Keep30 { get; set; } + + public bool Keep31 { get; set; } + + public void Load(object value) + { + uint data = (uint)value; + ACC = Core.GetBitValue(data, 0); + Location = Core.GetBitValue(data, 1); + Latitude = Core.GetBitValue(data, 2); + Longitude = Core.GetBitValue(data, 3); + Operate = Core.GetBitValue(data, 4); + Encryption = Core.GetBitValue(data, 5); + Keep6 = Core.GetBitValue(data, 6); + Keep7 = Core.GetBitValue(data, 7); + Keep8 = Core.GetBitValue(data, 8); + Keep9 = Core.GetBitValue(data, 9); + OilRoad = Core.GetBitValue(data, 10); + ElectricityRoad = Core.GetBitValue(data, 11); + DoorLock = Core.GetBitValue(data, 12); + Keep13 = Core.GetBitValue(data, 13); + Keep14 = Core.GetBitValue(data, 14); + Keep15 = Core.GetBitValue(data, 15); + Keep16 = Core.GetBitValue(data, 16); + Keep17 = Core.GetBitValue(data, 17); + Keep18 = Core.GetBitValue(data, 18); + Keep19 = Core.GetBitValue(data, 19); + Keep20 = Core.GetBitValue(data, 20); + Keep21 = Core.GetBitValue(data, 21); + Keep22 = Core.GetBitValue(data, 22); + Keep23 = Core.GetBitValue(data, 23); + Keep24 = Core.GetBitValue(data, 24); + Keep25 = Core.GetBitValue(data, 25); + Keep26 = Core.GetBitValue(data, 26); + Keep27 = Core.GetBitValue(data, 27); + Keep28 = Core.GetBitValue(data, 28); + Keep29 = Core.GetBitValue(data, 29); + Keep30 = Core.GetBitValue(data, 30); + Keep31 = Core.GetBitValue(data, 31); + + } + + public object Save() + { + return Core.GetUIntBitValue(ACC, Location, Latitude, Longitude, Operate, Encryption, Keep6, Keep7, Keep8, Keep9, + OilRoad, ElectricityRoad, DoorLock, Keep13, Keep14, Keep15, Keep16, Keep17, Keep18, Keep19, Keep20, Keep21, Keep22, + Keep23, Keep24, Keep25, Keep26, Keep27, Keep28, Keep29, Keep30, Keep31); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientRegister.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientRegister.cs new file mode 100644 index 0000000..0afaf25 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientRegister.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace Beetle.JT808.Messages +{ + [MessageType(ID = 0x0100)] + public class ClientRegister + { + [UInt16Handler] + public ushort Province { get; set; } + [UInt16Handler] + public ushort City { get; set; } + [ASCIIHandler(5)] + public string Provider { get; set; } + [ASCIIHandler(8)] + public string DeviceNumber { get; set; } + [ASCIIHandler(7)] + public string DeviceID { get; set; } + [ByteHandler] + public byte Color { get; set; } + [GBKHandler] + public string PlateNumber { get; set; } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientRegisterCancel.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientRegisterCancel.cs new file mode 100644 index 0000000..0812b40 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientRegisterCancel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + [MessageType(NoBody = true, ID = 0x0003)] + public class ClientRegisterCancel + { + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientRegisterResponse.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientRegisterResponse.cs new file mode 100644 index 0000000..16a53d3 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientRegisterResponse.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + [MessageType(ID = 0x8100)] + public class ClientRegisterResponse + { + [UInt16Handler] + public ushort BusinessNO { get; set; } + [ByteHandler] + public byte Result { get; set; } + [GBKHandler] + public string Signature { get; set; } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientResponse.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientResponse.cs new file mode 100644 index 0000000..283a735 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientResponse.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + + public enum ResultType : byte + { + Success = 0, + Failure = 1, + Error = 2, + NotSupport = 3 + + } + [MessageType(ID = 0x0001)] + public class ClientResponse + { + [UInt16Handler] + public ushort BussinessNO { get; set; } + [UInt16Handler] + public ushort ResultID { get; set; } + [ByteHandler] + public ResultType Result { get; set; } + } + + +} diff --git a/Beetle.JT808/Beetle.JT808/Messages/ClientSignature.cs b/Beetle.JT808/Beetle.JT808/Messages/ClientSignature.cs new file mode 100644 index 0000000..fd4f03f --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Messages/ClientSignature.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Messages +{ + [MessageType(ID = 0x0102)] + public class ClientSignature + { + [GBKHandler] + public string Signature { get; set; } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Properties/AssemblyInfo.cs b/Beetle.JT808/Beetle.JT808/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..cb03f18 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Beetle.JT808")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Beetle.JT808")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +//将 ComVisible 设置为 false 将使此程序集中的类型 +//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("ad6a9116-9faf-475d-8451-4316f2df910f")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Beetle.JT808/Beetle.JT808/ProtocolProcessError.cs b/Beetle.JT808/Beetle.JT808/ProtocolProcessError.cs new file mode 100644 index 0000000..bf010dd --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/ProtocolProcessError.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + public class ProtocolProcessError : Exception + { + public ProtocolProcessError() + { + } + + public ProtocolProcessError(string error) : base(error) { } + + public ProtocolProcessError(string error, Exception e) : base(error, e) { } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Serializes/EmitHandler.cs b/Beetle.JT808/Beetle.JT808/Serializes/EmitHandler.cs new file mode 100644 index 0000000..8362a9e --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Serializes/EmitHandler.cs @@ -0,0 +1,532 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Serializes +{ + public class FieldHandler + { + public FieldHandler(FieldInfo field) + { + mGetValue = ReflectionHandlerFactory.FieldGetHandler(field); + mSetValue = ReflectionHandlerFactory.FieldSetHandler(field); + Field = field; + } + private FieldInfo mField; + public FieldInfo Field + { + get + { + return mField; + } + private set + { + mField = value; + } + } + private GetValueHandler mGetValue; + public GetValueHandler GetValue + { + get + { + return mGetValue; + } + + } + private SetValueHandler mSetValue; + public SetValueHandler SetValue + { + get + { + return mSetValue; + } + + } + } + public class PropertyHandler + { + public PropertyHandler(PropertyInfo property) + { + if (property.CanWrite) + mSetValue = ReflectionHandlerFactory.PropertySetHandler(property); + if (property.CanRead) + mGetValue = ReflectionHandlerFactory.PropertyGetHandler(property); + mProperty = property; + IndexProperty = mProperty.GetGetMethod().GetParameters().Length > 0; + } + private bool mIndexProperty; + public bool IndexProperty + { + get + { + return mIndexProperty; + } + set + { + mIndexProperty = value; + } + } + private PropertyInfo mProperty; + public PropertyInfo Property + { + get + { + return mProperty; + } + set + { + mProperty = value; + } + } + private GetValueHandler mGetValue; + public GetValueHandler Get + { + get + { + return mGetValue; + } + + } + private SetValueHandler mSetValue; + public SetValueHandler Set + { + get + { + return mSetValue; + } + + } + } + public class MethodHandler + { + public MethodHandler(MethodInfo method) + { + mExecute = ReflectionHandlerFactory.MethodHandler(method); + mInfo = method; + } + private MethodInfo mInfo; + public MethodInfo Info + { + get + { + return mInfo; + } + } + private FastMethodHandler mExecute; + public FastMethodHandler Execute + { + get + { + return mExecute; + } + } + } + public class InstanceHandler + { + public InstanceHandler(Type type) + { + mInstance = ReflectionHandlerFactory.InstanceHandler(type); + } + private ObjectInstanceHandler mInstance; + public ObjectInstanceHandler Instance + { + get + { + return mInstance; + } + } + } + public delegate object GetValueHandler(object source); + public delegate object ObjectInstanceHandler(); + public delegate void SetValueHandler(object source, object value); + public delegate object FastMethodHandler(object target, object[] paramters); + public class ReflectionHandlerFactory + { + + #region field handler + + private static Dictionary mFieldGetHandlers = new Dictionary(); + private static Dictionary mFieldSetHandlers = new Dictionary(); + public static GetValueHandler FieldGetHandler(FieldInfo field) + { + GetValueHandler handler; + if (mFieldGetHandlers.ContainsKey(field)) + { + handler = mFieldGetHandlers[field]; + } + else + { + lock (typeof(ReflectionHandlerFactory)) + { + if (mFieldGetHandlers.ContainsKey(field)) + { + handler = mFieldGetHandlers[field]; + } + else + { + handler = CreateFieldGetHandler(field); + mFieldGetHandlers.Add(field, handler); + } + + } + } + return handler; + } + private static GetValueHandler CreateFieldGetHandler(FieldInfo field) + { + DynamicMethod dm = new DynamicMethod("", typeof(object), new Type[] { typeof(object) }, field.DeclaringType); + ILGenerator ilGenerator = dm.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ldarg_0); + ilGenerator.Emit(OpCodes.Ldfld, field); + EmitBoxIfNeeded(ilGenerator, field.FieldType); + ilGenerator.Emit(OpCodes.Ret); + return (GetValueHandler)dm.CreateDelegate(typeof(GetValueHandler)); + } + public static SetValueHandler FieldSetHandler(FieldInfo field) + { + SetValueHandler handler; + if (mFieldSetHandlers.ContainsKey(field)) + { + handler = mFieldSetHandlers[field]; + } + else + { + lock (typeof(ReflectionHandlerFactory)) + { + if (mFieldSetHandlers.ContainsKey(field)) + { + handler = mFieldSetHandlers[field]; + } + else + { + handler = CreateFieldSetHandler(field); + mFieldSetHandlers.Add(field, handler); + } + } + } + return handler; + } + private static SetValueHandler CreateFieldSetHandler(FieldInfo field) + { + DynamicMethod dm = new DynamicMethod("", null, new Type[] { typeof(object), typeof(object) }, field.DeclaringType); + ILGenerator ilGenerator = dm.GetILGenerator(); + ilGenerator.Emit(OpCodes.Ldarg_0); + ilGenerator.Emit(OpCodes.Ldarg_1); + EmitCastToReference(ilGenerator, field.FieldType); + ilGenerator.Emit(OpCodes.Stfld, field); + ilGenerator.Emit(OpCodes.Ret); + return (SetValueHandler)dm.CreateDelegate(typeof(SetValueHandler)); + } + + #endregion + + #region Property Handler + + private static Dictionary mPropertyGetHandlers = new Dictionary(); + private static Dictionary mPropertySetHandlers = new Dictionary(); + public static SetValueHandler PropertySetHandler(PropertyInfo property) + { + SetValueHandler handler; + if (mPropertySetHandlers.ContainsKey(property)) + { + handler = mPropertySetHandlers[property]; + } + else + { + lock (typeof(ReflectionHandlerFactory)) + { + if (mPropertySetHandlers.ContainsKey(property)) + { + handler = mPropertySetHandlers[property]; + } + else + { + handler = CreatePropertySetHandler(property); + mPropertySetHandlers.Add(property, handler); + } + } + } + return handler; + } + private static SetValueHandler CreatePropertySetHandler(PropertyInfo property) + { + DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, null, new Type[] { typeof(object), typeof(object) }, property.DeclaringType.Module); + + ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); + + + ilGenerator.Emit(OpCodes.Ldarg_0); + + + ilGenerator.Emit(OpCodes.Ldarg_1); + + + EmitCastToReference(ilGenerator, property.PropertyType); + + + ilGenerator.EmitCall(OpCodes.Callvirt, property.GetSetMethod(), null); + + + ilGenerator.Emit(OpCodes.Ret); + + + SetValueHandler setter = (SetValueHandler)dynamicMethod.CreateDelegate(typeof(SetValueHandler)); + + return setter; + } + public static GetValueHandler PropertyGetHandler(PropertyInfo property) + { + GetValueHandler handler; + if (mPropertyGetHandlers.ContainsKey(property)) + { + handler = mPropertyGetHandlers[property]; + } + else + { + lock (typeof(ReflectionHandlerFactory)) + { + if (mPropertyGetHandlers.ContainsKey(property)) + { + handler = mPropertyGetHandlers[property]; + } + else + { + handler = CreatePropertyGetHandler(property); + mPropertyGetHandlers.Add(property, handler); + } + } + } + return handler; + } + private static GetValueHandler CreatePropertyGetHandler(PropertyInfo property) + { + + DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[] { typeof(object) }, property.DeclaringType.Module); + + ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); + + + ilGenerator.Emit(OpCodes.Ldarg_0); + + + ilGenerator.EmitCall(OpCodes.Callvirt, property.GetGetMethod(), null); + + + EmitBoxIfNeeded(ilGenerator, property.PropertyType); + + + ilGenerator.Emit(OpCodes.Ret); + + + GetValueHandler getter = (GetValueHandler)dynamicMethod.CreateDelegate(typeof(GetValueHandler)); + + return getter; + } + #endregion + + #region Method Handler + + private static Dictionary mMethodHandlers = new Dictionary(); + public static FastMethodHandler MethodHandler(MethodInfo method) + { + FastMethodHandler handler = null; + if (mMethodHandlers.ContainsKey(method)) + { + handler = mMethodHandlers[method]; + } + else + { + lock (typeof(ReflectionHandlerFactory)) + { + if (mMethodHandlers.ContainsKey(method)) + { + handler = mMethodHandlers[method]; + } + else + { + handler = CreateMethodHandler(method); + mMethodHandlers.Add(method, handler); + } + } + } + return handler; + } + private static FastMethodHandler CreateMethodHandler(MethodInfo methodInfo) + { + DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[] { typeof(object), typeof(object[]) }, methodInfo.DeclaringType.Module); + ILGenerator il = dynamicMethod.GetILGenerator(); + ParameterInfo[] ps = methodInfo.GetParameters(); + Type[] paramTypes = new Type[ps.Length]; + for (int i = 0; i < paramTypes.Length; i++) + { + if (ps[i].ParameterType.IsByRef) + paramTypes[i] = ps[i].ParameterType.GetElementType(); + else + paramTypes[i] = ps[i].ParameterType; + } + LocalBuilder[] locals = new LocalBuilder[paramTypes.Length]; + + for (int i = 0; i < paramTypes.Length; i++) + { + locals[i] = il.DeclareLocal(paramTypes[i], true); + } + for (int i = 0; i < paramTypes.Length; i++) + { + il.Emit(OpCodes.Ldarg_1); + EmitFastInt(il, i); + il.Emit(OpCodes.Ldelem_Ref); + EmitCastToReference(il, paramTypes[i]); + il.Emit(OpCodes.Stloc, locals[i]); + } + if (!methodInfo.IsStatic) + { + il.Emit(OpCodes.Ldarg_0); + } + for (int i = 0; i < paramTypes.Length; i++) + { + if (ps[i].ParameterType.IsByRef) + il.Emit(OpCodes.Ldloca_S, locals[i]); + else + il.Emit(OpCodes.Ldloc, locals[i]); + } + if (methodInfo.IsStatic) + il.EmitCall(OpCodes.Call, methodInfo, null); + else + il.EmitCall(OpCodes.Callvirt, methodInfo, null); + if (methodInfo.ReturnType == typeof(void)) + il.Emit(OpCodes.Ldnull); + else + EmitBoxIfNeeded(il, methodInfo.ReturnType); + + for (int i = 0; i < paramTypes.Length; i++) + { + if (ps[i].ParameterType.IsByRef) + { + il.Emit(OpCodes.Ldarg_1); + EmitFastInt(il, i); + il.Emit(OpCodes.Ldloc, locals[i]); + if (locals[i].LocalType.IsValueType) + il.Emit(OpCodes.Box, locals[i].LocalType); + il.Emit(OpCodes.Stelem_Ref); + } + } + + il.Emit(OpCodes.Ret); + FastMethodHandler invoder = (FastMethodHandler)dynamicMethod.CreateDelegate(typeof(FastMethodHandler)); + return invoder; + } + #endregion + + #region Instance Handler + + private static Dictionary mInstanceHandlers = new Dictionary(); + public static ObjectInstanceHandler InstanceHandler(Type type) + { + ObjectInstanceHandler handler; + if (mInstanceHandlers.ContainsKey(type)) + { + handler = mInstanceHandlers[type]; + } + else + { + lock (typeof(ReflectionHandlerFactory)) + { + if (mInstanceHandlers.ContainsKey(type)) + { + handler = mInstanceHandlers[type]; + } + else + { + handler = CreateInstanceHandler(type); + mInstanceHandlers.Add(type, handler); + } + } + } + return handler; + } + private static ObjectInstanceHandler CreateInstanceHandler(Type type) + { + DynamicMethod method = new DynamicMethod(string.Empty, type, null, type.Module); + ILGenerator il = method.GetILGenerator(); + il.DeclareLocal(type, true); + il.Emit(OpCodes.Newobj, type.GetConstructor(new Type[0])); + il.Emit(OpCodes.Stloc_0); + il.Emit(OpCodes.Ldloc_0); + il.Emit(OpCodes.Ret); + ObjectInstanceHandler creater = (ObjectInstanceHandler)method.CreateDelegate(typeof(ObjectInstanceHandler)); + return creater; + + } + #endregion + + + private static void EmitCastToReference(ILGenerator il, System.Type type) + { + if (type.IsValueType) + { + il.Emit(OpCodes.Unbox_Any, type); + } + else + { + il.Emit(OpCodes.Castclass, type); + } + } + private static void EmitBoxIfNeeded(ILGenerator il, System.Type type) + { + if (type.IsValueType) + { + il.Emit(OpCodes.Box, type); + } + } + private static void EmitFastInt(ILGenerator il, int value) + { + switch (value) + { + case -1: + il.Emit(OpCodes.Ldc_I4_M1); + return; + case 0: + il.Emit(OpCodes.Ldc_I4_0); + return; + case 1: + il.Emit(OpCodes.Ldc_I4_1); + return; + case 2: + il.Emit(OpCodes.Ldc_I4_2); + return; + case 3: + il.Emit(OpCodes.Ldc_I4_3); + return; + case 4: + il.Emit(OpCodes.Ldc_I4_4); + return; + case 5: + il.Emit(OpCodes.Ldc_I4_5); + return; + case 6: + il.Emit(OpCodes.Ldc_I4_6); + return; + case 7: + il.Emit(OpCodes.Ldc_I4_7); + return; + case 8: + il.Emit(OpCodes.Ldc_I4_8); + return; + } + + if (value > -129 && value < 128) + { + il.Emit(OpCodes.Ldc_I4_S, (SByte)value); + } + else + { + il.Emit(OpCodes.Ldc_I4, value); + } + } + } +} + diff --git a/Beetle.JT808/Beetle.JT808/Serializes/IBitType.cs b/Beetle.JT808/Beetle.JT808/Serializes/IBitType.cs new file mode 100644 index 0000000..ba53bd0 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Serializes/IBitType.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + + + + public interface IBitCustomType + { + void Load(object value); + + object Save(); + } +} diff --git a/Beetle.JT808/Beetle.JT808/Serializes/IReadWriteHandler.cs b/Beetle.JT808/Beetle.JT808/Serializes/IReadWriteHandler.cs new file mode 100644 index 0000000..8ab2997 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Serializes/IReadWriteHandler.cs @@ -0,0 +1,242 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + [AttributeUsage(AttributeTargets.Property)] + public abstract class ReadWriteHandlerAttribute : Attribute + { + public abstract object Read(IProtocolBuffer buffer); + + public abstract void Write(object value, IProtocolBuffer buffer); + } + + [AttributeUsage(AttributeTargets.Property)] + public class ByteHandler : ReadWriteHandlerAttribute + { + + public override object Read(IProtocolBuffer buffer) + { + return buffer.Read(); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.Write((byte)value); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class BytesHandler : ReadWriteHandlerAttribute + { + + public BytesHandler(int length) + { + Length = length; + } + public int Length { get; set; } + + public override object Read(IProtocolBuffer buffer) + { + return buffer.Read(Length); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.Write((byte[])value); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class UInt16Handler : ReadWriteHandlerAttribute + { + + public override object Read(IProtocolBuffer buffer) + { + return buffer.ReadUInt16(); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.Write((UInt16)value); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class UIntHandler : ReadWriteHandlerAttribute + { + + public override object Read(IProtocolBuffer buffer) + { + return buffer.ReadUInt(); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.Write((uint)value); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class ASCIIHandler : ReadWriteHandlerAttribute + { + public ASCIIHandler(int length) + { + Length = length; + } + public int Length { get; set; } + + public override object Read(IProtocolBuffer buffer) + { + return buffer.ReadASCII(Length).TrimStart(' '); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.WriteASCII((string)value, Length); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class CBDHandler : ReadWriteHandlerAttribute + { + public CBDHandler(int length) + { + Length = length; + } + + public int Length { get; set; } + + public override object Read(IProtocolBuffer buffer) + { + return buffer.ReadBCD(Length); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.WriteBCD((string)value); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class GBKHandler : ReadWriteHandlerAttribute + { + public GBKHandler(int length = -1) + { + Length = length; + } + + public int Length { get; set; } + + public override object Read(IProtocolBuffer buffer) + { + return buffer.ReadGBK(Length); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + buffer.WriteGBK((string)value); + } + } + + [AttributeUsage(AttributeTargets.Property)] + public class TimeBCD : ReadWriteHandlerAttribute + { + public override object Read(IProtocolBuffer buffer) + { + string value = buffer.ReadBCD(6); + int year = int.Parse("20" + value.Substring(0, 2)); + int month = int.Parse(value.Substring(2, 2)); + int day = int.Parse(value.Substring(4, 2)); + int hh = int.Parse(value.Substring(6, 2)); + int mm = int.Parse(value.Substring(8, 2)); + int ss = int.Parse(value.Substring(10, 2)); + return new DateTime(year, month, day, hh, mm, ss); + } + + public override void Write(object value, IProtocolBuffer buffer) + { + string time = ((DateTime)value).ToString("yyMMddHHmmss"); + buffer.WriteBCD(time); + } + } + + + [AttributeUsage(AttributeTargets.Property)] + public class ByteBitHandler : ReadWriteHandlerAttribute + { + public ByteBitHandler(Type type) + { + mType = type; + } + private Type mType; + + public override object Read(IProtocolBuffer buffer) + { + byte data = buffer.Read(); + IBitCustomType result = (IBitCustomType)Activator.CreateInstance(mType); + result.Load(data); + return result; + } + + public override void Write(object value, IProtocolBuffer buffer) + { + byte data = (byte)((IBitCustomType)value).Save(); + buffer.Write(data); + } + + } + + [AttributeUsage(AttributeTargets.Property)] + public class UInt16BitHandler : ReadWriteHandlerAttribute + { + public UInt16BitHandler(Type type) + { + mType = type; + } + private Type mType; + + public override object Read(IProtocolBuffer buffer) + { + UInt16 data = buffer.ReadUInt16(); + IBitCustomType result = (IBitCustomType)Activator.CreateInstance(mType); + result.Load(data); + return result; + } + + public override void Write(object value, IProtocolBuffer buffer) + { + UInt16 data = (UInt16)((IBitCustomType)value).Save(); + buffer.Write(data); + } + + } + + [AttributeUsage(AttributeTargets.Property)] + public class UIntBitHandler : ReadWriteHandlerAttribute + { + public UIntBitHandler(Type type) + { + mType = type; + } + private Type mType; + + public override object Read(IProtocolBuffer buffer) + { + uint data = buffer.ReadUInt(); + IBitCustomType result = (IBitCustomType)Activator.CreateInstance(mType); + result.Load(data); + return result; + } + + public override void Write(object value, IProtocolBuffer buffer) + { + uint data = (uint)((IBitCustomType)value).Save(); + buffer.Write(data); + } + + } +} diff --git a/Beetle.JT808/Beetle.JT808/Serializes/MessageTypeAttribute.cs b/Beetle.JT808/Beetle.JT808/Serializes/MessageTypeAttribute.cs new file mode 100644 index 0000000..5ef43f7 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Serializes/MessageTypeAttribute.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808 +{ + [AttributeUsage(AttributeTargets.Class)] + public class MessageTypeAttribute : Attribute + { + public MessageTypeAttribute() + { + NoBody = false; + } + + public ushort ID { get; set; } + + public bool NoBody { get; set; } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Serializes/PropertySerializeHandler.cs b/Beetle.JT808/Beetle.JT808/Serializes/PropertySerializeHandler.cs new file mode 100644 index 0000000..974e760 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Serializes/PropertySerializeHandler.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Serializes +{ + class PropertySerializeHandler + { + public PropertySerializeHandler(System.Reflection.PropertyInfo property, ReadWriteHandlerAttribute readwritehandler) + { + Handler = new PropertyHandler(property); + ReadWriteHandler = readwritehandler; + } + + public PropertyHandler Handler { get; private set; } + + public ReadWriteHandlerAttribute ReadWriteHandler { get; private set; } + + public void Read(object target, IProtocolBuffer buffer) + { + object value = ReadWriteHandler.Read(buffer); + Handler.Set(target, value); + } + + public void Write(object target, IProtocolBuffer buffer) + { + object value = Handler.Get(target); + if (value == null) + throw new ProtocolProcessError(string.Format("{0}.{1} value can't be null!", Handler.Property.DeclaringType, Handler.Property.PropertyType)); + ReadWriteHandler.Write(value, buffer); + } + } +} diff --git a/Beetle.JT808/Beetle.JT808/Serializes/Serializer.cs b/Beetle.JT808/Beetle.JT808/Serializes/Serializer.cs new file mode 100644 index 0000000..3881806 --- /dev/null +++ b/Beetle.JT808/Beetle.JT808/Serializes/Serializer.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.JT808.Serializes +{ + public class Serializer + { + + public Serializer(Type bodyType, MessageTypeAttribute msgType) + { + mBodyType = bodyType; + MessageType = msgType; + Init(); + } + + private Type mBodyType; + + private List mProperties = new List(); + + private void Init() + { + try + { + + + foreach (System.Reflection.PropertyInfo p in mBodyType.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)) + { + ReadWriteHandlerAttribute[] rwha = (ReadWriteHandlerAttribute[])p.GetCustomAttributes(typeof(ReadWriteHandlerAttribute), true); + if (rwha != null && rwha.Length > 0) + { + PropertySerializeHandler handler = new PropertySerializeHandler(p, rwha[0]); + mProperties.Add(handler); + } + } + } + catch (Exception e_) + { + throw new ProtocolProcessError(string.Format("{0} init error {1}", mBodyType.Name, e_.Message), e_); + } + } + + public MessageTypeAttribute MessageType { get; set; } + + public void Deserialize(object obj, IProtocolBuffer buffer) + { + if (obj is IMessageBody) + { + ((IMessageBody)obj).Load(buffer); + } + else + { + foreach (PropertySerializeHandler item in mProperties) + { + item.Read(obj, buffer); + } + } + } + + public void Serialize(object obj, IProtocolBuffer buffer) + { + if (obj is IMessageBody) + { + ((IMessageBody)obj).Save(buffer); + } + else + { + foreach (PropertySerializeHandler item in mProperties) + { + item.Write(obj, buffer); + } + } + } + + public object CreateObject() + { + return Activator.CreateInstance(mBodyType); + } + + } + + public class SerializerFactory + { + + private Dictionary mTypeSerializersMap = new Dictionary(); + + private Dictionary mIDSerializersMap = new Dictionary(); + + private void Register(Type type, MessageTypeAttribute msgType) + { + + Serializer serializer = new Serializer(type, msgType); + mTypeSerializersMap[type] = serializer; + mIDSerializersMap[serializer.MessageType.ID] = serializer; + } + + public Serializer Get(ushort id) + { + Serializer result = null; + mIDSerializersMap.TryGetValue(id, out result); + return result; + } + + public Serializer Get(Type type) + { + Serializer result = null; + mTypeSerializersMap.TryGetValue(type, out result); + return result; + } + + public static void Init() + { + SerializerFactory factory = Defalut; + } + + private static SerializerFactory mDefault = null; + + public static SerializerFactory Defalut + { + get + { + if (mDefault == null) + { + mDefault = new SerializerFactory(); + foreach (Type type in typeof(SerializerFactory).Assembly.GetTypes()) + { + MessageTypeAttribute[] mta = (MessageTypeAttribute[])type.GetCustomAttributes(typeof(MessageTypeAttribute), false); + if (mta != null && mta.Length > 0) + { + mDefault.Register(type, mta[0]); + } + } + } + return mDefault; + } + + } + } +} diff --git a/Beetle.JT808/Beetle.TJ808.BaseServer/App.config b/Beetle.JT808/Beetle.TJ808.BaseServer/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/Beetle.JT808/Beetle.TJ808.BaseServer/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.TJ808.BaseServer/Beetle.TJ808.BaseServer.csproj b/Beetle.JT808/Beetle.TJ808.BaseServer/Beetle.TJ808.BaseServer.csproj new file mode 100644 index 0000000..4e66309 --- /dev/null +++ b/Beetle.JT808/Beetle.TJ808.BaseServer/Beetle.TJ808.BaseServer.csproj @@ -0,0 +1,67 @@ + + + + + Debug + AnyCPU + {03EB1725-EC1F-4933-B0E9-E7F48A5B414D} + Exe + Properties + Beetle.TJ808.BaseServer + Beetle.TJ808.BaseServer + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\Lib\Beetle.Express.dll + + + + + + + + + + + + + + + + + + + {ad6a9116-9faf-475d-8451-4316f2df910f} + Beetle.JT808 + + + + + \ No newline at end of file diff --git a/Beetle.JT808/Beetle.TJ808.BaseServer/Program.cs b/Beetle.JT808/Beetle.TJ808.BaseServer/Program.cs new file mode 100644 index 0000000..16676f6 --- /dev/null +++ b/Beetle.JT808/Beetle.TJ808.BaseServer/Program.cs @@ -0,0 +1,91 @@ +using Beetle.JT808; +using Beetle.JT808.Messages; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Beetle.TJ808.BaseServer +{ + class Program : Beetle.Express.IServerHandler + { + private static Beetle.Express.IServer mServer; + + static void Main(string[] args) + { + Beetle.JT808.Serializes.SerializerFactory.Init(); + mServer = Beetle.Express.ServerFactory.CreateTCP(); + mServer.Port = 9091; + mServer.Handler = new Program(); + mServer.Open(false, false, false, 1, 1, "JT808"); + Console.WriteLine("server start!"); + Console.Read(); + } + + public void Connect(Express.IServer server, Express.ChannelConnectEventArgs e) + { + + } + + public void Disposed(Express.IServer server, Express.ChannelEventArgs e) + { + + } + + public void Error(Express.IServer server, Express.ErrorEventArgs e) + { + Console.WriteLine(e.Error.Message); + } + + public void Opened(Express.IServer server) + { + + } + + public void Receive(Express.IServer server, Express.ChannelReceiveEventArgs e) + { + IProtocolBuffer protocolbuffer; + if (e.Channel.Tag == null) + { + protocolbuffer = ProtocolBufferPool.Default.Pop(); + e.Channel.Tag = protocolbuffer; + } + else + { + protocolbuffer = (IProtocolBuffer)e.Channel.Tag; + + } + int offset = 0; int count = e.Data.Count; + while (count > 0) + { + int readcout = protocolbuffer.Import(e.Data.Array, 0, count); + if (readcout > 0) + { + Message message = MessageFactory.MessageFromBuffer(protocolbuffer); + ProtocolBufferPool.Default.Push(protocolbuffer); + protocolbuffer = ProtocolBufferPool.Default.Pop(); + e.Channel.Tag = protocolbuffer; + IProtocolBuffer result = MessageFactory.MessateToBuffer( + message.BussinessNO, message.SIM, (m, b) => + { + b.BussinessNO = message.BussinessNO; + b.ResultID = message.ID; + b.Result = ResultType.Success; + }); + Beetle.Express.Data data = new Express.Data(result.Array, result.Length); + data.Tag = result; + server.Send(data, e.Channel); + offset += readcout; + count -= readcout; + } + } + } + + public void SendCompleted(Express.IServer server, Express.ChannelSendEventArgs e) + { + IProtocolBuffer buffer = (IProtocolBuffer)e.Data.Tag; + ProtocolBufferPool.Default.Push(buffer); + } + } +} diff --git a/Beetle.JT808/Beetle.TJ808.BaseServer/Properties/AssemblyInfo.cs b/Beetle.JT808/Beetle.TJ808.BaseServer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9402025 --- /dev/null +++ b/Beetle.JT808/Beetle.TJ808.BaseServer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的常规信息通过以下 +// 特性集控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Beetle.TJ808.BaseServer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Beetle.TJ808.BaseServer")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 使此程序集中的类型 +// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, +// 则将该类型上的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("d70b0006-9a9b-440f-b870-6ba531c2bc2c")] + +// 程序集的版本信息由下面四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram b/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram new file mode 100644 index 0000000..19a76f1 --- /dev/null +++ b/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram.layout b/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram.layout new file mode 100644 index 0000000..19a76f1 --- /dev/null +++ b/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram.layout @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram.suppressions b/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram.suppressions new file mode 100644 index 0000000..19a76f1 --- /dev/null +++ b/Beetle.JT808/ModelingProject1/LayerDiagram1.layerdiagram.suppressions @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Beetle.JT808/ModelingProject1/ModelDefinition/ModelingProject1.uml b/Beetle.JT808/ModelingProject1/ModelDefinition/ModelingProject1.uml new file mode 100644 index 0000000..67d418c --- /dev/null +++ b/Beetle.JT808/ModelingProject1/ModelDefinition/ModelingProject1.uml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + diff --git a/Beetle.JT808/ModelingProject1/ModelingProject1.modelproj b/Beetle.JT808/ModelingProject1/ModelingProject1.modelproj new file mode 100644 index 0000000..6a00299 --- /dev/null +++ b/Beetle.JT808/ModelingProject1/ModelingProject1.modelproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {3217fbd5-5679-4dd6-9c43-e1c1735e97df} + 2.0.0.0 + ModelingProject1 + ModelingProject1 + + + bin\Debug\ + + + bin\Release\ + + + 10.0 + $(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + Content + + + + + Content + + + Content + LayerDiagram1.layerdiagram + + + Content + LayerDiagram1.layerdiagram + + + \ No newline at end of file diff --git a/Lib/Beetle.Express.dll b/Lib/Beetle.Express.dll new file mode 100644 index 0000000..29ec43e Binary files /dev/null and b/Lib/Beetle.Express.dll differ