Last modified by Hera Guo on 2025/02/18 09:29

From version 10.1
edited by Chxy
on 2024/10/11 09:41
Change comment: There is no comment for this version
To version 11.1
edited by Chxy
on 2024/10/14 23:49
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -95,107 +95,17 @@
95 95  
96 96  [[image:1728535941851-388.png||height="466" width="398"]]
97 97  
98 -Demo Code as below:
98 +Demo JavaScript Code: [[https:~~/~~/raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Uplink_Converter.js >>https://raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Uplink_Converter.js]]
99 99  
100 ->var data = decodeToJson(payload);
101 ->var deviceName = data.end_device_ids.device_id;
102 ->var deviceType = data.end_device_ids.application_ids.application_id;
103 ->var model = {};
104 ->var data2 = data.uplink_message.decoded_payload;
105 ->var flg = data.uplink_message.f_port
106 ->for (var key in data2) {
107 -> model[key] = data2[key];
108 ->}
109 ->var obj =  {"devid":deviceName}
110 ->var result = {
111 -> deviceName: deviceName,
112 -> deviceType: deviceType,
113 -> telemetry: model,
114 -> groupName: "Case Study",
115 -> attributes:{"devid":deviceName,
116 -> "timevalue":"test",
117 -> "inactivityTimeout":1260000
118 -> }
119 ->};
120 ->function decodeToString(payload) {
121 -> return String.fromCharCode.apply(String, payload);
122 ->}function decodeToJson(payload) {
123 -> var str = decodeToString(payload);
124 -> var data = JSON.parse(str);
125 -> return data;
126 ->}
127 ->if (flg===2){
128 ->return result;
129 ->}
130 130  
131 -
132 132  (% class="lead" %)
133 133  Input Downlink Converter
134 134  
135 135  [[image:1728536142721-488.png||height="470" width="407"]]
136 136  
137 -Example Code as below:
106 +Example Javascript Code as below: [[https:~~/~~/raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Downlink_Converter.js>>https://raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Downlink_Converter.js]]
138 138  
139 ->function hexToBase64(hexString) {
140 -> var bytes = hexString.match(/.{2}/g);
141 -> var binaryString = bytes.map(function(byte) {
142 -> return String.fromCharCode(parseInt(byte, 16));
143 -> }).join('');
144 ->
145 -> return btoa(binaryString);
146 ->}
147 ->var flg = "shared_value" in metadata
148 ->var value2 = parseInt(metadata.ss_timevalue).toString(16)if (value2.length==1){
149 -> value2 = "00000"+parseInt(metadata.ss_timevalue).toString(16)
150 ->}
151 ->else if (value2.length==2){
152 -> value2 = "0000"+parseInt(metadata.ss_timevalue).toString(16)
153 ->}
154 ->else if (value2.length==3){
155 -> value2 = "000"+parseInt(metadata.ss_timevalue).toString(16)
156 ->}
157 ->else if (value2.length==4){
158 -> value2 = "00"+parseInt(metadata.ss_timevalue).toString(16)
159 ->}
160 ->else if (value2.length==5){
161 -> value2 = "0"+parseInt(metadata.ss_timevalue).toString(16)
162 ->}
163 ->else {
164 -> value2 = value2
165 ->}
166 ->var data = "01"+value2
167 ->if (flg === true){
168 ->data = {
169 -> downlinks: [{
170 -> f_port: 1,
171 -> confirmed: false,
172 -> frm_payload: hexToBase64(metadata.shared_value),
173 -> priority: "NORMAL"
174 -> }]
175 -> };
176 ->}
177 ->else{
178 -> data = {
179 -> downlinks: [{
180 -> f_port: 1,
181 -> confirmed: false,
182 -> frm_payload: hexToBase64(data),
183 -> priority: "NORMAL"
184 -> }]
185 -> };
186 ->}
187 ->var result = {
188 -> contentType: "JSON",
189 -> data: JSON.stringify(data),
190 -> metadata: {
191 -> devId: metadata.ss_devid
192 -> }
193 ->};
194 ->if (metadata.shared_timevalue!=="test" || metadata.ss_timevalue!=="test"){
195 ->return result;
196 ->}
197 197  
198 -
199 199  (% class="lead" %)
200 200  Set up Connection to The Things Network application
201 201