Commit 2e6cd515 authored by tongzifang's avatar tongzifang

Merge branch 'feature/20220215-zqtoolkit'

parents 246a71c8 244bd2d1
......@@ -52,6 +52,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation project(path: ':KeyBoardView')
implementation project(path: ':log')
implementation project(path: ':zqtoolkit')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
......
......@@ -36,6 +36,7 @@
<activity android:name=".BtListActivity" />
<activity android:name=".MainActivityPtinter"></activity>
<activity android:name=".UsbActivity" />
<activity android:name=".ZqToolActivity" />
</application>
</manifest>
\ No newline at end of file
package com.qimai.android.widget
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.qimai.android.widget.databinding.ActivityLogBinding
import com.qimai.log.QLog
import com.qmai.zqtoolkit.ZqScaleKit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
......@@ -30,6 +35,13 @@ class LogActivity : AppCompatActivity() {
}
}
}
//测试称重reply
lifecycleScope.launch {
ZqScaleKit.instance.connectState.collect {
Log.d("LogActivity", it.toString())
}
}
}
/**
......
......@@ -64,6 +64,9 @@ class MainActivity : AppCompatActivity() {
findViewById<Button>(R.id.btn_crash).setOnClickListener {
throw RuntimeException("主线程崩溃")
}
findViewById<Button>(R.id.btn_zqtool).setOnClickListener {
startActivity(Intent(this, ZqToolActivity::class.java))
}
keyboard.btnText = "结账"
keyboard.invalidate()
......
This diff is collapsed.
......@@ -56,6 +56,11 @@
android:text="崩溃"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/btn_zqtool"
android:text="中琦电子称"
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
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>
<Button
android:id="@+id/ButAutoStopRead"
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
......@@ -20,7 +20,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "com.whl:gradle-publish-plugin:0.1.16-SNAPSHOT"
......
......@@ -7,3 +7,4 @@ include ':pickerview'
include ':dialog'
include ':websocket'
include ':rebootoncrash'
include ':zqtoolkit'
......@@ -22,7 +22,7 @@ android {
}
group 'com.qmai.android.sdk'
version '3.0.0.2-SNAPSHOT'
version '3.0.0.4-SNAPSHOT'
gradlePublish {
......
......@@ -19,7 +19,7 @@
For example:
<version value="1" />
-->
<version value="4" />
<version value="5" />
<!--
Define your models in the list with mapping tag, LitePal will
......@@ -35,6 +35,7 @@
<mapping class="com.zhimai.websocket.bean.MessageSqlBean" />
<mapping class="com.zhimai.websocket.bean.SocketOrderInfoBean"/>
<mapping class="com.zhimai.websocket.bean.SocketCheckMessage"/>
<mapping class="com.zhimai.websocket.bean.SocketRecordBean"/>
</list>
<!--
......
......@@ -17,9 +17,7 @@ class SocketRecordBean : LitePalSupport() {
var error: String? = null
//说明
var desc: String? = null
//预留扩展字段1
var otherOne:String? = null
//预留扩展字段2
var otherTwo:String? =null
var other:String? = null
}
\ No newline at end of file
/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.6'
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,
val tare: String,
val isReadErr: Boolean?
)
package com.qmai.zqtoolkit
import android.content.Context
import android.util.Log
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() {
companion object {
val instance = Helper.instance
}
private object Helper {
val instance = ZqScaleKit()
}
private val zqeb by lazy {
zqebsdk()
}
private var stableWeight = mutableListOf<String>()
private val _connectState = MutableSharedFlow<ConnectState>(replay = 1)
val connectState: SharedFlow<ConnectState> = _connectState
private val _livingWeight = MutableSharedFlow<LivingWeight>()
val livingWeight: SharedFlow<LivingWeight> = _livingWeight
private var job: Job? = null
fun connect(cxt: Context? = null, port: String = "ttyS4", buad: String = "9600") {
GlobalScope.launch(context = Dispatchers.IO) {
val ebConnect = zqeb.EB_Connect(String.format("%s:%s", port, buad), cxt)
Log.v(
"ZqScaleKit", String.format("%s:%s", port, buad)
)
val msg: String = when (ebConnect) {
PrinterConst.ErrorCode.SUCCESS -> {
"打开成功"
}
PrinterConst.ErrorCode.INVALIDPARAM -> {
"打印机参数无效"
}
PrinterConst.ErrorCode.PORTERROR -> {
"端口错误"
}
PrinterConst.ErrorCode.NOPERMISSION -> {
"没有该端口的访问权限"
}
else -> "未知错误"
}
delay(1000)
_connectState.emit(ConnectState(ebConnect == PrinterConst.ErrorCode.SUCCESS, msg))
}
}
fun disconnect() {
// stopReadData()
val ebDisconnect = zqeb.EB_Disconnect()
Log.v("ZqScaleKit", String.format("ebDisconnect: %s", ebDisconnect))
}
//置零
fun setZero() {
if (!_connectState.replayCache.isNullOrEmpty() && _connectState.replayCache.last().isConnect) {
val ebSetzero = zqeb.EB_SetZero()
Log.v("ZqScaleKit", String.format("ebSetzero: %s", ebSetzero))
}
}
//去皮
fun setNetWeight() {
if (!_connectState.replayCache.isNullOrEmpty() && _connectState.replayCache.last().isConnect) {
val ebSetnetweight = zqeb.EB_SetNetWeight()
Log.v("ZqScaleKit", String.format("ebSetnetweight: %s", ebSetnetweight))
}
}
//清皮
fun clearTare() {
if (!_connectState.replayCache.isNullOrEmpty() && _connectState.replayCache.last().isConnect) {
val ebCleartare = zqeb.EB_ClearTare()
Log.v("ZqScaleKit", String.format("ebCleartare: %s", ebCleartare))
}
}
//读取电子秤参数
fun getParameter(): Array<String> {
var parameter: Array<String> = arrayOf()
if (!_connectState.replayCache.isNullOrEmpty() && _connectState.replayCache.last().isConnect) {
parameter = zqeb.EB_GetParameter()
Log.v("ZqScaleKit", String.format("parameter: %s", parameter))
}
return parameter
}
private val weightFlow = flow {
while (true) {
emit(Unit)
delay(80)
}
}
fun stopReadData() {
job?.cancel()
job = null
}
fun readData() {
if (job != null && job?.isActive == true) {
return
}
job = GlobalScope.launch {
if (isActive) {
weightFlow.flowOn(Dispatchers.IO)
.collect {
getWeightTare()
}
}
}
}
//读取重量、皮重
private suspend fun getWeightTare() {
val strRet: Array<String> = zqeb.EB_GetWeightTare()
Log.v(
"ZqScaleKit", String.format(
"%s,%s,%s",
strRet[0], strRet[1], strRet[2]
)
)
var strState = ""
var strWeight = ""
var strTare = ""
var strUnit = ""
if (strRet[1].isNotEmpty()) {
strUnit = strRet[1].replace("[^a-z^A-Z]".toRegex(), "")
strWeight = strRet[1].replace(strUnit, "")
}
if (strUnit.isEmpty()&& strRet[2].isNotEmpty()) {
strTare = strRet[2].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,
strTare,
false
)
)
}
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, strTare,
Integer.valueOf(strRet[0]) == -2 || Integer.valueOf(strRet[0]) == -3
)
)
}
}
}
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