Пример #1
0
        /// <summary>
        /// 升级传感器
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public string UpdateFirmware(Stream stream)
        {
            UpdateFirmwareTaskModel param  = new UpdateFirmwareTaskModel();
            UploadResult            result = new UploadResult();

            try
            {
                StreamReader sr = new StreamReader(stream);

                string content = sr.ReadToEnd();
                sr.Dispose();
                NameValueCollection nvc = HttpUtility.ParseQueryString(content);
                param = Json.Parse <UpdateFirmwareTaskModel>(nvc["content"]);



                if (iCMS.WG.AgentServer.Common.syncTools == null)
                {
                    Common.Init();
                }
                Common.asyncTools.AddCmd(param);
                result.Result = 0;
            }
            catch
            {
                result.Result = 1;
                result.Reason = "发生错误无法受理请求";
            }

            return(Json.Stringify(result));
        }
        public object doOperator()
        {
            try
            {
                iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "准备升级");
                CommunicationWithServer communication2Server = new CommunicationWithServer();
                if (ComFunction.WSUpdatingInfo.UpdatingAllWSInfo.Count == 0)
                {
                    UpdateFirmwareTaskModel model = (updateModel as UpdateFirmwareTaskModel);
                    iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "开始验证升级数据包...");
                    if (!ComFunction.JudgeLegitimacyOfUpgradeFile(model.updateFile))
                    {
                        foreach (string mac in model.macList)
                        {
                            communication2Server.UploadConfigResponse(mac, Enum_ConfigType.ConfigType_UpdateFirmware, Convert.ToInt32(EnmuReceiveStatus.Faild), "升级信息非法,退出对该WS的升级");
                            iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), mac + " 升级信息非法,退出对该WS的升级");
                        }
                        return(false);
                    }
                    iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "升级数据包符合要求");
                    tFirmware firmware;
                    try
                    {
                        firmware = new tFirmware(model.updateFile);
                    }
                    catch
                    {
                        return(null);
                    }

                    iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "开始:PreludeUpdate ");
                    if (ComFunction.meshAdapter.PreludeUpdate())
                    {
                        iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "PreludeUpdate 完成 ");
                        lock (ComFunction.WSUpdatingInfo)
                        {
                            foreach (string mac in model.macList)
                            {
                                if (!string.IsNullOrWhiteSpace(mac))
                                {
                                    ComFunction.WSUpdatingInfo.UpdatingAllWSInfo.Add(mac, null);
                                }
                            }
                        }

                        List <tMAC> tempMACList = model.macList
                                                  .Where(macInfo => !string.IsNullOrWhiteSpace(macInfo))
                                                  .Select(macInfo => new tMAC(macInfo))
                                                  .ToList();
                        if (ComFunction.meshAdapter.Update(tempMACList, firmware))
                        {
                            if (model.CommandSuccessed != null)
                            {
                                model.CommandSuccessed();
                            }

                            iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "Update成功,开始升级已选择的WS ");
                        }
                        else
                        {
                            iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "Update失败,退出升级");

                            if (model.CommandFailed != null)
                            {
                                model.CommandFailed();
                            }

                            lock (ComFunction.WSUpdatingInfo)
                            {
                                foreach (string mac in model.macList)
                                {
                                    iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(),
                                                                            " Update失败,无法升级,退出对该WS的升级 :" + mac);
                                    communication2Server.UploadConfigResponse(mac, Enum_ConfigType.ConfigType_UpdateFirmware, Convert.ToInt32(EnmuReceiveStatus.Faild), "Update失败,退出对该WS的升级");
                                }
                                try
                                {
                                    ComFunction.WSUpdatingInfo.UpdatingAllWSInfo.Clear();
                                    if (ComFunction.WSUpdatingInfo.UpdateAllWSTimer != null)
                                    {
                                        ComFunction.WSUpdatingInfo.UpdateAllWSTimer.Stop();
                                        ComFunction.WSUpdatingInfo.UpdateAllWSTimer.Close();
                                        ComFunction.WSUpdatingInfo.UpdateAllWSTimer.Dispose();
                                        ComFunction.WSUpdatingInfo.UpdateAllWSTimer = null;
                                    }
                                }
                                catch
                                {
                                }
                            }

                            iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "开始 PostludeUpdate");
                            ComFunction.meshAdapter.PostludeUpdate();
                            iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "开始 PostludeUpdate 完成");
                            return(false);
                        }
                    }
                    else
                    {
                        iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "PreludeUpdate 失败");

                        if (model.CommandFailed != null)
                        {
                            model.CommandFailed();
                        }

                        lock (ComFunction.WSUpdatingInfo)
                        {
                            foreach (string mac in model.macList)
                            {
                                iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(),
                                                                        " PreludeUpdate 失败,网络异常无法升级,退出对该WS的升级 :" + mac);
                                communication2Server.UploadConfigResponse(mac, Enum_ConfigType.ConfigType_UpdateFirmware, Convert.ToInt32(EnmuReceiveStatus.Faild), "PreludeUpdate 失败,退出对该WS的升级");
                            }
                            try
                            {
                                ComFunction.WSUpdatingInfo.UpdatingAllWSInfo.Clear();
                                if (ComFunction.WSUpdatingInfo.UpdateAllWSTimer != null)
                                {
                                    ComFunction.WSUpdatingInfo.UpdateAllWSTimer.Stop();
                                    ComFunction.WSUpdatingInfo.UpdateAllWSTimer.Close();
                                    ComFunction.WSUpdatingInfo.UpdateAllWSTimer.Dispose();
                                    ComFunction.WSUpdatingInfo.UpdateAllWSTimer = null;
                                }
                            }
                            catch
                            {
                            }
                        }

                        iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "开始 PostludeUpdate!");
                        ComFunction.meshAdapter.PostludeUpdate();
                        iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "PostludeUpdate 完成!");
                        return(false);
                    }
                }
                else
                {
                    iCMS.WG.Agent.Common.LogHelper.WriteLog(iCMS.WG.Agent.Common.Enum.EnumLogType.Agent2WS.ToString(), "当前有WS正在进行升级");
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
            return(null);
        }