// Décodeur LoRaWAN officiel : CPL03-LB (Dragino) // Source : https://github.com/TheThingsNetwork/lorawan-devices/blob/master/vendor/dragino/cpl03-lb.js // The Things Network Device Repository, fichier publié par le constructeur, distribué sans modification. // Format : decodeUplink(input) (TTN v3 / ChirpStack v4). Hébergé par smartbuildings.fr. function decodeUplink(input) {\n return { \n data: Decode(input.fPort, input.bytes, input.variables)\n }; \n}\nfunction datalog(i,bytes){\n var aa=(bytes[0+i]&0x08)?\"CPL03\":\"CPL01\";\n var bb=(bytes[0+i]&0x20)?\"PART\":\"SUM\";\n var cc=(bytes[0+i]&0x10)?\"YES\":\"NO\"; \n var dd=(bytes[0+i]&0x02)?\"TRUE\":\"FALSE\"; \n var ee=(bytes[0+i]&0x01)?\"OPEN\":\"CLOSE\"; \n var ff=(bytes[0+i]&0x04)>>2;\n var gg=(bytes[0+i]&0x07);\n var hh=(bytes[1+i]<<16 | bytes[2+i]<<8 | bytes[3+i]).toString(10);\n var ii=(bytes[4+i]<<16 | bytes[5+i]<<8 | bytes[6+i]).toString(10);\n var jj= getMyDate((bytes[7+i]<<24 | bytes[8+i]<<16 | bytes[9+i]<<8 | bytes[10+i]).toString(10));\n var string;\n \n if(aa==\"CPL01\")\n string='['+aa+','+bb+','+cc+','+dd+','+ee+','+ff+','+hh+','+ii+','+jj+']'+','; \n else\n string='['+aa+','+bb+','+cc+','+gg+','+hh+','+ii+','+jj+']'+','; \n \n return string;\n}\n\nfunction getzf(c_num){ \n if(parseInt(c_num) < 10)\n c_num = '0' + c_num; \n\n return c_num; \n}\n\nfunction getMyDate(str){ \n var c_Date;\n if(str > 9999999999)\n c_Date = new Date(parseInt(str));\n else \n c_Date = new Date(parseInt(str) * 1000);\n \n var c_Year = c_Date.getFullYear(), \n c_Month = c_Date.getMonth()+1, \n c_Day = c_Date.getDate(),\n c_Hour = c_Date.getHours(), \n c_Min = c_Date.getMinutes(), \n c_Sen = c_Date.getSeconds();\n var c_Time = c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) +' '+ getzf(c_Hour) +':'+ getzf(c_Min) +':'+getzf(c_Sen); \n \n return c_Time;\n}\n\nfunction Decode(fPort, bytes, variables) {\n if(fPort==0x03)\n {\n var pnack= ((bytes[0]>>7)&0x01) ? \"True\":\"False\"; \n for(var i=0;i>4&0x0f)+'.'+(bytes[2]&0x0f);\n var bat= (bytes[5]<<8 | bytes[6])/1000;\n \n return {\n SENSOR_MODEL:sensor,\n FIRMWARE_VERSION:firm_ver,\n FREQUENCY_BAND:freq_band,\n SUB_BAND:sub_band,\n battery:bat,\n };\n }\n else if(fPort==0x07)\n {\n var decode={};\n decode.EXTI_PA8_Trigger= (bytes[0] & 0x02)? \"TRUE\":\"FALSE\"; \n decode.EXTI_PA8_Status= (bytes[0] & 0x01)? \"CLOSE\":\"OPEN\"; \n decode.EXTI_PA4_Trigger= (bytes[0] & 0x08)? \"TRUE\":\"FALSE\";\n decode.EXTI_PA4_Status= (bytes[0] & 0x04)? \"CLOSE\":\"OPEN\"; \n decode.EXTI_PB15_Trigger= (bytes[0] & 0x20)? \"TRUE\":\"FALSE\";\n decode.EXTI_PB15_Status= (bytes[0] & 0x10)? \"CLOSE\":\"OPEN\";\n return decode;\n }\n else\n { \n var work_mod=(bytes[0]&0x08)?\"CPL03\":\"CPL01\";\n var count_mod=(bytes[0]&0x20)?\"PART\":\"SUM\"; \n var tdc_interval=(bytes[0]&0x10)?\"YES\":\"NO\"; \n var ttrig_mod=(bytes[0]&0x04)>>2; \n var alarm=(bytes[0]&0x02)?\"TRUE\":\"FALSE\"; \n var pin_status=(bytes[0]&0x01)?\"OPEN\":\"CLOSE\"; \n var total_pulse=bytes[1]<<16 | bytes[2]<<8 | bytes[3];\n var last_duration=bytes[4]<<16 | bytes[5]<<8 | bytes[6]; \n var data_time= getMyDate((bytes[7]<<24 | bytes[8]<<16 | bytes[9]<<8 | bytes[10]).toString(10));\n var calculate_flag=(bytes[0]&0x07);\n var pa8_total_pulse=bytes[1]<<16 | bytes[2]<<8 | bytes[3];\n var pa4_total_pulse=bytes[4]<<16 | bytes[5]<<8 | bytes[6];\n var pb15_total_pulse=bytes[7]<<16 | bytes[8]<<8 | bytes[9];\n var pa8_roc_flag=(bytes[10]&0x01)?\"TRUE\":\"FALSE\";\n var pa4_roc_flag=(bytes[10]&0x02)?\"TRUE\":\"FALSE\";\n var pb15_roc_flag=(bytes[10]&0x04)?\"TRUE\":\"FALSE\";\n \n if(work_mod==\"CPL01\")\n {\n return {\n work_mod:work_mod, \n count_mod:count_mod, \n tdc:tdc_interval, \n alarm:alarm,\n pin_status:pin_status,\n ttrig_mod:ttrig_mod,\n pulsecounter:total_pulse,\n last_duration:last_duration,\n time:data_time\n };\n }\n else if(work_mod==\"CPL03\")\n {\n return {\n work_mod:work_mod,\n count_mod:count_mod, \n tdc:tdc_interval,\n calculate_flag:calculate_flag,\n pa8_pulsecounter:pa8_total_pulse,\n pa4_pulsecounter:pa4_total_pulse,\n pb15_pulsecounter:pb15_total_pulse,\n pa8_roc:pa8_roc_flag,\n pa4_roc:pa4_roc_flag,\n pb15_roc:pb15_roc_flag\n }; \n }\n }\n}