Files
cms_thermo_active/Step/Controllers/WebApi/LanguageController.cs
T
2018-03-21 09:43:55 +01:00

118 lines
4.7 KiB
C#

using Step.Model.DTOModels;
using Step.NC;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using static CMS_CORE_Library.DataStructures;
using static Step.Config.ServerConfig;
using static Step.Model.Constants;
using static Step.Utils.LanguageController;
namespace Step.Controllers.WebApi
{
[RoutePrefix("api/language")]
public class LanguageController : ApiController
{
[Route("languages"), HttpGet]
public IHttpActionResult GetLanguageList()
{
List<DTOLanguageModel> availableLanguages = GetLanguageListFromDirectory();
if (availableLanguages == null)
return NotFound();
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> maintenance = GetLocalizeMaintenanceName(language);
Dictionary<string, string> softKeys = GetLocalizedSoftKeysNames(language);
Dictionary<string, string> alarms = GetPlcAlarmsTranslations(language);
// Concat maintenances dictionary with translations dictionary
translations = translations.Concat(maintenance).ToDictionary(x => x.Key, x => x.Value);
translations = translations.Concat(softKeys).ToDictionary(x => x.Key, x => x.Value);
translations = translations.Concat(alarms).ToDictionary(x => x.Key, x => x.Value);
if (translations == null)
return InternalServerError();
return Ok(translations);
}
private static Dictionary<string, string> GetLocalizeMaintenanceName(string language)
{
return MaintenancesConfig
.ToDictionary(
x => MAINTENANCE_PREFIX_ID + x.Id.ToString(), // Prefix + maintenance id
x => GetValueFromMaintenanceNameList(x.LocalizedNames, language, "Maintenance_" + x.Id)
);
}
private static Dictionary<string, string> GetLocalizedSoftKeysNames(string language)
{
return SoftKeysConfig
.ToDictionary(
x => SOFTKEY_PREFIX_ID + x.Id.ToString(), // PREFIX + softkey Id
x => GetValueFromMaintenanceNameList(x.LocalizedNames, language, "SoftKey_" + x.Id)
);
}
private static string GetValueFromMaintenanceNameList(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;
}
private static Dictionary<string, string> GetPlcAlarmsTranslations(string language)
{
using (NcHandler ncHandler = new NcHandler())
{
CmsError cmsError = ncHandler.Connect();
Dictionary<string, string> returnValue = new Dictionary<string, string>();
Dictionary<int, string> messages = new Dictionary<int, string>();
// Read data from CN
cmsError = ncHandler.numericalControl.NC_GetTranslatedPlcMessages(language, ref messages);
// 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 = "Alarm_id_" + i + ": Message not configurated";
// If is configurated
if (tmpAlarmConfig != null)
{
// Find translated string
message = messages.Where(x => x.Key == tmpAlarmConfig.AlarmId).FirstOrDefault().Value;
if (message == null)
message = "Alarm_id_" + i + ": Message not found";
}
// Add to dictionary
returnValue.Add(
ALARM_PREFIX + i,
message
);
}
return returnValue;
}
}
}
}