#include "Frm01.h"

Frm01::Frm01(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm01::~Frm01() {
}

void Frm01::onInit(){
}

void Frm01::createParts() {
	setMainArg(0, 0, 800, 480);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(439, 124, 152, 63, false);
	wMButton1->setPressedArg("读单/多条指令[03]", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("读单/多条指令[03]", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm01::wMButton1_clk_cb);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(449, 10, 131, 59, "", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel3->bindData(_V("Modbus/RTU_按分区.STATUS"));

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(439, 257, 152, 63, false);
	wMButton2->setPressedArg("写单个寄存器[06]", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("写单个寄存器[06]", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm01::wMButton2_clk_cb);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(439, 375, 152, 63, false);
	wMButton3->setPressedArg("写多个寄存器[10]", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->setReleasedArg("写多个寄存器[10]", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm01::wMButton3_clk_cb);

	/*----------------------------- TextNumber : wMTextNumber4           -----------------------------*/
	wMTextNumber4 = new STTTextArea(p_rt, this, "wMTextNumber4");
	wMTextNumber4->setMainArg(178, 263, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber4->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber4->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber4->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel5                     -----------------------------*/
	wMLabel5 = new STTLabel(p_rt, this, "wMLabel5");
	wMLabel5->setMainArg(343, 21, 96, 37, "连接状态：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel5->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel5->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel5->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel6                     -----------------------------*/
	wMLabel6 = new STTLabel(p_rt, this, "wMLabel6");
	wMLabel6->setMainArg(170, 213, 108, 37, "寄存器地址：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel6->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel6->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel6->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber5           -----------------------------*/
	wMTextNumber5 = new STTTextArea(p_rt, this, "wMTextNumber5");
	wMTextNumber5->setMainArg(309, 263, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber5->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber5->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber5->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel7                     -----------------------------*/
	wMLabel7 = new STTLabel(p_rt, this, "wMLabel7");
	wMLabel7->setMainArg(301, 213, 124, 37, "寄存器写入值：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel7->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel7->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel7->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber6           -----------------------------*/
	wMTextNumber6 = new STTTextArea(p_rt, this, "wMTextNumber6");
	wMTextNumber6->setMainArg(31, 388, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber6->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber6->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber6->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel8                     -----------------------------*/
	wMLabel8 = new STTLabel(p_rt, this, "wMLabel8");
	wMLabel8->setMainArg(16, 338, 132, 37, "寄存器起始地址：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel8->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel8->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel8->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber7           -----------------------------*/
	wMTextNumber7 = new STTTextArea(p_rt, this, "wMTextNumber7");
	wMTextNumber7->setMainArg(162, 388, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber7->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber7->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber7->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel9                     -----------------------------*/
	wMLabel9 = new STTLabel(p_rt, this, "wMLabel9");
	wMLabel9->setMainArg(154, 338, 124, 37, "寄存器写入值1：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel9->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel9->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel9->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber8           -----------------------------*/
	wMTextNumber8 = new STTTextArea(p_rt, this, "wMTextNumber8");
	wMTextNumber8->setMainArg(301, 388, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber8->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber8->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber8->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel10                    -----------------------------*/
	wMLabel10 = new STTLabel(p_rt, this, "wMLabel10");
	wMLabel10->setMainArg(293, 338, 124, 37, "寄存器写入值2：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel10->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel10->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel10->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel11                    -----------------------------*/
	wMLabel11 = new STTLabel(p_rt, this, "wMLabel11");
	wMLabel11->setMainArg(601, 374, 192, 65, "该案例演示固定写两个值", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel11->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel11->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel11->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber9           -----------------------------*/
	wMTextNumber9 = new STTTextArea(p_rt, this, "wMTextNumber9");
	wMTextNumber9->setMainArg(169, 137, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber9->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber9->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber9->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel12                    -----------------------------*/
	wMLabel12 = new STTLabel(p_rt, this, "wMLabel12");
	wMLabel12->setMainArg(162, 87, 108, 37, "寄存器地址：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel12->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel12->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel12->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- TextNumber : wMTextNumber10          -----------------------------*/
	wMTextNumber10 = new STTTextArea(p_rt, this, "wMTextNumber10");
	wMTextNumber10->setMainArg(311, 137, 88, 51, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMTextNumber10->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 15, 0, 4, 4, 255);
	wMTextNumber10->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::DIGITAL);
	wMTextNumber10->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Label : wMLabel13                    -----------------------------*/
	wMLabel13 = new STTLabel(p_rt, this, "wMLabel13");
	wMLabel13->setMainArg(303, 87, 108, 37, "寄存器个数：", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel13->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel13->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel13->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel14                    -----------------------------*/
	wMLabel14 = new STTLabel(p_rt, this, "wMLabel14");
	wMLabel14->setMainArg(16, 7, 147, 65, "此案例默认站号为1", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel14->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel14->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel14->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*called after widget created.*/

	return; //createParts END
}

void Frm01::onLoad(){
}

bool Frm01::onClosing(){
	return true;
}

void Frm01::onDispose(){
}

void Frm01::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
//	char * buff,uint8_t slave_addr, uint16_t reg_addr, uint16_t length
	char buff[256] = {0};
	uint8_t slave_addr = 0x01;
	uint16_t reg_addr = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber9->getText()));
	uint16_t length = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber10->getText()));
	this->hmiApp->modbus03_read(buff, slave_addr, reg_addr, length);
	cout<<"END"<<endl;
}

void Frm01::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
	//	uint8_t slave_addr, uint16_t reg_addr, uint16_t value
	uint8_t slave_addr = 0x01;
	uint16_t reg_addr = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber4->getText()));
	uint16_t value = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber5->getText()));
	this->hmiApp->modbus06_write(slave_addr, reg_addr, value);
	cout << "END" << endl;
}

void Frm01::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
	//	uint8_t slave_addr, uint16_t start_addr, const std::vector<uint16_t>& values
	vector<uint16_t> values;
	uint8_t slave_addr = 0x01;
	uint16_t reg_addr = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber6->getText()));
	uint16_t values1 = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber7->getText()));
	uint16_t values2 = static_cast<uint16_t>(StringUtil::safeStoi(wMTextNumber8->getText()));
	values.emplace_back(values1);
	values.emplace_back(values2);
	this->hmiApp->modbus10_write(slave_addr, reg_addr, values);
	cout << "END" << endl;
}
