public T Get <T>(string key, T defaultValue) { if (!Keys.Contains(key)) { return(defaultValue); } return(UniversalConverter.ConvertTo(this[key].ObjectValue, defaultValue)); }
private void AddPoints(string indent, IEnumerable <XmlNode> points) { foreach (XmlNode point in points) { if (point == null) { continue; } var name = point.Attributes["id"].InnerText; var sunSpecType = point.Attributes["type"].InnerText; var typeName = SunSpecType.ToSystemType(sunSpecType).Name; if (sunSpecType.StartsWith("enum") || sunSpecType.StartsWith("bitfield")) { var flags = sunSpecType.StartsWith("bitfield"); AddEnum(indent, point, typeName, flags); typeName = "E_" + name; } var offset = UniversalConverter.ConvertTo <long>(point.Attributes["offset"].InnerText); var length = UniversalConverter.ConvertTo <long>(point.Attributes["len"]?.InnerText ?? "1"); var access = point.Attributes["access"]?.InnerText ?? "rw"; var mandatory = UniversalConverter.ConvertTo <bool>(point.Attributes["mandatory"]?.InnerText) ? "" : "?"; var units = point.Attributes["units"]?.InnerText; if (!string.IsNullOrEmpty(units)) { _codeText.AppendLine($"{indent}/// [{units}]"); } AddComment(indent, name); _codeText.AppendLine($"{indent}[SunSpecProperty(offset: {offset}, length: {length})]"); _codeText.Append($"{indent}public {typeName}{mandatory} {name} {{ get; "); if (access == "r") { _codeText.Append("private "); } _codeText.AppendLine("set; }"); } }
private void CreateModelFile(string fileName, string structName, XmlNode model) { var id = UniversalConverter.ConvertTo <long>(model.Attributes["id"].InnerText); var length = UniversalConverter.ConvertTo <long>(model.Attributes["len"].InnerText); if (structName == "Common" && id != 1) { // Test Model return; } Console.WriteLine($"Generating struct {structName}"); foreach (var @using in GeneratorSettings.Usings) { _codeText.AppendLine($"using {@using};"); } _codeText.AppendLine(); _codeText.AppendLine("// ReSharper disable InconsistentNaming"); _codeText.AppendLine("// ReSharper disable IdentifierTypo"); _codeText.AppendLine("// ReSharper disable CommentTypo"); _codeText.AppendLine("// ReSharper disable UnusedType.Global"); _codeText.AppendLine("// ReSharper disable UnusedMember.Global"); _codeText.AppendLine("// ReSharper disable MemberCanBePrivate.Global"); _codeText.AppendLine("// ReSharper disable UnusedAutoPropertyAccessor.Local"); _codeText.AppendLine("// ReSharper disable ArgumentsStyleLiteral"); _codeText.AppendLine("// ReSharper disable BuiltInTypeReferenceStyle"); _codeText.AppendLine(); _codeText.AppendLine($"namespace {GeneratorSettings.Namespace}"); _codeText.AppendLine("{"); { AddComment(" ", null); _codeText.AppendLine($" [SunSpecModel(id: {id}, length: {length})]"); _codeText.AppendLine($" public struct {structName}"); _codeText.AppendLine(" {"); { var repeating = 1; var blocks = model.ChildNodes .Cast <XmlNode>() .Where(n => n.Name == "block") .ToArray(); foreach (var block in blocks) { var blockName = block.Attributes["name"]?.InnerText ?? $"Block{repeating++}"; var blockType = block.Attributes["type"]?.InnerText ?? "fixed"; var points = block .ChildNodes .Cast <XmlNode>(); blockName = blockName.Trim() .Replace(" ", "_") .Replace("-", "_"); if (blockType == "fixed") { AddPoints(" ", points); } else { blockName = NamingConverter.SnakeToPascalCase(blockName); _codeText.AppendLine($" public struct S_{blockName}"); _codeText.AppendLine(" {"); AddPoints(" ", points); _codeText.AppendLine(" };"); _codeText.AppendLine($" public S_{blockName}[] {blockName};"); } } } _codeText.AppendLine(" }"); } _codeText.AppendLine("}"); File.WriteAllText(fileName, _codeText.ToString()); }
private bool WriteValue(object value) { var client = _connection?.PlcClient; if (client == null) { return(false); } if (!client.Connected) { return(false); } var buffer = new byte[_info.ByteCount + 64]; _value = value ?? throw new ArgumentNullException(nameof(value)); try { switch (_info.PlcDataType) { case PlcDataTypes.X: S7.SetBitAt(ref buffer, 0, _info.Bit, UniversalConverter.ConvertTo <bool>(_value)); break; case PlcDataTypes.B: S7.SetByteAt(buffer, 0, UniversalConverter.ConvertTo <byte>(_value)); break; case PlcDataTypes.INT: S7.SetIntAt(buffer, 0, UniversalConverter.ConvertTo <short>(_value)); break; case PlcDataTypes.DINT: S7.SetDIntAt(buffer, 0, UniversalConverter.ConvertTo <int>(_value)); break; case PlcDataTypes.DT: S7.SetDateTimeAt(buffer, 0, UniversalConverter.ConvertTo <DateTime>(_value)); break; case PlcDataTypes.STRING: var text = UniversalConverter.ConvertTo <string>(_value); if (text.Length > _info.MaxStringLength) { text = text.Substring(0, _info.MaxStringLength); } S7.SetStringAt(buffer, 0, _info.MaxStringLength, text); break; } var result = (_info.PlcDataType == PlcDataTypes.X) ? client.WriteArea(_info.PlcArea, _info.DbNumber, _info.Offset, _info.ByteCount, _info.PlcWordLen, buffer) : client.DBWrite(_info.DbNumber, _info.Offset, _info.ByteCount, buffer); if (result == 0) { return(true); } TronTrace.TraceError($"PlcItem.DBWrite({_info.DbNumber}, {_info.Offset}) failed - {result} {PlcResult.GetResultText(result)}"); } catch (Exception ex) { TronTrace.TraceError($"PlcItem.DBWrite({_info.DbNumber}, {_info.Offset}) failed - {ex.Message}"); } return(false); }