UserCache.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.naz.sdkdemo.weight;
  2. import android.text.TextUtils;
  3. import android.util.Log;
  4. import com.google.gson.Gson;
  5. import com.naz.sdkdemo.bean.UserDetail;
  6. import com.naz.sdkdemo.comm.Constants;
  7. import com.naz.sdkdemo.utils.SPUtils;
  8. import android_serialport_api.SerialPortFinder;
  9. /**
  10. * Description:
  11. * Author: ydd
  12. * Date:2020/12/7
  13. */
  14. public class UserCache extends SPUtils {
  15. /**
  16. * 保存用户详情信息
  17. */
  18. public static void saveUserInfoDetail(UserDetail userInfo) {
  19. setString(Constants.USERCACHEKEY, new Gson().toJson(userInfo));
  20. }
  21. /**
  22. * 获取用户详情信息
  23. */
  24. public static UserDetail getUserInfoDetail() {
  25. String str = getString(Constants.USERCACHEKEY);
  26. if (!TextUtils.isEmpty(str)) {
  27. return new Gson().fromJson(str, UserDetail.class);
  28. } else {
  29. return null;
  30. }
  31. }
  32. /**
  33. * 清除用户详情数据
  34. *
  35. * @return true is success
  36. */
  37. public static void clearUserInfoDetail() {
  38. remove(Constants.USERCACHEKEY);
  39. }
  40. public static void saveDeviceId(String code) {
  41. setString(Constants.DEVICE_CODE, code);
  42. }
  43. public static String getDeviceId() {
  44. String str = getString(Constants.DEVICE_CODE);
  45. if (!TextUtils.isEmpty(str)) {
  46. return str;
  47. } else {
  48. return "DV00001";
  49. }
  50. }
  51. public static void saveSerialInfo(String ip, int port, String name, int rate) {
  52. setString(Constants.IP, ip);
  53. setInt(Constants.PORT, port);
  54. setString(Constants.SERIAL_NAME, name);
  55. setInt(Constants.SERIAL_RATE, rate);
  56. }
  57. public static String getIP() {
  58. String str = getString(Constants.IP);
  59. if (!TextUtils.isEmpty(str)) {
  60. return str;
  61. } else {
  62. return "192.168.1.253";
  63. }
  64. }
  65. public static int getPort() {
  66. int value = getInt(Constants.PORT);
  67. if (value == -1){
  68. value = 1030;
  69. }
  70. return value;
  71. }
  72. public static int getSerialRate() {
  73. int value = getInt(Constants.SERIAL_RATE);
  74. if (value == -1){
  75. value = 19200;
  76. }
  77. return value;
  78. }
  79. public static String getSerialName() {
  80. SerialPortFinder finder = new SerialPortFinder();
  81. if (finder.getAllDevicesPath().length>0){
  82. for (int i = 0; i < finder.getAllDevicesPath().length; i++) {
  83. Log.e("------------>","---------->finder.getAllDevicesPath("+i+")="+finder.getAllDevicesPath()[i]);
  84. }
  85. Log.e("------------>","---------->finder.getAllDevicesPath()="+finder.getAllDevicesPath());
  86. String sName = finder.getAllDevicesPath()[0];
  87. return sName;
  88. }
  89. return "";
  90. }
  91. }