react-native-chaktl-temp
v1.0.2-e
Published
library to provide all APIs to communicate with the CHAKTL Smart Temperature Scanner by Thinkerbell Labs
Downloads
6
Readme
react-native-chaktl-temp
Getting started
- Use this library to make your application compatible with the Chakravyuh Smart Temperature Scanner
- Use the Chakravyuh Scanner with the provided Type A to C converter to use with you Android phone
- Enable OTG Storage in Settings > System (some older Android phone may not require this step)
Installation
iOS
- NOT COMPATIBLE
Android
npm install react-native-chaktl-temp
in the Application directory
OR
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.reactlibrary.RNReactNativeChaktlTemp_2Package;
to the imports at the top of the file - Add
new RNReactNativeChaktlTemp_2Package()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-chaktl-temp' project(':react-native-chaktl-temp').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-chaktl-temp/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-chaktl-temp')
API
// call to test library
@ReactMethod
public void toastTest() {
Toast.makeText(getReactApplicationContext(), "CHAKTL Toast Test", Toast.LENGTH_SHORT).show();
}
// call on mount to initialize the gun
@ReactMethod
public void addUsbReceivers(){
String result = this.tempGun.addUsbReceivers(getCurrentActivity(), getReactApplicationContext());
Toast.makeText(getReactApplicationContext(), "USB Receivers added: " + result , Toast.LENGTH_SHORT).show();
}
// Deprecated
@ReactMethod
public void initMcpConnection(){
String result = this.tempGun.setMcp2221(getCurrentActivity(), getReactApplicationContext());
Toast.makeText(getReactApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
// call to get last unread temperature (check getUnreadTempStatus) // -1 failure, -2: Scannee too close, -3 Scannne too far
@ReactMethod
public void getTempFromGun(Callback tempCallback){
double temp = this.tempGun.getBodyTempFromGun();
tempCallback.invoke(temp);
}
// DO NOT USE IN PRODUCTION :: THIS DOES NOT RESTRICT OUT OF RANGE TEMP COLLECTION
@RequiresApi(api = Build.VERSION_CODES.O)
@ReactMethod
public void getForceTempFromGun(Callback tempCallback){
double temp = this.tempGun.getTempFromGun();
tempCallback.invoke(temp);
}
// DO NOT USE IN PRODUCTION
@ReactMethod
public void getTrigger(Callback triggerCallback){
boolean trigger = this.tempGun.getTriggerStatus();
triggerCallback.invoke(trigger);
}
// call after reading temp to reset the flag
@ReactMethod
public void resetUnreadTempStatus(){
this.tempGun.setUnReadTempFromGunPresent(Boolean.FALSE);
}
// call to check if new temp has been recorded by the temp gun
@ReactMethod
public void getUnreadTempStatus(Callback getUnreadTempStatusCallback){
boolean unReadTempFromGunPresent = this.tempGun.getUnReadTempFromGunPresent();
getUnreadTempStatusCallback.invoke(unReadTempFromGunPresent);
}
// call to check if anyone is in temperature gun's range // 0: TOO CLOSE, 1: IN RANGE, 2: TOO FAR
@ReactMethod
public void getProximityStatusFromTempGun(Callback getProximityStatusFromTempGunCallback){
int proximityStatusFromTempGun = this.tempGun.getProximityStatusFromTempGun();
getProximityStatusFromTempGunCallback.invoke(proximityStatusFromTempGun);
}
// call to set whether the gun is in hands free mode
@ReactMethod
public void setHandsFreeMode(Boolean handsFreeMode){
this.tempGun.setHandsFreeModeActivated(handsFreeMode);
}
// call to get whether the gun is in hands free mode
@ReactMethod
public void getHandsFreeMode(Callback getHandsFreeModeCallback){
boolean handsFreeModeActivated = this.tempGun.getHandsFreeModeActivated();
getHandsFreeModeCallback.invoke(handsFreeModeActivated);
}
// call to check if Chakravyuh temperature Gun is successfully connected
@ReactMethod
public void getIsTempGunConnected(Callback getIsTempGunConnectedCallback){
boolean isTempGunConnected = this.tempGun.getTempGunConnected();
getIsTempGunConnectedCallback.invoke(isTempGunConnected);
}
// call to set whether the buzzer on the gun is activated or not
@ReactMethod
public void setBuzzerActivation(Boolean BuzzerActivation){
this.tempGun.setBuzzerActivated(BuzzerActivation);
}
// call to get whether the buzzer on the gun is activated or not
@ReactMethod
public void getBuzzerActivation(Callback getBuzzerActivationCallback){
boolean isBuzzerActivated = this.tempGun.getBuzzerActivated();
getBuzzerActivationCallback.invoke(isBuzzerActivated);
}
// call to set whether the gun returns temperature in Celcius or not
@ReactMethod
public void setTempUnitCelcius(Boolean isTempUnitCelcius){
this.tempGun.setTempUnitCelcius(isTempUnitCelcius);
}
// call to get whether the gun returns temperature in Celcius or not
@ReactMethod
public void getTempUnitCelcius(Callback getTempUnitCelciusCallback){
boolean isTempUnitCelcius = this.tempGun.getTempUnitCelcius();
getTempUnitCelciusCallback.invoke(isTempUnitCelcius);
}
Sample Usage
import {NativeModules} from 'react-native';
export default class App extends React.Component {
constructor(props) {
// initialize the package
this.testChakTl = NativeModules.RNReactNativeChaktlTemp_2;
}
componentDidMount() {
// attach USB handlers to detect the temperature gun and initialize the connection on detection
this.testChakTl.addUsbReceivers();
// checking if temp gun is connected
var timer;
function checkIfGunConnected(repeat, _this) {
if (repeat) {
_this.testChakTl.getIsTempGunConnected((t) => {
_this.setState({
gunConnected: t,
});
if (_this.state.gunConnected) {
repeat = false;
}
console.log('temp gun connected logged :' + t);
});
timer = setTimeout(() => checkIfGunConnected(repeat, _this), 1000);
} else {
clearTimeout(timer);
_this.checkTempContinuously();
console.log(' checkIfGunConnected stopped');
return;
}
}
checkIfGunConnected(true, this);
}
// checking if new temp is available post successful connection
checkTempContinuously = () => {
(function checkAndFetchNewtemp(repeat, _this) {
if (repeat) {
// checking if temp gun is connected
if (!_this.state.gunConnected) {
_this.testChakTl.getIsTempGunConnected((t) => {
_this.setState({
gunConnected: t,
});
if (_this.state.gunConnected) {
repeat = false;
}
console.log('temp gun connected logged :' + t);
});
} else {
// Checking if new temp is available to be read
_this.testChakTl.getUnreadTempStatus((t) => {
if (t) {
_this.testChakTl.getTempFromGun((t) => {
// if temp gun fails mark as disconnected
if (t == -1) {
repeat = false;
_this.setState({
gunConnected: false,
});
console.log('temp request failed :' + t);
}
_this.setState({
temp: t,
});
console.log('temp request logged :' + t);
});
// resetting unread Temp flag to avoid rereading same value
_this.testChakTl.resetUnreadTempStatus();
}
console.log('getUnreadTempStatus request logged :' + t);
});
// check if someone is in range
_this.testChakTl.getProximityStatusFromTempGun((t) => {
if (t != _this.state.range) {
_this.setState({
range: t,
});
console.log('range request logged :' + t);
}
});
}
if (repeat) {
_this.timer = setTimeout(
() => _this.checkTempContinuously(repeat, _this),
500,
);
} else {
clearTimeout(_this.timer);
_this.checkGunContinuously();
console.log(' checkIfGunConnected stopped');
return;
}
} else {
clearTimeout(_this.timer);
_this.checkGunContinuously();
console.log(' checkIfGunConnected stopped');
return;
}
})(true, this);
};
// enabling/disabling the buzzer sound on the temperature gun
onBuzzerToggle = () => {
if (this.state.buzzerActivated) {
this.setState({
buzzerActivated: false,
});
} else {
this.setState({
buzzerActivated: true,
});
}
console.log('buzzerActivated state logged :' + this.state.buzzerActivated);
this.testChakTl.setBuzzerActivation(this.state.buzzerActivated);
};
// enabling/disabling the hands free mode on the temperature gun
onHandsFreeToggle = () => {
if (this.state.handsFree) {
this.setState({
handsFree: false,
});
} else {
this.setState({
handsFree: true,
});
}
console.log('handsfree state logged :' + this.state.handsFree);
this.testChakTl.setHandsFreeMode(this.state.handsFree);
this.testChakTl.getHandsFreeMode((t) => {
console.log('handsfree request logged :' + t);
});
};
// changing the unit of temperature on the temperature gun
onCelciusToggle = () => {
if (this.state.isCelcius) {
this.setState({
isCelcius: false,
});
} else {
this.setState({
isCelcius: true,
});
}
console.log('isCelcius state logged :' + this.state.isCelcius);
this.testChakTl.setTempUnitCelcius(this.state.isCelcius);
this.testChakTl.getTempUnitCelcius((t) => {
console.log('isCelcius request logged :' + t);
});
};