Exemple #1
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="agent"></param>
 /// <param name="notifyEvent"></param>
 /// <param name="msg"></param>
 private void OnNotifyAtUIThread(DataAgent agent, NotifyEvents ne, Notify p)
 {
     string msg = string.Format("{0}: {1}", DateTime.Now, p.Message);
     if (ne == NotifyEvents.UploadFileOK)
     {
         fileUploadInfoListBox.Items.Add(msg);
     }
     else if (ne == NotifyEvents.UploadFileFailed)
     {
         fileUploadInfoListBox.Items.Add(msg);
     }
     else if (ne == NotifyEvents.DebugMessage)
     {
         this.debugConsole.Text += string.Format("{0}\n", p.Message);
     }
     else if (ne == NotifyEvents.EventMessage)
     {
         mainListBox.Items.Add(msg);
     }
     else if (ne == NotifyEvents.SendDataOK)
     {
         this.UpdateSendDataRecord(p.DeviceKey, false);
     }
     else if (ne == NotifyEvents.SendDataFailed)
     {
         //this.UpdateSendDataRecord(p.DeviceKey, false);
     }
     else if (ne == NotifyEvents.HistoryData)
     {
         this.HandleHistoryData(p.Payload);
     }
 }
Exemple #2
0
 private void OnNotify(DataAgent agent, NotifyEvents notifyEvent, Notify p)
 {
     this.SafeInvoke(() =>
     {
         this.OnNotifyAtUIThread(agent, notifyEvent, p);
     });
 }
Exemple #3
0
        /// <summary>
        /// Upload File
        /// </summary>
        /// <param name="packet"></param>
        internal bool 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);

                // 上传失败,移除占用标志
                RemoveOccupiedToken(packet);

                return(false);
            }

            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 folder = GetPacketSID(packet);

                var param = "";
                try
                {
                    param = this.GetHpGeParams(packet.Path);
                }
                catch (Exception)
                {
                    RemoveOccupiedToken(packet);
                    return(false);
                }

                param     = param.Replace('/', '-');
                uploadUrl = this.GetUploadApi(packet.FileType, folder, param);
            }

            Uri uri = new Uri(this.DataCenter.GetUrl(uploadUrl));

            try
            {
                using (WebClient wc = new WebClient())
                {
                    // 同步上传
                    Byte[] result    = wc.UploadFile(uri, Post, packet.Path);
                    string strResult = Encoding.UTF8.GetString(result);
                    string msg       = string.Format("成功上传 {0},信息 {1}", this.GetRelFilePath(packet), strResult);
                    this.NotifyEvent(this, NotifyEvents.UploadFileOK, new Notify()
                    {
                        Message = msg
                    });

                    return(true);
                }
            }
            catch (WebException e)
            {
                // 上传失败,移除占用标志
                RemoveOccupiedToken(packet);

                string msg = string.Format("错误上传 {0},错误信息 {1}", this.GetRelFilePath(packet), e.Message);
                this.NotifyEvent(this, NotifyEvents.UploadFileFailed, new Notify()
                {
                    Message = msg
                });

                return(false);
            }
        }
Exemple #4
0
        /// <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)
            {
            }
        }
Exemple #5
0
        /// <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)
            {
             
            }
        }
Exemple #6
0
 private void HandleHistoryData(string device, string start, string end, string times)
 {
     Notify n = new Notify();
     n.SetValue("device", device);
     n.SetValue("start", start);
     n.SetValue("end", end);
     n.SetValue("times", times);
     this.NotifyEvent(this, NotifyEvents.HistoryData, n);
 }