/** Main app logic
*/
var app = (function () {
var self = this;
/** @private */
var map = null,
session = null,
overlay = null,
unitsData = {};
//todo
//для новых маршрутов нужно создать
и заголовок приукрасить
var r18 = ["C324ET", "C272ET", "C318ET", "C434ET", "T645PP", "C650ET", "T229EC", "C467ET", "A560TH", "C330ET", "C503ET", "C469ET", "K226TC", "C523ET", "K232KB", "C979PA", "T102EC", "154", "150", "272", "240", "294", "234", "78", "274", "358", "84", "288", "86", "344", "342", "112", "6", "330", "152", "90", "214", "М265ТЕ", "192", "304", "110", "126", "190", "58", "98", "T538PP", "76", "34", "350", "296"]; //маршрут 18
var r43 = ["A842ET", "A901ET", "C085EX", "C077EX", "C040EX", "O085EY", "Y894MC", "C150EX", "334", "40", "230", "100", "80", "242", "68", "88", "66", "228", "132", "186", "C039EX", "204"]; //перечислить номера 43 маршруток
var r1 = ["A060EP", "T418MX", "T573PP", "H724MX", "E488EB", "E500EB", "A042EP", "E479EB", "H083EB", "M276TE", "14", "244", "136", "0", "K006ET", "174", "10", "2", "18", "22", "280", "H012EB", "166", "72", "184", "170", "222", "46", "140", "336", "H081EB", "198", "178", "284", "282", "114", "160", "286", "60", "164", "134", "226", "196", "320", "182", "128", "276"]; //перечислить номера 1 маршрута
var r27 = ["K468ET", "O378MX", "Y556MX", "K465ET", "M592PY", "O392MX", "P773EB", "C492ET", "H082EB", "246", "T005AB", "94", "264", "356", "258", "200", "250", "32", "232", "180", "120", "290", "162", "202", "74", "T767EC", "270", "48", "236", "4", "322", "96", "102", "172"]; //перечислить номера 27 маршрута
var r10 = ["007327", "007337", "007386", "007390", "007256", "007358", "007264", "254", "108", "262", "104", "00794", "00708", "142", "12", "50", "007252", "220", "238", "260", "82", "007355", "007237", "92", "252", "248", "00767", "00753", "007240", "302", "007231", "007389", "007364", "00770", "352", "176", "206", "146", "340", "70", "00745", "00766", "007261", "256", "007325", "00715", "00732", "224", "118", "26", "007354", "52", "278", "A667KK", "00756", "007328", "64", "00739", "266", "54", "00796", "00713", "314", "210", "00759", "00775", "00717", "44", "20", "338", "00741", "00790", "007369", "00795", "00724", "332", "007385", "00783", "00758", "188", "168", "8", "00793", "00754", "007025", "16", "00788", "00711", "00710", "216", "007391", "268", "007388", "007370", "007349", "007326", "00781", "00719", "62", "00714", "310", "00786", "354", "00709", "00704"]; //маршрутки 10
var r81 = ["O122EY", "C975PA", "O060EY"]; //автобус 81
var r1a = ["528", "529", "527"]; //автобус 1a
/** Print message to log
*/
function msg(text) {
}
/** Random color generator
*/
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
/** Execute after login succeed
*/
function init() {
var flags = wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.messageParams;
session.loadLibrary('itemIcon'); // load Icon Library
session.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));
} else {
showUnits();
}
});
}
/** Show units on map and bind event
*/
function showUnits(){
// get loaded 'avl_units's items
var units = session.getItems('avl_unit');
// check if units found
if (!units || !units.length){
msg('Units not found');
return;
}
var bounds = [];
for (var i = 0, params = null, pos = null, s = null, data = {}; i< units.length; i++) {
params = units[i].getMessageParams(); // get unit state
pos = params['posinfo'];
if (pos && pos.v) {
getAddr([pos.v.y, pos.v.x], units[i]._id,units[i].getName());
};
// check if map created and we can detect position of unit
if (map && pos && pos.v) {
// add point to bounds
bounds.push([pos.v.y, pos.v.x]);
var icon = L.icon({
iconUrl: units[i].getIconUrl(32),
iconAnchor: [16, 16]
});
s = params['speed'] ? params['speed'].v : 'n/a';
// construct data to store it and reuse
unitsData[units[i].getId()] = {
marker: L.marker({lat: pos.v.y, lng: pos.v.x}, {icon: icon})
.addTo(map)
.bindPopup(units[i].getName()),
tail: L.polyline({lat: pos.v.y, lng: pos.v.x}, {color: getRandomColor(), opacity: 0.8})
.addTo(map)
};
}
// register event listener
units[i].addListener('changeMessageParams', handleParamsChange);
}
// fit bounds to show all units
// !!! UNCOMENT IF YOU WANT TO SEE ALL UNITS
//map.fitBounds(bounds);
}
/** Message parameters event handler
* @param {Event} event
*/
function handleParamsChange(event) {
// get data from event
var data = event.getData();
var pos = data['posinfo'];
if (pos) {
var unit = event.getTarget();
//call custom getAddr function
if (pos.v) {
getAddr([pos.v.y, pos.v.x],unit.getId(),unit.getName());
};
if (unit.getId() in unitsData) {
// move marker
var marker = unitsData[unit.getId()].marker;
marker.setLatLng({lat: pos.v.y, lng: pos.v.x});
//поворот маркера
if (pos.v.c) {
//$(marker._icon)[0].style['-webkit-transform'] = $(marker._icon)[0].style['-webkit-transform'] + " rotate("+pos.v.c+"deg)";
//$(marker._icon)[0].style['-ms-transform'] = $(marker._icon)[0].style['-ms-transform'] + " rotate("+pos.v.c+"deg)";
//$(marker._icon)[0].style['-moz-transform'] = $(marker._icon)[0].style['-moz-transform'] + " rotate("+pos.v.c+"deg)";
$(marker._icon)[0].style.transform = $(marker._icon)[0].style.transform + " rotate("+pos.v.c+"deg)";
};
// update marker content
if (marker.getPopup() && data['speed']) {
marker.getPopup().setContent(
unit.getName()
).update();
}
// add point to tail
unitsData[unit.getId()].tail.addLatLng({lat: pos.v.y, lng: pos.v.x});
// remove oldest point if tail too long
if (unitsData[unit.getId()].tail.getLatLngs().length > 10) {
unitsData[unit.getId()].tail.spliceLatLngs(0, 1);
}
}
}
}
function getLocalCoords(jsonData){
for(coord in jsonData){
var event = prepareMockObject(jsonData[coord]);
var unit = event.getTarget(),
data = event.getData();
var pos = data.posinfo,
s = data.speed,
route = data.route;
var icon = L.icon({
iconUrl: "/auto_bus/G_" + route + ".png",
iconAnchor: [16, 16]
});
if (!(unit.getId() in unitsData)) {
unitsData[unit.getId()] = {
marker: L.marker({lat: pos.v.y, lng: pos.v.x}, {icon: icon})
.addTo(map)
.bindPopup(unit.getName()),
tail: L.polyline({lat: pos.v.y, lng: pos.v.x}, {color: getRandomColor(), opacity: 0.8})
.addTo(map)
};
}
handleParamsChange(event);
}
}
function prepareMockObject(coord){
var x = coord.x,
y = coord.y,
angle = coord.angle,
speed = coord.speed,
id = coord.id,
route = coord.route,
name = coord.name;
return {
data :{
posinfo: {
v:{
x: x,
y: y,
c: angle,
}
},
speed : {
v: speed
},
route : route
},
getData: function(){
return this.data;
},
getTarget : function(){
return {
getId : function(){
return id;
},
getName : function(){
return name;
}
}
}
}
}
function getCoords(){
var jqxhr = jQuery.getJSON( "/auto_bus/getCoords.php?t=" + Number((new Date()).getTime()))
.done(function(data) {
getLocalCoords(data);
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
}
/**
*
* App login
*
*/
function login() {
var sess = wialon.core.Session.getInstance(); // get instance of current Session
var user = sess.getCurrUser(); // get current User
sess.initSession("https://hst-api.wialon.com"); // initialize Wialon session
sess.loginToken("3b8f1b1bca4abc65cfb61ec484c81b0c4DF68EEA168C778A17EB943C95DE4EC2F2C3BB51", // trying login
function (code) { // login callback
if (code) msg(wialon.core.Errors.getErrorText(code)); // login failed, print error
else msg("Logged successfully"); // login succeed
}
);
}
/**
*
* Get address
*/
function getAddr(pos, id, name){
pos = [{lat:pos[0],lon:pos[1]}];
wialon.util.Gis.getLocations(pos, function(code, locations){
if (!code) {
var number = 18;
switch(true){
case jQuery.inArray(name, r18 )>-1:
//18 autobus
number = 18;
break;
case jQuery.inArray(name, r43 )>-1:
//43 autobus
number = 43;
break;
case jQuery.inArray(name, r10 )>-1:
//10 autobus
number = 10;
break;
case jQuery.inArray(name, r81 )>-1:
//81 autobus
number = 81;
break;
case jQuery.inArray(name, r1 )>-1:
//1 autobus
number = 1;
break;
case jQuery.inArray(name, r27 )>-1:
//27 autobus
number = 27;
break;
default:
break;
}
var elem = $('#'+id);
if(elem.length){
//find element by id and update it
elem.text(locations);
}
else{
$('.addr'+number).append(""+locations+"");
}
}
else{
msg('Can`t get location');
}
});
}
/** App initializition
* Executes after DOM loaded
*/
self.initialize = function () {
// bind login function to click
/*document.getElementById('loginBtn').onclick = function () {
var user = document.getElementById('username').value;
var password = document.getElementById('password').value;
self.login(user, password);
};*/
login();
// create a map in the "map" div
//map = L.map('map').setView([52.32728615559, 9.798388481140], 14);
map = L.map('map').setView([47.313171, 39.677884], 14);
// add an OpenStreetMap tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OSM Developers: Nozdrin Sergey & Andryianov Dmitriy'
}).addTo(map);
/* All ways init */
var bus_43 = L.polyline([
[-84.47406, 23.20313],
]).addTo(map);
var bus_81 = L.polyline([
[-84.47406, 23.70313],
]).addTo(map);
var bus_18l = L.polyline([
[-84.47406, 23.70313],
]).addTo(map);
var bus_27 = L.polyline([
[-84.47406, 23.70313],
]).addTo(map);
var bus_1 = L.polyline([
[-84.47406, 23.70313],
]).addTo(map);
var bus_10 = L.polyline([
[-84.47406, 23.80313],
]).addTo(map);
/* #43 way create, on, off */
document.getElementById('way_43_go').onclick=function(){
map.removeLayer(bus_43);
var line_points = [
[47.319864, 39.683786],
[47.319981, 39.682069],
[47.320214, 39.67355],
[47.320054, 39.673035],
[47.318157, 39.673035],
[47.317792, 39.67252],
[47.317369, 39.672585],
[47.317048, 39.673186],
[47.314465, 39.673057],
[47.307762, 39.672971],
[47.307558, 39.673357],
[47.306376, 39.678099],
[47.305471, 39.677456],
[47.302854, 39.67679],
[47.302299, 39.676748],
[47.300564, 39.67722],
[47.298797, 39.677542],
[47.297264, 39.677584],
[47.297162, 39.677134],
[47.297207, 39.668364],
[47.293871, 39.662318],
[47.293228, 39.660258],
[47.292794, 39.660645],
[47.290051, 39.661095],
[47.289073, 39.661396],
[47.281913, 39.662898],
[47.274069, 39.664593],
[47.273149, 39.66485],
[47.271849, 39.664507],
[47.265358, 39.666138],
[47.262705, 39.666696],
[47.262631, 39.667361],
[47.262982, 39.66912],
[47.260028, 39.674675],
[47.259985, 39.67504],
[47.260846, 39.681205],
[47.262366, 39.683952],
[47.260619, 39.687967],
[47.26023, 39.689009],
[47.259923, 39.689374],
[47.257396, 39.691284],
[47.251146, 39.69599],
[47.24951, 39.697149],
[47.244513, 39.696688],
[47.238261, 39.69939],
[47.2381, 39.699862],
[47.237852, 39.700055],
[47.237559, 39.69969],
[47.226346, 39.704822],
[47.218029, 39.708556],
[47.213672, 39.710358]
];
var polyline_options = {
color: '#260ED8'
};
bus_43 = L.polyline(line_points, polyline_options).addTo(map).bindPopup("Маршрут следования №43");
return false;
};
document.getElementById('way_43_close').onclick=function(){
map.removeLayer(bus_43);
return false;
};
/* #18l way create, on, off */
document.getElementById('way_18l_go').onclick=function(){
map.removeLayer(bus_18l);
var line_points = [
[47.319915, 39.683807],
[47.320207, 39.674752],
[47.320236, 39.673507],
[47.319828, 39.673035],
[47.318281, 39.672949],
[47.317639, 39.672434],
[47.31688, 39.673121],
[47.307745, 39.672992],
[47.306402, 39.678099],
[47.305293, 39.677327],
[47.30249, 39.67664],
[47.299796, 39.67737],
[47.297227, 39.677627],
[47.297227, 39.668486],
[47.293913, 39.662382],
[47.293212, 39.660279],
[47.284864, 39.668046],
[47.284309, 39.669806],
[47.284105, 39.677874],
[47.2839, 39.679805],
[47.283287, 39.681393],
[47.28171, 39.682809],
[47.28133, 39.683153],
[47.281993, 39.683824],
[47.28284, 39.690518],
[47.284242, 39.693093],
[47.287337, 39.694381],
[47.292768, 39.695797],
[47.294783, 39.699488],
[47.295133, 39.701676],
[47.29472, 39.702478],
[47.295304, 39.703207],
[47.294837, 39.706426],
[47.295245, 39.712091],
[47.294895, 39.713292],
[47.295479, 39.714451],
[47.296023, 39.719914],
[47.29776, 39.723176],
[47.297877, 39.724184],
[47.297716, 39.724914],
[47.291973, 39.732833],
[47.291156, 39.733305],
[47.290426, 39.736202],
[47.290134, 39.737854],
[47.288879, 39.740064],
[47.286024, 39.744296],
[47.283833, 39.753233],
[47.281993, 39.760005],
[47.280095, 39.768331],
[47.279204, 39.76846],
[47.277846, 39.76773],
[47.27555, 39.764275],
[47.272164, 39.762151],
[47.271867, 39.761872],
[47.271414, 39.762258],
[47.267953, 39.760306],
[47.265597, 39.769082],
[47.265028, 39.769705],
[47.263859, 39.770584],
[47.262997, 39.771786],
[47.262488, 39.772232],
[47.262313, 39.772039],
[47.26205, 39.771331],
[47.26129, 39.768799],
[47.259476, 39.766524],
[47.258439, 39.765816],
[47.252404, 39.762508],
[47.251265, 39.761607],
[47.249745, 39.759783],
[47.248674, 39.757864],
[47.249434, 39.756898],
[47.249448, 39.755761],
[47.249083, 39.747435],
[47.24901, 39.739128],
[47.249156, 39.731816],
[47.249434, 39.720894],
[47.249697, 39.712712],
[47.250792, 39.712712],
[47.26724, 39.72193],
[47.267415, 39.722724],
[47.273725, 39.719548],
[47.27374, 39.718711],
[47.274274, 39.718368],
[47.294589, 39.713683]
];
var polyline_options = {
color: '#0439BB'
};
bus_18l = L.polyline(line_points, polyline_options).addTo(map).bindPopup("Маршрут следования №18 (левый)");
return false;
};
document.getElementById('way_18l_close').onclick=function(){
map.removeLayer(bus_18l);
return false;
};
/* #27 way create, on, off */
document.getElementById('way_27_go').onclick=function(){
map.removeLayer(bus_27);
var line_points = [
[47.31991078033464, 39.683736674487605],
[47.320290152738515, 39.67317949980499],
[47.31815979525999, 39.672879126446794],
[47.3175469366901, 39.672510054224404],
[47.31699243944006, 39.673196699732216],
[47.30776941619596, 39.672982189804316],
[47.30639749906734, 39.678131964319626],
[47.30555097913587, 39.677488300949356],
[47.304529298984185, 39.677187893539674],
[47.30254426360419, 39.67667284261552],
[47.2995081820753, 39.677402403467575],
[47.29717261537046, 39.677574064844535],
[47.297201810594935, 39.66839018117754],
[47.293815056370185, 39.66221037160723],
[47.29323111116921, 39.66032209646075],
[47.2849675911552, 39.66787519704668],
[47.284354345316096, 39.66942014943927],
[47.284003915910716, 39.67864694845051],
[47.28359507865237, 39.68057813894122],
[47.282894207384416, 39.68186559926837],
[47.276177050601525, 39.68632879506916],
[47.27533004382996, 39.68654337179033],
[47.27427856816979, 39.68632879506916],
[47.264960407045585, 39.68242349874346],
[47.2624188038938, 39.683839705103324],
[47.26060747154874, 39.688045408838676],
[47.2601692366135, 39.68903246175617],
[47.25952648543965, 39.689676191919744],
[47.24967563045819, 39.6971434618172],
[47.24447392690401, 39.696628477686346],
[47.23830719682166, 39.69941797506183],
[47.237868761259946, 39.69894597306848],
[47.237342666615234, 39.699847128504196],
[47.2175024989002, 39.7088165022433],
[47.21861363989956, 39.71435251485673],
[47.219169175109776, 39.71405210744708],
[47.21802885966683, 39.70881643545]
];
var polyline_options = {
color: '#000000'
};
bus_27 = L.polyline(line_points, polyline_options).addTo(map).bindPopup("Маршрут следования №27");
return false;
};
document.getElementById('way_27_close').onclick=function(){
map.removeLayer(bus_27);
return false;
};
/* #81 way create, on, off */
document.getElementById('way_81_go').onclick=function(){
map.removeLayer(bus_81);
var line_points = [
[47.3243, 39.6840],
[47.3196, 39.6838],
[47.3197, 39.6726],
[47.3077, 39.6728],
[47.3063, 39.6779],
[47.3033, 39.6769],
[47.2997, 39.6774],
[47.2972, 39.6774],
[47.2971, 39.6679],
[47.2931, 39.6603],
[47.2845, 39.6689],
[47.2837, 39.6804],
[47.2818, 39.6831],
[47.2828, 39.6904],
[47.2846, 39.6934],
[47.2924, 39.6956],
[47.2947, 39.6990],
[47.2951, 39.7024],
[47.2953, 39.7052],
[47.2948, 39.7088],
[47.2953, 39.7132],
[47.2741, 39.7182],
[47.2671, 39.7216],
[47.2499, 39.7126],
[47.2433, 39.7131],
[47.2443, 39.7148],
[47.2437, 39.7327],
[47.2408, 39.7349],
[47.2275, 39.7428],
[47.2277, 39.7464],
[47.2306, 39.7621],
[47.2305, 39.7621],
[47.2241, 39.7647],
[47.2261, 39.7751],
[47.2244, 39.7758],
[47.2255, 39.7811],
[47.2271, 39.7804]
];
var polyline_options = {
color: '#00875a'
};
bus_81 = L.polyline(line_points, polyline_options).addTo(map).bindPopup("Маршрут следования №81");
return false;
};
document.getElementById('way_81_close').onclick=function(){
map.removeLayer(bus_81);
return false;
};
/* #10 way create, on, off */
document.getElementById('way_10_go').onclick=function(){
map.removeLayer(bus_10);
var line_points = [
[47.3244, 39.6838],
[47.3197, 39.6839],
[47.3198, 39.6729],
[47.3076, 39.6731],
[47.3064, 39.6778],
[47.3027, 39.6767],
[47.2972, 39.6776],
[47.2972, 39.6683],
[47.2930, 39.6605],
[47.2728, 39.6649],
[47.2713, 39.6646],
[47.2627, 39.6667],
[47.2628, 39.6608],
[47.2625, 39.6576],
[47.2574, 39.6436],
[47.2573, 39.6397],
[47.2602, 39.6212],
[47.2540, 39.6141],
[47.2323, 39.6139],
[47.2323, 39.6295],
[47.2254, 39.6299],
[47.2241, 39.6300],
[47.2148, 39.6346],
[47.2080, 39.6351],
[47.2076, 39.6134],
[47.1999, 39.6136]
];
var polyline_options = {
color: '#4B6DFF'
};
bus_10 = L.polyline(line_points, polyline_options).addTo(map).bindPopup("Маршрут следования №10");
return false;
};
document.getElementById('way_10_close').onclick=function(){
map.removeLayer(bus_10);
return false;
};
/* #1 way create, on, off */
document.getElementById('way_1_go').onclick=function(){
map.removeLayer(bus_1);
var line_points = [
[47.31991078033464, 39.68385720625522],
[47.32017342306001, 39.6730853880581],
[47.31824734590041, 39.67304247271389],
[47.31769285604597, 39.67248457323877],
[47.317021623651804, 39.673128303402336],
[47.30769515780519, 39.67299955736966],
[47.30635242887772, 39.67819231402246],
[47.305247336504344, 39.677334007137695],
[47.30264929584282, 39.67673319231836],
[47.29713167360256, 39.67776322737337],
[47.29719006408077, 39.66832185164096],
[47.293861711946136, 39.66231363665431],
[47.293190175001584, 39.66033996129736],
[47.29266624046069, 39.66076911473973],
[47.290009179322595, 39.66111243749366],
[47.27317400117909, 39.664803157098156],
[47.27183039013683, 39.66454566503271],
[47.26268708190781, 39.666734347588864],
[47.2629931107279, 39.66935218358739],
[47.266206628706925, 39.68274174258113],
[47.264716721243026, 39.682484278924306],
[47.26237960861355, 39.68394340062841],
[47.26088964512472, 39.681411395318364],
[47.25991092065791, 39.67492899904358],
[47.26251112630804, 39.669950790703325],
[47.26043680472387, 39.67419943819153],
[47.26017386334786, 39.674971914387825],
[47.260991898869406, 39.681108808613885],
[47.262598717332715, 39.68364081392397],
[47.26049523596232, 39.68823275575745],
[47.26004426381191, 39.68927233072068],
[47.25116189480744, 39.696052955110346],
[47.24961317042866, 39.69712583871629],
[47.250314485226575, 39.69742624612597],
[47.25136643990368, 39.696997092683574],
[47.253704041694085, 39.704163955171374],
[47.259606024186, 39.69030229898241],
[47.26071622414952, 39.68914358468799],
[47.26057014654522, 39.688714431245586],
[47.249993057140735, 39.69678243622185],
[47.249592568399386, 39.71278431182118],
[47.23772721310098, 39.713434768832485],
[47.23661652818036, 39.713992668307576],
[47.23622951501713, 39.71283395401315],
[47.23535263550962, 39.713048530734355],
[47.2202724234106, 39.71959392646673],
[47.21914675112099, 39.714100762404236],
[47.218605835103276, 39.71435825446964],
[47.21976075716914, 39.71982996086002],
[47.22012623356287, 39.71965829948309]
];
var polyline_options = {
color: '#FF0000'
};
bus_1 = L.polyline(line_points, polyline_options).addTo(map).bindPopup("Маршрут следования №1");
return false;
};
document.getElementById('way_1_close').onclick=function(){
map.removeLayer(bus_1);
return false;
};
setInterval(getCoords, 5000);
getCoords();
};
return self;
})();