|
@@ -1,11 +1,10 @@
|
|
|
#include "reader_method_call.h"
|
|
#include "reader_method_call.h"
|
|
|
#include "reader_service.h"
|
|
#include "reader_service.h"
|
|
|
|
|
|
|
|
-#include <flutter/flutter_engine.h>
|
|
|
|
|
#include <flutter/method_channel.h>
|
|
#include <flutter/method_channel.h>
|
|
|
|
|
+#include <flutter/plugin_registrar_windows.h>
|
|
|
#include <flutter/standard_method_codec.h>
|
|
#include <flutter/standard_method_codec.h>
|
|
|
#include <flutter/event_channel.h>
|
|
#include <flutter/event_channel.h>
|
|
|
-#include <flutter/event_stream_handler_functions.h>
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
#include <memory>
|
|
|
#include <string>
|
|
#include <string>
|
|
@@ -13,138 +12,124 @@
|
|
|
#include <functional>
|
|
#include <functional>
|
|
|
#include <map>
|
|
#include <map>
|
|
|
|
|
|
|
|
-static std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> g_event_channel = nullptr;
|
|
|
|
|
-static std::unique_ptr<flutter::StreamHandler<flutter::EncodableValue>> g_stream_handler = nullptr;
|
|
|
|
|
|
|
+// 全局变量存储事件发送器
|
|
|
|
|
+static std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> event_sink = nullptr;
|
|
|
|
|
|
|
|
void ReaderMethodCall::RegisterWindowsAPI(flutter::FlutterEngine *engine)
|
|
void ReaderMethodCall::RegisterWindowsAPI(flutter::FlutterEngine *engine)
|
|
|
{
|
|
{
|
|
|
|
|
+ // 注册MethodChannel用于方法调用
|
|
|
auto channel = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
|
auto channel = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
|
|
engine->messenger(), "com.vber.chicken_farm/win_reader",
|
|
engine->messenger(), "com.vber.chicken_farm/win_reader",
|
|
|
&flutter::StandardMethodCodec::GetInstance());
|
|
&flutter::StandardMethodCodec::GetInstance());
|
|
|
|
|
+
|
|
|
channel->SetMethodCallHandler([](const auto &call, auto result)
|
|
channel->SetMethodCallHandler([](const auto &call, auto result)
|
|
|
{ HandleMethodCall(call, std::move(result)); });
|
|
{ HandleMethodCall(call, std::move(result)); });
|
|
|
|
|
|
|
|
- g_event_channel = std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
|
|
|
|
|
|
|
+ // 注册EventChannel用于数据回调
|
|
|
|
|
+ auto event_channel = std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
|
|
|
engine->messenger(), "com.vber.chicken_farm/win_reader_events",
|
|
engine->messenger(), "com.vber.chicken_farm/win_reader_events",
|
|
|
&flutter::StandardMethodCodec::GetInstance());
|
|
&flutter::StandardMethodCodec::GetInstance());
|
|
|
|
|
|
|
|
- g_stream_handler =
|
|
|
|
|
- std::make_unique<flutter::StreamHandlerFunctions<flutter::EncodableValue>>(
|
|
|
|
|
- [](const flutter::EncodableValue *arguments,
|
|
|
|
|
- std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> &&events)
|
|
|
|
|
- -> std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>>
|
|
|
|
|
- {
|
|
|
|
|
- // 使用shared_ptr包装,使其可拷贝
|
|
|
|
|
- auto events_ptr = std::shared_ptr<flutter::EventSink<flutter::EncodableValue>>(std::move(events));
|
|
|
|
|
- ReaderService::GetInstance()->SetDataCallback([events_ptr](const std::string &data)
|
|
|
|
|
- { events_ptr->Success(flutter::EncodableValue(data)); });
|
|
|
|
|
- return nullptr;
|
|
|
|
|
- },
|
|
|
|
|
- [](const flutter::EncodableValue *arguments)
|
|
|
|
|
- -> std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>>
|
|
|
|
|
- {
|
|
|
|
|
- ReaderService::GetInstance()->SetDataCallback(nullptr);
|
|
|
|
|
- return nullptr;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ event_channel->SetStreamHandler(std::make_unique<flutter::StreamHandler<flutter::EncodableValue>>(
|
|
|
|
|
+ [engine](const flutter::EncodableValue* arguments,
|
|
|
|
|
+ std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& sink) -> std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> {
|
|
|
|
|
+ event_sink = std::move(sink);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置数据回调
|
|
|
|
|
+ ReaderService::GetInstance()->SetDataCallback([](const std::string& data) {
|
|
|
|
|
+ if (event_sink) {
|
|
|
|
|
+ // 将数据发送到Flutter端
|
|
|
|
|
+ event_sink->Success(flutter::EncodableValue(data));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- g_event_channel->SetStreamHandler(std::move(g_stream_handler));
|
|
|
|
|
|
|
+ return nullptr;
|
|
|
|
|
+ },
|
|
|
|
|
+ [](const flutter::EncodableValue* arguments) -> std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> {
|
|
|
|
|
+ event_sink = nullptr;
|
|
|
|
|
+ return nullptr;
|
|
|
|
|
+ }));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ReaderMethodCall::HandleMethodCall(
|
|
void ReaderMethodCall::HandleMethodCall(
|
|
|
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
|
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
|
|
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
{
|
|
{
|
|
|
- // Use a map to handle different method calls
|
|
|
|
|
- static std::map<std::string, std::function<void(const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)>> handlers = {
|
|
|
|
|
- {"scanUsb", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- // Call the scan USB method of the reader service
|
|
|
|
|
- std::vector<std::string> usb = ReaderService::GetInstance()->ScanUsb();
|
|
|
|
|
|
|
+ // 使用映射表来处理不同的方法调用
|
|
|
|
|
+ static std::map<std::string, std::function<void(const flutter::MethodCall<flutter::EncodableValue>&, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>>)>> handlers = {
|
|
|
|
|
+ {"scanUsb", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ // 调用读取器服务的扫描USB方法
|
|
|
|
|
+ std::vector<std::string> usb = ReaderService::GetInstance()->ScanUsb();
|
|
|
|
|
|
|
|
- // Create a list to store USB device names
|
|
|
|
|
- flutter::EncodableList device_list;
|
|
|
|
|
- for (const auto &device : usb)
|
|
|
|
|
- {
|
|
|
|
|
- device_list.push_back(flutter::EncodableValue(device));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 创建列表以存储USB设备名称
|
|
|
|
|
+ flutter::EncodableList device_list;
|
|
|
|
|
+ for (const auto &device : usb)
|
|
|
|
|
+ {
|
|
|
|
|
+ device_list.push_back(flutter::EncodableValue(device));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Return the list of USB devices to Flutter (empty if no devices found)
|
|
|
|
|
- result->Success(flutter::EncodableValue(device_list));
|
|
|
|
|
- }},
|
|
|
|
|
- {"connect", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- const auto *args = std::get_if<flutter::EncodableMap>(method_call.arguments());
|
|
|
|
|
- if (args)
|
|
|
|
|
- {
|
|
|
|
|
- auto deviceIndexIt = args->find(flutter::EncodableValue("deviceIndex"));
|
|
|
|
|
- if (deviceIndexIt != args->end())
|
|
|
|
|
- {
|
|
|
|
|
- int deviceIndex = std::get<int>(deviceIndexIt->second);
|
|
|
|
|
- bool success = ReaderService::GetInstance()->Connect(deviceIndex);
|
|
|
|
|
- result->Success(flutter::EncodableValue(success));
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- result->Success(flutter::EncodableValue(false));
|
|
|
|
|
- }},
|
|
|
|
|
- {"disconnect", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- ReaderService::GetInstance()->Disconnect();
|
|
|
|
|
- result->Success();
|
|
|
|
|
- }},
|
|
|
|
|
- {"startRead", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- bool success = ReaderService::GetInstance()->StartRead();
|
|
|
|
|
- result->Success(flutter::EncodableValue(success));
|
|
|
|
|
- }},
|
|
|
|
|
- {"stopRead", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- ReaderService::GetInstance()->StopRead();
|
|
|
|
|
- result->Success();
|
|
|
|
|
- }},
|
|
|
|
|
- {"getDeviceInfo", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- std::string deviceInfo = ReaderService::GetInstance()->GetDeviceInfo();
|
|
|
|
|
- result->Success(flutter::EncodableValue(deviceInfo));
|
|
|
|
|
- }},
|
|
|
|
|
- {"getPower", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- unsigned char power = 0;
|
|
|
|
|
- bool success = ReaderService::GetInstance()->GetPower(power);
|
|
|
|
|
- if (success) {
|
|
|
|
|
- result->Success(flutter::EncodableValue(static_cast<int>(power)));
|
|
|
|
|
- } else {
|
|
|
|
|
- result->Success(flutter::EncodableValue());
|
|
|
|
|
- }
|
|
|
|
|
- }},
|
|
|
|
|
- {"setPower", [](const flutter::MethodCall<flutter::EncodableValue> &method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
|
|
|
|
- {
|
|
|
|
|
- const auto *args = std::get_if<flutter::EncodableMap>(method_call.arguments());
|
|
|
|
|
- if (args)
|
|
|
|
|
- {
|
|
|
|
|
- auto powerIt = args->find(flutter::EncodableValue("power"));
|
|
|
|
|
- if (powerIt != args->end())
|
|
|
|
|
- {
|
|
|
|
|
- int power = std::get<int>(powerIt->second);
|
|
|
|
|
- bool success = ReaderService::GetInstance()->SetPower(static_cast<unsigned char>(power));
|
|
|
|
|
- result->Success(flutter::EncodableValue(success));
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- result->Success(flutter::EncodableValue(false));
|
|
|
|
|
- }}
|
|
|
|
|
- // More method handlers can be added here
|
|
|
|
|
- // {"methodName", [](auto result) { /* Handle method */ }}
|
|
|
|
|
|
|
+ // 将USB设备列表返回给Flutter(如果没有找到设备则为空)
|
|
|
|
|
+ result->Success(flutter::EncodableValue(device_list));
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"connect", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ // 获取参数
|
|
|
|
|
+ const auto *arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
|
|
|
|
|
+ if (arguments) {
|
|
|
|
|
+ auto device_index_it = arguments->find(flutter::EncodableValue("deviceIndex"));
|
|
|
|
|
+ if (device_index_it != arguments->end()) {
|
|
|
|
|
+ int device_index = device_index_it->second.LongValue();
|
|
|
|
|
+ bool success = ReaderService::GetInstance()->Connect(device_index);
|
|
|
|
|
+ result->Success(flutter::EncodableValue(success));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ result->Error("Invalid arguments", "Device index not provided");
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"disconnect", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ ReaderService::GetInstance()->Disconnect();
|
|
|
|
|
+ result->Success();
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"startRead", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ bool success = ReaderService::GetInstance()->StartRead();
|
|
|
|
|
+ result->Success(flutter::EncodableValue(success));
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"stopRead", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ ReaderService::GetInstance()->StopRead();
|
|
|
|
|
+ result->Success();
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"getDeviceInfo", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ std::string deviceInfo = ReaderService::GetInstance()->GetDeviceInfo();
|
|
|
|
|
+ result->Success(flutter::EncodableValue(deviceInfo));
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"getPower", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ unsigned char power = 0;
|
|
|
|
|
+ bool success = ReaderService::GetInstance()->GetPower(power);
|
|
|
|
|
+ if (success) {
|
|
|
|
|
+ result->Success(flutter::EncodableValue(static_cast<int>(power)));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result->Success(flutter::EncodableValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ }},
|
|
|
|
|
+ {"setPower", [](const flutter::MethodCall<flutter::EncodableValue>& method_call, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
|
|
|
|
+ const auto *arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
|
|
|
|
|
+ if (arguments) {
|
|
|
|
|
+ auto power_it = arguments->find(flutter::EncodableValue("power"));
|
|
|
|
|
+ if (power_it != arguments->end()) {
|
|
|
|
|
+ int power = power_it->second.LongValue();
|
|
|
|
|
+ bool success = ReaderService::GetInstance()->SetPower(static_cast<unsigned char>(power));
|
|
|
|
|
+ result->Success(flutter::EncodableValue(success));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ result->Error("Invalid arguments", "Power value not provided");
|
|
|
|
|
+ }}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // Find and execute the corresponding method handler
|
|
|
|
|
|
|
+ // 查找并执行对应的方法处理器
|
|
|
auto handler = handlers.find(method_call.method_name());
|
|
auto handler = handlers.find(method_call.method_name());
|
|
|
- if (handler != handlers.end())
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (handler != handlers.end()) {
|
|
|
handler->second(method_call, std::move(result));
|
|
handler->second(method_call, std::move(result));
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
result->NotImplemented();
|
|
result->NotImplemented();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|