import 'package:flutter/services.dart'; import 'terra_platform_interface.dart'; class Terra { Future getPlatformVersion() { return TerraPlatform.instance.getPlatformVersion(); } static const MethodChannel _channel = MethodChannel('terra'); // ==================== 设备管理 ==================== /// 打开所有设备 static Future openAllDevices() async { var deviceCount = await _channel.invokeMethod("openAllDevices"); return deviceCount; } /// 关闭所有设备 static Future closeAllDevices() async { return await _channel.invokeMethod("closeAllDevices"); } /// 关闭当前设备 static Future closeDevice() async { return await _channel.invokeMethod("close"); } /// 获取设备SN static Future getDeviceSn() async { return await _channel.invokeMethod("getDeviceSn"); } /// 获取设备索引 static Future getDeviceIndex() async { return await _channel.invokeMethod("getIndex"); } /// 设备是否连接成功 static Future isConnect() async { return await _channel.invokeMethod("isConnect"); } /// 开启 SDK 调试日志 static Future openDebug() async { return await _channel.invokeMethod("openDebug"); } /// 检查 USB 权限 static Future> checkUsbPermission() async { final result = await _channel.invokeMethod("checkUsbPermission"); return Map.from(result as Map); } // ==================== 参数配置 ==================== /// 设置积分时间 static Future setIntegrationTime(double integrationTime) async { Map args = {"integrationTime": integrationTime}; return await _channel.invokeMethod("setIntegrationTime", args); } /// 设置平均次数 static Future setAverageTimes(int averageTimes) async { Map args = {"averageTimes": averageTimes}; return await _channel.invokeMethod("setAverageTimes", args); } /// 设置触发模式 /// 0: Free Running /// 1: Hardware Level /// 2: Synchronous /// 3: Hardware Edge /// 4: Edge And Free static Future setTriggerMode(int triggerMode) async { Map args = {"triggerMode": triggerMode}; return await _channel.invokeMethod("setTriggerMode", args); } // ==================== 光谱数据获取 ==================== /// 获取光谱(获取当前光谱,可能拿到缓存光谱) static Future> getSpectrum() async { return await _channel.invokeMethod("getSpectrum"); } /// 获取光谱(获取光谱前会重新设置积分时间) static Future> getResetSpectrum() async { return await _channel.invokeMethod("getResetSpectrum"); } /// 获取波长 static Future> getWavelength() async { return await _channel.invokeMethod("getWavelength"); } /// 获取波数 static Future> getWaveNumber() async { return await _channel.invokeMethod("getWaveNumber"); } /// 获取处理完的光谱 static Future> getRamanSpectrum() async { List spectrum = await _channel.invokeMethod("getRamanSpectrum"); return spectrum; } /// 获取插值 X 轴数据(200~3200,步长 2) static Future> getXvalue() async { return await _channel.invokeMethod("getXvalue"); } // ==================== CCD 制冷控制 ==================== /// 开 CCD 制冷 static Future setCCDTECOn() async { return await _channel.invokeMethod("setCCDTECOn"); } /// 关 CCD 制冷 static Future setCCDTECOff() async { return await _channel.invokeMethod("setCCDTECOff"); } /// 设置 CCD 制冷目标温度(-20 ~ 40) static Future setCCDTECTemperature(int temperature) async { Map args = {"temperature": temperature}; return await _channel.invokeMethod("setCCDTECTemperature", args); } /// 获取 CCD 制冷状态(返回 16 字节状态数组) static Future> getCCDTECState() async { final result = await _channel.invokeMethod("getCCDTECState"); return List.from(result as List); } // ==================== 灯泡/脉冲控制 ==================== /// 设置灯泡延迟时间(微秒) static Future setLampDelayTime(double delayTime) async { return await _channel.invokeMethod("setLampDelayTime", {"delayTime": delayTime}); } /// 设置灯泡脉宽(微秒) static Future setLampWidth(double width) async { return await _channel.invokeMethod("setLampWidth", {"width": width}); } /// 设置灯泡周期(微秒) static Future setLampInterval(double interval) async { return await _channel.invokeMethod("setLampInterval", {"interval": interval}); } /// 获取灯泡延迟时间(微秒) static Future> getLampDelayTime() async { final result = await _channel.invokeMethod("getLampDelayTime"); return List.from(result as List); } /// 获取灯泡脉宽(微秒) static Future> getLampWidth() async { final result = await _channel.invokeMethod("getLampWidth"); return List.from(result as List); } /// 获取灯泡周期(微秒) static Future> getLampInterval() async { final result = await _channel.invokeMethod("getLampInterval"); return List.from(result as List); } /// 开关灯泡控制 static Future setLampEnable(bool enable) async { return await _channel.invokeMethod("setLampEnable", {"enable": enable}); } // ==================== 激光控制 ==================== /// 设置激发波长 static Future setExcitedWaveLength(int excitedWaveLength) async { return await _channel.invokeMethod("setExcitedWaveLength", {"excitedWaveLength": excitedWaveLength}); } /// 设置激光功率(范围 0-2000) static Future setLaserPower(double power) async { if (power < 0 || power > 2000) { print('[Terra] setLaserPower 参数超出范围 (0-2000): $power,拒绝执行'); return false; } Map args = {"power": power}; print('[Terra] setLaserPower 发送: power=$power'); return await _channel.invokeMethod("setLaserPower", args) .timeout(const Duration(seconds: 5), onTimeout: () { print('[Terra] setLaserPower 超时,设备可能未正确响应'); return false; }); } /// 开激光 static Future setLaserOn() async { return await _channel.invokeMethod("setLaserOn"); } /// 关激光 static Future setLaserOff() async { return await _channel.invokeMethod("setLaserOff"); } /// 开激光器电源 static Future setLaserPowerOn() async { return await _channel.invokeMethod("setLaserPowerOn"); } /// 关激光器电源 static Future setLaserPowerOff() async { return await _channel.invokeMethod("setLaserPowerOff"); } // ==================== 硬件开关 ==================== /// 开 TEC static Future setTECOn() async { return await _channel.invokeMethod("setTECOn"); } /// 关 TEC static Future setTECOff() async { return await _channel.invokeMethod("setTECOff"); } /// 开主板 static Future setMainBoardOn() async { return await _channel.invokeMethod("setMainBoardOn"); } /// 关主板 static Future setMainBoardOff() async { return await _channel.invokeMethod("setMainBoardOff"); } /// 主板是否已打开 static Future isMainBoardOpen() async { return await _channel.invokeMethod("isMainBoardOpen"); } /// TEC 是否已打开 static Future isTECOpen() async { return await _channel.invokeMethod("isTECOpen"); } // ==================== 光谱数据处理 ==================== /// 小波平滑 static Future> waveletSmooth(List spectrum) async { return await _channel.invokeMethod("waveletSmooth", {"spectrum": spectrum}); } /// 消荧光(side 为消荧光因子,默认 15) static Future> removeFluorescence(List spectrum, {int side = 15}) async { return await _channel.invokeMethod("removeFluorescence", {"spectrum": spectrum, "side": side}); } /// 线性插值 static Future> lineInterpolation( List xDataRaw, List yDataRaw, List xData, ) async { return await _channel.invokeMethod("lineInterpolation", { "xDataRaw": xDataRaw, "yDataRaw": yDataRaw, "xData": xData, }); } /// 寻峰 static Future> findPeaks( List spectrum, int minIndicesBetweenPeaks, double baseline, ) async { Map args = { "spectrum": spectrum, "minIndicesBetweenPeaks": minIndicesBetweenPeaks, "baseline": baseline, }; return await _channel.invokeMethod("findPeaks", args); } // ==================== 校准系数 ==================== /// 获取激光校正系数 static Future> getLaserPowerCorrectCoefficient() async { return await _channel.invokeMethod("getLaserPowerCorrectCoefficient"); } /// 设置激光校正系数 /// calibrationCoefficient 格式:16 位字符串,4 个数据,每个数据 4 位 /// 如 "0070030008001400" static Future setLaserPowerCorrectCoefficient(String calibrationCoefficient) async { Map args = {"calibrationCoefficient": calibrationCoefficient}; return await _channel.invokeMethod("setLaserPowerCorrectCoefficient", args); } /// 获取乙腈校准数据 static Future> getYJCorrectCoefficient() async { return await _channel.invokeMethod("getYJCorrectCoefficient"); } /// 设置乙腈定标系数 static Future setYJCorrectCoefficient(List coffes) async { Map> args = {"coffes": coffes}; return await _channel.invokeMethod("setYJCorrectCoefficient", args); } /// 获取 Y 轴校准系数 static Future> getYAxisCorrectCoefficient() async { final result = await _channel.invokeMethod("getYAxisCorrectCoefficient"); return List.from(result as List); } /// 获取探测器非线性校正系数 static Future> getCorrectForDetectorNonlinear() async { final result = await _channel.invokeMethod("getCorrectForDetectorNonlinear"); return List.from(result as List); } /// 获取波长校准系数 static Future> getWavelengthCalibrationCoefficients() async { final result = await _channel.invokeMethod("getWavelengthCalibrationCoefficients"); return List.from(result as List); } /// 获取 FPGA 版本 static Future> getFpgaVersion() async { final result = await _channel.invokeMethod("getFpgaVersion"); return List.from(result as List); } /// 获取狭缝大小 static Future getSlit() async { return await _channel.invokeMethod("getSlit"); } /// 获取坏点 static Future> getBadPoints() async { final result = await _channel.invokeMethod("getBadPoints"); return List.from(result as List); } // ==================== 算法匹配与校准 ==================== /// 算法注册 static Future ramanMatchRegister() async { return await _channel.invokeMethod("ramanMatchRegister"); } /// 光谱匹配,返回相似度 static Future ramanMatchCalcSimilarity( List spectrum, List libSpectrumList, ) async { Map> args = { "spectrum": spectrum, "libSpectrumList": libSpectrumList, }; return await _channel.invokeMethod("ramanMatchCalcSimilarity", args); } /// 校准 static Future> calibration( List spectrum, List YJCoeff, ) async { Map> args = { "spectrum": spectrum, "YJCoeff": YJCoeff, }; return await _channel.invokeMethod("calibration", args); } // ==================== 其他 ==================== /// SN 码 MD5 加密 static Future bytesToMD5(String sn) async { Map args = {"sn": sn}; return await _channel.invokeMethod("bytesToMD5", args); } }