...
Milesight Codec Downlink Encoder
Code Block |
---|
|
function Encode(fPort, obj)
{// Possible Options:
// userMode var encoded = []; : if ("Ambient_Temperature" or "Valve_Position_RCI_10_minutes"
in// obj)setValue { : 0 to 40 var new_vp_10min = obj.Valve_Position_RCI_10_minutes;
(0.5° resolution) for "Ambient_Temperature"
// setValue encoded = [new_vp_10min,0x00,0x26,0x00,0x00,0x00]; : 0 }to 100 (1% resolution) iffor ("Valve_Position_RCI_5_minutes"
in
obj)// safetyMode { : "Ambient_Temperature" or "Valve_Position"
// safetyValue var new_vp_5min = obj.Valve_Position_RCI_5_minutes; : 0 to 40 encoded = [new_vp_5min,0x00,0x26,0x10,0x00,0x00];
(0.5° resolution) for "Ambient_Temperature"
// safetyValue } : 0 to if100 ("Set_Point_Ambient_Temperature_RCI_10_Minutes" in obj1% resolution) for "Valve_Position"
{
// roomTemperature : 0 (Internal Ambient var new_spt_10min = (obj.Set_Point_Ambient_Temperature_RCI_10_Minutes)*2;
encoded = [new_spt_10min,0x00,0x26,0x08,0x00,0x00];
}
if ("Set_Point_Ambient_Temperature_RCI_5_Minutes" in obj)
{
var new_spt_5min = (obj.Set_Point_Ambient_Temperature_RCI_5_Minutes)*2;
encoded = [new_spt_5min,0x00,0x26,0x18,0x00,0x00];
}
return encoded;
} |
Downlink example
Code Block |
---|
|
{"Set_Point_Ambient_Temperature_RCI_10_Minutes":22}
{"Set_Point_Ambient_Temperature_RCI_5_Minutes":20}
{"Valve_Position_RCI_10_minutes":73}
{"Valve_Position_RCI_5_minutes":42Estimate) or Temperature from External Sensor (0.25° resolution)
// radioInterval : 5 or 10 or 60 or 120 or 480 (minutes)
// doReferenceRunNow : 0 or 1 (0-False; 1-True)
function Encode(fPort, obj) {
var mode = obj.userMode; // "Ambient_Temperature" or "Valve_Position"
var safetyMode = obj.safetyMode; // "Ambient_Temperature" or "Valve_Position"
var setValue = obj.setValue; // 0-40 for Ambient_Temperature, 0-100 for Valve_Position
var roomTemperature = obj.roomTemperature; // 0-40
var safetyValue = obj.safetyValue; // 0-40 for Ambient_Temperature, 0-100 for Valve_Position
var radioInterval = obj.radioInterval; // 5, 10, 60, 120, 480
var doReferenceRunNow = obj.doReferenceRunNow; // 0 or 1
var bytes = [0, 0, 0, 0, 0, 0];
// Byte 1: Set value
if (mode === "Ambient_Temperature") {
if (setValue < 0 || setValue > 40) {
throw new Error("Set value out of range for ambient mode");
}
else {
bytes[0] = setValue *2;
}
} else if (mode === "Valve_Position") {
if (setValue < 0 || setValue > 100) {
throw new Error("Set value out of range for valve mode");
}
else {
bytes[0] = setValue;
}
} else {
throw new Error("Invalid user mode");
}
// Byte 2: Room temperature (0-40)
if (roomTemperature < 0 || roomTemperature > 40) {
throw new Error("Room temperature out of range");
}
else {
bytes[1] = roomTemperature * 4;
}
// Byte 3: Safety value
if (safetyMode === "Ambient_Temperature") {
if (safetyValue < 0 || safetyValue > 40) {
throw new Error("Safety value out of range for ambient mode");
}
else {
bytes[2] = safetyValue * 2;
}
} else if (safetyMode === "Valve_Position") {
if (safetyValue < 0 || safetyValue > 100) {
throw new Error("Safety value out of range for valve mode");
}
else {
bytes[2] = safetyValue;
}
} else {
throw new Error("Invalid safety mode");
}
// Byte 4: Radio interval, user mode, safety mode
var radioBits;
switch (radioInterval) {
case 5:
radioBits = 1 << 4; // Radio interval 5 minutes
break;
case 10:
radioBits = 0 << 4; // Radio interval 10 minutes
break;
case 60:
radioBits = 2 << 4; // Radio interval 60 minutes
break;
case 120:
radioBits = 3 << 4; // Radio interval 120 minutes
break;
case 480:
radioBits = 4 << 4; // Radio interval 480 minutes
break;
default:
throw new Error("Invalid radio interval");
}
var userModeBits;
if (mode === "Ambient_Temperature") {
userModeBits = 2 << 2; // User mode "Ambient_Temperature" in bits 3 and 4
} else {
userModeBits = 0 << 2; // User mode "Valve_Position" in bits 3 and 4
}
var safetyModeBits;
if (safetyMode === "Ambient_Temperature") {
safetyModeBits = 0 << 0; // Safety mode "Ambient_Temperature" in bits 1 and 2
} else {
safetyModeBits = 2 << 0; // Safety mode "Valve_Position" in bits 1 and 2
}
bytes[3] = radioBits | userModeBits | safetyModeBits;
// Byte 5: Reserved (set to 0)
bytes[4] = 0;
// Byte 6: doReferenceRunNow bit (bit 8)
if (doReferenceRunNow < 0 || doReferenceRunNow > 1) {
throw new Error("Invalid doReferenceRunNow value");
}
else {
bytes[5] = doReferenceRunNow << 7;
}
return bytes;
} |
Downlink example
Code Block |
---|
|
// Example 1
{
"userMode": "Valve_Position",
"setValue": 42,
"roomTemperature": 0,
"safetyMode": "Ambient_Temperature",
"safetyValue": 19,
"radioInterval": 5,
"doReferenceRunNow": 1
}
// Example 2
{
"userMode": "Ambient_Temperature",
"setValue": 21.5,
"roomTemperature": 24,
"safetyMode": "Valve_Position",
"safetyValue": 0,
"radioInterval": 60,
"doReferenceRunNow": 0
}
// Example 3
{
"userMode": "Valve_Position",
"setValue": 36,
"roomTemperature": 0,
"safetyMode": "Valve_Position",
"safetyValue": 21,
"radioInterval": 5,
"doReferenceRunNow": 1
}
// Example 4
{
"userMode": "Ambient_Temperature",
"setValue": 20.5,
"roomTemperature": 23.75,
"safetyMode": "Ambient_Temperature",
"safetyValue": 18,
"radioInterval": 10,
"doReferenceRunNow": 0
} |