Skip to content

Commit 461cb62

Browse files
authored
Create D20-L.js
1 parent bea8110 commit 461cb62

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

LoRaWAN/decoders/Dragino/D20-L.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
function decodeUplink(input) {
2+
return {
3+
data: Decode(input.fPort, input.bytes, input.variables)
4+
};
5+
}
6+
function datalog(i,bytes){
7+
var aa= parseFloat(((bytes[0+i]<<24>>16 | bytes[1+i])/10).toFixed(1));
8+
var bb= parseFloat(((bytes[2+i]<<24>>16 | bytes[3+i])/10).toFixed(1));
9+
var cc= parseFloat(((bytes[4+i]<<24>>16 | bytes[5+i])/10).toFixed(1));
10+
var dd= (bytes[6+i]&0x02) ? "High":"Low";
11+
var ee= (bytes[6+i]&0x01) ? "True":"False";
12+
var ff= getMyDate((bytes[7+i]<<24 | bytes[8+i]<<16 | bytes[9+i]<<8 | bytes[10+i]).toString(10));
13+
var string='['+aa+','+bb+','+cc+','+dd+','+ee+','+ff+']'+',';
14+
15+
return string;
16+
}
17+
18+
function getzf(c_num){
19+
if(parseInt(c_num) < 10)
20+
c_num = '0' + c_num;
21+
22+
return c_num;
23+
}
24+
25+
function getMyDate(str){
26+
var c_Date;
27+
if(str > 9999999999)
28+
c_Date = new Date(parseInt(str));
29+
else
30+
c_Date = new Date(parseInt(str) * 1000);
31+
32+
var c_Year = c_Date.getFullYear(),
33+
c_Month = c_Date.getMonth()+1,
34+
c_Day = c_Date.getDate(),
35+
c_Hour = c_Date.getHours(),
36+
c_Min = c_Date.getMinutes(),
37+
c_Sen = c_Date.getSeconds();
38+
var c_Time = c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) +' '+ getzf(c_Hour) +':'+ getzf(c_Min) +':'+getzf(c_Sen);
39+
40+
return c_Time;
41+
}
42+
43+
function Decode(fPort, bytes, variables) {
44+
//D23-LB Decode
45+
if(fPort==0x02)
46+
{
47+
var decode = {};
48+
var mode=(bytes[6] & 0x7C)>>2;
49+
decode.Node_type="D20-L";
50+
if(mode===3)
51+
{
52+
decode.BatV=(bytes[0]<<8 | bytes[1])/1000;
53+
decode.EXTI_Trigger=(bytes[6] & 0x01)? "TRUE":"FALSE";
54+
decode.Door_status=(bytes[6] & 0x80)? "CLOSE":"OPEN";
55+
if((bytes[2]==0x7f)&&(bytes[3]==0xff))
56+
decode.TempC1= "NULL";
57+
else
58+
{
59+
decode.TempC1= parseFloat(((bytes[2]<<24>>16 | bytes[3])/10).toFixed(1));
60+
}
61+
62+
if(!((bytes[7]==0x7f)&&(bytes[8]==0xff)))
63+
{
64+
decode.TempC2= parseFloat(((bytes[7]<<24>>16 | bytes[8])/10).toFixed(1));
65+
}
66+
67+
if(!((bytes[9]==0x7f)&&(bytes[10]==0xff)))
68+
{
69+
decode.TempC3= parseFloat(((bytes[9]<<24>>16 | bytes[10])/10).toFixed(1));
70+
}
71+
}
72+
else if(mode==31)
73+
{
74+
decode.TEMPC1_MIN= bytes[4]<<24>>24;
75+
decode.TEMPC1_MAX= bytes[5]<<24>>24;
76+
decode.TEMPC2_MIN= bytes[7]<<24>>24;
77+
decode.TEMPC2_MAX= bytes[8]<<24>>24;
78+
decode.TEMPC3_MIN= bytes[9]<<24>>24;
79+
decode.TEMPC3_MAX= bytes[10]<<24>>24;
80+
}
81+
82+
if(bytes.length==11)
83+
return decode;
84+
}
85+
else if(fPort==3)
86+
{
87+
var pnack= ((bytes[6]>>7)&0x01) ? "True":"False";
88+
for(var i=0;i<bytes.length;i=i+11)
89+
{
90+
var data= datalog(i,bytes);
91+
if(i=='0')
92+
data_sum=data;
93+
else
94+
data_sum+=data;
95+
}
96+
return{
97+
Node_type:"D23-L",
98+
DATALOG:data_sum,
99+
PNACKMD:pnack,
100+
};
101+
}
102+
else if(fPort==5)
103+
{
104+
var freq_band;
105+
var sub_band;
106+
var sensor;
107+
108+
if(bytes[0]==0x19)
109+
sensor= "D23-LB";
110+
111+
var firm_ver= (bytes[1]&0x0f)+'.'+(bytes[2]>>4&0x0f)+'.'+(bytes[2]&0x0f);
112+
113+
if(bytes[3]==0x01)
114+
freq_band="EU868";
115+
else if(bytes[3]==0x02)
116+
freq_band="US915";
117+
else if(bytes[3]==0x03)
118+
freq_band="IN865";
119+
else if(bytes[3]==0x04)
120+
freq_band="AU915";
121+
else if(bytes[3]==0x05)
122+
freq_band="KZ865";
123+
else if(bytes[3]==0x06)
124+
freq_band="RU864";
125+
else if(bytes[3]==0x07)
126+
freq_band="AS923";
127+
else if(bytes[3]==0x08)
128+
freq_band="AS923_1";
129+
else if(bytes[3]==0x09)
130+
freq_band="AS923_2";
131+
else if(bytes[3]==0x0A)
132+
freq_band="AS923_3";
133+
else if(bytes[3]==0x0F)
134+
freq_band="AS923_4";
135+
else if(bytes[3]==0x0B)
136+
freq_band="CN470";
137+
else if(bytes[3]==0x0C)
138+
freq_band="EU433";
139+
else if(bytes[3]==0x0D)
140+
freq_band="KR920";
141+
else if(bytes[3]==0x0E)
142+
freq_band="MA869";
143+
144+
if(bytes[4]==0xff)
145+
sub_band="NULL";
146+
else
147+
sub_band=bytes[4];
148+
149+
var bat= (bytes[5]<<8 | bytes[6])/1000;
150+
151+
return {
152+
SENSOR_MODEL:sensor,
153+
FIRMWARE_VERSION:firm_ver,
154+
FREQUENCY_BAND:freq_band,
155+
SUB_BAND:sub_band,
156+
BAT:bat,
157+
}
158+
}
159+
}
160+
161+
function encodeDownlink(input) {
162+
var command = input.data.command;
163+
var output = [];
164+
165+
for (var i=0,j=0; i<command.length; i+=2,j++) {
166+
output[j]=parseInt(command.substr(i,2), 16);
167+
}
168+
169+
return {bytes: output};
170+
}

0 commit comments

Comments
 (0)