Manages the 1-wire bus

Dependents:   oldheating heating

web/web-1wire-script.js

Committer:
andrewboyson
Date:
2020-06-10
Revision:
8:ceafef18cbf7
Parent:
2:79cad6a51fd0
Child:
9:6f663ad53c7e

File content as of revision 8:ceafef18cbf7:

//1wire script
'use strict';

let allRomValues        = new Map();
let oneWireScanTime     = 0;
let oneWireLowTweak     = 0;
let oneWireFloatTweak   = 0;
let oneWireReadTweak    = 0;
let oneWireHighTweak    = 0;
let oneWireReleaseTweak = 0;
let oneWireTrace        = false;
let assignedRoms        = new Array();
let assignedNames       = new Array();

function getAssignedName(rom)
{
    for (let i = 0; i < assignedRoms.length; i++)
    {
        if (assignedRoms[i] === rom) return assignedNames[i]; 
    }
    return '';
}

function addRomValue(line)
{
    if (line) allRomValues.set(line.substr(0, 16), line.substr(16));
}
function parseAllRomValues(topic)
{
    allRomValues.clear();
    topic.split('\n').forEach(addRomValue);
}
function parseTimings(topic)
{
    let lines = topic.split('\n');
    oneWireScanTime     = Ajax.hexToSignedInt16(lines[0]);
    oneWireLowTweak     = Ajax.hexToSignedInt16(lines[1]);
    oneWireFloatTweak   = Ajax.hexToSignedInt16(lines[2]);
    oneWireReadTweak    = Ajax.hexToSignedInt16(lines[3]);
    oneWireHighTweak    = Ajax.hexToSignedInt16(lines[4]);
    oneWireReleaseTweak = Ajax.hexToSignedInt16(lines[5]);
    oneWireTrace        =                       lines[6] != '0';
}
function addRom(line)
{
    if (line)
    {
        assignedRoms.push(line.substr(0, 16))
        assignedNames.push(line.substr(16));
    }
}
function parseAssigned(topic)
{
    assignedRoms  = new Array();
    assignedNames = new Array();
    topic.split('\n').forEach(addRom);
}
function parse()
{
    let topics = Ajax.response.split('\f');
    parseTimings     (topics[0]);
    parseAllRomValues(topics[1]);
    parseAssigned    (topics[2]);
}
function display()
{
    let elem;
    elem = Ajax.getElementOrNull('ajax-device-values');
    if (elem) 
    {
        elem.textContent = '';
        for (let [key, value] of allRomValues)
        {
            elem.textContent += key;
            elem.textContent += ' '; 
            elem.textContent += OneWire.parseDS18B20(value).toFixed(1);
            elem.textContent += ' ';
            elem.textContent += getAssignedName(key);
            elem.textContent += '\r\n';
        }
    }
    elem = Ajax.getElementOrNull('ajax-1-wire-scan-time'    ); if (elem) elem.textContent = oneWireScanTime;
    elem = Ajax.getElementOrNull('ajax-1-wire-tweak-low'    ); if (elem) elem.textContent = oneWireLowTweak;
    elem = Ajax.getElementOrNull('ajax-1-wire-tweak-float'  ); if (elem) elem.textContent = oneWireFloatTweak;
    elem = Ajax.getElementOrNull('ajax-1-wire-tweak-read'   ); if (elem) elem.textContent = oneWireReadTweak;
    elem = Ajax.getElementOrNull('ajax-1-wire-tweak-high'   ); if (elem) elem.textContent = oneWireHighTweak;
    elem = Ajax.getElementOrNull('ajax-1-wire-tweak-release'); if (elem) elem.textContent = oneWireReleaseTweak;
    elem = Ajax.getElementOrNull('ajax-1-wire-trace'        ); if (elem) elem.setAttribute('dir', oneWireTrace ? 'rtl' : 'ltr');

    elem = Ajax.getElementOrNull('ajax-name-0'); if (elem) elem.textContent = assignedNames[0];
    elem = Ajax.getElementOrNull('ajax-name-1'); if (elem) elem.textContent = assignedNames[1];
    elem = Ajax.getElementOrNull('ajax-name-2'); if (elem) elem.textContent = assignedNames[2];
    elem = Ajax.getElementOrNull('ajax-name-3'); if (elem) elem.textContent = assignedNames[3];
    elem = Ajax.getElementOrNull('ajax-rom-0' ); if (elem) elem.value = assignedRoms[0];
    elem = Ajax.getElementOrNull('ajax-rom-1' ); if (elem) elem.value = assignedRoms[1];
    elem = Ajax.getElementOrNull('ajax-rom-2' ); if (elem) elem.value = assignedRoms[2];
    elem = Ajax.getElementOrNull('ajax-rom-3' ); if (elem) elem.value = assignedRoms[3];
}

Ajax.server     = '/1wire-ajax';
Ajax.onResponse = function() { parse(); display(); };
Ajax.init();