UniPRT SDK v1.0.0.0
 
载入中...
搜索中...
未找到
Mgmt

与 JSONMng 相关的类和方法。 更多...

class  JsonMessenger
 用于管理基于 JSON 的讯息传递的类。 更多...
 
class  JsonMng
 用于管理 JSON 讯息的类。 更多...
 

详细描述

与 JSONMng 相关的类和方法。

Objective-C 示例,请参见此处

Swift 示例,请参见此处

Example

Objective-C:

#import "ViewController.h"
#define MAX_INPUT_MSG_CAPACITY 20
#define MAX_WAIT_TIME_SECS 15
@import UniPRT;
@interface ViewController ()
@property (strong) TcpComm *tcpComm;
@property (nonatomic, strong) UITextField *ipTextField;
@property (nonatomic, strong) UITextView *resultTextView;
@property (nonatomic, strong) UIButton *openButton;
@property (nonatomic, strong) UIButton *connectCloseButton;
@property (nonatomic, strong) UIButton *connectMgmtCommTestButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Create and configure IP address text field
self.ipTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 200, 30)];
self.ipTextField.placeholder = @"Enter IP Address";
self.ipTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.ipTextField];
self.ipTextField.text = @"10.0.10.180";
// Create and configure result text view
self.resultTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 150, 300, 200)];
self.resultTextView.editable = NO;
self.resultTextView.scrollEnabled = YES;
self.resultTextView.layer.borderWidth = 1.0;
self.resultTextView.layer.borderColor = [[UIColor blackColor] CGColor];
[self.view addSubview:self.resultTextView];
// Create and configure buttons
self.openButton = [self createButtonWithTitle:@"Open" frame:CGRectMake(20, 370, 80, 30)];
self.openButton.backgroundColor = [UIColor redColor];
[self.openButton addTarget:self action:@selector(openButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.openButton];
self.connectCloseButton = [self createButtonWithTitle:@"Connect" frame:CGRectMake(20, 420, 120, 30)];
self.connectCloseButton.backgroundColor = [UIColor redColor];
[self.connectCloseButton addTarget:self action:@selector(connectCloseButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.connectCloseButton];
self.connectMgmtCommTestButton = [self createButtonWithTitle:@"MgmtCommTest" frame:CGRectMake(20, 470, 300, 30)];
self.connectMgmtCommTestButton.backgroundColor = [UIColor redColor];
[self.connectMgmtCommTestButton addTarget:self action:@selector(MgmtCommTestButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.connectMgmtCommTestButton];
}
- (UIButton *)createButtonWithTitle:(NSString *)title frame:(CGRect)frame {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
[button setTitle:title forState:UIControlStateNormal];
return button;
}
- (void)openButtonPressed {
[self.tcpComm open];
}
- (void)connectCloseButtonPressed {
NSString *currentTitle = [self.connectCloseButton titleForState:UIControlStateNormal];
if ([currentTitle isEqualToString:@"Connect"]) {
NSString *ipAddress = self.ipTextField.text;
self.tcpComm = [[TcpComm alloc] initWithIPAddress:ipAddress port:DEFAULT_MGMT_PORT];
[self.connectCloseButton setTitle:@"Close" forState:UIControlStateNormal];
} else if ([currentTitle isEqualToString:@"Close"]) {
[self.tcpComm close];
[self.connectCloseButton setTitle:@"Connect" forState:UIControlStateNormal];
}
}
- (void)MgmtCommTestButtonPressed {
CommType commType = TCP_COMM;
JsonMessenger *jsonMessenger = [[JsonMessenger alloc] initWithCommToPtr:self.tcpComm iCommTyp:commType maxInputMsgCapacity:MAX_INPUT_MSG_CAPACITY usingDataPort:false];
if (jsonMessenger != nil)
{
NSString *command = @"Cfg.Prop";
NSString *content = @"{\r\n\"all\" : null\r\n}\r\n";
NSLog(@"\nSend to Printer:\n\"Command\": \"%@\"\n\"Content\":\n%@", command, content);
JsonMng *trackNumber = [jsonMessenger sendMsgAndWaitForResponseWithCommand:command content:content maxWaitTimeSecs:MAX_WAIT_TIME_SECS];
NSString *receivedString = trackNumber.strResponse;
self.resultTextView.text = receivedString;
}
}
@end
用于管理基于 JSON 的讯息传递的类。
Definition JsonMessenger.h:43
JsonMng * sendMsgAndWaitForResponseWithCommand:content:maxWaitTimeSecs:(NSString *command,[content] NSString *content,[maxWaitTimeSecs] int maxWaitTimeSecs)
发送管理命令和数据/内容到印表机并等待最大等待时间秒数的响应。
用于管理 JSON 讯息的类。
Definition JsonMng.h:31
NSString * strResponse
JSON 讯息的响应字符串。
Definition JsonMng.h:90
一个用于 TCP 通信的类,继承自 AComm 并符合 NSStreamDelegate 协议。
Definition TcpComm.h:101

Swift:

import SwiftUI
import UniPRT
struct ContentView: View {
@State private var ipText = "10.0.10.178"
@State private var communicationType = 0 // 0 for TCP
@StateObject private var viewModel = CommunicationViewModel()
var body: some View {
NavigationView {
Form {
Section(header: Text("Connection Type")) {
Picker("Select Type", selection: $communicationType) {
Text("TCP").tag(0)
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("IP Address")) {
TextField("Enter IP Address", text: $ipText)
.keyboardType(.decimalPad)
}
Section(header: Text("Output")) {
ScrollView {
Text(viewModel.resultText)
.frame(maxWidth: .infinity, alignment: .leading)
.fixedSize(horizontal: false, vertical: true)
.font(.system(size: 8))
}
.frame(height: 300)
.border(Color.secondary, width: 1)
}
Button("Connect/Close") {
viewModel.connectCloseAction(communicationType: communicationType, ip: ipText)
}
Button("Open") {
viewModel.openAction(communicationType: communicationType)
}
Button("TestMgmtComm") {
viewModel.testMgmtComm(communicationType: communicationType)
}
Button("Set Values") {
viewModel.setValues()
}
Button("Get Properties for Key") {
viewModel.getPropertiesForKey()
}
Button("Get Values for Keys") {
viewModel.getValuesForKeys()
}
Button("Get All Values") {
viewModel.getAllValues()
}
Button("Get Properties for Keys") {
viewModel.getPropertiesForKeys()
}
Button("Get All Properties") {
viewModel.getAllProperties()
}
Button("Configure JSON") {
viewModel.configureJSON()
}
}
.navigationBarTitle("Communication")
}
}
}
class CommunicationViewModel: ObservableObject {
@Published var resultText = ""
var tcpComm: TcpComm?
func openAction(communicationType: Int) {
tcpComm?.open()
self.resultText = "TCP Connection opened"
}
func testMgmtComm(communicationType: Int) {
let jsonMessenger = JsonMessenger(commToPtr: self.tcpComm, iCommTyp: UniPRT.CommType.TCP_COMM, maxInputMsgCapacity: 20, usingDataPort: false)
let command = "Cfg.Prop"
let content = "{\r\n\"all\" : null\r\n}\r\n"
let trackNumber = jsonMessenger?.sendMsgAndWaitForResponse(withCommand: command, content: content, maxWaitTimeSecs: 15)
self.resultText = trackNumber?.strResponse ?? "Error or no response"
}
func setValues() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
jsonComm?.usingDataPort = true
let settingReadWrite = SettingsReadWrite(jsonComm: jsonComm)
self.resultText = String(settingReadWrite?.setValues(["BT.PairMethod":"eNumericComp","BT.ConnectName":"BT-PTX3"]) ?? false)
}
func getPropertiesForKey() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
jsonComm?.usingDataPort = true
let settingReadWrite = SettingsReadWrite(jsonComm: jsonComm)
if let propkey = settingReadWrite?.getPropertiesForKey("BT.PairMethod") {
self.resultText = "\‍(propkey.description)"
} else {
self.resultText = "Default Value"
}
}
func getValuesForKeys() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
jsonComm?.usingDataPort = true
let settingReadWrite = SettingsReadWrite(jsonComm: jsonComm)
let partOfKeys = ["BT.PairMethod", "BT.ConnectName"]
if let values = settingReadWrite?.getValuesForKeys(partOfKeys) {
self.resultText = convertDictionaryToString(values)
} else {
self.resultText = "No values found"
}
}
func getAllValues() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
jsonComm?.usingDataPort = true
let settingReadWrite = SettingsReadWrite(jsonComm: jsonComm)
if let values = settingReadWrite?.getAllValues() {
self.resultText = convertDictionaryToString(values)
} else {
self.resultText = "No values found"
}
}
func getPropertiesForKeys() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
jsonComm?.usingDataPort = true
let settingReadWrite = SettingsReadWrite(jsonComm: jsonComm)
let partOfKeys = ["BT.PairMethod", "BT.ConnectName"]
if let properties = settingReadWrite?.getPropertiesForKeys(partOfKeys) {
let propertiesDescriptions = properties.mapValues { $0.description }
self.resultText = convertDictionaryToString(propertiesDescriptions)
} else {
self.resultText = "No properties found"
}
}
func getAllProperties() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
jsonComm?.usingDataPort = true
let settingReadWrite = SettingsReadWrite(jsonComm: jsonComm)
if let properties = settingReadWrite?.getAllProperties() {
let propertiesDescriptions = properties.mapValues { $0.description }
self.resultText = convertDictionaryToString(propertiesDescriptions)
} else {
self.resultText = "No properties found"
}
}
func configureJSON() {
let jsonComm = JsonComm(comm: self.tcpComm, commType: CommType.TCP_COMM.rawValue)
if let jsonconfig = JsonConfig(jsonComm: jsonComm) {
if let config = jsonconfig.getWithNumber(3) {
self.resultText = "Config: \‍(config)"
config.setName("123")
config.setNumber(3)
// config.setModel("T820")
let success = jsonconfig.setConfig(config)
if success {
self.resultText = "Configuration set successfully."
} else {
self.resultText = "Failed to set configuration."
}
let tempConfig: Config? = config
self.resultText += "\ntempConfig:::\‍(String(describing: tempConfig))"
if let allCfg = jsonconfig.getAllConfig() {
self.resultText += "\nallConfig:::\‍(allCfg)"
} else {
self.resultText += "\nFailed to get all configurations."
}
} else {
self.resultText = "Failed to get configuration with number 1."
}
}
}
func convertDictionaryToString(_ dict: [String: Any]) -> String {
if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
return String(data: jsonData, encoding: .utf8) ?? "Conversion failed"
}
return "Conversion failed"
}
func connectCloseAction(communicationType: Int, ip: String) {
if tcpComm != nil {
tcpComm?.close()
tcpComm = nil
self.resultText = "TCP Connection closed"
} else {
// Reconnect logic
tcpComm = TcpComm(ipAddress: ip, port: 3007)
self.resultText = "TCP Reconnected"
}
}
}
#Preview {
ContentView()
}