242 lines
10 KiB
C#
242 lines
10 KiB
C#
using CMS_CORE_Library.Models;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Web.Http;
|
|
using Thermo.Active.Model.DTOModels;
|
|
using Thermo.Active.NC;
|
|
using static Thermo.Active.Config.ServerConfig;
|
|
using static Thermo.Active.Model.Constants;
|
|
using static Thermo.Active.Utils.LanguageController;
|
|
|
|
namespace Thermo.Active.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/language")]
|
|
public class LanguageController : ApiController
|
|
{
|
|
/// <summary>
|
|
/// Oggetto adapter condiviso da WebAPI
|
|
/// </summary>
|
|
protected static NcAdapter ncAdapter = new NcAdapter();
|
|
|
|
[Route("languages"), HttpGet]
|
|
public IHttpActionResult GetLanguageList()
|
|
{
|
|
List<DTOLanguageModel> availableLanguages = GetLanguageListFromDirectory();
|
|
if (availableLanguages == null)
|
|
return NotFound();
|
|
|
|
ICollection<CultureInfo> cultureInfos = new List<CultureInfo>();
|
|
|
|
// Get nc available language
|
|
CmsError libraryError = ncAdapter.numericalControl.NC_GetAvailableLanguages(ref cultureInfos);
|
|
if (libraryError.IsError())
|
|
return BadRequest(libraryError.localizationKey);
|
|
// Filter available language with
|
|
availableLanguages = availableLanguages.Where(x => cultureInfos.Any(y => y.TwoLetterISOLanguageName == x.IsoId)).ToList();
|
|
|
|
return Ok(availableLanguages);
|
|
}
|
|
|
|
[Route("{language}"), HttpGet()]
|
|
public IHttpActionResult GetTranslations(string language)
|
|
{
|
|
if (!IsValidLanguage(language))
|
|
return BadRequest("Language not exists");
|
|
|
|
if (!LanguageIsAvailable(language))
|
|
return NotFound();
|
|
|
|
Dictionary<string, string> translations = GetTranslationsFromFile(language);
|
|
|
|
// Get Maintenance, Softkey, Plc Alarm translations
|
|
Dictionary<string, string> maintenanceNames = GetLocalizeMaintenanceName(language);
|
|
Dictionary<string, string> softKeysNames = GetLocalizedSoftKeysNames(language);
|
|
Dictionary<string, string> alarmsNames = GetPlcAlarmsTranslations(language);
|
|
Dictionary<string, string> headsNames = GetLocalizedHeadsNames(language);
|
|
|
|
Dictionary<string, string> scadaTranslations = GetScadaTranslations(language);
|
|
Dictionary<string, string> m156Translations = GetM156Translations(language);
|
|
|
|
// Thermo (modules and recipe enums) translations
|
|
Dictionary<string, string> thermoTranslations = GetThermoTranslations(language);
|
|
|
|
|
|
// Concat maintenances dictionary with translations dictionary
|
|
translations = translations.Concat(maintenanceNames).ToDictionary(x => x.Key, x => x.Value);
|
|
// Softkeys
|
|
translations = translations.Concat(softKeysNames).ToDictionary(x => x.Key, x => x.Value);
|
|
// Alarms
|
|
translations = translations.Concat(alarmsNames).ToDictionary(x => x.Key, x => x.Value);
|
|
// Heads
|
|
translations = translations.Concat(headsNames).ToDictionary(x => x.Key, x => x.Value);
|
|
// Scada
|
|
translations = translations.Concat(scadaTranslations).ToDictionary(x => x.Key, x => x.Value);
|
|
// M156
|
|
translations = translations.Concat(m156Translations).ToDictionary(x => x.Key, x => x.Value);
|
|
// Thermo
|
|
translations = translations.Concat(thermoTranslations).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
if (translations == null)
|
|
return InternalServerError();
|
|
|
|
return Ok(translations);
|
|
}
|
|
|
|
private static Dictionary<string, string> GetLocalizeMaintenanceName(string language)
|
|
{
|
|
Dictionary<string, string> localizedStrings = new Dictionary<string, string>();
|
|
|
|
// Get localized name
|
|
localizedStrings = MaintenancesConfig
|
|
.ToDictionary(
|
|
x => MAINTENANCE_PREFIX_ID + x.Id.ToString(), // Prefix + maintenance id
|
|
x => GetValueFromLocalizationList(x.LocalizedName, language, "Maintenance_" + x.Id)
|
|
);
|
|
|
|
// Get localized description
|
|
localizedStrings = localizedStrings.Concat(MaintenancesConfig
|
|
.ToDictionary(
|
|
x => MAINTENANCE_DESC_PREFIX_ID + x.Id.ToString(), // Description Prefix + maintenance id
|
|
x => GetValueFromLocalizationList(x.LocalizedDescription, language, "Maintenance_" + x.Id)
|
|
)).ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
return localizedStrings;
|
|
}
|
|
|
|
private static Dictionary<string, string> GetLocalizedSoftKeysNames(string language)
|
|
{
|
|
return SoftKeysConfig
|
|
.ToDictionary(
|
|
x => SOFTKEY_PREFIX_ID + x.Id.ToString(), // PREFIX + softkey Id
|
|
x => GetValueFromLocalizationList(x.LocalizedNames, language, "SoftKey_" + x.Id)
|
|
);
|
|
}
|
|
|
|
|
|
private static Dictionary<string, string> GetLocalizedHeadsNames(string language)
|
|
{
|
|
return HeadsConfig
|
|
.ToDictionary(
|
|
x => SOFTKEY_HEAD_ID + x.Id.ToString(), // PREFIX + softkey Id
|
|
x => GetValueFromLocalizationList(x.LocalizedNames, language, "Head_" + x.Id)
|
|
);
|
|
}
|
|
|
|
|
|
private static string GetValueFromLocalizationList(Dictionary<string, string> localizedNames, string language, string defaultString)
|
|
{
|
|
// Find text from names by language id
|
|
var value = localizedNames.Where(y => y.Key == language).FirstOrDefault();
|
|
// Set default value
|
|
if (value.Key == null)
|
|
return defaultString;
|
|
else
|
|
return value.Value;
|
|
}
|
|
|
|
public static Dictionary<string, string> GetPlcAlarmsTranslations(string language)
|
|
{
|
|
Dictionary<string, string> returnValue = new Dictionary<string, string>();
|
|
|
|
Dictionary<int, string> messages = new Dictionary<int, string>();
|
|
// Read data from CN
|
|
ncAdapter.numericalControl.NC_GetTranslatedPlcMessages(language, ref messages); // Avoid checking error because in the worst case "messages" is empty
|
|
|
|
// Id start from 1
|
|
for (int i = 1; i <= 1024; i++)
|
|
{
|
|
// Get configurated alarms
|
|
var tmpAlarmConfig = InitialAlarmsConfig.Where(x => x.PlcId == i).FirstOrDefault();
|
|
// Default string
|
|
string message = string.Format(NOT_CONFIGURATED_ALARM_MESSAGE, i);
|
|
// If is configurated
|
|
if (tmpAlarmConfig != null)
|
|
{
|
|
// Find translated string
|
|
message = messages.Where(x => x.Key == tmpAlarmConfig.AlarmId).FirstOrDefault().Value;
|
|
if (message == null)
|
|
message = string.Format(NOT_FOUND_ALARM_MESSAGE, i);
|
|
}
|
|
// Add to dictionary
|
|
returnValue.Add(
|
|
ALARM_PREFIX + i,
|
|
message
|
|
);
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
public static Dictionary<string, string> GetThermoTranslations(string language)
|
|
{
|
|
Dictionary<string, string> returnValue = new Dictionary<string, string>();
|
|
|
|
// Read data from CN
|
|
ncAdapter.numericalControl.NC_GetTranslatedThermoLabels(language, ref returnValue);
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
private static Dictionary<string, string> GetScadaTranslations(string language)
|
|
{
|
|
Dictionary<string, string> translatedNames = new Dictionary<string, string>();
|
|
|
|
// Loop throught labels Scada -> Layers -> Labels
|
|
foreach (var scada in ProductionScadaSchema.Concat(ConfiguredScadaSchema))
|
|
{
|
|
foreach (var layer in scada.Layers)
|
|
{
|
|
foreach (var label in layer.Labels)
|
|
{
|
|
// Find selected language
|
|
var translatedValue = label.Label.TextContents?.Where(y => y.LangKey == language).FirstOrDefault();
|
|
if (translatedValue != null)
|
|
translatedNames.Add("scada_label_" + label.Id, translatedValue.TextContent);
|
|
else
|
|
// default value
|
|
translatedNames.Add("scada_label_" + label.Id, "scada_label_" + label.Id);
|
|
}
|
|
|
|
foreach (var button in layer.Buttons)
|
|
{
|
|
// Find selected language
|
|
var translatedValue = button.Label.TextContents.Where(y => y.LangKey == language).FirstOrDefault();
|
|
if (translatedValue != null)
|
|
translatedNames.Add("scada_button_" + button.Id, translatedValue.TextContent);
|
|
else
|
|
// default value
|
|
translatedNames.Add("scada_button_" + button.Id, "scada_button_" + button.Id);
|
|
}
|
|
}
|
|
}
|
|
|
|
return translatedNames;
|
|
}
|
|
|
|
public static Dictionary<string, string> GetM156Translations(string language)
|
|
{
|
|
Dictionary<string, string> translatedNames = new Dictionary<string, string>();
|
|
|
|
foreach (var input in InputsOperatorConfig)
|
|
{
|
|
translatedNames.Add(
|
|
"m156_title_" + input.Id,
|
|
GetValueFromLocalizationList(input.Messages, language, "Title_" + input.Id)
|
|
);
|
|
|
|
if (input.Buttons != null && input.Buttons.Count() > 0)
|
|
foreach (var button in input.Buttons)
|
|
{
|
|
// Add translated button text to the list
|
|
// Buttons have a Dictionary<string, string> with the translations
|
|
translatedNames.Add(
|
|
"m156_" + input.Id + "_button_" + button.Key,
|
|
GetValueFromLocalizationList(button.Value, language, "Button_" + button.Key)
|
|
);
|
|
}
|
|
}
|
|
|
|
return translatedNames;
|
|
}
|
|
}
|
|
} |