From d6cea4d302f8b5337ee97dfae38b8992555b0721 Mon Sep 17 00:00:00 2001 From: Nicola Carminati Date: Tue, 19 Mar 2019 18:12:34 +0100 Subject: [PATCH] Export of Alarm History --- Client/View/MainForm.cs | 7 +++ .../AlarmModels/DTOAlarmHistoricModel.cs | 19 ++++++- Step/Controllers/WebApi/ApiAlarmController.cs | 50 +++++++++++++++++++ .../alarms/components/alarm-history.ts | 22 ++++++++ .../alarms/components/alarm-history.vue | 2 +- .../tooling/components/tooling-equipment.ts | 1 - Step/wwwroot/src/services/alarmsService.ts | 27 +++++++++- 7 files changed, 123 insertions(+), 5 deletions(-) diff --git a/Client/View/MainForm.cs b/Client/View/MainForm.cs index a5b1ae48..32863d46 100644 --- a/Client/View/MainForm.cs +++ b/Client/View/MainForm.cs @@ -221,6 +221,7 @@ namespace CMS_Client.View Browser.KeyboardHandler.OnKeyEvent += BrowserKeyPress; Browser.ContextMenuHandler.OnBeforeContextMenu += BrowserContextMenu; Browser.DisplayHandler.OnConsoleMessage += BrowserConsoleMessage; + Browser.DownloadHandler.OnBeforeDownload += BeforeDownload; //Filter only < Win_10 Platform if (Config.ClientConfig.ShowVirtualKeyboard && Environment.OSVersion.Version.Major < 10) ChromiumWebBrowser.RemoteProcessCreated += (e) => { e.RenderProcessHandler.OnFocusedNodeChanged += BrowserNodeChanged; }; @@ -231,6 +232,12 @@ namespace CMS_Client.View /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region BROWSER_EVENT_HANDLERS_METHODS + + private void BeforeDownload(object sender, CfxOnBeforeDownloadEventArgs e) + { + e.Callback.Continue("",true); + } + //On browser Load-Error. Event Handler called by Browser private void BrowserLoadsError(object sender, CfxOnLoadErrorEventArgs e) { diff --git a/Step.Model/DTOModels/AlarmModels/DTOAlarmHistoricModel.cs b/Step.Model/DTOModels/AlarmModels/DTOAlarmHistoricModel.cs index 5e4d8c4c..b13ff223 100644 --- a/Step.Model/DTOModels/AlarmModels/DTOAlarmHistoricModel.cs +++ b/Step.Model/DTOModels/AlarmModels/DTOAlarmHistoricModel.cs @@ -35,8 +35,8 @@ namespace Step.Model.DTOModels.AlarmModels bool[] statusBits = new BitArray(new int[] { obj.Processes }) .Cast() - .ToArray(); - + .ToArray(); + for (int i = 0; i < 8; i++) { if (statusBits[i]) @@ -56,6 +56,21 @@ namespace Step.Model.DTOModels.AlarmModels Users = obj.Users.Select(x => x.UserId).ToList() }; } + public string ToCsvString(String alarm) + { + var alarmCode = 0; + var alarmText = alarm; + + if (Source == ALARM_SOURCE.NC) + { + alarmCode = AlarmId & 0xFFFFFF; + alarmText = Title.Replace(System.Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " ").Replace(";", ","); + } + else + alarmCode = AlarmId; + + return alarmCode + ";" + alarmText + ";" + Source + ";" + Type + ";" + String.Join("|", Processes.Select(x => x.ToString()).ToArray()) + ";" + TimeStamp + ";" + String.Join("|", Users.Select(x => x.ToString()).ToArray()); + } } public class DTOAlarmsFilterModel diff --git a/Step/Controllers/WebApi/ApiAlarmController.cs b/Step/Controllers/WebApi/ApiAlarmController.cs index 11866315..1717bbe4 100644 --- a/Step/Controllers/WebApi/ApiAlarmController.cs +++ b/Step/Controllers/WebApi/ApiAlarmController.cs @@ -9,7 +9,9 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Security.Claims; +using System.Text; using System.Threading.Tasks; using System.Web.Http; using static Step.Model.Constants; @@ -41,6 +43,54 @@ namespace Step.Controllers.WebApi Pages = pages }); } + } + + [Route("export"), HttpPost] + public IHttpActionResult ExportAlarms([FromBody]DTOAlarmsFilterModel filter) + { + if (!ModelState.IsValid) + return BadRequest(ModelState); + + Dictionary plcMessages = LanguageController.GetPlcAlarmsTranslations(filter.Language) + .ToDictionary( + x => Convert.ToInt32(x.Key.Split('_').Last()), // This function return "alarm_id" as id, i need only the id number + x => x.Value + ); + + using (AlarmsController alarm = new AlarmsController()) + { + List alarms = alarm.GetPaginatedWithFilter(filter.Title, filter.Sources, 0, int.MaxValue, filter.StartDate.Value, filter.EndDate, filter.UserIds, plcMessages, out int pages); + + Dictionary AlmPLCTras = LanguageController.GetPlcAlarmsTranslations(filter.Language); + + string csv = ""; + foreach (DTOAlarmHistoricModel model in alarms) + { + var alm = ""; + if(model.Source == ALARM_SOURCE.PLC) + alm = AlmPLCTras["alarm_"+model.AlarmId]; + csv += model.ToCsvString(alm) + Environment.NewLine; + } + + + + var stream = new MemoryStream(Encoding.UTF8.GetBytes(csv),0, Encoding.UTF8.GetBytes(csv).Length, false,true); + + var result = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new ByteArrayContent(stream.GetBuffer()) + }; + result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") + { + FileName = "AlarmExport.csv" + }; + result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv"); + + return ResponseMessage(result); + + + } + } [Route("data"), HttpPost] diff --git a/Step/wwwroot/src/app_modules/alarms/components/alarm-history.ts b/Step/wwwroot/src/app_modules/alarms/components/alarm-history.ts index f292786d..eee2b566 100644 --- a/Step/wwwroot/src/app_modules/alarms/components/alarm-history.ts +++ b/Step/wwwroot/src/app_modules/alarms/components/alarm-history.ts @@ -179,6 +179,28 @@ export default class AlarmHistory extends Vue { this.notes = await awaiter(alarmsService.getNote(this.selectedAlarm.alarmId, this.selectedAlarm.source)); } + async exportAlarms(){ + let $this = this; + let from = this.filter.interval && this.filter.interval.length ? this.filter.interval[0] : null; + let to = this.filter.interval && this.filter.interval.length ? this.filter.interval[1] : null; + + let arraySourceFilter: Array = []; + let arrayUserFilter: Array = []; + + this.filter.sources.forEach(function (item) { + arraySourceFilter.push(item.id); + }); + + this.filter.multiUser.forEach(function (item) { + $this.users.forEach(function (items) { + if (items.username == item) { + arrayUserFilter.push(items.id); + } + }) + }) + await awaiter(alarmsService.exportAlarms(this.filter.title, arraySourceFilter, arrayUserFilter, from, to)); + } + updateLimitation() { if (this.limitationList > 3) this.limitationList = 3; diff --git a/Step/wwwroot/src/app_modules/alarms/components/alarm-history.vue b/Step/wwwroot/src/app_modules/alarms/components/alarm-history.vue index efd8011b..584db813 100644 --- a/Step/wwwroot/src/app_modules/alarms/components/alarm-history.vue +++ b/Step/wwwroot/src/app_modules/alarms/components/alarm-history.vue @@ -82,7 +82,7 @@
-
diff --git a/Step/wwwroot/src/app_modules/tooling/components/tooling-equipment.ts b/Step/wwwroot/src/app_modules/tooling/components/tooling-equipment.ts index 629ee2dd..3b1b9ef1 100644 --- a/Step/wwwroot/src/app_modules/tooling/components/tooling-equipment.ts +++ b/Step/wwwroot/src/app_modules/tooling/components/tooling-equipment.ts @@ -749,7 +749,6 @@ public isEquipmentSelected(offset){ () => { if(this.isSiemens){ awaiter(new ToolingService().DeleteEdgeData(tool, model).then(response => { - this.selectedEquipment = null; this.enableOffsets = false; this.enableParameters = true; diff --git a/Step/wwwroot/src/services/alarmsService.ts b/Step/wwwroot/src/services/alarmsService.ts index 97fa6e3a..be4e0f0c 100644 --- a/Step/wwwroot/src/services/alarmsService.ts +++ b/Step/wwwroot/src/services/alarmsService.ts @@ -11,6 +11,32 @@ export class AlarmsService extends baseRestService { return (await this.Post(this.BASE_URL + `data?pagesize=${pagesize}`, null, true)) as any; } + async exportAlarms(title: string, sources: Array, users?: Array, fromDate?: Date, toDate?: Date) + { + + let result = await this.Post(this.BASE_URL + "export", + { + "page": 0, + "pageSize": 100, + "title": title, + "sources": sources, + "startDate": fromDate, + "endDate": toDate, + "userIds": users, + language: "en" + } + , true).then(function (response) { + const url = window.URL.createObjectURL(new Blob([response])); + const link = document.createElement('a'); + link.href = url; + link.setAttribute('download', 'AlarmExport.csv'); + document.body.appendChild(link); + link.click(); + }); + + + } + async getAlarms(title: string, sources: Array, users?: Array, fromDate?: Date, toDate?: Date, page: number = 0, pagesize: number = 100): Promise<{pages:number, alarms:AlarmModel[]}> { let result = await this.Post<{pages:number, alarms:AlarmModel[]}>(this.BASE_URL + "paginated", @@ -65,7 +91,6 @@ export class AlarmsService extends baseRestService { ModalHelper.ShowModal(ModalIframe); } - } }