using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Lmi3d.GoSdk;
using Lmi3d.GoSdk.Messages;
using Lmi3d.Zen;
using Lmi3d.Zen.Io;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Bro.Device.Gocator
{
[Device("Gocator", "Gocator激光扫描仪", EnumHelper.DeviceAttributeType.Device)]
public class GocatorDriver : CameraBase
{
#region CameraBase
public override IOperationConfig GetOperationConfigFromDevice()
{
var opConfig = new GocatorOperationConfig();
GoSetup setup = sensor.Setup;
opConfig.Exposure = (float)setup.GetExposure(GoRole.Main);
opConfig.JobName = sensor.DefaultJob;
return opConfig;
}
public override void Snapshot()
{
if (IIConfig.IsAsyncMode)
{
}
else
{
GoDataSet dataSet = null;
try
{
dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
}
catch (Exception ex)
{
LogAsync(DateTime.Now, $"{Name}获取图像异常", ex.GetExceptionMessage());
return;
}
HandleGoData(dataSet);
}
}
private void HandleGoData(GoDataSet dataSet)
{
if (dataSet == null)
return;
for (UInt32 i = 0; i < dataSet.Count; i++)
{
GoDataMsg dataObj = (GoDataMsg)dataSet.Get(i);
switch (dataObj.MessageType)
{
//case GoDataMessageType.Stamp:
// {
// GoStampMsg stampMsg = (GoStampMsg)dataObj;
// for (UInt32 j = 0; j < stampMsg.Count; j++)
// {
// GoStamp stamp = stampMsg.Get(j);
// Console.WriteLine("Frame Index = {0}", stamp.FrameIndex);
// Console.WriteLine("Time Stamp = {0}", stamp.Timestamp);
// Console.WriteLine("Encoder Value = {0}", stamp.Encoder);
// }
// }
// break;
case GoDataMessageType.Surface:
{
GoSurfaceMsg surfaceMsg = (GoSurfaceMsg)dataObj;
long width = surfaceMsg.Width;
long height = surfaceMsg.Length;
long bufferSize = width * height;
IntPtr bufferPointer = surfaceMsg.Data;
//Console.WriteLine("Whole Part Height Map received:");
//Console.WriteLine(" Buffer width: {0}", width);
//Console.WriteLine(" Buffer height: {0}", height);
//short[] ranges = new short[bufferSize];
//Marshal.Copy(bufferPointer, ranges, 0, ranges.Length);
Generate16GrayImageByPointer((int)(width / 1), (int)(height / 1), bufferPointer, "");
}
break;
//case GoDataMessageType.SurfaceIntensity:
// {
// GoSurfaceIntensityMsg surfaceMsg = (GoSurfaceIntensityMsg)dataObj;
// long width = surfaceMsg.Width;
// long height = surfaceMsg.Length;
// long bufferSize = width * height;
// IntPtr bufferPointeri = surfaceMsg.Data;
// //Console.WriteLine("Whole Part Intensity Image received:");
// //Console.WriteLine(" Buffer width: {0}", width);
// //Console.WriteLine(" Buffer height: {0}", height);
// //byte[] ranges = new byte[bufferSize];
// //Marshal.Copy(bufferPointeri, ranges, 0, ranges.Length);
// //GenerateGrayImageByPointer((int)width, (int)height, bufferPointeri, "");
// }
// break;
}
}
}
public override ImageSet Snapshot(IOperationConfig config)
{
ImageSet imgSet = base.Snapshot(config);
return imgSet;
}
float _currentExposure = 0;
string _currentJob = "";
public override void UploadOperationConfig(IOperationConfig config)
{
if (config is GocatorOperationConfig opConfig)
{
if (opConfig.Exposure > 0 && opConfig.Exposure != _currentExposure)
{
sensor.Setup.SetExposure(GoRole.Main, opConfig.Exposure);
_currentExposure = opConfig.Exposure;
}
if (!string.IsNullOrWhiteSpace(opConfig.JobName) && _currentJob != opConfig.JobName)
{
_currentJob = sensor.DefaultJob = opConfig.JobName;
}
sensor.Flush();
}
}
protected override void Init()
{
KApiLib.Construct();
GoSdkLib.Construct();
system = new GoSystem();
if (IIConfig.IsUseAccelerator)
{
accelerator = new GoAccelerator();
}
KIpAddress ip = KIpAddress.Parse(IConfig.CameraIP);
if (IIConfig.IsUseAccelerator)
{
accelerator.Start();
}
sensor = system.FindSensorByIpAddress(ip);
if (IIConfig.IsUseAccelerator)
{
accelerator.Attach(sensor);
}
sensor.Connect();
if (IIConfig.IsUseAccelerator)
{
sensor.Flush();
accelerator.Start();
}
sensor.Setup.ScanMode = GoMode.Surface;
system.EnableData(true);
if (IIConfig.IsAsyncMode)
{
system.SetDataHandler(onData);
}
_currentExposure = (float)sensor.Setup.GetExposure(GoRole.Main);
if (IIConfig.DefaultExposure > 0 && _currentExposure != IIConfig.DefaultExposure)
{
sensor.Setup.SetExposure(GoRole.Main, IIConfig.DefaultExposure);
_currentExposure = (float)sensor.Setup.GetExposure(GoRole.Main);
}
_currentJob = sensor.DefaultJob;
if (!string.IsNullOrWhiteSpace(IIConfig.DefaultJob) && _currentJob != IIConfig.DefaultJob)
{
_currentJob = sensor.DefaultJob = IIConfig.DefaultJob;
}
sensor.Flush();
}
protected override void Start()
{
base.Start();
if (IIConfig.IsAsyncMode)
system.Start();
}
protected override void Stop()
{
base.Stop();
if (IIConfig.IsAsyncMode)
system.Stop();
if (IIConfig.IsUseAccelerator)
{
accelerator.Stop();
}
sensor.Disconnect();
}
protected override void Pause()
{
}
protected override void Resume()
{
}
#endregion
GoSystem system = null;
GoAccelerator accelerator = null;
GoSensor sensor = null;
public GocatorInitialConfig IIConfig
{
get => InitialConfig as GocatorInitialConfig;
}
///
/// 异步模式获取数据
///
///
private void onData(KObject data)
{
GoDataSet dataSet = (GoDataSet)data;
}
}
}