private static DeviceInfo GetDeviceInfo(SmartWeb.Models.Device model) { var device = new DeviceInfo() { did = model.Id, factory = (DeviceInfo.Factory)model.factory, ip = model.IP, name = model.Name, nickname = model.Nickname, password = model.Password, enable = model.Enable, port = model.Port, type = (DeviceInfo.Type)model.type }; if (model.CameraModelList != null && model.CameraModelList.Count > 0) { device.camerainfos = new List <CameraInfo>(); foreach (var item in model.CameraModelList) { device.camerainfos.Add(new CameraInfo() { cid = item.Id, enable = item.Enable, nickname = item.NickName, index = item.Index, ip = item.IP }); } } return(device); }
/// <summary> /// 添加设备 /// </summary> /// <param name="shipId"></param> /// <param name="protoModel"></param> /// <returns></returns> public static SmartWeb.Models.Device DeviceAdd(DeviceInfo protoModel) { if (protoModel != null) { using (var _context = new MyContext()) { string shipId = "";//_context.Ship.FirstOrDefault().Id; var factory = (SmartWeb.Models.Device.Factory)protoModel.factory; var type = (SmartWeb.Models.Device.Type)protoModel.type; SmartWeb.Models.Device model = _context.Device.FirstOrDefault(c => c.factory == factory && c.type == type && c.IP == protoModel.ip); if (model == null) { model = new SmartWeb.Models.Device(); model.factory = factory; model.IP = protoModel.ip; model.Name = protoModel.name; model.Nickname = protoModel.nickname; model.Password = protoModel.password; model.Port = protoModel.port; model.type = type; model.Enable = protoModel.enable; model.Id = Guid.NewGuid().ToString(); _context.Device.Add(model); _context.SaveChanges(); if (protoModel.camerainfos != null && protoModel.camerainfos.Count > 0) { AddCameras(protoModel.camerainfos, protoModel.did); } } return(model); } } return(null); }
/// <summary> /// 发送设备增加请求 /// </summary> /// <param name="deviceInfo"></param> /// <param name="did"></param> public void SendDeveiceAdd(SmartWeb.Models.Device model, string nextIdentity) { DeviceInfo deviceInfo = GetDeviceInfo(model); MSG msg = new MSG() { type = MSG.Type.DEVICE, sequence = 5, timestamp = ProtoBufHelp.TimeSpan(), device = new Models.Device() { command = Models.Device.Command.NEW_REQ, devicerequest = new DeviceRequest() { deviceinfo = deviceInfo } } }; dealer.Send(msg, nextIdentity); }