Manages the 1-wire bus

Dependents:   oldheating heating

web/web-1wire-script.inc

Committer:
andrewboyson
Date:
2019-04-27
Revision:
2:79cad6a51fd0
Parent:
http/http-1wire-script.inc@ 1:c272b1fcc834
Child:
8:ceafef18cbf7

File content as of revision 2:79cad6a51fd0:

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