物聯網涉及到各種設備、各種傳感器、各種數據源、各種協議,並且很難統一,那麼就要有一個結構性的框架解決這些問題。SSIO就是根據時代發展的階段和現實實際情況的結合產物。
各種數據信息,如下圖:
解決方案,配合SIO使用:
一、SSIO特點
二、SSIO發布到NuGet平台
三、搜索SSIO
四、安裝SSIO
五、事例代碼(Demo)
Demo下載地址:https://github.com/wxzz/ServerSuperIO/tree/2.0
1.客戶端(發送文件)
static void SendFile() { if (!System.IO.File.Exists(_file)) { Console.WriteLine("文件不存在:"+_file); return; } FileStream fs = null; try { Console.WriteLine("開始傳輸>>"); string fileName=DateTime.Now.ToString("yyMMddHHmmss") + ".txt"; int bufferSize = _sendBufferSize; byte[] sendBuffer = new byte[bufferSize]; fs = new FileStream(_file, FileMode.Open,FileAccess.Read,FileShare.Read); long length = fs.Length; int count = 0; Stopwatch watch = new Stopwatch(); watch.Start(); while (length > 0) { int sendNum = fs.Read(sendBuffer, 0, sendBuffer.Length); byte[] package = GetDataPackage(fileName,sendBuffer, sendNum); count+=_tcpClient.Client.Send(package, 0, package.Length, SocketFlags.None); length -= sendNum; float percent = ((fs.Length - length)/(float) fs.Length)*100.0f; Console.WriteLine("已傳:" + percent.ToString("0.00") + "%"); } watch.Stop(); Console.WriteLine("傳輸完畢!總數:" + count.ToString()+",耗時:"+ watch.Elapsed.TotalSeconds.ToString(CultureInfo.InvariantCulture)); } catch { throw; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } } } static byte[] GetDataPackage(string fileName,byte[] sendBuffer, int sendNum) { byte[] sendPackage = new byte[sendNum + 24]; sendPackage[0] = 0x35; sendPackage[1] = 0x35; string code = "0001"; byte[] codeBytes = System.Text.Encoding.ASCII.GetBytes(code); Buffer.BlockCopy(codeBytes, 0, sendPackage, 2, 4); byte[] fileBytes= System.Text.Encoding.ASCII.GetBytes(fileName); Buffer.BlockCopy(fileBytes, 0, sendPackage, 6, 16); Buffer.BlockCopy(sendBuffer, 0, sendPackage, 22, sendNum); sendPackage[sendPackage.Length - 2] = 0x33; sendPackage[sendPackage.Length - 1] = 0x33; return sendPackage; }
2.設備驅動
//設備驅動 public class ReceiveFileDriver:RunDevice { private Dynamic _Dyn; private Parameter _Parameter; private Protocol _Protocol; public ReceiveFileDriver() : base() { _Dyn = new Dynamic(); _Parameter = new Parameter(); _Protocol = new Protocol(); } public override void Initialize(int devid) { this.Protocol.InitDriver(this, new FixedHeadAndEndReceiveFliter(TransFileDriver.Protocol.Head, TransFileDriver.Protocol.End)); //初始化協議驅動 } //省略...... } //協議驅動,並處理數據 public class Command : ProtocolCommand { public Command() { } public override string Name { get { return "writefile"; } } public override object Analysis(byte[] data, object obj) { try { //count += data.Length - 24; //Console.WriteLine(count.ToString()+","+data[0].ToString() + "," + data[data.Length - 1].ToString()); string path = Path.Combine(Environment.CurrentDirectory, "rev"); if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } string fileName = System.Text.Encoding.ASCII.GetString(data, 6, 16); path=Path.Combine(path, fileName); using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { fs.Seek(fs.Length, SeekOrigin.Current); byte[] content = new byte[data.Length - 24]; Buffer.BlockCopy(data, 22, content, 0, content.Length); fs.Write(content, 0, content.Length); fs.Flush(); } } catch { return -1; } return 0; } public override byte[] Package(string code, object obj) { throw new NotImplementedException(); } }
3.宿主程序
static void Main(string[] args) { ReceiveFileDriver dev = new ReceiveFileDriver(); dev.DeviceParameter.DeviceName = "設備4"; dev.DeviceParameter.DeviceAddr = 0; dev.DeviceParameter.DeviceCode = "0001"; dev.DeviceParameter.DeviceID = 0; dev.DeviceDynamic.DeviceID = 0; dev.DeviceParameter.NET.RemoteIP = "127.0.0.1"; dev.DeviceParameter.NET.RemotePort = 9600; dev.CommunicateType = CommunicateType.NET; dev.Initialize(0); IServer server = new ServerFactory().CreateServer(new ServerConfig() { ServerName = "接收文件服務", ListenPort = 6699, NetReceiveBufferSize = 2048, ControlMode = ControlMode.Self, SocketMode = SocketMode.Tcp, DeliveryMode = DeliveryMode.DeviceCode, StartReceiveDataFliter = true, ClearSocketSession = false, }); server.AddDeviceCompleted += server_AddDeviceCompleted; server.DeleteDeviceCompleted += server_DeleteDeviceCompleted; server.Start(); server.AddDevice(dev); while ("exit" == Console.ReadLine()) { server.Stop(); } }
六、實驗效果
兩天的時間,將近3GB的數據信息,穩定性、擴展性都非常不錯。