Created
September 1, 2017 07:40
-
-
Save davidrenne/8b80246fcfe8182722c1d000026a2014 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var originalRoom model.Room | |
err = queries.GetAllRoomQS().Filter(model.Q(model.FIELD_ROOM_ID, originalId)).One(&originalRoom) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
message = constants.ERRORS_ROOM_COPY | |
return | |
} | |
oldAndNewDevices := make(map[string]string) | |
newMacros := make([]model.Macro, 0) | |
//inputMigration := make(map[string]model.RoomDevice) | |
if originalRoom.Joins.RoomFeatures.Count > 0 { | |
for _, f := range *originalRoom.Joins.RoomFeatures.Items { | |
feature := f | |
var newMacro viewModel.MacroModifyViewModel | |
newMacro, _, err = Macros.CopyById(context, feature.MacroId, t) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
message = constants.ERRORS_ROOM_COPY | |
return | |
} | |
newMacros = append(newMacros, newMacro.Macro) | |
feature.MacroId = newMacro.Macro.Id.Hex() | |
feature.RoomId = copyVm.Room.Id.Hex() | |
_, _, err = RoomFeatures.Copy(context, RoomFeatures.WrapViewModel(feature), t) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
message = constants.ERRORS_ROOM_COPY | |
return | |
} | |
} | |
} | |
if originalRoom.Joins.RoomDevices.Count > 0 { | |
//Dereference the pointer | |
roomDevices := *originalRoom.Joins.RoomDevices.Items | |
for _, d := range roomDevices { | |
device := d | |
if device.Joins.Device != nil && device.Joins.Device.Joins.Equipment != nil && device.Joins.Device.Joins.Equipment.Category == "DedicatedControlPanel" { | |
continue | |
} | |
originalRoomDevice := device.Id.Hex() | |
oldDeviceId, vmDevice := self.SeedNewDeviceVMForCopy(context, device.DeviceId) | |
vmDevice.Device.IPAddress = "" | |
message, err = Devices.Create(context, &vmDevice, t) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
message = constants.ERRORS_ROOM_COPY | |
return | |
} | |
//for i := range newMacros { | |
// macId := newMacros[i].Id.Hex() | |
// session_functions.VelocityDump(macId) | |
// | |
// for idx, command := range newMacros[i].MacroCommands { | |
// if command.DeviceId == d.DeviceId { | |
// newMacros[i].MacroCommands[idx].DeviceId = vmDevice.Device.Id.Hex() | |
// err = newMacros[i].SaveWithTran(t) | |
// if err != nil { | |
// err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
// message = constants.ERRORS_ROOM_COPY | |
// return | |
// } | |
// } | |
// } | |
//} | |
for _, macro := range newMacros { | |
for idx, command := range macro.MacroCommands { | |
if command.DeviceId == d.DeviceId { | |
macro.MacroCommands[idx].DeviceId = vmDevice.Device.Id.Hex() | |
err = macro.SaveWithTran(t) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
message = constants.ERRORS_ROOM_COPY | |
return | |
} | |
} | |
} | |
} | |
if originalRoom.AudioDeviceId != "" && d.DeviceId == originalRoom.AudioDeviceId { | |
copyVm.Room.AudioDeviceId = vmDevice.Device.Id.Hex() | |
err = copyVm.Room.SaveWithTran(t) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
message = constants.ERRORS_ROOM_COPY | |
return | |
} | |
} | |
controlEngine.AddDevice(vmDevice.Device.Id.Hex()) | |
oldAndNewDevices[oldDeviceId] = vmDevice.Device.Id.Hex() | |
device.Id = "" | |
vmRoomDevice := RoomDevices.WrapViewModel(device) | |
vmRoomDevice.RoomDevice.DeviceId = vmDevice.Device.Id.Hex() | |
vmRoomDevice.RoomDevice.RoomId = copyVm.Room.Id.Hex() | |
message, err = RoomDevices.Create(context, &vmRoomDevice, t) | |
if err != nil { | |
err = errors.Wrap(err, core.Debug.ErrLineAndFile(err)) | |
return | |
} | |
oldToNewDevices[originalRoomDevice] = vmDevice.Device.Id.Hex() | |
newRoomDevices = append(newRoomDevices, vmRoomDevice.RoomDevice.Id.Hex()) | |
//inputMigration[originalRoomDevice] = vmRoomDevice.RoomDevice | |
for idx, rd := range vmRoomDevice.RoomDevice.VideoRemoteState.Items { | |
var fileObj model.FileObject | |
err = model.FileObjects.Query().ById(rd.FileObjectId, &fileObj) | |
if err == nil { | |
fo := FileObjects.WrapViewModel(fileObj) | |
_, errImg := FileObjects.Copy(context, &fo, t) | |
if errImg != nil { | |
vmRoomDevice.RoomDevice.VideoRemoteState.Items[idx].FileObjectId = "" | |
} else { | |
vmRoomDevice.RoomDevice.VideoRemoteState.Items[idx].FileObjectId = fo.FileObject.Id.Hex() | |
} | |
} else { | |
vmRoomDevice.RoomDevice.VideoRemoteState.Items[idx].FileObjectId = "" | |
} | |
} | |
} | |
// Re-assign output devices to new devices we just created | |
//for i := range roomDevices { | |
// device := &roomDevices[i] | |
// if device.Joins.Device != nil && device.Joins.Device.Joins.Equipment != nil && device.Joins.Device.Joins.Equipment.Category == "DedicatedControlPanel" { | |
// continue | |
// } | |
// if len(device.Inputs) > 0 { | |
// isDirty := false | |
// newRoomDevice := inputMigration[device.Id.Hex()] | |
// | |
// for j, inp := range device.Inputs { | |
// isDirty = true | |
// input := inp | |
// newRoomDevice.Inputs[j].OutputDeviceId = oldAndNewDevices[input.OutputDeviceId] | |
// } | |
// if isDirty { | |
// newRoomDevice.SaveWithTran(t) | |
// } | |
// } | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment