Source: functions_voltage.js

"use strict";

/**
* Gets a list of phase IDs and associated description. 
* PhaseID is used in many calls to differentiate between systems (i.e. single phase or three phase).
* Gives PhaseID, Description
* @param {boolean} header header optional parameter, will display header row if set to true
* @return PhaseID, Description
* @customfunction
*/
function phases(header) {
  var json = httpGetCall("electrical/voltage/phases");
  var data = JSON.parse(json);
  var result = dataItemArray(data, enDataType.phases, header);
  return result;
}

/**
* AC voltage drop across an impedance (I*Z), V.
* @param {number} resistance the resistance of the circuit in Ohm.
* @param {number} reactance the reactance of the circuit in Ohm.
* @param {number} current the current flowing in the circuit.
* @param {number} powerFactor the power factor of the circuit.
* @return Voltage drop.
* @customfunction
*/
function voltageDropAC(resistance, reactance, current, powerFactor) {
  //I = complexCurrentPF(current,powerFactor);
  var json = httpGetCall("electrical/voltage/vd/" + resistance + "/" + reactance + "/" + current + "/" + powerFactor);
  var data = JSON.parse(json);
  return data.m_magnitude;
}


/**
* DC voltage drop across a resistance (I*R), V.
 * @param {number} resistance the resistance of the circuit in Ohm.
 * @param {number} current the current flowing in the circuit.
* @return Voltage drop, V.
* @customfunction
*/
function voltageDropDC(resistance, current) {
  var json = httpGetCall("electrical/voltage/vd/" + resistance + "/" + 0 + "/" + current + "/" + 1);
  var data = JSON.parse(json);
  return data.m_magnitude;
}

/**
* Voltage drop (in V) calculated in accordance with CENELEC technical report CLC/TR 50480. 
* This is intended for calculation of voltage drop across cables, where conductor impedance is known.
* @param {number} voltage magnitude of the voltage, V.
* @param {number} voltagePhase phase angle of the voltage in radians
* @param {number} phase phase configuration, see =phases()
 * @param {number} resistance the resistance of the cable/circuit in Ohm.
 * @param {number} reactance the reactance of the cable/circuit in Ohm.
 * @param {number} current the current flowing in the cable/circuit, A.
 * @param {number} powerFactor the power factor of the cable/circuit, A.
* @return Voltage drop, V.
* @customfunction
*/
function voltageDropCenelec(voltage, voltagePhase, phase, resistance, reactance, current, powerFactor) {
  var json = httpGetCall("electrical/voltage/vd/" + voltage + "/" + voltagePhase + "/" + phase + "/" + resistance + "/" + reactance + "/" + current + "/" + powerFactor);
  var data = JSON.parse(json);
  return data.VoltageDrop.m_magnitude;
}

/**
* Voltage drop (in V) calculated in accordance with CENELEC technical report CLC/TR 50480. 
* This is intended for calculation of voltage drop across cables, where conductor impedance is known.
* @param {complex} voltage  voltage in complex form, V.
* @param {number} phase phase configuration, see =phases()
 * @param {complex} impedance impedance in complex form of the cable/circuit in Ohm.
 * @param {complex} current current in complex form flowing in the cable/circuit, A.
* @return Voltage drop, V.
* @customfunction
*/
function voltageDropCenelecCmplx(voltage, phase, impedance, current) {
  var json = httpGetCall("electrical/voltage/vd/" + complexMag(voltage) + "/" + complexPhase(voltage) + "/" + phase + "/" + complexRe(impedance) + "/" + complexIm(impedance) + "/" + complexMag(current) + "/" + complexPF(current));
  var data = JSON.parse(json);
  return cmplx(data.VoltageDrop.m_real, data.VoltageDrop.m_imaginary);
}

/**
* Voltage drop (in % decimal) calculated in accordance with CENELEC technical report CLC/TR 50480. 
* This is intended for calculation of voltage drop across cables, where conductor impedance is known.
* @param {complex} voltage  voltage in complex form, V.
* @param {number} phase phase configuration, see =phases()
* @param {complex} impedance impedance in complex form of the cable/circuit in Ohm.
* @param {complex} current current in complex form flowing in the cable/circuit, A.
* @return Voltage drop, % decimal.
* @customfunction
*/
function voltageDropCenelecCmplxPercent(voltage, phase, impedance, current) {
  var json = httpGetCall("electrical/voltage/vd/" + complexMag(voltage) + "/" + complexPhase(voltage) + "/" + phase + "/" + complexRe(impedance) + "/" + complexIm(impedance) + "/" + complexMag(current) + "/" + complexPF(current));
  var data = JSON.parse(json);
  return data.Percentage / 100;
}

/**
* Voltage drop (in % decimal) calculated in accordance with CENELEC technical report CLC/TR 50480. 
* This is intended for calculation of voltage drop across cables, where conductor impedance is known.
* @param {number} voltage magnitude of the voltage, V.
* @param {number} voltagePhase phase angle of the voltage in radians
* @param {number} phase phase configuration, see =phases()
 * @param {number} resistance the resistance of the cable/circuit in Ohm.
 * @param {number} reactance the reactance of the cable/circuit in Ohm.
 * @param {number} current the current flowing in the cable/circuit, A.
 * @param {number} powerFactor the power factor of the cable/circuit, A.
* @return Voltage drop, % decimal.
* @customfunction
*/
function voltageDropCenelecPercent(voltage, voltagePhase, phase, resistance, reactance, current, powerFactor) {
  var json = httpGetCall("electrical/voltage/vd/" + voltage + "/" + voltagePhase + "/" + phase + "/" + resistance + "/" + reactance + "/" + current + "/" + powerFactor);
  var data = JSON.parse(json);
  return data.Percentage / 100;
}

/**
* Calculates a voltage drop across a cable or circuit element. 
* Voltage drop across an impedance (I*Z), V.
 * @param {complex} impedance (complex form) the impedance of the circuit in Ohm.
 * @param {complex} current (complex form) the current of the circuit in A.
* @return Voltage drop, V.
* @customfunction
*/
function voltageDropCmplx(impedance, current) {
  var json = httpGetCall("electrical/voltage/vd/" + complexRe(impedance) + "/" + complexIm(impedance) + "/" + complexMag(current) + "/" + complexPF(current));
  var data = JSON.parse(json);
  return cmplx(data.m_real, data.m_imaginary);
}


/**
* Calculates the 3-phase Line-line voltage, given the line-neutral. 
* If available standards are used (i.e. 400/230V),otherwise value is calculated.
* @param {number} voltage magnitude of the voltage (line-line), V.
* @return Line-line voltage.
* @customfunction
*/
function voltage3pLL(voltage) {
  var json = httpGetCall("electrical/voltage/" + voltage + "/" + 0 + "/" + 1);
  var data = JSON.parse(json);
  return complexMag(complexReIm(data.U_LL.m_real, data.U_LL.m_imaginary));
}


/**
* Calculates the 3-phase Line-line voltage, given the line-neutral. 
* If available standards are used (i.e. 400/230V),otherwise value is calculated.
* @param {complex} voltage voltage in complex form (line-line), V.
* @return Line-line voltage.
* @customfunction
*/
function voltage3pLLCmplx(voltage) {
  var json = httpGetCall("electrical/voltage/" + complexMag(voltage) + "/" + complexPhase(voltage) + "/" + 1);
  var data = JSON.parse(json);
  return cmplx(data.U_LL.m_real, data.U_LL.m_imaginary);
}

/**
* Calculates the 3-phase Line-neutral voltage, given the line-line. 
* If available standards are used (i.e. 400/230V),otherwise value is calculated.
* @param {number} voltage magnitude of the voltage (line-neutral), V.
* @return Line-neutral voltage.
* @customfunction
*/
function voltage3pLN(voltage) {
  var json = httpGetCall("electrical/voltage/" + voltage + "/" + 0 + "/" + 3);
  var data = JSON.parse(json);
  return complexMag(complexReIm(data.U_LN.m_real, data.U_LN.m_imaginary));
}


/**
* Calculates the 3-phase Line-neutral voltage, given the line-line. 
* If available standards are used (i.e. 400/230V),otherwise value is calculated.
* @param {complex} voltage voltage in complex form (line-neutral), V.
* @return Line-neutral voltage.
* @customfunction
*/
function voltage3pLNCmplx(voltage) {
  var json = httpGetCall("electrical/voltage/" + complexMag(voltage) + "/" + complexPhase(voltage) + "/" + 3);
  var data = JSON.parse(json);
  return cmplx(data.U_LN.m_real, data.U_LN.m_imaginary);
}