Commit cf5b8ccd authored by fanghaitong's avatar fanghaitong

1.0.2

parent bfc13c15
...@@ -52,6 +52,7 @@ dependencies { ...@@ -52,6 +52,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation project(path: ':KeyBoardView') implementation project(path: ':KeyBoardView')
implementation project(path: ':log') implementation project(path: ':log')
implementation project(path: ':zqtoolkit')
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.3.0' androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
<activity android:name=".BtListActivity" /> <activity android:name=".BtListActivity" />
<activity android:name=".MainActivityPtinter"></activity> <activity android:name=".MainActivityPtinter"></activity>
<activity android:name=".UsbActivity" /> <activity android:name=".UsbActivity" />
<activity android:name=".ZqToolActivity" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -61,6 +61,9 @@ class MainActivity : AppCompatActivity() { ...@@ -61,6 +61,9 @@ class MainActivity : AppCompatActivity() {
findViewById<Button>(R.id.btn_crash).setOnClickListener { findViewById<Button>(R.id.btn_crash).setOnClickListener {
throw RuntimeException("主线程崩溃") throw RuntimeException("主线程崩溃")
} }
findViewById<Button>(R.id.btn_zqtool).setOnClickListener {
startActivity(Intent(this, ZqToolActivity::class.java))
}
keyboard.btnText = "结账" keyboard.btnText = "结账"
keyboard.invalidate() keyboard.invalidate()
} }
......
package com.qimai.android.widget
import android.content.SharedPreferences
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.qmai.zqtoolkit.ZqScaleKit
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
class ZqToolActivity : AppCompatActivity() {
private var TAG = "EBSDK"
private var adpBaud: ArrayAdapter<String>? = null
private var spinBaud: Spinner? = null
private var nSelSpinBaud = 2
private var adpPort: ArrayAdapter<String>? = null
private var spinPort: Spinner? = null
private var nSelSpinPort = 1
private var etTare: EditText? = null
private var tvWeight: TextView? = null
private var tvLivingWeight: TextView? = null
private var tvTare: TextView? = null
private var tvState: TextView? = null
private var butConnect: Button? = null
private var butSetZero: Button? = null
private var butNetWeight: Button? = null
private var butClearTare: Button? = null
private var butSetTare: Button? = null
private var butAutoRead: Button? = null
private lateinit var sp: SharedPreferences;
private var m_bConnected = false
private var m_bThreadAutoReadExit = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_zqtool)
//
//
//
etTare = findViewById<View>(R.id.etTare) as EditText
tvWeight = findViewById<View>(R.id.tvWeight) as TextView
tvLivingWeight = findViewById<View>(R.id.tvLivingWeight) as TextView
tvTare = findViewById<View>(R.id.tvTare) as TextView
tvState = findViewById<View>(R.id.tvState) as TextView
butConnect = findViewById<View>(R.id.ButConnect) as Button
butConnect!!.setOnClickListener(MyClickListener)
butSetZero = findViewById<View>(R.id.ButSetZero) as Button
butSetZero!!.setOnClickListener(MyClickListener)
butNetWeight = findViewById<View>(R.id.ButNetweight) as Button
butNetWeight!!.setOnClickListener(MyClickListener)
butClearTare = findViewById<View>(R.id.ButClearTare) as Button
butClearTare!!.setOnClickListener(MyClickListener)
butSetTare = findViewById<View>(R.id.ButSetTare) as Button
butSetTare!!.setOnClickListener(MyClickListener)
butAutoRead = findViewById<View>(R.id.ButAutoRead) as Button
butAutoRead!!.setOnClickListener(MyClickListener)
//
sp = getSharedPreferences("com.example.zqebsdksample_preferences", MODE_PRIVATE)
//
val ListPort: MutableList<String> = ArrayList()
ListPort.add("ttyS0")
ListPort.add("ttyS1")
ListPort.add("ttyS2")
ListPort.add("ttyS3")
ListPort.add("ttyS4")
ListPort.add("ttyUSB0")
ListPort.add("ttyUSB1")
ListPort.add("ttyUSB2")
ListPort.add("s3c2410_serial3")
adpPort = ArrayAdapter(this, android.R.layout.simple_spinner_item, ListPort)
adpPort!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinPort = findViewById<View>(R.id.SpinPort) as Spinner
spinPort!!.adapter = adpPort
nSelSpinPort = sp.getInt("SelSpinPort", 2)
spinPort!!.setSelection(nSelSpinPort)
spinPort!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(arg0: AdapterView<*>?, arg1: View, arg2: Int, arg3: Long) {
nSelSpinPort = arg2
val editor = sp.edit()
editor.putInt("SelSpinPort", nSelSpinPort)
editor.commit()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
}
//
val ListBaud: MutableList<String> = ArrayList()
ListBaud.add("2400")
ListBaud.add("4800")
ListBaud.add("9600")
ListBaud.add("19200")
ListBaud.add("38400")
ListBaud.add("57600")
ListBaud.add("115200")
adpBaud = ArrayAdapter(this, android.R.layout.simple_spinner_item, ListBaud)
adpBaud!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinBaud = findViewById<View>(R.id.SpinBaud) as Spinner
spinBaud!!.adapter = adpBaud
nSelSpinBaud = sp.getInt("SelSpinBaud", 2)
spinBaud!!.setSelection(nSelSpinBaud)
spinBaud!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(arg0: AdapterView<*>?, arg1: View, arg2: Int, arg3: Long) {
nSelSpinBaud = arg2
val editor = sp.edit()
editor.putInt("SelSpinBaud", nSelSpinBaud)
editor.commit()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
}
lifecycleScope.launch {
ZqScaleKit.instance.connectState.collect {
if (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
m_bConnected = it.isConnect
if (m_bConnected) {
butConnect?.text = "断开"
} else butConnect?.text = "连接"
Log.d(TAG, it.toString())
}
}
}
lifecycleScope.launch {
ZqScaleKit.instance.livingWeight.collect {
if (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
Log.d(TAG, it.toString())
if (it.isStable) {
tvWeight?.text = it.weight
tvState?.text = "稳定"
tvState?.setBackgroundColor(Color.GREEN)
tvLivingWeight?.text = "实时重量" + it.weight
} else {
tvState?.setBackgroundColor(Color.RED)
tvState?.text = "不稳定"
tvLivingWeight?.text = "实时重量" + it.weight
}
}
}
}
}
override fun onPause() {
super.onPause()
m_bThreadAutoReadExit = true
if (m_bConnected) {
m_bConnected = false
ZqScaleKit.instance.disconnect()
butConnect!!.text = "连接"
}
}
private val MyClickListener =
View.OnClickListener { v ->
var nRet: Int
when (v.id) {
R.id.ButConnect -> {
if (!m_bConnected) {
ZqScaleKit.instance.connect(v.context)
// Thread {
// if (zqeb.EB_Connect(
// String.format(
// "%s:%s",
// spinPort!!.selectedItem.toString(),
// spinBaud!!.selectedItem.toString()
// ),
// v.context
// ) == PrinterConst.ErrorCode.SUCCESS
// ) {
// m_bConnected = true
// butConnect!!.text = "断开"
// }
// }.start()
} else {
m_bThreadAutoReadExit = true
ZqScaleKit.instance.disconnect()
m_bConnected = false
butConnect!!.text = "连接"
}
}
R.id.ButSetZero -> {
if (m_bConnected) {
ZqScaleKit.instance.setZero()
}
}
R.id.ButNetweight -> {
if (m_bConnected) {
ZqScaleKit.instance.setNetWeight()
}
}
R.id.ButClearTare -> {
if (m_bConnected) {
ZqScaleKit.instance.clearTare()
}
}
R.id.ButSetTare -> {
if (m_bConnected) {
// zqeb.EB_SetTare(java.lang.Double.valueOf(etTare!!.text.toString()))
}
}
R.id.ButAutoRead -> {
if (m_bConnected) {
ZqScaleKit.instance.readData()
}
// val strText = butAutoRead!!.text.toString()
// if (strText == "开始读数") {
// butAutoRead!!.text = "停止读数"
// m_bThreadAutoReadExit = false
// mHandlerAutoRead = Handler()
// Thread { //
// m_bThreadAutoReadRunning = true
// while (!m_bThreadAutoReadExit) {
// if (m_bConnected) {
// ReadData()
// }
// try {
// Thread.sleep(80)
// } catch (e: InterruptedException) {
// // TODO Auto-generated catch block
// e.printStackTrace()
// }
// }
// m_bThreadAutoReadRunning = false
// mHandlerAutoRead!!.post { butAutoRead!!.text = "开始读数" }
// }.start()
// } else {
// m_bThreadAutoReadExit = true
// }
}
}
}
}
\ No newline at end of file
...@@ -56,6 +56,11 @@ ...@@ -56,6 +56,11 @@
android:text="崩溃" android:text="崩溃"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" /> android:layout_height="50dp" />
<Button
android:id="@+id/btn_zqtool"
android:text="中琦电子称"
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="端口:"
android:textSize="32dp"></TextView>
<Spinner
android:id="@+id/SpinPort"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2.0"
android:text="端口"
android:textSize="32dp"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="波特率:"
android:textSize="32dp"></TextView>
<Spinner
android:id="@+id/SpinBaud"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2.0"
android:text="波特率"
android:textSize="32dp"></Spinner>
<Button
android:id="@+id/ButConnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="连接"
android:textSize="32dp"></Button>
</TableRow>
<TableRow>
<Button
android:id="@+id/ButSetZero"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="置零"
android:textSize="32dp"></Button>
<Button
android:id="@+id/ButNetweight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="除皮"
android:textSize="32dp"></Button>
<Button
android:id="@+id/ButClearTare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="清皮"
android:textSize="32dp"></Button>
<EditText
android:id="@+id/etTare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="0.05"
android:textSize="32dp"></EditText>
<Button
android:id="@+id/ButSetTare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="置皮"
android:textSize="32dp"></Button>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="重量"
android:textSize="32dp"></TextView>
<TextView
android:id="@+id/tvWeight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text=""
android:textSize="32dp"></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="皮重"
android:textSize="32dp"></TextView>
<TextView
android:id="@+id/tvTare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text=""
android:textSize="32dp"></TextView>
<Button
android:id="@+id/ButAutoRead"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="开始读数"
android:textSize="32dp"></Button>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tvState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="#ff0000"
android:gravity="center"
android:text=""
android:textSize="32dp"></TextView>
</TableRow>
<TextView
android:id="@+id/tvLivingWeight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:textColor="@color/black"
android:text=""
android:textSize="32dp"></TextView>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
</TableLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -8,3 +8,4 @@ include ':pickerview' ...@@ -8,3 +8,4 @@ include ':pickerview'
include ':dialog' include ':dialog'
include ':websocket' include ':websocket'
include ':rebootoncrash' include ':rebootoncrash'
include ':zqtoolkit'
/build
\ No newline at end of file
# 中琦电子称
## 操作
1.连接与断开(串口模式)
```
ZqScaleKit.instance.connect(context)
ZqScaleKit.instance.disconnect()
```
2.去皮
```
ZqScaleKit.instance.setNetWeight()
```
3.取消去皮
```
ZqScaleKit.instance.clearTare()
```
## 设置监听
```
lifecycleScope.launch {
ZqScaleKit.instance.connectState.collect {
if (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
}
}
}
```
\ No newline at end of file
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'com.whl.gradle-publish-plugin'
}
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 21
targetSdkVersion 31
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
group 'com.qmai.android.zqtoolkit'
version '1.0.2'
gradlePublish {
sourceJarEnabled = true
javaDocEnabled = false
signEnabled = false
releaseRepository {
url = "https://hub.zmcms.cn/repository/maven-releases/"
userName = "wanglei1"
password = "woshiwanglei123"
}
snapshotRepository {
url = "https://hub.zmcms.cn/repository/maven-snapshots/"
userName = "wanglei1"
password = "woshiwanglei123"
}
}
dependencies {
implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
\ No newline at end of file
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
\ No newline at end of file
package com.qmai.zqtoolkit
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.qmai.zqtoolkit.test", appContext.packageName)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qmai.zqtoolkit">
</manifest>
\ No newline at end of file
package com.qmai.zqtoolkit
/**
* author : tongzi
* date : 2022/2/15 14:00
* description :
*/
data class ConnectState(val isConnect: Boolean, val msg: String)
package com.qmai.zqtoolkit
/**
* author : tongzi
* date : 2022/2/15 14:00
* description :
*/
data class LivingWeight(val isStable: Boolean, val weight: String, val unit: String)
package com.qmai.zqtoolkit
import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel
import com.zqebsdk.zqebsdk
import com.zqprintersdk.PrinterConst
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
/**
* author : tongzi
* date : 2022/2/10 14:18
* description :
*/
class ZqScaleKit private constructor() : ViewModel() {
companion object {
val instance = Helper.instance
}
private object Helper {
val instance = ZqScaleKit()
}
private val zqeb by lazy {
zqebsdk()
}
private var stableWeight = mutableListOf<String>()
private var ieRead = false
private val _connectState = MutableSharedFlow<ConnectState>()
val connectState: SharedFlow<ConnectState> = _connectState
private val _livingWeight = MutableSharedFlow<LivingWeight>()
val livingWeight: SharedFlow<LivingWeight> = _livingWeight
fun connect(cxt: Context, port: String = "ttyS4", buad: String = "9600") {
GlobalScope.launch(context = Dispatchers.IO) {
val ebConnect = zqeb.EB_Connect(String.format("%s:%s", port, buad), cxt)
var msg = "";
msg = when (ebConnect) {
PrinterConst.ErrorCode.SUCCESS -> {
ieRead = true
"打开成功"
}
PrinterConst.ErrorCode.INVALIDPARAM -> {
"打印机参数无效"
}
PrinterConst.ErrorCode.PORTERROR -> {
"端口错误"
}
PrinterConst.ErrorCode.NOPERMISSION -> {
"没有该端口的访问权限"
}
else -> "未知错误"
}
_connectState.emit(ConnectState(ebConnect == PrinterConst.ErrorCode.SUCCESS, msg))
}
}
fun disconnect() {
ieRead = false
zqeb.EB_Disconnect()
}
fun setZero() {
zqeb.EB_SetZero()
}
fun setNetWeight() {
zqeb.EB_SetNetWeight()
}
fun clearTare() {
zqeb.EB_ClearTare()
}
val weightFlow = flow {
while (ieRead) {
emit(Unit)
delay(100)
}
}
fun readData() {
GlobalScope.launch() {
weightFlow.flowOn(Dispatchers.IO)
.collect {
if (ieRead) {
getWeightTare()
}
}
}
}
suspend fun getWeightTare() {
val strRet: Array<String> = zqeb.EB_GetWeightTare()
Log.v(
"ZqScaleKit", String.format(
"%s,%s,%s",
strRet[0], strRet[1], strRet[2]
)
)
if (strRet != null) {
var strState = ""
var strWeight = ""
var strTare = ""
var strUnit = ""
if (!strRet[1].isNullOrEmpty()) {
strUnit = strRet[1].replace("[^a-z^A-Z]".toRegex(), "")
strWeight = strRet[1].replace(strUnit, "")
}
if (Integer.valueOf(strRet[0]) == 0) {
strState = "OK"
strTare = strRet[2]
when {
stableWeight.size == 4 -> {
if (stableWeight.toSet().size == 1) {
_livingWeight.emit(LivingWeight(true, strWeight, strUnit))
}
stableWeight.clear()
}
stableWeight.size > 4 -> {
stableWeight.clear()
}
else -> stableWeight.add(strWeight)
}
} else {
// when (Integer.valueOf(strRet[0])) {
// -3 -> strState = "Data wrong"
// -2 -> strState = "Read Data Error"
// -1 -> strState = "Port Error"
//
// 1 -> {
// strState = "Weight instability"
// strWeight = strRet[1]
// strTare = strRet[2]
// }
// 2 -> {
// strState = "Weight overflow or not ZERO"
// strWeight = strRet[1]
// strTare = strRet[2]
// }
//
// }
_livingWeight.emit(LivingWeight(false, strWeight, strUnit))
}
}
}
}
package com.qmai.zqtoolkit
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment