LocationMonitor.js 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
var refreshMapTimer, mouseOnpointTimer
var LocationMap, PointMap, ContainersInfo, CarInfo;
var angle = 90;
var RoadWay = 1;

var locationc = "blue"; //点颜色
var carc = "red"; //小车颜色
var shelfc = "green"; //货架颜色

var mappixSize = 38;
var locationpixSize = mappixSize * 0.9;
var containerpixSize = mappixSize * 0.8;

var eachWidth = mappixSize / 1.2 / 1.2 / 1.2; //单元格大小
var pointr = 3 / 1.2 / 1.2 / 1.2; //点的半径为3

var locationw = locationpixSize / 1.2 / 1.2 / 1.2; //库位宽度
var locationh = locationpixSize / 1.2 / 1.2 / 1.2; //库位高度
var locationb = 1 / 1.2 / 1.2 / 1.2; //库位边框宽度

var carr = 18 / 1.2 / 1.2 / 1.2; //小车半径
var carb = 4 / 1.2 / 1.2;       //小车线条宽度

var containerw = containerpixSize / 1.2 / 1.2 / 1.2; //容器宽度
var containerh = containerpixSize / 1.2 / 1.2 / 1.2; //容器高度
var containerb = 2 / 1.2 / 1.2 / 1.2; //容器边框宽度

var MapWidth, MapHeight;
var MaxRow, MaxLine, MaxLayer;

$(document).ready(function () {
    LocationMap = $("#LocationMap");
    PointMap = $("#PointMap");
    ContainersInfo = $("#ContainersInfo");
    CarInfo = $("#CarInfo");

    if (window.localStorage.getItem("RoadWay") != undefined) {
        RoadWay = window.localStorage.getItem("RoadWay");
    }

    BindRoadWay();
    //加载并刷新地图
    DrawMap();

    var connection = new signalR.HubConnectionBuilder()
        .withUrl("/ChartHub", {
            skipNegotiation: true,
            transport: signalR.HttpTransportType.WebSockets
        })
        .build();

    connection.on("sendAgvData", function (json) {
        var x = json.X; //AGV x坐标
        x = x % MaxRow + 2
        if (MaxLine == 1) {
            RefreshCar({ X: x, Y: 1, AgvNo: 'Test' });
        }
        else {
            RefreshCar({ X: x, Y: (MaxLine / 2) * (MaxLayer + 1) + 1, AgvNo: 'Test' });
        }
    });

    connection.start().then(function () {
    }).catch(function (err) {
        return console.error(err.toString());
    });
});

function Rotate() {
    angle -= 90;
    if (angle < -360) { angle = -90 };
    $("#mycanvas").rotate({
        angle: angle,
        center: ["50%", "50%"],
        animateTo: angle - 90,
    })
}

//获取所有巷道
function BindRoadWay() {
    $.post("/monitor/LocationMonitor/GetRoadWays",
        null,
        function (result) {
            if (result.count > 0) {
                $("#RoadWays").html("");
                var doms = "";
                var RoadWayExist = false;
                for (var i = 0; i < result.data.length; i++) {
                    doms += '<a id = btnRoadway' + result.data[i] + ' herf="javascript:;" class="layui-btn layui-btn-sm layui-btn-warm" onclick="javascript: Change(' + result.data[i] + ');" title="切换">' + result.data[i] + '号巷道</a>';
                    if (RoadWay == result.data[i]) {
                        RoadWayExist = true;
                    }
                }
                doms += '<a herf="javascript:;" class="layui-btn layui-btn-sm layui-btn-normal" id="btnRefresh" onclick="javascript: Refresh();" title="刷新"><i class="layui-icon">&#xe669;</i>刷新</a>';

                if (!RoadWayExist) {
                    RoadWay = result.data[0];
                }
                $("#RoadWays").html(doms);

                $('#btnRoadway' + RoadWay).css("background-color", 'green');
            } else {
                layer.msg(result.msg);
            }
        },
        "json");
}

//切换地图
function Change(iRoadWay) {
    layer.load();
    window.localStorage.setItem("RoadWay", iRoadWay);
    window.location.reload(true)
    layer.closeAll();
}

//画地图,并刷新地图
function DrawMap() {
    var WidthMax = 35; //行最大值
    var HeightMax = 20 + (4 - 1); //一行共多少仓位 + (列数 - 1) //每列间隔开
    var WidthMin = 1;  //固定值
    var HeightMin = 1; //固定值

    //获取地图X,Y轴最大值
    $.post("/monitor/LocationMonitor/GetRoadWayConfig",
        { roadway: RoadWay },
        function (result) {
            MaxRow = result.data.MaxRow;
            MaxLine = result.data.MaxLine;
            MaxLayer = result.data.MaxLayer;

            WidthMax = MaxRow + 1;
            HeightMax = (MaxLine * MaxLayer) + (MaxLine - 1) + 2;

            var maxX = WidthMax + 1;
            var maxY = HeightMax + 1;
            var minX = WidthMin - 1;
            var minY = HeightMin - 1;

            MapWidth = (maxX - minX) * mappixSize / 1.728 + 12;
            MapHeight = (maxY - minY) * mappixSize / 1.728 + 12;

            LocationMap.attr("width", MapWidth);
            LocationMap.attr("height", MapHeight - eachWidth / 1.9);
            PointMap.attr("width", MapWidth);
            PointMap.attr("height", MapHeight - eachWidth / 1.9);
            ContainersInfo.attr("width", MapWidth);
            ContainersInfo.attr("height", MapHeight - eachWidth / 1.9);
            CarInfo.attr("width", MapWidth);
            CarInfo.attr("height", MapHeight - eachWidth / 1.9);

            $(PointMap).removeLayer().drawLayers();
            $(PointMap).clearCanvas();

            for (var i = 1; i <= MaxRow; i++) {
                var x = GetAdjustX(i + 1) * eachWidth;
                var y = GetAdjustY(MaxLine * (MaxLayer + 1) + 1) * eachWidth + MapHeight;
                $(PointMap).drawText({
                    name: 'PointR' + i,
                    fillStyle: 'blue',
                    strokeWidth: 2 / (90 / mappixSize),
                    x: x, y: y,
                    fontSize: 20 / (90 / mappixSize) + 'px',
                    fontFamily: 'Verdana, sans-serif',
                    text: i
                })
                    .drawArc({
                        strokeStyle: 'green',
                        strokeWidth: 4,
                        x: x, y: y,
                        radius: 18 / (90 / mappixSize)
                    });
            }

            for (var i = 1; i <= MaxLine; i++) {
                var x = eachWidth;
                var y = GetAdjustY((MaxLine - i) * (MaxLayer + 1) + 1 + MaxLayer / 2 + 0.5) * eachWidth + MapHeight;
                $(PointMap).drawText({
                    name: 'PointL' + i,
                    fillStyle: 'blue',
                    strokeWidth: 2 / (90 / mappixSize),
                    x: x, y: y,
                    fontSize: 20 / (90 / mappixSize) + 'px',
                    fontFamily: 'Verdana, sans-serif',
                    text: i
                })
                    .drawArc({
                        strokeStyle: 'red',
                        strokeWidth: 4,
                        x: x, y: y,
                        radius: 18 / (90 / mappixSize)
                    });
            }

            //刷新地图
            Refresh();
        },
        "json");
}

//刷新地图
function Refresh() {
    layer.load();
    //加载库位
    CreateLocationMap();

    //加载堆垛机
    if (MaxLine == 1) {
        RefreshCar({ X: MaxRow / 2 + 1, Y: 1, AgvNo: 'Test' });
    }
    else {
        RefreshCar({ X: MaxRow / 2 + 1, Y: (MaxLine / 2) * (MaxLayer + 1) + 1, AgvNo: 'Test' });
    }
    layer.closeAll();
}

//启停自动刷新
function StartStop() {
    if (refreshMapTimer != undefined) {
        window.clearInterval(refreshMapTimer);
        refreshMapTimer = undefined;
    }
    refreshMapTimer = setInterval(function () {
        Refresh();
    }, 1000);
}

//获取地图库位数据
function CreateLocationMap() {
    $.post("/monitor/LocationMonitor/GetLocations",
        { roadway: RoadWay },
        function (result) {
            if (result.count > 0) {
                DrawLocationMap(result.data);
            } else {
                layer.msg(result.msg);
            }
        },
        "json");
}

//画库位
function DrawLocationMap(data) {
    for (var i = 0; i < data.length; i++) {
        var irow = data[i].Row;
        var iline = data[i].Line;
        var ilayer = data[i].Layer;
        var irowindex = data[i].RowIndex;
        var locationname = data[i].Code;
        var iStatus = data[i].Status;

        var locationx = GetAdjustX(irow + 1);
        var locationy = GetAdjustY((MaxLine - iline) * (MaxLayer + 1) + ilayer + 1);

        var content = {
            "Row": irow,
            "Line": iline,
            "Layer": ilayer,
            "RowIndex": irowindex,
            "Code": locationname,
            "Status": iStatus,
        };

        var locationLayer = $(LocationMap).getLayer(locationname);
        var imgsrc = '/images/container/空柜空闲.png';

        if (iStatus == "empty") {
            imgsrc = '/images/container/空柜空闲.png';
        }
        else if (iStatus == "some") {
            imgsrc = '/images/container/半盘空闲.png';
        }
        else if (iStatus == "full") {
            imgsrc = '/images/container/整盘空闲.png';
        }
        else if (iStatus == "lock") {
            imgsrc = '/images/container/空柜锁定.png';
        }
        else if (iStatus == "emptycontainer") {
            imgsrc = '/images/container/空盘空闲.png';
        }
        
        if (locationLayer == undefined) {
            $(LocationMap).drawImage({
                layer: true,
                name: locationname,
                source: imgsrc,
                x: locationx * eachWidth - (eachWidth / 2), y: locationy * eachWidth - (eachWidth / 2) + MapHeight,
                fromCenter: false,
                width: locationw * 1.1,
                height: locationh * 1.1,
                data: content,
                shadowColor: '#222',
                shadowBlur: 1,
            });

            $(ContainersInfo).drawRect({
                layer: true,
                name: locationname,
                fillStyle: "transparent",
                x: locationx * eachWidth, y: locationy * eachWidth + MapHeight,
                width: locationw * 1.1,
                height: locationh * 1.1,
                data: content,
                click: function (canvaslayer) {
                    //$(LocationMap).animateLayer(canvaslayer.name, {
                    //    shadowBlur: 20,
                    //}, 10, null);
                    $(LocationMap).getLayer(locationname).shadowBlur = 20;
                    $(LocationMap).drawLayers();
                    
                    var msgLayer = $(LocationMap).getLayer(canvaslayer.name);
                    var msg = "&emsp;库位编码:&nbsp" + msgLayer['data']['Code'] + "<br/>";
                    msg += "&emsp;行:&nbsp" + msgLayer['data']['Row'] + "&emsp;";
                    msg += "列:&nbsp" + msgLayer['data']['Line'] + "&emsp;";
                    msg += "层:&nbsp" + msgLayer['data']['Layer'] + "&emsp;<br/>";
                    msg += "&emsp;双深位索引:&nbsp" + msgLayer['data']['RowIndex'] + "&emsp;";
                    msg += "状态:&nbsp" + ContainerStatus(msgLayer['data']['Status']) + "<br/>";
                    layer.msg(msg, { time: 0, offset: [canvaslayer.y - eachWidth - 30 - $(document).scrollTop(), canvaslayer.x + eachWidth + 30] });
                },
                mouseover: function (canvaslayer) {
                    //mouseOnpointTimer = setTimeout(function () {
                    //    $(LocationMap).animateLayer(canvaslayer.name, {
                    //        shadowBlur: 20,
                    //    }, 0, null);

                        var msgLayer = $(LocationMap).getLayer(canvaslayer.name);
                        var msg = "&emsp;库位编码:&nbsp" + msgLayer['data']['Code'] + "<br/>";
                        msg += "&emsp;行:&nbsp" + msgLayer['data']['Row'] + "&emsp;";
                        msg += "列:&nbsp" + msgLayer['data']['Line'] + "&emsp;";
                        msg += "层:&nbsp" + msgLayer['data']['Layer'] + "&emsp;<br/>";
                        msg += "&emsp;双深位索引:&nbsp" + msgLayer['data']['RowIndex'] + "&emsp;";
                        msg += "状态:&nbsp" + ContainerStatus(msgLayer['data']['Status']) + "<br/>";
                        layer.msg(msg, { time: 0, offset: [canvaslayer.y - eachWidth - 30 - $(document).scrollTop(), canvaslayer.x + eachWidth + 30] });
                    //}, 10);
                },
                mouseout: function (canvaslayer) {
                    //if (mouseOnpointTimer != undefined) {
                    //    window.clearInterval(mouseOnpointTimer);
                    //    mouseOnpointTimer = undefined;
                    //}

                    //$(LocationMap).animateLayer(canvaslayer.name, {
                    //    shadowBlur: 1,
                    //}, 0, null);

                    layer.closeAll();
                },
            })
        }
        else {
            $(LocationMap).removeLayer(locationname);

            $(LocationMap).drawImage({
                layer: true,
                name: locationname,
                source: imgsrc,
                x: locationx * eachWidth - (eachWidth / 2), y: locationy * eachWidth - (eachWidth / 2) + MapHeight,
                fromCenter: false,
                width: locationw * 1.1,
                height: locationh * 1.1,
                data: content,
                shadowColor: '#222',
                shadowBlur: 1,
            });
        }
    }
}

//获取小车数据
function CreateCars() {
    $.post("/Acs/AcsMap/GetCars",
        null,
        function (data) {
            if (data.Status) {
                RefreshCars(data.Result);
            } else {
                layer.msg(data.Message);
            }
        },
        "json");
}

//刷新小车
function RefreshCars(data) {
    for (var i = 0; i < data.length; i++) {
        RefreshCar(data[i])
    }
}

//刷新单个小车
function RefreshCar(data) {
    var carname = data.AgvNo;

    var carLayer = $(CarInfo).getLayer(carname);
    if (carLayer == undefined) {
        DrawCar(data);
    }
    else {
        EraseCar(data);
        DrawCar(data);
    }
}

//画小车
function DrawCar(data) {
    var carx = GetAdjustX(data.X);
    var cary = GetAdjustY(data.Y);
    var carname = data.AgvNo;

    var imgsrc = "/images/car.png";

    $(CarInfo).drawImage({
        layer: true,
        name: carname,
        source: imgsrc,
        x: carx * eachWidth - (eachWidth / 2), y: cary * eachWidth - (eachWidth / 2) + MapHeight,
        fromCenter: false,
        width: eachWidth * 0.9,
        height: eachWidth * 0.9,
        shadowColor: '#222',
        shadowBlur: 5,
    });
}

//移除小车
function EraseCar(data) {
    $(CarInfo).removeLayer(data.AgvNo).drawLayers();
}

//地图X坐标调整
function GetAdjustX(x) {
    //return x - 14;
    return x;
}

//地图Y坐标调整
function GetAdjustY(y) {
    //return y * -1 + 100;
    return y * -1;
}

//获取容器状态
function ContainerStatus(data) {
    if (data == "some") {
        return "半盘";
    }
    else if (data == "full") {
        return "整盘";
    }
    else if (data == "emptycontainer") {
        return "空盘";
    }
    else {
        return "空仓位";
    }
}