forked from andrewrapp/xbee-api
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rolling-frame-id' into dev
- Loading branch information
Showing
10 changed files
with
85 additions
and
18 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
xbee-api/src/main/java/com/homeclimatecontrol/xbee/FrameIdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.homeclimatecontrol.xbee; | ||
|
||
import com.rapplogic.xbee.api.XBeeRequest; | ||
|
||
/** | ||
* A singleton to generate a packet frame ID. | ||
*/ | ||
public class FrameIdGenerator { | ||
|
||
private static final byte MAX_ID = (byte) 0xFF; | ||
|
||
private byte currentId = MAX_ID; | ||
|
||
private static final FrameIdGenerator instance = new FrameIdGenerator(); | ||
|
||
public static FrameIdGenerator getInstance() { | ||
return instance; | ||
} | ||
|
||
public synchronized void reset() { | ||
currentId = MAX_ID; | ||
} | ||
|
||
public synchronized byte getNext() { | ||
|
||
if (currentId == MAX_ID) { | ||
// Can't be 0 or we won't get the response | ||
// Also, skip the tainted DEFAULT_FRAME_ID to avoid the warning | ||
currentId = XBeeRequest.DEFAULT_FRAME_ID; | ||
} | ||
|
||
return ++currentId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
xbee-api/src/test/java/com/homeclimatecontrol/xbee/FrameIdGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.homeclimatecontrol.xbee; | ||
|
||
import com.rapplogic.xbee.api.XBeeRequest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class FrameIdGeneratorTest { | ||
|
||
/** | ||
* Make sure we never get zero, or {@link com.rapplogic.xbee.api.XBeeRequest#DEFAULT_FRAME_ID}. | ||
*/ | ||
@Test | ||
void rollOverTest() { | ||
|
||
for (int i = 0; i < 0xFF * 4; i++) { | ||
|
||
var id = FrameIdGenerator.getInstance().getNext(); | ||
|
||
assertThat(id).isNotZero(); | ||
assertThat(id).isNotEqualTo(XBeeRequest.DEFAULT_FRAME_ID); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters