-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (127 loc) · 3.13 KB
/
index.js
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
//Lib for plc communication
//First case is panasonic plc, following is omron
//Panasonic PLC code here
class panasonic{
//Validate input for panasonic plc
PanFormatChk(inputs)
{
let result = false;
if (inputs == null)
{
console.log("Input incorrect, a parameter required, example PanSwitchOn('X0000').");
return;
}
result = false;
if (len(inputs) != 5)
{
console.log("Input incorrect, the correct should be like R0001, 5 chars.");
return;
}else
{
let pattern = /^[RXYT]\d{4}$/;
if(pattern.test(inputs))
{
result = true;
}else
{
console.log("Input incorrect, the relay should be X,Y,T,R, follow by four char. ex: R0001");
}
}
return result;
}
//Panasonic PLC communication, check version.
PanasonicVer(){
return "%EE#RT**"+ "\r";
}
//Swtich on a contact
PanSwitchOn(contact)
{
if(!PanFormatChk(contact))
{
return;
}
return "%EE#WCS"+contact+"1**"+"\r";
}
//Switch off a contact
PanSwitchOff(contact)
{
if(!PanFormatChk(contact))
{
return;
}
return "%EE#WCS"+contact+"1**"+"\r";
}
//Switch on a range of contacts
PanSWMultiOn(start,end)
{
if(start == null || end == null)
{
console.log("Input incorrect, two parameter required, example PanSWMultiOn('X0000','X0009').");
return;
}
if(!PanFormatChk(start) || !PanFormatChk(start))
{
return;
}
return"%EE#WCP"+start+end+"1**"+"\r";
}
//Switch off a range of contacts
PanSWMultiOff(start,end)
{
if(start == null || end == null)
{
console.log("Input incorrect, two parameter required, example PanSWMultiOn('X0000','X0009').");
return;
}
if(!PanFormatChk(start) || !PanFormatChk(start))
{
return;
}
return"%EE#WCP"+start+end+"0**"+"\r";
}
//Read a contact status
PanReadSingle(contact)
{
if(!PanFormatChk(contact))
{
return;
}
return "%EE#RCS"+contact+"**"+"\r";
}
//Read a range contacts status
PanReadMulti(start,end)
{
if(start == null || end == null)
{
console.log("Input incorrect, two parameter required, example PanSWMultiOn('X0000','X0009').");
return;
}
if(!PanFormatChk(start) || !PanFormatChk(start))
{
return;
}
return "%EE#RCP" + start + end + "**" + "\r"
}
//Read register
PanReadDT(start, end)
{
if(start == null || end == null)
{
console.log("Input incorrect, two parameter required, example PanSWMultiOn('X0000','X0009').");
return;
}
if(start == null || end == null)
{
console.log("Missing parameter, there are two parameters, example: PanReadDT(0001,1234)");
return;
}
let pattern = /^[0-9]+$/;
if(!pattern.test(start) || !pattern.test(end))
{
console.log("Input incorrect, please input 4 digit each, example: PanReadDT(0001,1234)");
return;
}
return "%EE#RDD"+str(Start)+str(End)+"**"+"\r"
}
}
module.exports = panasonic;