新增:主动控制增加远程WOL功能(用于远程唤醒同一个局域网其他设备) #190

This commit is contained in:
pppscn 2022-07-29 11:42:38 +08:00
parent 230957b731
commit 5b402895dc
9 changed files with 107 additions and 16 deletions

View File

@ -88,8 +88,8 @@ class WolSendFragment : BaseFragment<FragmentClientWolSendBinding?>(), View.OnCl
.items(wolHistory.keys)
.itemsCallbackSingleChoice(0) { _: MaterialDialog?, _: View?, _: Int, text: CharSequence ->
//XToastUtils.info("$which: $text")
binding!!.etIp.setText(text)
binding!!.etMac.setText(wolHistory[text])
binding!!.etMac.setText(text)
binding!!.etIp.setText(wolHistory[text])
true // allow selection
}
.positiveText(R.string.select)
@ -128,9 +128,17 @@ class WolSendFragment : BaseFragment<FragmentClientWolSendBinding?>(), View.OnCl
return
}
val port = binding!!.etPort.text.toString()
val portRegex = getString(R.string.wol_port_regex).toRegex()
if (!TextUtils.isEmpty(port) && !portRegex.matches(port)) {
XToastUtils.error(ResUtils.getString(R.string.wol_port_error))
return
}
val dataMap: MutableMap<String, Any> = mutableMapOf()
dataMap["ip"] = ip
dataMap["mac"] = mac
dataMap["port"] = port
msgMap["data"] = dataMap
val requestMsg: String = Gson().toJson(msgMap)

View File

@ -2,7 +2,7 @@ package com.idormy.sms.forwarder.server.component
import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.idormy.sms.forwarder.server.model.BaseRequest
import com.idormy.sms.forwarder.utils.HttpServerUtils
import com.yanzhenjie.andserver.annotation.Converter
@ -36,8 +36,12 @@ class AppMessageConverter : MessageConverter {
val json = if (charset == null) IOUtils.toString(stream) else IOUtils.toString(stream, charset)
Log.d(TAG, "Json: $json")
//TODO:待迁移kotlinx.serializationtype转换问题
val t: T? = Gson().fromJson(json, type)
//修改接口数据中的null、“”为默认值
val builder = GsonBuilder()
builder.registerTypeAdapter(Int::class.java, IntegerDefaultAdapter())
builder.registerTypeAdapter(String::class.java, StringDefaultAdapter())
val gson = builder.create()
val t: T? = gson.fromJson(json, type)
Log.d(TAG, "Bean: $t")
//校验时间戳时间误差不能超过1小时&& 签名

View File

@ -0,0 +1,23 @@
package com.idormy.sms.forwarder.server.component
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import java.io.IOException
class IntegerDefaultAdapter : TypeAdapter<Int>() {
@Throws(IOException::class)
override fun write(jsonWriter: JsonWriter, value: Int) {
jsonWriter.value(value.toString())
}
@Throws(IOException::class)
override fun read(jsonReader: JsonReader): Int {
return try {
Integer.valueOf(jsonReader.nextString())
} catch (e: NumberFormatException) {
e.printStackTrace()
-1
}
}
}

View File

@ -0,0 +1,24 @@
package com.idormy.sms.forwarder.server.component
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import java.io.IOException
class StringDefaultAdapter : TypeAdapter<String?>() {
@Throws(IOException::class)
override fun write(jsonWriter: JsonWriter, s: String?) {
jsonWriter.value(s)
}
@Throws(IOException::class)
override fun read(jsonReader: JsonReader): String {
return if (jsonReader.peek() === JsonToken.NULL) {
jsonReader.nextNull()
""
} else {
jsonReader.nextString()
}
}
}

View File

@ -39,13 +39,12 @@ class WolController {
System.arraycopy(macBytes, 0, bytes, i, macBytes.size)
i += macBytes.size
}
val packet = if (TextUtils.isEmpty(wolData.ip)) {
val address: InetAddress = InetAddress.getByName(wolData.ip)
DatagramPacket(bytes, bytes.size, address, 9)
} else {
DatagramPacket(bytes, bytes.size)
}
val host = if (TextUtils.isEmpty(wolData.ip)) "230.0.0.1" else wolData.ip
val port = if (wolData.port > 0) wolData.port else 9
val address: InetAddress = InetAddress.getByName(host)
val packet = DatagramPacket(bytes, bytes.size, address, port)
socket.send(packet)
socket.close()
Log.d(TAG, "Wake-on-LAN packet sent.")
} catch (e: Exception) {
Log.e(TAG, "Failed to send Wake-on-LAN packet: $e")

View File

@ -8,4 +8,6 @@ data class WolData(
var mac: String,
@SerializedName("ip")
var ip: String = "",
@SerializedName("port")
var port: Int = 9,
) : Serializable

View File

@ -75,6 +75,31 @@
</LinearLayout>
<LinearLayout
style="@style/senderBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/port" />
<com.xuexiang.xui.widget.edittext.materialedittext.MaterialEditText
android:id="@+id/et_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/wol_port_hint"
android:singleLine="true"
android:value="9"
app:met_clearButton="true"
app:met_errorMessage="@string/wol_port_error"
app:met_regexp="@string/wol_port_regex"
app:met_validateOnFocusLost="true" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -883,13 +883,16 @@
<string name="appsecret">AppSecret</string>
<string name="sampleText">Sample Text</string>
<string name="sampleMarkdown">Sample Markdown</string>
<string name="ip_hint">Please enter an IP address, eg. 192.168.168.168</string>
<string name="ip_hint">IP address/broadcast address, eg. 192.168.1.255</string>
<string name="ip_error">Malformed IP address, eg. 192.168.168.168</string>
<string name="ip_regex">^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])$</string>
<string name="mac_hint">Please enter the network card mac, eg. AA:BB:CC:DD:EE:FF</string>
<string name="mac_hint">Network card mac, eg. AA:BB:CC:DD:EE:FF</string>
<string name="mac_error">The network card mac format is incorrect, eg. AA:BB:CC:DD:EE:FF</string>
<string name="mac_regex">^((([a-fA-F0-9]{2}:){5})|(([a-fA-F0-9]{2}-){5}))[a-fA-F0-9]{2}$</string>
<string name="ip">IP</string>
<string name="mac">MAC</string>
<string name="no_wol_history">There is no history record, WOL will be added automatically after successful sending</string>
<string name="wol_port_hint">WOL is generally sent over port 7 or port 9</string>
<string name="wol_port_error">Port number value range: 1~65535</string>
<string name="wol_port_regex">^([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$</string>
</resources>

View File

@ -884,13 +884,16 @@
<string name="appsecret">AppSecret</string>
<string name="sampleText">文本类型</string>
<string name="sampleMarkdown">Markdown类型</string>
<string name="ip_hint">请输入IP地址例如192.168.168.168</string>
<string name="ip_hint">可选主机IP/广播地址例如192.168.1.255</string>
<string name="ip_error">IP地址格式错误例如192.168.168.168</string>
<string name="ip_regex">^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])$</string>
<string name="mac_hint">请输入网卡mac例如AA:BB:CC:DD:EE:FF</string>
<string name="mac_hint">必填,网卡mac例如AA:BB:CC:DD:EE:FF</string>
<string name="mac_error">网卡mac格式错误例如AA:BB:CC:DD:EE:FF</string>
<string name="mac_regex">^((([a-fA-F0-9]{2}:){5})|(([a-fA-F0-9]{2}-){5}))[a-fA-F0-9]{2}$</string>
<string name="ip">IP地址</string>
<string name="ip">主机IP/广播地址</string>
<string name="mac">网卡MAC</string>
<string name="no_wol_history">暂无历史记录WOL发送成功后自动加入</string>
<string name="wol_port_hint">可选WOL一般透过端口7或端口9进行发送</string>
<string name="wol_port_error">端口号取值范围1~65535</string>
<string name="wol_port_regex">^([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$</string>
</resources>