ur20web模块

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Common;
using Extensions;

namespace
{

/// UR20WEB模块
/// </summary>
public class UR20WEBUnit : YZXWriteableUnit
{
public string IP{get; set;}
public string TO{get; set;}

public ushort FromAddress { get; set; }

public UR20WEBUnit(string ip = "192.168.2.203",
string to = "d41d8cd98f00b204e9800998ecf8427e",
ushort from = 0, ushort count =100):
base(ip,count)
{

Name = string.Format("UR20WEB-{0}-{1}", ip ,to);
IP = ip;
TO = to;
FromAddress = from;
Length = count;

Init();
}
public override void Init()
{
bits = new List<bool>(Length);

}

public bool ActiveForcen()
{
string url = string.Format("http://{0}/mock/write.php", IP);
try
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("to", TO);
nvc.Add("fr[0][cn]", "fm");
Http.Post(url, nvc);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
public bool DeactiveForcen()
{
string url = string.Format("http://{0}/mock/write.php", IP);
try
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("to", TO);
nvc.Add("fr[0][cn]", "uc");
Http.Post(url, nvc);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}


public override List<bool> UpdateBits()
{
throw new NotImplementedException();
}


///
/// </summary>
/// <param name="wIndex"></param>
/// <param name="bIndex"></param>
/// <param name="v"></param>
/// <returns></returns>
public override bool WriteB(ushort wIndex, byte bIndex, bool v)
{
string url = string.Format("http://{0}/mock/write.php", IP);
try
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("to", TO);
nvc.Add("fr[0][cn]", "fc");
string bIndexString = bIndex.ToString().PadLeft(2, '0');
string wIndexString = wIndex.ToString().PadLeft(2, '0');
string keystring = string.Format("{0}{1}{2}", wIndexString, bIndexString, v ? 1 : 0);
nvc.Add("fr[0][df]", keystring);
Http.Post(url,nvc);
return true;
}catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}

public bool UpdateUshort(ushort wIndex, short oldValue, short newValue)
{
return UpdateUshort(wIndex, (ushort)oldValue, (ushort)newValue);
}

/// 更新值
/// 为减少POST请求数量
/// </summary>
/// <param name="wIndex">字地址</param>
/// <param name="oldValue">旧值</param>
/// <param name="newValue">新值</param>
/// <returns></returns>
public bool UpdateUshort(ushort wIndex, ushort oldValue, ushort newValue)
{
string url = string.Format("http://{0}/mock/write.php", IP);
try
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("to", TO);
bool[] oldBits = oldValue.ToBoolArray();
bool[] newBits = newValue.ToBoolArray();
string wIndexString = wIndex.ToString().PadLeft(2, '0');
for (byte bIndex = 0; bIndex <= 15; bIndex++)
{
bool oldV = oldBits[bIndex];
bool newV = newBits[bIndex];

if(oldV != newV)
{
WriteB(wIndex, bIndex, newV);
}
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}

public override bool WriteUshort(ushort wIndex, ushort value)
{
string url = string.Format("http://{0}/mock/write.php", IP);
try
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("to", TO);
bool[] bits = value.ToBoolArray();
string wIndexString = wIndex.ToString().PadLeft(2, '0');
for (byte bIndex = 0; bIndex <= 15; bIndex++)
{
bool v = bits[bIndex];
//string bIndexString = bIndex.ToString().PadLeft(2, '0');

//nvc.Add(String.Format("fr[{0}][cn]", bIndexString), "fc");
//string keystring = String.Format("{0}{1}{2}", wIndexString, bIndex, v ? 1 : 0);
//nvc.Add(String.Format("fr[{0}][df]", bIndexString), keystring);
WriteB(wIndex, bIndex, v);
}
//Http.Post(url, nvc);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}

public override bool WriteD(ushort dwIndex, int d)
{
throw new NotImplementedException();
}

public override bool WriteBits(List<bool> bits)
{
throw new NotImplementedException();
}
#endregion YZXUnit
}
}