/* Psalm 127:1-2
As die HERE die huis nie bou nie, tevergeefs werk die wat daaraan bou;
as die HERE die stad nie bewaar nie, tevergeefs waak die wagter.
Tevergeefs dat julle vroeg opstaan, laat opbly, brood van smarte eet —
net so goed gee Hy dit aan sy beminde in die slaap! */
var dns = "https://hosting.wialon.com";
const API_ERROR = 'API_ERROR';
async function loadMessages(units) {
var sess = wialon.core.Session.getInstance();
var timeTo = sess.getServerTime(); // get ServerTime
var timeFrom = timeTo - 3600; // 1 hour
var ml = sess.getMessagesLoader();
for (var j = 0; j < units.length; j++) {
var unitId = units[j].unitId;
let data = await loadIntervalAsync(ml, {
unitId,
timeFrom,
timeTo,
loadCount: 10,
});
let custSensorValues = [];
let redflags = 0;
for (var i = data.messages.length - 1; i >= 0; i--) { // get last received message with values
if (data.messages[i].p.data_type === "E6") { // E6 = custom sensor values??
let custSensorValue = {};
for (property in data.messages[i].p) {
if (property !== "data_type") {
if (property.includes("pressure")) {
custSensorValue["" + property + ""] = (data.messages[i].p[property] * 0.0689475729).toFixed(2);
if (custSensorValue["" + property + ""] < 3 || custSensorValue["" + property + ""] > 4.45) redflags++;
} else {
custSensorValue["" + property + ""] = data.messages[i].p[property];
}
}
}
if (custSensorValues.length < 1)
custSensorValues.push(custSensorValue);
//check as alreeds sensor values, break
}
}
units[j].redFlags = redflags;
units[j].custSensorValues = custSensorValues;
}
return units;
}
function loadIntervalAsync(messageLoader, options) {
let {
unitId,
timeFrom,
timeTo,
flags = 0,
flagsMask = 0,
loadCount,
} = options;
return new Promise((resolve, reject) => {
messageLoader.loadInterval(
unitId,
timeFrom,
timeTo,
flags,
flagsMask,
loadCount,
(error, result) => {
if (error) {
reject({ type: API_ERROR, code: error, fullError: result });
return;
}
resolve(result);
},
);
});
}
//log message
function logmsg(text) { $("#log").empty().prepend(text + " ");}
function init() {
var session = wialon.core.Session.getInstance(); // get instance of current Session
// flags to specify what kind of data should be returned
var unit_flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.lastMessage),
resource_flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Resource.dataFlag.trailers);
session.loadLibrary("resourceTrailers");
session.loadLibrary("itemIcon");
session.updateDataFlags(
[{ type: "type", data: "avl_unit", flags: unit_flags, mode: 0 },
{ type: "type", data: "avl_resource", flags: resource_flags, mode: 0 }],
function (code) {
if (code) { logmsg(wialon.core.Errors.getErrorText(code)); return; }
var units = session.getItems("avl_unit");
if (!units || !units.length) { logmsg("No units found"); return; }
var resources = session.getItems("avl_resource"); // get loaded 'avl_resource's items
if (!resources || !resources.length) { logmsg("No trailers found!"); }
// Get all trailers that are linked/binded
let unitTrailers = [];
for (let i = 0; i < units.length; i++) {
let u = units[i];
unitId = u.getId();
for (let j = 0; j < resources.length; j++) {
let res = resources[j], // get loaded 'avl_resource's items
trailers = res.getTrailers() || {}; // get loaded trailer`s items from any resource
for (let trl in trailers) {
trailer = trailers[trl]; // iterate all trailers
if (u.getId() === trailer.bu) {
let unitTrailer = {};
unitTrailer.unitLinkedToId = unitId;
unitTrailer.trailerId = trailer.id;
unitTrailer.trailerName = trailer.n;
unitTrailers.push(unitTrailer);
}
}
}
}
// Create array of Units with trailers and properties
let unitWithTrailers = [];
// Get all linked trailers's unitId's - validate by name, position, speed - refactor this code
for (let i = 0; i < units.length; i++) {
var u = units[i];
for (let j = 0; j < unitTrailers.length; j++) {
var uTrailer = unitTrailers[j];
if (uTrailer.trailerName.trim() === u.getName()) {
let trailerPosition = u.getPosition();
//if (unitPosition && trailerPosition) {
// //unit.getPosition() === a.getPosition() && speed validation
//}
uTrailer.trailerUnitId = u.getId();
//move to above when sufficient date and remove from array if conditions not met - could only maybe happen in next for loop
//Remove trailers that are linked from units list
//units.splice(i, 1); i--;
}
}
}
// Create array of Units with trailers and properties
let allUnits = [];
for (let i = 0; i < units.length; i++) {
let unit = units[i];
let unitWithTrailer = {};
unitWithTrailer.unitId = unit.getId();
unitWithTrailer.unitIcon = unit.getIconUrl(32);
unitWithTrailer.unitName = unit.getName();
unitWithTrailer.unitPosition = unit.getPosition();
unitWithTrailer.linked = false;
let unitTrailersFound = [];
for (let j = 0; j < unitTrailers.length; j++) {
let uTrailer = unitTrailers[j];
if (unitWithTrailer.unitId === uTrailer.unitLinkedToId) {
unitTrailersFound.push(uTrailer);
unitWithTrailer.linked = true;
} //binded?
if (unitWithTrailer.unitName === uTrailer.trailerName) {
unitWithTrailer.isTrailer = true;
if (uTrailer.unitLinkedToId)
unitWithTrailer.linked = true;
}
else { unitWithTrailer.isTrailer = false; }
}
unitWithTrailer.trailers = unitTrailersFound;
unitWithTrailer.redFlags = 0;
allUnits.push(unitWithTrailer);
}
//loadAllUnitsMessages(units, unitWithTrailers);
main(allUnits).catch((error) => {
if (error.type === API_ERROR) {
let reason = error.fullError && error.fullError.reason;
logmsg(
wialon.core.Errors.getErrorText(error.code) +
(reason ? ' ' + reason : ''),
);
} else {
logmsg('See more detailed error in console: ' + error);
console.error(error);
}
});
}
);
}
async function main(units) {
units = await loadMessages(units);
displayUnits(units);
// There you can use `custSensorValues`
}
function joinTruckAndTrailers(units) {
let unitWithTrailers = [...units];
for (let i = 0; i < unitWithTrailers.length; i++) {
//currently only ONE trailer per unit
if (unitWithTrailers[i].trailers.length > 0) {
for (let j = 0; j < units.length; j++) {
if (unitWithTrailers[i].trailers[0].trailerUnitId === units[j].unitId) {
for (property in units[j].custSensorValues[0]) {
if (property.includes("pressure") || property.includes("temp") || property.includes("volt")) {
unitWithTrailers[i].custSensorValues[0]["" + property + ""] = units[j].custSensorValues[0][property];
}
}
unitWithTrailers[i].redFlags = unitWithTrailers[i].redFlags + units[j].redFlags;
//unitsToRemove.push(units[j].unitId);
}
}
}
}
//for (let i = 0; i < unitWithTrailers.length; i++) {
// for (let j = 0; j < unitsToRemove.length; j++) {
// if (unitWithTrailers[i] && (unitWithTrailers[i].unitId === unitsToRemove[j])) {
// unitWithTrailers.splice(i, 1); i--;
// }
// }
//}
return unitWithTrailers;
}
function displayUnits(uwt) {
uwt = joinTruckAndTrailers(uwt)
uwt.sort(function (a, b) {
if (a.unitName > b.unitName) return 1;
if (a.unitName < b.unitName) return -1;
return 0;
});
uwt.sort(function (a, b) {
if (a.redFlags > b.redFlags) return -1;
if (a.redFlags < b.redFlags) return 1;
return 0;
});
//$('.leftpane').empty().append('
Units
');
$('.leftpane').empty().append('
');
for (let i = 0; i < uwt.length; i++) {
var u = uwt[i];
var li = document.createElement("li");
li.id = u.unitId;
var icon = u.unitIcon; // get unit Icon url
var img = document.createElement('img');
img.src = icon;
li.appendChild(img);
var text = " " + u.unitName;
var t = document.createTextNode(text);
li.appendChild(t);
var span = document.createElement("SPAN");
if (u.trailers.length > 0) {
var linkicon = "images/trailericon.png"; // get unit Icon url
var linkimg = document.createElement('img');
linkimg.src = linkicon;
linkimg.className = "trailerimg listspanicons"
span.appendChild(linkimg);
}
if (u.isTrailer && u.linked) {
var linkicon = "images/truckicon.png"; // get unit Icon url
var linkimg = document.createElement('img');
linkimg.src = linkicon;
linkimg.className = "truckimg listspanicons"
span.appendChild(linkimg);
}
if (u.redFlags > 0) {
var txt = document.createTextNode("!");
span.className = "redflag listspanicons";
span.appendChild(txt);
}
li.appendChild(span);
document.getElementById("units").appendChild(li);
}
var list = document.querySelector('ul');
list.addEventListener('click', function (ev) {
var clearselect = document.getElementById("units").getElementsByTagName("li");
for (let i = 0; i < clearselect.length; i++) {
clearselect[i].classList.remove('checked')
}
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
$("#units").click(getSensors(parseInt(ev.target.id), uwt));
$("#units").click(getSelectedUnitInfo(ev.target.id, uwt));
//$("#units").click(getSelectedUnitDriverInfo(ev.target.id, uwt));
}
}, false);
}
function getSensors(unitId, uwt) { // construct sensors Select list for selected unit
var selectedUnitSensorIds = [];
var sess = wialon.core.Session.getInstance(); // get instance of current Session
var flags = wialon.item.Item.dataFlag.base |
wialon.item.Unit.dataFlag.sensors |
wialon.item.Unit.dataFlag.lastMessage;
sess.loadLibrary("unitSensors"); // load Sensor Library
sess.updateDataFlags( // load items to current session
[{ type: "type", data: "avl_unit", flags: flags, mode: 0 }], // Items specification
function (code) { // updateDataFlags callback
if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
var unit = sess.getItem(unitId);
var sens = unit.getSensors(); // get unit's sensors
for (var i in sens)
selectedUnitSensorIds.push(sens[i].n);
if (selectedUnitSensorIds.length > 0) getTrailerSensors(unitId, selectedUnitSensorIds, uwt);
});
}
function getTrailerSensors(unitId, selectedUnitSensorIds, uwt) { // construct sensors Select list for selected unit
let trailerUnitId = 0;
for (let i = 0; i < uwt.length; i++) {
if (uwt[i].unitId === unitId && uwt[i].trailers.length>0) {
trailerUnitId = uwt[i].trailers[0].trailerUnitId;
}
}
if (trailerUnitId) {
var sess = wialon.core.Session.getInstance(); // get instance of current Session
var flags = wialon.item.Item.dataFlag.base |
wialon.item.Unit.dataFlag.sensors |
wialon.item.Unit.dataFlag.lastMessage;
sess.loadLibrary("unitSensors"); // load Sensor Library
sess.updateDataFlags( // load items to current session
[{ type: "type", data: "avl_unit", flags: flags, mode: 0 }], // Items specification
function (code) { // updateDataFlags callback
if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
var unit = sess.getItem(trailerUnitId);
var sens = unit.getSensors(); // get unit's sensors
for (var i in sens)
selectedUnitSensorIds.push(sens[i].n);
if (selectedUnitSensorIds.length > 0) loadTruck(unitId, selectedUnitSensorIds, uwt);
});
} else {
loadTruck(unitId, selectedUnitSensorIds, uwt)
}
}
function loadTruck(unitId, selectedUnitSensorIds, uwt) {
//uwt = getTestUnits(1);
let unitIdx = 0;
for (let i = 0; i < uwt.length; i++) {
if (uwt[i].unitId === unitId) {
unitIdx = i;
}
}
let axeWheels = [];
let axeWheelValues = [];
let duplicate = "";
for (property in uwt[unitIdx].custSensorValues[0]) {
if (property.includes("pressure") || property.includes("temp") || property.includes("volt")) {
axeWheelsId = property.substring(1, 3);
if (duplicate !== property.substring(1, 3))
axeWheels.push(property.substring(1, 3));
duplicate = property.substring(1, 3);
}
}
for (let k = 0; k < axeWheels.length; k++) {
let axeWheel = {};
axeWheel.wheelType = "wheelactive"
axeWheel.id = axeWheels[k];
axeWheel.name = "A" + axeWheels[k].substring(0, 1) + "-T" + axeWheels[k].substring(1, 2);
axeWheel.pressureLabelColour = "green";
axeWheel.temperatureLabelColour = "green";
axeWheel.voltageLabelColour = "green";
axeWheel.sensorId = 0;
for (let s = 0; s < selectedUnitSensorIds.length; s++) {
if (axeWheel.id === selectedUnitSensorIds[s].substring(0, 2))
axeWheel.sensorId = selectedUnitSensorIds[s];
}
for (property in uwt[unitIdx].custSensorValues[0]) {
if (property.includes(axeWheels[k])) {
if (property.includes("pressure")) {
axeWheel.pressure = parseFloat(uwt[unitIdx].custSensorValues[0][property]).toFixed(2);
if (axeWheel.pressure < 3 || axeWheel.pressure > 5) {
axeWheel.wheelType = "wheelred";
axeWheel.pressureLabelColour = "red";
}
}
if (property.includes("temp")) {
axeWheel.temperature = parseFloat(uwt[unitIdx].custSensorValues[0][property]);
if (axeWheel.temperature < 12 || axeWheel.temperature >= 20) {
if (axeWheel.wheelType !== "wheelred") axeWheel.wheelType = "wheelorange";
axeWheel.temperatureLabelColour = "orange";
}
}
if (property.includes("volt")) {
axeWheel.voltage = parseFloat(uwt[unitIdx].custSensorValues[0][property]);
if (axeWheel.voltage < 3.2 || axeWheel.voltage > 4) {
if (axeWheel.wheelType !== "wheelred" && axeWheel.wheelType !== "wheelorange") axeWheel.wheelType = "wheelyellow";
axeWheel.voltageLabelColour = "yellow";
}
}
}
}
let tableInfo = "
" +
"" +
"
" + axeWheel.name + "
" +
"
" + axeWheel.sensorId + "
" +
"
" + axeWheel.pressure + " bar
" +
"
" + axeWheel.temperature + " ℃
" +
"
" + axeWheel.voltage + " V
" +
"" +
"
";
axeWheel.tableInfo = tableInfo;
axeWheelValues.push(axeWheel);
}
let wheelHTML = "";
let noOfActiveAxes = (axeWheelValues.length - 2) / 4
for (let axeNo = 2; axeNo < 10; axeNo++) {
if (axeNo <= noOfActiveAxes + 1) {
wheelHTML += "
";
wheelHTML += ""
for (let k = 0; k < axeWheelValues.length; k++) {
if (axeWheelValues[k].id.substring(0, 1) === axeNo.toString()) {
let wheelNo = (axeWheelValues[k].id.substring(1, 2)).toString();
wheelHTML += "
" + "
"
+ "
" + axeWheelValues[k].tableInfo + "
"
}
}
wheelHTML += "
";
} else {
wheelHTML += "
";
wheelHTML += ""
for (let k = 1; k < 5; k++) {
let wheelNo = k.toString();
wheelHTML +=
"
" + "
"
}
wheelHTML += "
";
}
}
let idx11 = 0;
let idx14 = 0;
for (let k = 0; k < axeWheelValues.length; k++) {
if (axeWheelValues[k].id === "11") {
idx11 = k
}
if (axeWheelValues[k].id === "14") {
idx14 = k;
}
}
$("#truckunit").empty().append(
"
"
+ ""
+ "
"
+ ""
+ "
" + axeWheelValues[idx11].tableInfo + "
"
+ ""
+ "
" + axeWheelValues[idx14].tableInfo + "
"
+ "
"
+ "" +
"
" +
"" +
"" +
"" +
"
"
+ wheelHTML +
"
"
);
}
function getSelectedUnitDriverInfo(unitId) {
//$('.rightpaneunitinfo').empty().append('
');
if (!unitId) return;
//var unit = wialon.core.Session.getInstance().getItem(unitId);
var sess = wialon.core.Session.getInstance(); // get instance of current Session
// flags to specify what kind of data should be returned
var unit_flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base),
res_flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Resource.dataFlag.drivers);
// load Icon & Driver Library
sess.loadLibrary("resourceDrivers");
sess.loadLibrary("itemIcon");
// load items to current session
sess.updateDataFlags(
[{ type: "type", data: "avl_unit", flags: unit_flags, mode: 0 },
{ type: "type", data: "avl_resource", flags: res_flags, mode: 0 }],
function (code) { // updateDataFlags callback
if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
// get loaded 'avl_unit's items
var units = sess.getItems("avl_unit");
if (!units || !units.length) { msg("Units not found"); return; }
// get loaded 'avl_resource's items
var ress = sess.getItems("avl_resource"); // get loaded 'avl_resource's items
if (!ress || !ress.length) { msg("Resours not found"); return; }
var select = "", // html-string of any select object
all_drivers = {}, // object with all drivers info
d_obj; // any element of all_drivers list
for (var j = 0; j < ress.length; j++) {
var res = ress[j], // get loaded 'avl_resource's items
drivers = res.getDrivers() || {}; // get loaded driver`s items from any resource
for (var drv in drivers) {
driver = drivers[drv]; // iterate all drivers
select += ""; // append option to select object for any row
// any element of all_drivers list
d_obj = {
id: driver.id,
name: driver.n,
icon: res.getDriverImageUrl(driver, 32)
};
// all_drivers list consists from bind unit key and array of drivers
if (!all_drivers[driver.bu])
all_drivers[driver.bu] = [d_obj]; // if first element
else
all_drivers[driver.bu].push(d_obj); // if next element
}
}
// construct table data using found units
for (var i = 0; i < units.length; i++) {
var u = units[i],
u_id = u.getId(), // id of current unit
row = ""; // html-string of any row in table
// begin row html-string
row += "
";
row += "";
row += "
" + u.getName() + "
";
if (typeof all_drivers[u_id] != "undefined") { // check, bind driver to unit or no
array = all_drivers[u_id]; // read regular array of drivers
for (var k = 0, len = array.length; k < len; k++) {
d_obj = array[k]; // read any element of array and build div with info about driver
row += "
');
if (!unitId) return;
var unit = wialon.core.Session.getInstance().getItem(unitId);
if (!unit) { logmsg("Unit not found"); return; }
var pos = unit.getPosition();
var text = "
" + unit.getName() + "
";
var icon = unit.getIconUrl(32);
if (icon) text = "" + text + "
";
if (pos) {
var time = wialon.util.DateTime.formatTime(pos.t);
text += "Last message: " + time + " " +
"Speed: " + pos.s;
wialon.util.Gis.getLocations([{ lon: pos.x, lat: pos.y }], function (code, address) {
if (code) { logmsg(wialon.core.Errors.getErrorText(code)); return; }
//logmsg(text + " Location of unit: " + address + "
");
$("#truckinfo").empty().append(text + " Location: " + address + "");
});
} else
$("#truckinfo").empty().append(text + " Location Unknown");
for (let i = 0; i < uwt.length; i++) {
if (uwt[i].unitId === parseInt(unitId)) {
//currently only ONE trailer per unit
if (!uwt[i].isTrailer && uwt[i].trailers.length === 0) {
getAllTrailersNotLinked(uwt[i].unitId);
$('.truckinfobox').append('');
}
}
}
for (let i = 0; i < uwt.length; i++) {
if (uwt[i].unitId === parseInt(unitId)) {
//currently only ONE trailer per unit
if (uwt[i].trailers.length > 0) {
var uTrailer = wialon.core.Session.getInstance().getItem(uwt[i].trailers[0].trailerUnitId);
if (!uTrailer) { logmsg("Unit not found"); return; }
var uTrailerPos = uTrailer.getPosition();
}
$('.rightpaneunitinfo').append('');
displaySelectedTrailerInfo(uTrailer, uTrailerPos);
}
}
}
function displaySelectedTrailerInfo(unit, pos) {
var text = "
" + unit.getName() + "
";
var icon = unit.getIconUrl(32);
if (icon) text = "" + text + "
";
if (pos) {
var time = wialon.util.DateTime.formatTime(pos.t);
text += "Last message: " + time + " " +
"Speed: " + pos.s;
wialon.util.Gis.getLocations([{ lon: pos.x, lat: pos.y }],
function (code, address) {
if (code) { logmsg(wialon.core.Errors.getErrorText(code)); return; }
//logmsg(text + " Location of unit: " + address + "
");
$("#trailerinfobox").empty().append(text + " Location: " + address + "");
});
} else
$("#trailerinfobox").empty().append(text + " Location Unknown");
}
function UnlinkTrailer() {
alert("I'm going to unnlink the trailer");
}
function logout() {
var sess = wialon.core.Session.getInstance();
if (sess && sess.getId()) {
sess.logout(function () {
});
}
logmsg("");
$(".leftpane").empty();
$(".middlepane").empty();
$('.rightpaneunitinfo').empty()
document.getElementById("logout").setAttribute("disabled", "");
document.getElementById("login").removeAttribute("disabled");
}
function getToken() {
var url = dns + "/login.html"; // Tyretrack site DNS + "/login.html"
url += "?client_id=" + "App"; // Tyretrack application name
url += "&access_type=" + 0x900; // access level, 0x800 = "Edit sensitive data"
url += "&activation_time=" + 0;
url += "&duration=" + 604800;
url += "&flags=" + 0x1;
url += "&redirect_uri=" + dns + "/post_token.html";
window.addEventListener("message", tokenRecieved);
window.open(url, "_blank", "width=650, height=350, top=300, left=500");
}
function tokenRecieved(e) {
var msg = e.data;
window.removeEventListener("message", tokenRecieved);
if (typeof msg == "string" && msg.indexOf("access_token=") >= 0) {
var token = msg.replace("access_token=", "");
document.getElementById("login").setAttribute("disabled", "");
document.getElementById("logout").removeAttribute("disabled");
var sess = wialon.core.Session.getInstance();
sess.initSession("https://hst-api.wialon.com");
sess.loginToken(token, "", function (code) {
if (code)
{ logmsg(wialon.core.Errors.getErrorText(code)); return; }
var user = wialon.core.Session.getInstance().getCurrUser().getName();
logmsg(user + " - Logged successfully");
init();
});
}
}
function getAllTrailersNotLinked(unitToLinkTo) {
var sess = wialon.core.Session.getInstance(); // get instance of current Session
// flags to specify what kind of data should be returned
var unit_flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base),
res_flags = wialon.util.Number.or(wialon.item.Item.dataFlag.base, wialon.item.Resource.dataFlag.drivers);
// load Icon & Driver Library
sess.loadLibrary("resourceTrailers");
sess.loadLibrary("itemIcon");
//load items to current session
sess.updateDataFlags(
[{ type: "type", data: "avl_unit", flags: unit_flags, mode: 0 },
{ type: "type", data: "avl_resource", flags: res_flags, mode: 0 }],
function (code) { // updateDataFlags callback
if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
// get loaded 'avl_unit's items
var units = sess.getItems("avl_unit");
if (!units || !units.length) { msg("Units not found"); return; }
// get loaded 'avl_resource's items
var resources = sess.getItems("avl_resource"); // get loaded 'avl_resource's items
if (!resources || !resources.length) { msg("Resours not found"); return; }
let resourceId = 0;
// Get all trailers that are linked/binded
let unitTrailers = [];
for (let j = 0; j < resources.length; j++) {
let res = resources[j]; // get loaded 'avl_resource's items
if (res.getName() === 'tyretrack1') {
resourceId = res.getId();
trailers = res.getTrailers() || {}; // get loaded trailer`s items from any resource
for (let trl in trailers) {
trailer = trailers[trl]; // iterate all trailers
if (trailer.bu === 0) { //not binded/linked to unit
let unitTrailer = {};
//unitTrailer.unitLinkedToId = unitId;
unitTrailer.trailerId = trailer.id;
unitTrailer.trailerName = trailer.n;
for (let i = 0; i < units.length; i++) {
let u = units[i];
if (u.getName() === unitTrailer.trailerName) {
unitTrailer.unitIcon = u.getIconUrl(32);
}
}
unitTrailers.push(unitTrailer);
}
}
break;
}
}
$("#trailerlinkcontent").empty().append(
'×'
);
$('#trailerlinkcontent').append('
Select a trailer to link:
');
$('#trailerlinkcontent').append('
');
for (let i = 0; i < unitTrailers.length; i++) {
var u = unitTrailers[i];
var li = document.createElement("li");
li.id = u.trailerId;
var icon = u.unitIcon; // get unit Icon url
var img = document.createElement('img');
img.src = icon;
li.appendChild(img);
var text = " " + u.trailerName;
var t = document.createTextNode(text);
li.appendChild(t);
document.getElementById("trailersNotLinked").appendChild(li);
}
var modal = document.getElementById("unitlinkmodal");
var btn = document.getElementById("linkTrailerButton");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function () {
modal.style.display = "block";
}
span.onclick = function () {
modal.style.display = "none";
}
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
var list = document.getElementById("trailersNotLinked");
list.addEventListener('click', function (ev) {
if (ev.target.tagName === 'LI') {
$("#trailerlinkcontent").click(linkTrailerToUnit(parseInt(ev.target.id), unitToLinkTo, resourceId));
modal.style.display = "none";
}
}, false);
}
);
}
function linkTrailerToUnit(trailerId, unitId, resourceId) {
var sess = wialon.core.Session.getInstance(), // session
unit = sess.getItem(unitId), // // get unit by id
resId = resourceId, // get resource id
trailerId = trailerId, // get driver id
res = sess.getItem(resId), // // get resource by id
trailer = res.getTrailer(trailerId), // get driver by id
isBind = trailer.bu != unitId; // check, bind trailer or not
res.bindTrailerToUnit(trailer, unit, 0, isBind, qx.lang.Function.bind(function (res, trailer, code, result) {
if (code) { logmsg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
var temp = 0;
}, this, res, trailer));
}
$(document).ready(function () {
//getToken(); #now on login button
});