internal Packet CombinePacket(Packet packet1, Packet packet2) { if (packet1 != null) { packet1.AppendEntry(packet2.GetEntry()); return packet1; } return packet2; }
/* private string RenameFileNameForUpload(string fileName) { string basePath = Path.GetDirectoryName(fileName); int fus = fileName.IndexOf('_'); string partFileName = fileName.Substring(fus + 1); string filePath = string.Format("{0}\\S{1}_{2}", basePath, Settings.Instance.Station, partFileName); File.Move(fileName, filePath); return filePath; } */ internal Packet GetFilePacket(string fileName, string fileType) { if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName)) { Packet packet = new Packet(this.Token); packet.Path = fileName; packet.FileType = fileType; packet.IsFilePacket = true; return packet; } return null; }
internal Packet GetFilePacket(string fileName) { Packet packet = new Packet("A", ""); packet.Path = fileName; return packet; }
private void Send(string api, Packet packet, DateTime time) { try { Uri uri = new Uri(this.GetUrl(api)); byte[] data = Encoding.ASCII.GetBytes(packet.ToString()); WebClient wc = new WebClient(); wc.UploadDataCompleted += (object sender, UploadDataCompletedEventArgs e) => { if (e.Error != null) { return; } Packet p = (Packet)e.UserState; if (p != null) { string result = Encoding.ASCII.GetString(e.Result); // TODO: with result } }; wc.UploadDataAsync(uri, Post, data, packet); } catch (Exception e) { } }
internal void SendReplyPacket(Packet p, DateTime time) { string s = p.ToString(); // this.Send(Encoding.ASCII.GetBytes(s)); }
internal void SendPacket(Packet p) { this.SendDataPacket(p, default(DateTime)); }
internal void SendFilePacket(Packet packet) { Uri uri = new Uri(this.GetUrl("data/upload")); try { using (WebClient wc = new WebClient()) { wc.UploadFileCompleted += (object sender, UploadFileCompletedEventArgs e) => { if (e.Error != null) { return; } Packet p = (Packet)e.UserState; if (p != null) { // TODO: with p.Path } }; wc.UploadFileAsync(uri, Post, packet.Path, packet); } } catch (WebException) { } }
/// <summary> /// Upload Data Implements /// </summary> /// <param name="api"></param> /// <param name="packet"></param> /// <param name="time"></param> private void Send(string api, Packet packet, DateTime time) { try { Uri uri = new Uri(api); byte[] data = Encoding.ASCII.GetBytes(packet.ToString()); using (WebClient wc = new WebClient()) { wc.UploadDataCompleted += (object sender, UploadDataCompletedEventArgs e) => { if (e.Error != null) { this.NotifyEvent(this, NotifyEvents.SendDataOK, new Notify() { DeviceKey = packet.DeviceKey, Message = e.Error.Message }); this.HandleWebException(e.Error); return; } Packet p = (Packet)e.UserState; if (p != null) { string result = Encoding.ASCII.GetString(e.Result); result = result.Trim(); if (!string.IsNullOrEmpty(result)) { this.NotifyEvent(this, NotifyEvents.SendDataOK, new Notify() { DeviceKey = packet.DeviceKey, Message = result }); } } }; wc.UploadDataAsync(uri, "POST", data, packet); } } catch (Exception e) { } }
public void Commit(Packet packet) { byte[] data = ASCIIEncoding.UTF8.GetBytes(packet.ToString()); this.Send("data/commit", data); }
internal Packet GetReplyCommandPacket() { Packet packet = new Packet(this.Token); return packet; }
/// <summary> /// Upload Data Entry /// </summary> /// <param name="packet"></param> /// <param name="time"></param> internal void SendDataPacket(Packet packet, DateTime time) { this.Send(this.DataCenter.GetUrl("data/commit"), packet, time); }
private string GetRelFilePath(Packet packet) { if (packet.FileType.Equals("labr", StringComparison.OrdinalIgnoreCase)) { string fileName = Path.GetFileName(packet.Path); return string.Format("{0}", fileName); } else if (packet.FileType.Equals("hpge", StringComparison.OrdinalIgnoreCase)) { string fileName = Path.GetFileName(packet.Path); string path = Path.GetDirectoryName(packet.Path); var sidFolder = Path.GetFileName(path); if (fileName.StartsWith("!")) { return string.Format("{0}\\{1}", sidFolder, fileName.Remove(0, 1)); } return string.Format("{0}\\{1}", sidFolder, fileName); } return string.Empty; }
/// <summary> /// Upload File /// </summary> /// <param name="packet"></param> internal void SendFilePacket(Packet packet) { if (string.IsNullOrEmpty(packet.Path) || !File.Exists(packet.Path)) { Notify msg = new Notify(); msg.Message = "No File Found"; this.NotifyEvent(this, NotifyEvents.EventMessage, msg); return; } string uploadUrl = string.Empty; if (packet.FileType.Equals("labr", StringComparison.OrdinalIgnoreCase)) { string path = Path.GetDirectoryName(packet.Path); var folder1 = Path.GetFileName(Path.GetDirectoryName(path)); var folder2 = Path.GetFileName(path); uploadUrl = this.GetUploadApi(packet.FileType, folder1, folder2); } else if (packet.FileType.Equals("hpge", StringComparison.OrdinalIgnoreCase)) { var folder = DataSource.GetCurrentSid(); var param = this.GetHpGeParams(packet.Path); // "2014-07-04 00:00:00,2014-07-04 00:00:00,2014-07-04 00:00:00,PRT"; param = param.Replace('/', '-'); uploadUrl = this.GetUploadApi(packet.FileType, folder, param); } Uri uri = new Uri(this.DataCenter.GetUrl(uploadUrl)); try { using (WebClient wc = new WebClient()) { wc.UploadFileCompleted += (object sender, UploadFileCompletedEventArgs e) => { Packet p = (Packet)e.UserState; if (p != null) { if (e.Error == null) { string result = Encoding.UTF8.GetString(e.Result); this.RemovePrefix(p.Path); // LogPath.GetDeviceLogFilePath(""); string msg = string.Format("成功上传 {0}", this.GetRelFilePath(packet)); this.NotifyEvent(this, NotifyEvents.UploadFileOK, new Notify() { Message = msg }); //this.NotifyEvent(this, NotifyEvents.UploadFileOK, new PacketBase() { Message = msg }); } else { this.NotifyEvent(this, NotifyEvents.UploadFileFailed, new Notify() { Message = e.Error.Message }); } } }; wc.UploadFileAsync(uri, Post, packet.Path, packet); } } catch (WebException) { } }
internal Packet GetPacket(string deviceKey, Dictionary<string, object> data, bool p) { Packet packet = new Packet("A", ""); packet.AddData(deviceKey, data); return packet; }
internal void SendDataPacket(Packet packet, DateTime time) { this.Send("data/commit", packet, time); }
internal Packet GetReplyCommandPacket() { Packet packet = new Packet("A", ""); return packet; }
/// <summary> /// Send Dispatcher /// </summary> /// <param name="p"></param> internal void SendPacket(Packet p) { if (!p.IsFilePacket) { this.SendDataPacket(p, default(DateTime)); } else { this.SendFilePacket(p); } }