jieshun-parking 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae1b05c5df13925f6987df22d4586859d7887d8cf9dbc4ce17c6c2c008994d14
4
- data.tar.gz: 21074f7efc9c3e4c0addd8715df66757ed1772cd551726553ef5cbb2f65c2084
3
+ metadata.gz: 318dc7eecf5a1390f6027cff4886d4beb60068a4cdfb220d4b280bd95625e8a6
4
+ data.tar.gz: 484b8893041d1c8ae4bb7b1880dc437dcaaaa0529f02cea311503fb0c12e87b4
5
5
  SHA512:
6
- metadata.gz: c36c19dc3179417597d1c7386d45e92e9423e42fcfa1f5d6a9972cdb50c1ea6197f05b49f12bbe90a9bdeabdaa44c4ba8110393fe2dae4e0fd2b81b5c8bc83bb
7
- data.tar.gz: 486cafe53c34bd9003c4cbadb32057060cca63a64ee37f3e0aec8491e298c1314b086d39bfdaf869b4199dfcddff06ee78e3b8f712708b9e119aef136dde19f3
6
+ metadata.gz: b6cea4b4d3353a966032dd245e4b2055c5f56f65af528f437f11bd4baab1c60fa4208aec10d64b43db82606ad836d3d8d55bee64a6af8706aff7b9b2db862d5c
7
+ data.tar.gz: f0dd3715ca33b6639dd3bd2a120ab0686d1f7affd9cd58e01fe01a2a050cac304b442c35480022fdc066b1076ad89e1eb02c2778b191d7bc303f226f1a650793
data/README.md CHANGED
@@ -21,8 +21,29 @@ Or install it yourself as:
21
21
  $ gem install jieshun-parking
22
22
 
23
23
  ## Usage
24
-
25
- TODO: Write usage instructions here
24
+ ```ruby
25
+ def jieshun_client
26
+ redis_config = Rails.application.config_for(:redis).symbolize_keys!
27
+ jieshun_config = Jieshun::Parking::Configuration.new(
28
+ Rails.application.config.configuration['jieshun']['server_url'],
29
+ Rails.application.config.configuration['jieshun']['businesser_code'],
30
+ Rails.application.config.configuration['jieshun']['cid'],
31
+ Rails.application.config.configuration['jieshun']['usr'],
32
+ Rails.application.config.configuration['jieshun']['psw'],
33
+ Rails.application.config.configuration['jieshun']['sign_key'],
34
+ redis_config
35
+ )
36
+ Jieshun::Parking::Client.new(jieshun_config)
37
+ end
38
+
39
+
40
+ def create_order_by_plate_number(parking_lot, plate_number)
41
+ jieshun_client.create_order_by_car_no(parking_lot.code, plate_number) do |result|
42
+ parking_order = create_or_update_parking_order(parking_lot, plate_number, result)
43
+ yield(parking_order)
44
+ end
45
+ end
46
+ ```
26
47
 
27
48
  ## Development
28
49
 
@@ -4,7 +4,8 @@ module Jieshun
4
4
  class DataCenterClient
5
5
 
6
6
  REDIS_AUTH_KEY = 'jieshun.parking.datacenter.auth.token'
7
-
7
+ REDIS_AUTH_KEY_LOCK = 'jieshun.parking.datacenter.auth.token.lock'
8
+ MAX_RETRY_COUNT = 3 # 定义refresh_token 最大重试次数
8
9
  def initialize(configuration, http_client = StandardHttpClient.new)
9
10
  @configuration = configuration
10
11
  @http_client = http_client
@@ -15,17 +16,38 @@ module Jieshun
15
16
  @redis_client.redis_get(REDIS_AUTH_KEY)
16
17
  end
17
18
 
19
+
18
20
  def login_if_required
19
21
  auth_token = get_auth_token
20
22
  if Jieshun::Parking.always_login || auth_token.nil? || auth_token.empty?
21
- hash = { pno: @configuration.pno, secret: @configuration.secret }
22
- @http_client.post_json("#{@configuration.server_url}/api/login", {}, hash) do |result|
23
- auth_token = result.body['tn']
24
- expire_time = result.body['timeOut'] - 1000
25
- @redis_client.redis_set(REDIS_AUTH_KEY, auth_token, expire_time) # token will be expired after 2 hours
26
- yield(auth_token)
23
+ # 使用 Redis 锁来避免并发刷新
24
+ lock_key = REDIS_AUTH_KEY_LOCK
25
+ begin
26
+ # 设置一个5秒过期的锁
27
+ if @redis_client.redis_set_nx_lock(lock_key, 5)
28
+ # 重试机制
29
+ Retry.with_retries(max_tries: MAX_RETRY_COUNT, sleep_seconds: 1) do
30
+ refresh_token do |auth_token|
31
+ yield(auth_token)
32
+ end
33
+ end
34
+ end
35
+ rescue StandardError => e
36
+ Jieshun::Parking.logger.info("Failed to refresh token;error: #{e.message}")
37
+ ensure
38
+ # 确保锁最终被释放
39
+ @redis_client.redis_del(lock_key)
27
40
  end
28
- else
41
+ end
42
+ yield(auth_token)
43
+ end
44
+
45
+ def refresh_token
46
+ hash = { pno: @configuration.pno, secret: @configuration.secret }
47
+ @http_client.post_json("#{@configuration.server_url}/api/login", {}, hash) do |result|
48
+ auth_token = result.body['tn']
49
+ expire_time = result.body['timeOut'] - 1000
50
+ @redis_client.redis_set(REDIS_AUTH_KEY, auth_token, expire_time) # token will be expired after 2 hours
29
51
  yield(auth_token)
30
52
  end
31
53
  end
@@ -7,6 +7,9 @@ module Jieshun
7
7
  require 'uri'
8
8
  require 'json'
9
9
 
10
+ # 设置连接和读取的超时时间(以秒为单位)
11
+ TIMEOUT = 30 # 例如,30 秒超时
12
+
10
13
  def post_form(server_url, headers, hash)
11
14
  post(server_url, 'application/x-www-form-urlencoded', headers, hash) do |result|
12
15
  yield(result)
@@ -24,7 +27,7 @@ module Jieshun
24
27
  req = Net::HTTP::Post.new(uri)
25
28
  headers.each { |k, v| req[k] = v }
26
29
  req["Content-Type"] = content_type
27
- Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
30
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', read_timeout: TIMEOUT, open_timeout: TIMEOUT) do |http|
28
31
  if Jieshun::Parking.debug_mode && Jieshun::Parking.logger
29
32
  Jieshun::Parking.logger.debug("Jieshun Parking Request, url: #{server_url}, body: #{JSON.pretty_generate(hash)}")
30
33
  end
@@ -35,6 +35,10 @@ module Jieshun
35
35
  redis_client.ttl key
36
36
  end
37
37
 
38
+
39
+ def redis_set_nx_lock(lock_key, expire_time)
40
+ redis_client.set(lock_key, 1, nx: true, ex: expire_time)
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jieshun
4
4
  module Parking
5
- VERSION = "0.6.2"
5
+ VERSION = "0.6.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jieshun-parking
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - LCola
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-26 00:00:00.000000000 Z
11
+ date: 2024-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  - !ruby/object:Gem::Version
81
81
  version: '0'
82
82
  requirements: []
83
- rubygems_version: 3.0.9
83
+ rubygems_version: 3.2.3
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: 捷顺停车场 API