-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExtensionsMixins.cs
145 lines (132 loc) · 4.68 KB
/
ExtensionsMixins.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Reactive.Linq;
namespace S7PlcRx;
/// <summary>
/// Extensions Class.
/// </summary>
public static class ExtensionsMixins
{
/// <summary>
/// Adds the update tag item.
/// </summary>
/// <typeparam name="T">The Type.</typeparam>
/// <param name="this">The this.</param>
/// <param name="tagName">The tag name.</param>
/// <param name="address">The address.</param>
/// <param name="arrayLength">Length of the array.</param>
/// <returns>A Tag.</returns>
public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this IRxS7 @this, string tagName, string address, int? arrayLength = null)
{
var tag = default(Tag);
if (@this is RxS7 plc)
{
if ((typeof(T) == typeof(string) || typeof(T).IsArray) && arrayLength.HasValue)
{
tag = new(tagName, address, typeof(T), arrayLength.Value);
}
else
{
tag = new(tagName, address, typeof(T));
}
plc.AddUpdateTagItem(tag);
}
return (tag, @this);
}
/// <summary>
/// Adds the update tag item.
/// </summary>
/// <typeparam name="T">The Type.</typeparam>
/// <param name="this">The this.</param>
/// <param name="tagName">The tag name.</param>
/// <param name="address">The address.</param>
/// <param name="arrayLength">Length of the array.</param>
/// <returns>A Tag.</returns>
public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this (ITag? _, IRxS7? plc) @this, string tagName, string address, int? arrayLength = null)
{
var tag = default(Tag);
if (@this.plc is RxS7 plc)
{
if ((typeof(T) == typeof(string) || typeof(T).IsArray) && arrayLength.HasValue)
{
tag = new(tagName, address, typeof(T), arrayLength.Value);
}
else
{
tag = new(tagName, address, typeof(T));
}
plc.AddUpdateTagItem(tag);
}
return (tag, @this.plc);
}
/// <summary>
/// Sets the tag to poll for values.
/// </summary>
/// <param name="this">The instance of tag.</param>
/// <param name="polling">if set to <c>true</c> [poll].</param>
/// <returns>The instance.</returns>
public static (ITag? tag, IRxS7? plc) SetTagPollIng(this (ITag? tag, IRxS7? plc) @this, bool polling = true)
{
@this.tag?.SetDoNotPoll(!polling);
return @this;
}
/// <summary>
/// Gets the tag.
/// </summary>
/// <param name="this">The rx s7 plc instance.</param>
/// <param name="tagName">Name of the tag.</param>
/// <returns>The instance of tag.</returns>
public static (ITag? tag, IRxS7? plc) GetTag(this IRxS7 @this, string tagName) =>
@this?.TagList[tagName!] switch
{
Tag tag => (tag, @this),
_ => (default, @this)
};
/// <summary>
/// Removes the tag item.
/// </summary>
/// <param name="this">The this.</param>
/// <param name="tagName">The tag name.</param>
public static void RemoveTagItem(this IRxS7 @this, string tagName)
{
if (@this is RxS7 plc)
{
plc.RemoveTagItem(tagName);
}
}
/// <summary>
/// Tags to dictionary.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="source">The source.</param>
/// <returns>
/// A Dictionary.
/// </returns>
public static IObservable<IDictionary<string, TValue>> TagToDictionary<TValue>(this IObservable<Tag?> source)
{
IDictionary<string, TValue> tagValues = new Dictionary<string, TValue>();
return source
.Where(t => t != null && t?.Value is TValue)
.Select(t => (Name: t!.Name!, Value: (TValue)t!.Value!))
.Where(t => t.Value != null)
.Select(t =>
{
tagValues[t.Name] = t.Value;
return tagValues;
});
}
/// <summary>
/// Tags to dictionary.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="source">The source.</param>
/// <param name="tag">The tag.</param>
/// <returns>
/// A Dictionary.
/// </returns>
public static IObservable<(string Tag, TValue Value)> ToTagValue<TValue>(this IObservable<TValue?> source, string tag) =>
source
.Where(t => t != null)
.Select(t => (Tag: tag, Value: t!))
.Where(t => t.Value != null);
}