WebService.cs 18.5 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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
using SmartShelfLight;
using System.IO;
using System;
using System.Collections.Generic;

namespace BLL
{
    [ServiceContract(Name = "Services")]
    public interface IWebService
    {
        #region Get
        [OperationContract]
        [WebGet(UriTemplate = "ShelfLight?rfid={rfid}&place={place}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result ShelfLight_Get(string rfid, string place);

        [OperationContract]
        [WebGet(UriTemplate = "ShelfDark?rfid={rfid}&place={place}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result ShelfDark_Get(string rfid, string place);

        [OperationContract]
        [WebGet(UriTemplate = "TransferLight?line={line}&place={place}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result TransferLight_Get(string line, string place);

        [OperationContract]
        [WebGet(UriTemplate = "TransferDark?line={line}&place={place}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result TransferDark_Get(string line, string place);

        /// <summary>
        /// 产线获取RFID
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "LineGetRFID?line={line}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result LineGetRFID_Get(string line);

        /// <summary>
        /// 产线清除RFID
        /// </summary>
        /// <param name="line"></param>
        /// <param name="rfid"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "LineClearRFID?line={line}&rfid={rfid}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result LineClearRFID_Get(string line, string rfid);
        #endregion

        #region Post Json
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "ShelfLight", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result ShelfLight_Post(Info1 info);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "ShelfDark", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result ShelfDark_Post(Info1 info);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "TransferLight", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result TransferLight_Post(Info1 info);
        
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "TransferDark", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result TransferDark_Post(Info1 info);

        #endregion

        #region Post x-www-form-urlencoded
        //[OperationContract]
        //[WebInvoke(Method = "POST", UriTemplate = "StealAgv/takeOld", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        //Result takeOld(Stream info);

        //[OperationContract]
        //[WebInvoke(Method = "POST", UriTemplate = "StealAgv/sendNew", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        //Result sendNew(Stream info);
        #endregion

        #region TEST
        //[OperationContract]
        //[WebGet(UriTemplate = "StealAgv/takeOld?Place={place}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        //Result takeOld1(string place);

        //[OperationContract]
        //[WebGet(UriTemplate = "StealAgv/sendNew?PlaceList={place}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        //Result sendNew1(string place);

        //[OperationContract]
        //[WebInvoke(Method = "POST", UriTemplate = "StealAgv/takeOld", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        //Result takeOld2(Stream info);

        //[OperationContract]
        //[WebInvoke(Method = "POST", UriTemplate = "StealAgv/sendNew", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        //Result sendNew2(Stream info);
        #endregion
    }

    [DataContract]
    public class Info1
    {
        [DataMember]
        public string RFID { get; set; }

        [DataMember]
        public string Place { get; set; }
    }

    //[DataContract]
    //public class Info2
    //{
    //    [DataMember]
    //    public string Line { get; set; }
        
    //    [DataMember]
    //    public string Place { get; set; }
    //}

    [DataContract]
    public class Result
    {
        [DataMember]
        public int Code { get; set; }

        [DataMember]
        public string Msg { get; set; }

        public Result()
        {
            Code = 0;
            Msg = "";
        }
    }



    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WebService : IWebService
    {
        public delegate void UpdateLightEvent(int lineIndex, int rfidIndex);
        public event UpdateLightEvent UpdateLight;
        public event UpdateLightEvent UpdateRfid;

        public WebService()
        {

        }

        #region Get
        public Result ShelfLight_Get(string rfid, string place)
        {
            Common.log.Info("ShelfLight[GET] rfid=" + rfid + ", place=" + place);
            return ShelfLight(rfid, place);
        }

        public Result ShelfDark_Get(string rfid, string place)
        {
            Common.log.Info("ShelfDark[GET] rfid=" + rfid + ", place=" + place);
            return ShelfDark(rfid, place);
        }

        public Result TransferLight_Get(string line, string place)
        {
            Common.log.Info("TransferLight[GET] line=" + line + " place=" + place);
            return TransferLight(line, place);
        }

        public Result TransferDark_Get(string line, string place)
        {
            Common.log.Info("TransferDark[GET] line=" + line + " place=" + place);
            return TransferDark(line, place);
        }

        public Result LineGetRFID_Get(string line)
        {
            Common.log.Info("LineGetRFID[GET] line=" + line);
            return LineGetRFID(line);
        }

        public Result LineClearRFID_Get(string line, string rfid)
        {
            Common.log.Info("LineClearRFID[GET] line=" + line + " rfid=" + rfid);
            return LineClearRFID(line, rfid);
        }

        #endregion

        #region Post
        public Result ShelfLight_Post(Info1 info)
        {
            Common.log.Info("ShelfLight[POST] rfid=" + info.RFID + ", place=" + info.Place);
            return ShelfLight(info.RFID, info.Place);
        }
        
        public Result ShelfLight_Post(Stream info)
        {
            StreamReader sr = new StreamReader(info);
            string query = sr.ReadToEnd();
            System.Collections.Specialized.NameValueCollection name = System.Web.HttpUtility.ParseQueryString(query);
            Common.log.Info("ShelfLight[POST] rfid=" + name["RFID"] + ", place=" + name["Place"]);
            return ShelfLight(name["RFID"], name["Place"]);
        }

        public Result ShelfDark_Post(Info1 info)
        {
            Common.log.Info("ShelfDark[POST] rfid=" + info.RFID + ", place=" + info.Place);
            return ShelfDark(info.RFID, info.Place);
        }

        public Result ShelfDark_Post(Stream info)
        {
            StreamReader sr = new StreamReader(info);
            string query = sr.ReadToEnd();
            System.Collections.Specialized.NameValueCollection name = System.Web.HttpUtility.ParseQueryString(query);
            Common.log.Info("ShelfDark[POST] rfid=" + name["RFID"] + ", place=" + name["Place"]);
            return ShelfDark(name["RFID"], name["Place"]);
        }

        public Result TransferLight_Post(Info1 info)
        {
            //这里的RFID实际是产线的名字,佳世达要求写这个
            Common.log.Info("TransferLight[POST] line=" + info.RFID + " place=" + info.Place);
            return TransferLight(info.RFID, info.Place);



            //StreamReader sr = new StreamReader(info);
            //string s = sr.ReadToEnd();

            //s = s.Trim('{', '}');
            //string[] arr = s.Split(',');
            //arr[0] = arr[0].Replace("\"line\":", "");
            //arr[0] = arr[0].Trim('\"');
            //arr[1] = arr[1].Replace("\"place\":", "");
            //arr[1] = arr[1].Trim('\"');

            //Common.log.Info("TransferLight[POST] line=" + arr[0] + " place=" + arr[1]);
            //return TransferLight(arr[0], arr[1]);
        }

        public Result TransferDark_Post(Info1 info)
        {
            //这里的RFID实际是产线的名字,佳世达要求写这个
            Common.log.Info("TransferDark[POST] line=" + info.RFID + " place=" + info.Place);
            return TransferDark(info.RFID, info.Place);



            //StreamReader sr = new StreamReader(info);
            //string s = sr.ReadToEnd();

            //s = s.Trim('{', '}');
            //string[] arr = s.Split(',');
            //arr[0] = arr[0].Replace("\"line\":", "");
            //arr[0] = arr[0].Trim('\"');
            //arr[1] = arr[1].Replace("\"place\":", "");
            //arr[1] = arr[1].Trim('\"');

            //Common.log.Info("TransferDark[POST] line=" + arr[0] + " place=" + arr[1]);
            //return TransferDark(arr[0], arr[1]);
        }

        #endregion
















        private Result ShelfLight(string rfid, string place)
        {
            Result res = new Result();

            //Common.log.Debug("Web调用Light");

            bool rtn = Common.FindRfidID(rfid, out int lineIndex, out int rfidIndex);
            if (rtn)
            {
                rtn = int.TryParse(place, out int result);
                if (rtn)
                {
                    if (result > 0 && result < 93)
                    {
                        //Common.lines[lineIndex].LED.LightOn(SmartShelf.DeviceLibrary.Light.BlueLight(rfidIndex, result - 1, 50));
                        Common.lines[lineIndex].LED.OnlyLightOn(SmartShelf.DeviceLibrary.Light.BlueLight(rfidIndex, result - 1, 50));
                        Common.lines[lineIndex].LED_Place[result - 1] = true;
                        Common.log.Info(string.Format("亮灯{0}-{1} RFID={2} place={3} dmxId={4} index={5}", Common.lines[lineIndex].Name, rfidIndex + 1, rfid, place, rfidIndex, result - 1));
                        UpdateLight?.Invoke(lineIndex, rfidIndex);
                        res.Code = 0;
                        res.Msg = "OK";
                    }
                    else
                    {
                        res.Code = -3;
                        res.Msg = "Place[" + place + "] not between 1 and 92";
                    }
                }
                else
                {
                    res.Code = -2;
                    res.Msg = "Place[" + place + "] isn't a number";
                }
            }
            else
            {
                res.Code = -1;
                res.Msg = "RFID[" + rfid + "] not found";
            }

            Common.log.Info("ShelfLight[Response] code=" + res.Code + ", msg=" + res.Msg);
            return res;
        }

        private Result ShelfDark(string rfid, string place)
        {
            Result res = new Result();

            bool rtn = Common.FindRfidID(rfid, out int lineIndex, out int rfidIndex);
            if (rtn)
            {
                rtn = int.TryParse(place, out int result);
                if (rtn)
                {
                    if (result > 0 && result < 93)
                    {
                        Common.lines[lineIndex].LED.LightOff(SmartShelf.DeviceLibrary.Light.BlueLight(rfidIndex, result - 1, 50));

                        //if (Common.lines[lineIndex].LED_Light[rfidIndex] == place)
                        //    Common.lines[lineIndex].LED_Light[rfidIndex] = "";
                        //else if (Common.lines[lineIndex].LED_Light[rfidIndex].StartsWith(place))
                        //    Common.lines[lineIndex].LED_Light[rfidIndex] = Common.lines[lineIndex].LED_Light[rfidIndex].Replace(place + ",", "");
                        //else if (Common.lines[lineIndex].LED_Light[rfidIndex].EndsWith(place))
                        //    Common.lines[lineIndex].LED_Light[rfidIndex] = Common.lines[lineIndex].LED_Light[rfidIndex].Replace("," + place, "");
                        //else
                        //    Common.lines[lineIndex].LED_Light[rfidIndex] = Common.lines[lineIndex].LED_Light[rfidIndex].Replace("," + place, "");

                        Common.lines[lineIndex].LED_Place[result - 1] = false;
                        Common.log.Info(string.Format("灭灯{0}-{1} RFID={2} place={3} dmxId={4} index={5}", Common.lines[lineIndex].Name, rfidIndex + 1, rfid, place, rfidIndex, result - 1));
                        UpdateLight?.Invoke(lineIndex, rfidIndex);
                        res.Code = 0;
                        res.Msg = "OK";
                    }
                    else
                    {
                        res.Code = -3;
                        res.Msg = "Place[" + place + "] not between 1 and 92";
                    }
                }
                else
                {
                    res.Code = -2;
                    res.Msg = "Place[" + place + "] isn't a number";
                }
            }
            else
            {
                res.Code = -1;
                res.Msg = "RFID[" + rfid + "] not found";
            }

            Common.log.Info("ShelfDark[Response] code=" + res.Code + ", msg=" + res.Msg);
            return res;
        }

        private Result TransferLight(string line, string place)
        {
            Result res = new Result();



            //临时
            if (!Common.name.TryGetValue(line, out string value))
            {
                res.Code = -1;
                res.Msg = "line[" + line + "] not found1";
                return res;
            }





            int lineIdx = Common.lines.FindIndex(s => s.Name == value);
            if (lineIdx == -1)
            {
                res.Code = -1;
                res.Msg = "line[" + value + "] not found2";
                return res;
            }

            int placeIdx = Common.lines[lineIdx].TransferPlace.FindIndex(s => s[0] == place);
            if (placeIdx == -1)
            {
                res.Code = -2;
                res.Msg = "place[" + place + "] not found";
                return res;
            }

            int dmxId = Convert.ToInt32(Common.lines[lineIdx].TransferPlace[placeIdx][1]);
            int index = Convert.ToInt32(Common.lines[lineIdx].TransferPlace[placeIdx][2]);
            Common.lines[lineIdx].LED.OnlyLightOn(SmartShelf.DeviceLibrary.Light.BlueLight(dmxId, index, 50));
            Common.log.Info(string.Format("转移亮灯 line={0} place={1}", line, place));
            res.Msg = "OK";
            return res;
        }

        private Result TransferDark(string line, string place)
        {
            Result res = new Result();

            //临时
            if (!Common.name.TryGetValue(line, out string value))
            {
                res.Code = -1;
                res.Msg = "line[" + line + "] not found1";
                return res;
            }



            int lineIdx = Common.lines.FindIndex(s => s.Name == value);
            if (lineIdx == -1)
            {
                res.Code = -1;
                res.Msg = "line[" + value + "] not found2";
                return res;
            }

            int placeIdx = Common.lines[lineIdx].TransferPlace.FindIndex(s => s[0] == place);
            if (placeIdx == -1)
            {
                res.Code = -2;
                res.Msg = "place[" + place + "] not found";
                return res;
            }

            int dmxId = Convert.ToInt32(Common.lines[lineIdx].TransferPlace[placeIdx][1]);
            int index = Convert.ToInt32(Common.lines[lineIdx].TransferPlace[placeIdx][2]);
            Common.lines[lineIdx].LED.LightOff(SmartShelf.DeviceLibrary.Light.BlueLight(dmxId, index, 50));
            Common.log.Info(string.Format("转移灭灯 line={0} place={1}", line, place));
            res.Msg = "OK";
            return res;
        }

        private Result LineGetRFID(string line)
        {
            Result res = new Result();
            int lineIdx = Common.lines.FindIndex(s => s.Name == line);
            if (lineIdx == -1)
            {
                res.Code = -1;
                res.Msg = string.Format("没有找到line {0}", line);
                return res;
            }

            res.Code = 0;
            res.Msg = string.Join(",", Common.lines[lineIdx].RFID_ID);
            return res;
        }

        private Result LineClearRFID(string line, string rfid)
        {
            Result res = new Result();
            int lineIndex = Common.lines.FindIndex(s => s.Name == line);
            if (lineIndex == -1)
            {
                res.Code = -1;
                res.Msg = string.Format("没有找到line {0}", line);
                return res;
            }

            int rfidIndex = Array.FindIndex(Common.lines[lineIndex].RFID_ID, s => s == rfid);
            if (rfidIndex == -1)
            {
                res.Code = -2;
                res.Msg = string.Format("没有找到rfid {0}", rfid);
                return res;
            }

            Common.lines[lineIndex].RFID_ID[rfidIndex] = "";
            Common.lines[lineIndex].RFID_DwellTime[rfidIndex] = DateTime.Now.Ticks;
            UpdateRfid?.Invoke(lineIndex, rfidIndex);

            res.Code = 0;
            res.Msg = "OK";
            return res;
        }

    }


}