Procedimentos para exportar para .CSV
1. No menu Edit > Tags > Objects, crie cinco novas tags conforme mostrado na tabela 1.
Name |
Type |
Array |
StartDateTime |
DateTime |
|
EndDateTime |
DateTime |
|
OutputFilename |
Text |
|
SelectedFileName |
Text |
|
Value |
Integer |
10 |
Tabela 1 – Tags que devem ser criadas.
2. No mesmo menu Edit, acesse Alarms > Items e preencha conforme indicado na tabela 2.
TagName |
Condition |
Limit |
Group |
Priority |
Message |
Value[1] |
HiHi |
25 |
Warning |
0 |
Value[1] está maior que 25 |
Value[2] |
HiHi |
50 |
Warning |
0 |
Value[2] está maior que 50 |
Value[3] |
HiHi |
85 |
Warning |
0 |
Value[3] está maior que 85 |
Tabela 2 – Configuração para a aba Items.
3. No mesmo menu Edit, acesse Scripts > Classes e crie uma nova classe conforme mostrado na tabela 3.
Name |
Code |
Domain |
ExportToCSV |
CSharp |
Client |
Tabela 3 – Configuração para a aba Classes.
4. Clique duas vezes sobre a nova classe criada e adicione o seguinte código no CodeEditor:
public void TreatAlarmCSV(string inFile, string outFile)
{
if(!System.IO.File.Exists(inFile))
return;
System.IO.FileStream fs = System.IO.File.Create(outFile);
fs.Close();
System.IO.StreamReader sr = new System.IO.StreamReader(inFile);
System.IO.StreamWriter sw = new System.IO.StreamWriter(outFile, true, Encoding.ASCII);
if(sr == null || sw == null)
return;
while(!sr.EndOfStream)
{
string line = sr.ReadLine();
string [] values = line.Split(',');
//value[0] - ActiveTime
//value[1] - NormTime
//value[2] - AckTime
//value[3] - AckStatus
//value[4] - AckRequired
//value[5] - TagName
//value[6] - Condition
//value[7] - Value
//value[8] - Message
//value[9] - Area
//value[10] - State
//value[11] - Priority
//value[12] - UserName
//value[13] - Comments
//value[14] - ColorFG
//value[15] - ColorBG
if(values.Length > 8)
{
string message = values[0] + "," + values[5] + "," + values[6] + "," + values[7] + "," + values[8];
sw.WriteLine(message);
}
}
sw.Flush();
sw.Close();
sr.Close();
}
5. O display que será usado para teste será como o da figura 1. Cada parte será explicada individualmente.
Figura 1 – Display de teste.
6. A parte 1 modifica os valores das tags relacionadas aos alarmes para acioná-los, além do botão RESET para zerar o valor de cada respectiva tag. Utilize a configuração mostrada na figura 2 para os sliders verticais. Troque para os outros sliders as tags Value[2] e Value[3]. A configuração para o botão RESET se encontra na figura 3.
Figura 2 – Configuração para os sliders.
Figura 3 – Configuração para o botão RESET.
7. A parte 2 mostra os alarmes disparados e a data e horário em que se pode filtrar os alarmes quando o arquivo for gerado.
- O AlarmWindow encontra-se no menu esquerdo e não precisa ser configurado.
- O Date picker control e o DateTime textbox encontram-se também no menu esquerdo. Para configurá-los, clique somente uma vez neles e insira no campo SelectedDate as tags StartDateTime para os controles da data inicial e EndDateTime para a data final.
8. A parte 3 consta no nome do arquivo que será criado, bem como na exportação propriamente dita. Para configurá-la, crie um textbox que se encontra no menu esquerdo e configure-a como indicado na figura 4.
Figura 4 – Configuração do textbox.
Para configurar o botão de Exportar, clique duas vezes sobre ele e selecione Action > RunScript > New e adicione o seguinte código no método relacionado ao botão:
String fileName = @Info.ExecutionPath + @Tag.OutputFilename + ".txt";
String fileName2 = @Info.ExecutionPath + @Tag.OutputFilename + ".csv";
@Alarm.Group.Warning.SaveToTextFile(@Tag.StartDateTime.LocalDateTime, @Tag.EndDateTime.LocalDateTime, fileName, ',', true, 6, null);
@Script.Class.ExportToCSV.TreatAlarmCSV(fileName, fileName2);
MessageBox.Show("Alarmes exportados para a pasta de projetos ActionNET. \nNome do arquivo: " + @Tag.OutputFilename+".csv");
Procedimentos para exportar para HTML
1. No mesmo projeto, acrescente três novas tags como indicado na tabela 4.
Name |
Type |
Array |
filterAlarm |
Text |
|
valueRow |
Text |
10 |
DBStatus |
Text |
Tabela 4 – Tags que devem ser criadas.
2. Acesse o menu Edit > Datasets > Tables e crie uma nova tabela como indicado na tabela 5.
Name |
DB |
TableName |
WhereCondition |
Access |
MyTable |
AlarmHistorian |
Alarms |
{Tag.filterAlarm} |
Read |
Tabela 5 – Configuração da tabela que deve ser criada.
No campo Mapping, clique no botão "..." e configure da maneira mostrada na figura 5.
Figura 5 – Configuração do Mapping.
3. Acesse o menu Edit > Scripts > Classes e clique duas vezes na classe ClientMain. Insira o código a seguir no CodeEditor:
// Create HTML from the Dataset
public void TreatAlarmDBToHtmlTable(string outFile)
{
System.IO.FileStream fs = System.IO.File.Create(outFile);
fs.Close();
System.IO.StreamWriter sw = new System.IO.StreamWriter(outFile, true, Encoding.UTF8);
if(sw == null)
return;
string header = "<HTML><!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\"><META content=\"MSHTML 6.00.6000.16788\" name=GENERATOR></HEAD><BODY STYLE=\"font-family:Tahoma;font-size:12;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-style:italic;font-size:20;font-family:tahoma;\">";
header += "Relatório";
header += "</SPAN></DIV><BR /><DIV><HR /></DIV><BR />";
header += "<DIV><SPAN STYLE=\"font-family:tahoma;font-size:11;\"> Data Inicial : " + @Tag.StartDateTime.ToString() + "</SPAN></DIV>";
header += "<DIV><SPAN STYLE=\"font-family:tahoma;font-size:11;\"> Data Final : " + @Tag.EndDateTime.ToString() + "</SPAN></DIV>";
header += "<BR /><DIV><HR /></DIV><BR />";
header += "<TABLE width=\"691\" align=\"Center\" vAlign=\"top\" STYLE=\"border-width:2 2 2 2;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\">";
header += "<TBODY>";
sw.WriteLine(header);
@Tag.filterAlarm = @Tag.StartDateTime.Value.UtcDateTime.Ticks.ToString() + " < [ActiveTime_Ticks] AND " + @Tag.EndDateTime.Value.UtcDateTime.Ticks.ToString() + " > [ActiveTime_Ticks]";
int status = 0;
string statusMessage = "";
@Tag.DBStatus = "";
DataTable table = @Dataset.Table.MyTable.SelectCommandWithStatus(out status, out statusMessage);
if (status == 0)
{
if(table.Rows.Count == 0)
{
@Tag.DBStatus = "";
}
for (int i = 0; i < @Dataset.Table.MyTable.RowCount; i++)
{
DateTime activeTime = new DateTime(TConvert.To<long>(@Tag.valueRow[0]));
DateTime normTime = new DateTime(TConvert.To<long>(@Tag.valueRow[1]));
DateTime ackTime = new DateTime(TConvert.To<long>(@Tag.valueRow[2]));
string message = "<TR>";
message += "<TD width=\"171\" align=\"left\" vAlign=\"top\" STYLE=\"border-width:1 1 1 1;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-family:tahoma;font-size:11;\">" + activeTime.ToLocalTime().ToString() + "</SPAN></DIV></TD>";
message += "<TD width=\"159\" align=\"left\" vAlign=\"top\" STYLE=\"border-width:1 1 1 1;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-family:tahoma;font-size:11;\">" + normTime.ToLocalTime().ToString() + "</SPAN></DIV></TD>";
message += "<TD width=\"152\" align=\"left\" vAlign=\"top\" STYLE=\"border-width:1 1 1 1;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-family:tahoma;font-size:11;\">" + ackTime.ToLocalTime().ToString() + "</SPAN></DIV></TD>";
message += "<TD width=\"100\" align=\"left\" vAlign=\"top\" STYLE=\"border-width:1 1 1 1;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-family:tahoma;font-size:11;\">" + @Tag.valueRow[3] + "</SPAN></DIV></TD>";
message += "<TD width=\"109\" align=\"left\" vAlign=\"top\" STYLE=\"border-width:1 1 1 1;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-family:tahoma;font-size:11;\">" + @Tag.valueRow[4] + "</SPAN></DIV></TD>";
message += "<TD width=\"300\" align=\"left\" vAlign=\"top\" STYLE=\"border-width:1 1 1 1;border-color:#000000;border-style:solid;mso-element:para-border-div;font-family:tahoma;font-size:11;\"><DIV STYLE=\"text-align:Center;font-family:tahoma;font-size:11;\"><SPAN STYLE=\"font-family:tahoma;font-size:11;\">" + @Tag.valueRow[5] + "</SPAN></DIV></TD>";
message += "</TR>";
sw.WriteLine(message);
@Dataset.Table.MyTable.NextCommand();
}
}
else
{
@Tag.DBStatus = statusMessage;
}
string footer = "</TBODY></TABLE><BR /><BR /><BR /></BODY></HTML>";
sw.WriteLine(footer);
sw.Flush();
sw.Close();
}
4. O display que será usado para teste será como o display para converter para .csv, porém, com alguns elementos extras, como indicado na figura 6.
Figura 6 – Parte do display de teste de conversão para HTML.
5. Diferente do display da figura 1, esse possui botão para exportar para HTML, além de uma WebBrowser Window e uma textbox indicando o caminho em que o arquivo foi salvo.
- Configure o botão clicando duas vezes nele, acesse Action > Run Script > New e adicione o seguinte código no método relacionado ao botão:
String fileName2 = @Info.ExecutionPath + @Tag.OutputFilename + ".html";
@Script.Class.ClientMain.TreatAlarmDBToHtmlTable(fileName2);
@Tag.SelectedFileName = fileName2;
MessageBox.Show("Alarmes exportados para a pasta de projetos \nNome do arquivo: " + @Tag.OutputFilename+".html"); - Configure a WebBrowser Window colocando file:\\{Tag.SelectedFileName} no campo Url, que encontra-se no canto esquerdo da tela.
- Configure a textbox com a tag Tag.SelectedFileName e aumente para 256 o campo MaxLenght.
Resultados
Se o procedimento ocorrer com sucesso, o arquivo .csv gerado será salvo na pasta de projetos, bem como o arquivo HTML (que também aparecerá na tela do SCADA) e terá a estrutura indicada nas figuras 7 e 8.
Figura 7 – Resultado esperado do arquivo gerado em .csv.
Figura 8 – Resultado esperado do arquivo gerado em HTML.
Caso isso não aconteça, repita o processo da seção Procedimentos e tente novamente.