@superrouteca/sindoor-react-native 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +118 -2
- package/SindoorReactNative.podspec +25 -0
- package/android/build.gradle +55 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/ca/superroute/sindoor/reactnative/SindoorModule.kt +261 -0
- package/android/src/main/java/ca/superroute/sindoor/reactnative/SindoorPackage.kt +26 -0
- package/ios/RNSindoor.mm +160 -0
- package/ios/RNSindoorBridge.swift +339 -0
- package/ios/sindoor_react_native.rb +48 -0
- package/package.json +85 -4
- package/src/NativeSindoor.ts +65 -0
- package/src/client.ts +91 -0
- package/src/errors.ts +29 -0
- package/src/geo.ts +29 -0
- package/src/hooks.ts +126 -0
- package/src/index.ts +15 -0
- package/src/navigator.ts +232 -0
- package/src/site.ts +291 -0
- package/src/types.ts +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Superroute SDK License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Superroute Inc. All rights reserved, except as granted below.
|
|
4
|
+
|
|
5
|
+
1. Permission is granted, free of charge, to any person obtaining a copy of this software and its documentation
|
|
6
|
+
(the "Software") to install, use, copy and distribute the Software, including as part of their own products and
|
|
7
|
+
services, to connect to Superroute services, on any other platform, and in their own commercial projects.
|
|
8
|
+
|
|
9
|
+
2. The Software may not be sold on its own, whether alone or as the main part of a product whose value lies in the
|
|
10
|
+
Software rather than in the product built with it.
|
|
11
|
+
|
|
12
|
+
3. The copyright notices, this license and the attribution of the Software to Superroute may not be removed or
|
|
13
|
+
changed, and must be kept in every copy or substantial portion of the Software that is distributed.
|
|
14
|
+
|
|
15
|
+
4. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
16
|
+
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
18
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
19
|
+
IN THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
5. Use of Superroute services through the Software is governed by the terms of those services.
|
package/README.md
CHANGED
|
@@ -1,3 +1,119 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @superrouteca/sindoor-react-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Version 0.1.0. Indoor routes, tours and positioning that run on the phone, the phone's motion sensors and BLE beacons
|
|
4
|
+
fed to the locator, and a ready navigation screen on an sMap map — for React Native (New Architecture, 0.81+) over the
|
|
5
|
+
sIndoor Android SDK (`ca.superroute:sindoor`) and iOS SDK (Swift package product `SIndoor`). The names are those of
|
|
6
|
+
`@superrouteca/sindoor` (web) and `@superrouteca/sindoor-react`; making a site is asynchronous here (the native engine
|
|
7
|
+
compiles it off the JavaScript thread), everything else is synchronous as on the web.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @superrouteca/sindoor-react-native @superrouteca/smap-react-native
|
|
11
|
+
cd ios && pod install
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { SMap, type SMapHandle } from '@superrouteca/smap-react-native';
|
|
18
|
+
import { useSindoorSite, useSindoorLocator, useSindoorNavigator } from '@superrouteca/sindoor-react-native';
|
|
19
|
+
|
|
20
|
+
function Indoor({ target }: { target: string }) {
|
|
21
|
+
const [map, setMap] = React.useState<SMapHandle | null>(null);
|
|
22
|
+
const { site, error } = useSindoorSite({ site: 'op-12-mall', key: 'smk_…' }); // downloaded once, then offline
|
|
23
|
+
// The phone's sensors and the site's beacons feed the locator (ask for the permissions first, see below).
|
|
24
|
+
const { estimate, observe } = useSindoorLocator(site, {}, { sensors: true, beacons: true });
|
|
25
|
+
const [steps, setSteps] = React.useState<{ text: string }[]>([]);
|
|
26
|
+
const nav = useSindoorNavigator(map, site, { position: estimate, onSteps: setSteps, text: { arrive: 'Ankunft' } });
|
|
27
|
+
React.useEffect(() => { if (nav && estimate) nav.navigateTo(target); }, [nav, target, estimate?.level]);
|
|
28
|
+
return (
|
|
29
|
+
<>
|
|
30
|
+
<SMap ref={setMap} apiKey="smk_…" style={{ flex: 1 }} />
|
|
31
|
+
{steps.map((s, i) => <Text key={i}>{s.text}</Text>)}
|
|
32
|
+
</>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Without hooks:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { SindoorSite, SindoorNavigator } from '@superrouteca/sindoor-react-native';
|
|
41
|
+
|
|
42
|
+
const site = await SindoorSite.load({ site: 'op-12-mall', key: 'smk_…' }); // or {token: 'si1.…'} for the full form
|
|
43
|
+
await AsyncStorage.setItem('site', JSON.stringify(site.download)); // later: SindoorSite.fromDownload(text)
|
|
44
|
+
const route = site.route('door', 'fixture:checkout', { role: 'customer' });
|
|
45
|
+
const nav = await SindoorNavigator.create(mapRef.current!, site, { onSteps: (lines) => setSteps(lines) });
|
|
46
|
+
const locator = site.locator();
|
|
47
|
+
locator.onEstimate((e) => nav.showPosition(e));
|
|
48
|
+
locator.onError((e) => console.warn(e.source, e.code)); // permission_missing, bluetooth_off, …
|
|
49
|
+
locator.observe({ t: Date.now(), kind: 'scan', ref: 'ABCD-EFGH' }); // a scanned position code
|
|
50
|
+
locator.startSensors();
|
|
51
|
+
locator.startBeacons(); // or startBeacons(['e2c56db5-…'])
|
|
52
|
+
nav.navigateTo('fixture:checkout');
|
|
53
|
+
// …
|
|
54
|
+
nav.destroy(); locator.dispose(); site.dispose();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- **SindoorClient** — the Open Platform's indoor routes in TypeScript (`package`, `layer`, `publicSites`,
|
|
58
|
+
`issueToken` — the last one server side only); `SindoorSite.load({site, client})` downloads through it instead of
|
|
59
|
+
the native SDK and keeps the download in JavaScript.
|
|
60
|
+
- **SindoorSite** — `load({site, key | token, form?, baseUrl?})`, `fromDownload(objectOrText)`, `fromPackage(pkg)`
|
|
61
|
+
(all asynchronous); `route(from, to, {role, profile, at})` with each end a ref, `{level, x, y}` in millimetres or
|
|
62
|
+
`{level, lng, lat}`; `tour`, `approach`, `levels`, `layer()` (the GeoJSON sMap's `addIndoor` draws), `anchor`,
|
|
63
|
+
`toLngLat` / `fromLngLat` (the engine's formulas), `download`, `id`, `version`, `locator(config)`, `dispose()`.
|
|
64
|
+
- **SindoorLocator** — `observe(obs | obs[])` → the estimate `{x, y, level, acc_mm, at, lng, lat}` or null;
|
|
65
|
+
`state()`, `setCalibration()`; `startSensors()` / `stopSensors()`, `startBeacons(uuids?)` / `stopBeacons()` (the
|
|
66
|
+
native SDKs feed the locator; each estimate reaches `onEstimate`, each problem `onError`); `dispose()`.
|
|
67
|
+
- **SindoorNavigator** — on an `@superrouteca/smap-react-native` map (1.6.2+, the indoor layer): the site as the
|
|
68
|
+
indoor layer with its level switcher, the blue dot and accuracy circle and the route lines tied to their level
|
|
69
|
+
(layer option `indoor`), the shown level following the walker; `navigateTo`, `showRoute`, `steps`, `clearRoute`,
|
|
70
|
+
`setLevel`, `level`, `destroy`; `onSteps(lines)` for a step list, `text` for translations (`DEFAULT_TEXT` keys).
|
|
71
|
+
- **Hooks** — `useSindoorSite`, `useSindoorLocator(site, config, {sensors, beacons})`,
|
|
72
|
+
`useSindoorNavigator(map, site, {…options, position})`.
|
|
73
|
+
- **Errors** — every failure is a `SindoorError` with a stable `code` (`invalid_site`, `bad_download`,
|
|
74
|
+
`unknown_point`, `unknown_level`, `site_not_found`, `http_<status>`, `no_position`, `disposed`, `unavailable`, …).
|
|
75
|
+
|
|
76
|
+
## Permissions
|
|
77
|
+
|
|
78
|
+
The SDK never asks for a permission: a missing one is answered by `startBeacons` and told to `onError`
|
|
79
|
+
(`permission_missing`, `permission_denied`). Ask in your app first (e.g. with `PermissionsAndroid` or
|
|
80
|
+
`react-native-permissions`).
|
|
81
|
+
|
|
82
|
+
| Platform | Declare | Needed for |
|
|
83
|
+
|---|---|---|
|
|
84
|
+
| iOS | `NSMotionUsageDescription` | Steps and pressure (`startSensors`). |
|
|
85
|
+
| iOS | `NSLocationWhenInUseUsageDescription` | iBeacon ranging (`startBeacons`) and a true-north heading; request When In Use first. |
|
|
86
|
+
| iOS | `NSBluetoothAlwaysUsageDescription` | Eddystone beacons (`startBeacons`). |
|
|
87
|
+
| Android | `BLUETOOTH_SCAN` (Android 12+, without `neverForLocation`) and `ACCESS_FINE_LOCATION` | `startBeacons`. |
|
|
88
|
+
| Android | `BLUETOOTH` / `BLUETOOTH_ADMIN` with `android:maxSdkVersion="30"` | `startBeacons` on Android 11 and older. |
|
|
89
|
+
| Android | (none) | `startSensors` (step detector, rotation vector, barometer). |
|
|
90
|
+
|
|
91
|
+
On Android every beacon heard is fed unless `startBeacons(uuids)` names iBeacon UUIDs (Eddystone beacons are always
|
|
92
|
+
kept); on iOS `startBeacons()` listens for the site's own beacons.
|
|
93
|
+
|
|
94
|
+
## Building from source (development)
|
|
95
|
+
|
|
96
|
+
The package depends on the native SDKs of this repository until they are published (`TODO(release)` markers in
|
|
97
|
+
`android/build.gradle`, `ios/sindoor_react_native.rb` and the podspec):
|
|
98
|
+
|
|
99
|
+
- **Android** — build the engine (`CARGO_TARGET_DIR=… scripts/build-sdk-android.sh --android` at the repository root,
|
|
100
|
+
which fills `sdk/android/sindoor/src/main/jniLibs`), then in the app's `android/settings.gradle`:
|
|
101
|
+
|
|
102
|
+
```groovy
|
|
103
|
+
include ':sindoor-android'
|
|
104
|
+
project(':sindoor-android').projectDir = new File('<sIndoor>/sdk/android/sindoor')
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
and in the app's root `build.gradle` `ext { sindoorHostLibrary = "" }` (the SDK's JVM tests read it). Without that
|
|
108
|
+
project the module takes `ca.superroute:sindoor:<sindoorAndroidVersion>` (default 0.1.0) from the sIndoor Maven
|
|
109
|
+
repository. With `@superrouteca/smap-react-native` from a local checkout, include `':smap-android'` as its README says.
|
|
110
|
+
- **iOS** — build the engine (`sdk/apple/scripts/build-sdk-apple.sh`); the podspec then takes the Swift package at
|
|
111
|
+
`SINDOOR_IOS_PACKAGE_PATH`, else `../apple` next to this package (its `SIndoorMap` product needs a local smap-sdk,
|
|
112
|
+
`SMAP_SDK_PATH`; this pod links only `SIndoor`), else the published `sindoor-releases` package of this version.
|
|
113
|
+
|
|
114
|
+
Checks: `npm test` (jest, the TypeScript parts against a stand-in native module and map), `npm run typecheck`,
|
|
115
|
+
`npm run check:android` (the Kotlin module and AARs against React Native and the local Android SDK),
|
|
116
|
+
`npm run check:ios` (the Swift bridge for the iOS simulator and devices, the Objective-C++ module against the
|
|
117
|
+
codegen's protocol).
|
|
118
|
+
|
|
119
|
+
Licence: Superroute SDK License (see LICENSE).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require_relative "ios/sindoor_react_native"
|
|
3
|
+
|
|
4
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
5
|
+
|
|
6
|
+
Pod::Spec.new do |s|
|
|
7
|
+
s.name = "SindoorReactNative"
|
|
8
|
+
s.version = package["version"]
|
|
9
|
+
s.summary = package["description"]
|
|
10
|
+
s.homepage = package["homepage"]
|
|
11
|
+
s.license = { :type => 'Superroute SDK License', :file => 'LICENSE' }
|
|
12
|
+
s.authors = package["author"]
|
|
13
|
+
s.platforms = { :ios => "15.1" }
|
|
14
|
+
# TODO(release): the source the published pod is built from.
|
|
15
|
+
s.source = { :git => "https://github.com/Goodneo-Solutions/sindoor-releases.git", :tag => s.version.to_s }
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
|
17
|
+
s.swift_version = "5.9"
|
|
18
|
+
s.pod_target_xcconfig = { "DEFINES_MODULE" => "YES" }
|
|
19
|
+
s.frameworks = "CoreLocation", "CoreMotion", "CoreBluetooth"
|
|
20
|
+
|
|
21
|
+
install_modules_dependencies(s)
|
|
22
|
+
|
|
23
|
+
# The sIndoor iOS SDK (Swift package); see ios/sindoor_react_native.rb for which copy.
|
|
24
|
+
SindoorReactNative.add_to_pod(s)
|
|
25
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The React Native module over the sIndoor Android SDK (ca.superroute:sindoor: the engine over JNI, phone sensors,
|
|
2
|
+
// BLE beacons).
|
|
3
|
+
//
|
|
4
|
+
// DEVELOPMENT: the app includes the SDK's source as the project ':sindoor-android' (see README, "Building from
|
|
5
|
+
// source"; the engine's libsindoor_ffi.so comes from sIndoor's scripts/build-sdk-android.sh --android), and this
|
|
6
|
+
// module depends on it. RELEASE: without that project the SDK comes from the sIndoor Maven repository
|
|
7
|
+
// (ca.superroute:sindoor:<sindoorAndroidVersion>, default this package's version).
|
|
8
|
+
// TODO(release): confirm the Maven URL once sindoor-releases publishes ca.superroute:sindoor 0.1.0.
|
|
9
|
+
def safeExtGet(prop, fallback) {
|
|
10
|
+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
apply plugin: "com.android.library"
|
|
14
|
+
apply plugin: "org.jetbrains.kotlin.android"
|
|
15
|
+
apply plugin: "com.facebook.react"
|
|
16
|
+
|
|
17
|
+
android {
|
|
18
|
+
namespace "ca.superroute.sindoor.reactnative"
|
|
19
|
+
compileSdk safeExtGet("compileSdkVersion", 36)
|
|
20
|
+
|
|
21
|
+
defaultConfig {
|
|
22
|
+
minSdk safeExtGet("minSdkVersion", 24)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
compileOptions {
|
|
26
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
27
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
kotlin {
|
|
32
|
+
jvmToolchain(17)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The app resolves the SDK (an api dependency) with its own repositories, so the sIndoor Maven repository is added
|
|
36
|
+
// to every project of the build, not only to this one.
|
|
37
|
+
rootProject.allprojects {
|
|
38
|
+
repositories {
|
|
39
|
+
google()
|
|
40
|
+
mavenCentral()
|
|
41
|
+
// TODO(release): the public Maven on Pages of sindoor-releases (IK4).
|
|
42
|
+
maven { url "https://goodneo-solutions.github.io/sindoor-releases/maven" }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
dependencies {
|
|
47
|
+
implementation "com.facebook.react:react-android"
|
|
48
|
+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2"
|
|
49
|
+
def localSdk = rootProject.findProject(":sindoor-android")
|
|
50
|
+
if (localSdk != null) {
|
|
51
|
+
api localSdk
|
|
52
|
+
} else {
|
|
53
|
+
api "ca.superroute:sindoor:${safeExtGet("sindoorAndroidVersion", "0.1.0")}"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
package ca.superroute.sindoor.reactnative
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import ca.superroute.sindoor.Sindoor
|
|
6
|
+
import ca.superroute.sindoor.SindoorBeacons
|
|
7
|
+
import ca.superroute.sindoor.SindoorClient
|
|
8
|
+
import ca.superroute.sindoor.SindoorException
|
|
9
|
+
import ca.superroute.sindoor.SindoorLocator
|
|
10
|
+
import ca.superroute.sindoor.SindoorPoint
|
|
11
|
+
import ca.superroute.sindoor.SindoorSensors
|
|
12
|
+
import ca.superroute.sindoor.SindoorSite
|
|
13
|
+
import com.facebook.react.bridge.Arguments
|
|
14
|
+
import com.facebook.react.bridge.Promise
|
|
15
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
16
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
17
|
+
import kotlinx.coroutines.CoroutineScope
|
|
18
|
+
import kotlinx.coroutines.Dispatchers
|
|
19
|
+
import kotlinx.coroutines.SupervisorJob
|
|
20
|
+
import kotlinx.coroutines.cancel
|
|
21
|
+
import kotlinx.coroutines.launch
|
|
22
|
+
import org.json.JSONArray
|
|
23
|
+
import org.json.JSONObject
|
|
24
|
+
import org.json.JSONTokener
|
|
25
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
26
|
+
import java.util.concurrent.atomic.AtomicLong
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The `Sindoor` TurboModule over the sIndoor Android SDK: sites and locators as handles, JSON in and out, every error
|
|
30
|
+
* answered as `{"error": {code, message, refs}}` (never thrown), the phone's sensors and beacons feeding a locator
|
|
31
|
+
* with `sindoorEstimate` / `sindoorError` events. Nothing here asks for a permission.
|
|
32
|
+
*/
|
|
33
|
+
@ReactModule(name = NativeSindoorSpec.NAME)
|
|
34
|
+
class SindoorModule(context: ReactApplicationContext) : NativeSindoorSpec(context) {
|
|
35
|
+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
36
|
+
private val main = Handler(Looper.getMainLooper())
|
|
37
|
+
private val ids = AtomicLong(0)
|
|
38
|
+
private val sites = ConcurrentHashMap<Long, SindoorSite>()
|
|
39
|
+
private val locators = ConcurrentHashMap<Long, SindoorLocator>()
|
|
40
|
+
private val sensors = ConcurrentHashMap<Long, SindoorSensors>()
|
|
41
|
+
private val beacons = ConcurrentHashMap<Long, SindoorBeacons>()
|
|
42
|
+
|
|
43
|
+
private fun error(code: String, message: String?, refs: List<String> = emptyList()): String =
|
|
44
|
+
JSONObject().put("error", JSONObject().put("code", code).put("message", message ?: code).put("refs", JSONArray(refs))).toString()
|
|
45
|
+
|
|
46
|
+
/** Runs an engine call; any failure becomes the error envelope. */
|
|
47
|
+
private inline fun answer(block: () -> String): String =
|
|
48
|
+
try {
|
|
49
|
+
block()
|
|
50
|
+
} catch (e: SindoorException) {
|
|
51
|
+
error(e.code, e.message, e.refs)
|
|
52
|
+
} catch (e: IllegalStateException) {
|
|
53
|
+
error("disposed", e.message)
|
|
54
|
+
} catch (e: org.json.JSONException) {
|
|
55
|
+
error("bad_request", e.message)
|
|
56
|
+
} catch (e: Exception) {
|
|
57
|
+
error("error", e.message ?: e.toString())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private fun site(h: Double): SindoorSite = sites[h.toLong()] ?: throw SindoorException("disposed", "no site ${h.toLong()} (disposed?)")
|
|
61
|
+
private fun locator(h: Double): SindoorLocator = locators[h.toLong()] ?: throw SindoorException("disposed", "no locator ${h.toLong()} (disposed?)")
|
|
62
|
+
|
|
63
|
+
private fun keep(s: SindoorSite): String {
|
|
64
|
+
val h = ids.incrementAndGet()
|
|
65
|
+
sites[h] = s
|
|
66
|
+
return JSONObject().put("handle", h).toString()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private fun str(o: JSONObject, k: String): String? = if (o.isNull(k)) null else o.optString(k).ifEmpty { null }
|
|
70
|
+
|
|
71
|
+
/** A route end as the TypeScript side sends it: a ref, or `{level?, x, y}` in millimetres. */
|
|
72
|
+
private fun point(v: Any?): SindoorPoint = when (v) {
|
|
73
|
+
is String -> SindoorPoint.Ref(v)
|
|
74
|
+
is JSONObject -> SindoorPoint.Xy(str(v, "level"), v.getDouble("x"), v.getDouble("y"))
|
|
75
|
+
else -> throw SindoorException("bad_request", "a route end is a ref or {level, x, y}")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
override fun version(): String = answer {
|
|
79
|
+
JSONObject().put("engine", Sindoor.version()).put("sdk", Sindoor.SDK_VERSION).toString()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
override fun loadSite(options: String, promise: Promise) {
|
|
83
|
+
scope.launch {
|
|
84
|
+
promise.resolve(answer {
|
|
85
|
+
val o = JSONObject(options)
|
|
86
|
+
val id = str(o, "site") ?: throw SindoorException("no_site", "pass the site id")
|
|
87
|
+
val client = SindoorClient(key = str(o, "key"), token = str(o, "token"), baseUrl = str(o, "baseUrl") ?: SindoorClient.DEFAULT_BASE_URL)
|
|
88
|
+
keep(SindoorSite.load(id, client, str(o, "form") ?: "customer"))
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
override fun siteFromDownload(download: String, promise: Promise) {
|
|
94
|
+
scope.launch { promise.resolve(answer { keep(SindoorSite.fromDownload(download)) }) }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
override fun siteDownload(site: Double): String = answer { site(site).download.toString() }
|
|
98
|
+
|
|
99
|
+
override fun siteFree(site: Double) {
|
|
100
|
+
sites.remove(site.toLong())?.close()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
override fun siteRoute(site: Double, request: String): String = answer {
|
|
104
|
+
val r = JSONObject(request)
|
|
105
|
+
val at = if (r.has("at") && !r.isNull("at")) r.getLong("at") else null
|
|
106
|
+
site(site).route(point(r.opt("from")), point(r.opt("to")), str(r, "role"), str(r, "profile"), at).json.toString()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
override fun siteTour(site: Double, request: String): String = answer { site(site).tour(JSONObject(request)).toString() }
|
|
110
|
+
|
|
111
|
+
override fun siteApproach(site: Double, request: String): String = answer { site(site).approach(JSONObject(request)).toString() }
|
|
112
|
+
|
|
113
|
+
override fun siteLevels(site: Double): String = answer { site(site).levels().toString() }
|
|
114
|
+
|
|
115
|
+
override fun siteLayer(site: Double): String = answer { site(site).layerJson() }
|
|
116
|
+
|
|
117
|
+
override fun toLngLat(site: Double, level: String, x: Double, y: Double): String = answer {
|
|
118
|
+
val p = site(site).toLngLat(level, x, y)
|
|
119
|
+
JSONArray().put(p.lng).put(p.lat).toString()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
override fun fromLngLat(site: Double, level: String, lng: Double, lat: Double): String = answer {
|
|
123
|
+
val p = site(site).fromLngLat(level, lng, lat)
|
|
124
|
+
JSONObject().put("level", p.level).put("x", p.x).put("y", p.y).toString()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
override fun locatorNew(site: Double, config: String): String = answer {
|
|
128
|
+
val l = site(site).locator(if (config.isBlank()) JSONObject() else JSONObject(config))
|
|
129
|
+
val h = ids.incrementAndGet()
|
|
130
|
+
locators[h] = l
|
|
131
|
+
JSONObject().put("handle", h).toString()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
override fun locatorObserve(locator: Double, observations: String): String = answer {
|
|
135
|
+
locator(locator).observeJson(observations)?.json?.toString() ?: "null"
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
override fun locatorState(locator: Double): String = answer { locator(locator).state().toString() }
|
|
139
|
+
|
|
140
|
+
override fun locatorSetCalibration(locator: Double, calibration: String): String = answer {
|
|
141
|
+
val v = JSONTokener(calibration).nextValue()
|
|
142
|
+
locator(locator).setCalibration(v as? JSONObject).toString()
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
override fun locatorFree(locator: Double) {
|
|
146
|
+
val h = locator.toLong()
|
|
147
|
+
stopFeeds(h)
|
|
148
|
+
locators.remove(h)?.close()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The phone's sensors and beacons. Estimates and problems go out as events.
|
|
152
|
+
|
|
153
|
+
private fun emitEstimate(h: Long, e: JSONObject) {
|
|
154
|
+
emitSindoorEstimate(Arguments.createMap().apply {
|
|
155
|
+
putDouble("locator", h.toDouble())
|
|
156
|
+
putString("estimate", e.toString())
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private fun emitError(h: Long, source: String, code: String, message: String) {
|
|
161
|
+
emitSindoorError(Arguments.createMap().apply {
|
|
162
|
+
putDouble("locator", h.toDouble())
|
|
163
|
+
putString("source", source)
|
|
164
|
+
putString("code", code)
|
|
165
|
+
putString("message", message)
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Observes on behalf of a sensor source; an engine error is told, not thrown. */
|
|
170
|
+
private fun feed(h: Long, source: String, o: JSONObject) {
|
|
171
|
+
val l = locators[h] ?: return
|
|
172
|
+
try {
|
|
173
|
+
l.observe(o)?.let { emitEstimate(h, it.json) }
|
|
174
|
+
} catch (e: SindoorException) {
|
|
175
|
+
emitError(h, source, e.code, e.message ?: e.code)
|
|
176
|
+
} catch (e: IllegalStateException) {
|
|
177
|
+
// Freed meanwhile.
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
override fun startSensors(locator: Double): String = answer {
|
|
182
|
+
locator(locator)
|
|
183
|
+
val h = locator.toLong()
|
|
184
|
+
val s = sensors.getOrPut(h) { SindoorSensors(reactApplicationContext) { o -> feed(h, "sensors", o) } }
|
|
185
|
+
// Thread-safe: the SDK registers its listeners on the main looper whatever thread starts it.
|
|
186
|
+
val started = s.start()
|
|
187
|
+
if (!started) emitError(h, "sensors", "sensors_unavailable", "this phone has none of the motion sensors used (steps, rotation, pressure)")
|
|
188
|
+
JSONObject().put("status", if (started) "started" else "unavailable").toString()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
override fun stopSensors(locator: Double) {
|
|
192
|
+
sensors.remove(locator.toLong())?.let { s -> main.post { s.stop() } }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
override fun startBeacons(locator: Double, uuids: String): String = answer {
|
|
196
|
+
locator(locator)
|
|
197
|
+
val h = locator.toLong()
|
|
198
|
+
val wanted = JSONArray(uuids.ifBlank { "[]" }).let { a -> (0 until a.length()).map { a.getString(it).lowercase() } }.toSet()
|
|
199
|
+
beacons.remove(h)?.stop()
|
|
200
|
+
// Android's scan has no UUID filter of its own here: iBeacons of other UUIDs are left out of each observation.
|
|
201
|
+
val b = SindoorBeacons(reactApplicationContext) { o ->
|
|
202
|
+
val kept = if (wanted.isEmpty()) o else filterBeacons(o, wanted)
|
|
203
|
+
if (kept != null) feed(h, "beacons", kept)
|
|
204
|
+
}
|
|
205
|
+
// The scan and its one-second tick run on the main looper whatever thread starts it.
|
|
206
|
+
val status = b.start()
|
|
207
|
+
val out = JSONObject()
|
|
208
|
+
when (status) {
|
|
209
|
+
is SindoorBeacons.Status.Started -> {
|
|
210
|
+
beacons[h] = b
|
|
211
|
+
out.put("status", "started")
|
|
212
|
+
}
|
|
213
|
+
is SindoorBeacons.Status.MissingPermissions -> {
|
|
214
|
+
out.put("status", "permission_missing").put("permissions", JSONArray(status.permissions))
|
|
215
|
+
emitError(h, "beacons", "permission_missing", "not granted: ${status.permissions.joinToString()}")
|
|
216
|
+
}
|
|
217
|
+
is SindoorBeacons.Status.BluetoothOff -> {
|
|
218
|
+
out.put("status", "bluetooth_off")
|
|
219
|
+
emitError(h, "beacons", "bluetooth_off", "Bluetooth is off")
|
|
220
|
+
}
|
|
221
|
+
is SindoorBeacons.Status.NotSupported -> {
|
|
222
|
+
out.put("status", "not_supported")
|
|
223
|
+
emitError(h, "beacons", "bluetooth_unsupported", "this phone has no Bluetooth LE")
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
out.toString()
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** A `ble` observation with only the iBeacons of [uuids] (and every Eddystone); null when none is left. */
|
|
230
|
+
private fun filterBeacons(o: JSONObject, uuids: Set<String>): JSONObject? {
|
|
231
|
+
val list = o.optJSONArray("beacons") ?: return o
|
|
232
|
+
val kept = JSONArray()
|
|
233
|
+
for (i in 0 until list.length()) {
|
|
234
|
+
val b = list.optJSONObject(i) ?: continue
|
|
235
|
+
val id = b.optString("id").lowercase()
|
|
236
|
+
val ok = !id.startsWith("ib:") || uuids.any { id.startsWith("ib:$it:") }
|
|
237
|
+
if (ok) kept.put(b)
|
|
238
|
+
}
|
|
239
|
+
if (kept.length() == 0) return null
|
|
240
|
+
return JSONObject(o.toString()).put("beacons", kept)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
override fun stopBeacons(locator: Double) {
|
|
244
|
+
beacons.remove(locator.toLong())?.let { b -> main.post { b.stop() } }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private fun stopFeeds(h: Long) {
|
|
248
|
+
sensors.remove(h)?.let { s -> main.post { s.stop() } }
|
|
249
|
+
beacons.remove(h)?.let { b -> main.post { b.stop() } }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
override fun invalidate() {
|
|
253
|
+
for (h in locators.keys.toList()) stopFeeds(h)
|
|
254
|
+
locators.values.forEach { it.close() }
|
|
255
|
+
locators.clear()
|
|
256
|
+
sites.values.forEach { it.close() }
|
|
257
|
+
sites.clear()
|
|
258
|
+
scope.cancel()
|
|
259
|
+
super.invalidate()
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
package ca.superroute.sindoor.reactnative
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
/** Registers the `Sindoor` TurboModule with React Native (found by autolinking). */
|
|
10
|
+
class SindoorPackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
|
|
12
|
+
if (name == NativeSindoorSpec.NAME) SindoorModule(reactContext) else null
|
|
13
|
+
|
|
14
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
|
|
15
|
+
mapOf(
|
|
16
|
+
NativeSindoorSpec.NAME to ReactModuleInfo(
|
|
17
|
+
name = NativeSindoorSpec.NAME,
|
|
18
|
+
className = NativeSindoorSpec.NAME,
|
|
19
|
+
canOverrideExistingModule = false,
|
|
20
|
+
needsEagerInit = false,
|
|
21
|
+
isCxxModule = false,
|
|
22
|
+
isTurboModule = true,
|
|
23
|
+
),
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
}
|
package/ios/RNSindoor.mm
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// The `Sindoor` TurboModule on iOS: forwards to RNSindoorBridge (Swift), which uses the sIndoor iOS SDK (`SIndoor`).
|
|
2
|
+
// Found by name through codegenConfig.ios.modulesProvider.
|
|
3
|
+
#import <Foundation/Foundation.h>
|
|
4
|
+
#import <SindoorReactNativeSpec/SindoorReactNativeSpec.h>
|
|
5
|
+
|
|
6
|
+
#if __has_include(<SindoorReactNative/SindoorReactNative-Swift.h>)
|
|
7
|
+
#import <SindoorReactNative/SindoorReactNative-Swift.h>
|
|
8
|
+
#else
|
|
9
|
+
#import "SindoorReactNative-Swift.h"
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
@interface RNSindoor : NativeSindoorSpecBase <NativeSindoorSpec>
|
|
13
|
+
@end
|
|
14
|
+
|
|
15
|
+
@implementation RNSindoor {
|
|
16
|
+
RNSindoorBridge *_bridge;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
RCT_EXPORT_MODULE(Sindoor)
|
|
20
|
+
|
|
21
|
+
- (instancetype)init
|
|
22
|
+
{
|
|
23
|
+
if (self = [super init]) {
|
|
24
|
+
__weak RNSindoor *weakSelf = self;
|
|
25
|
+
_bridge = [[RNSindoorBridge alloc]
|
|
26
|
+
initWithEmitEstimate:^(double locator, NSString *_Nonnull estimate) {
|
|
27
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
28
|
+
[weakSelf emitSindoorEstimate:@{@"locator" : @(locator), @"estimate" : estimate}];
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
emitError:^(double locator, NSString *_Nonnull source, NSString *_Nonnull code, NSString *_Nonnull message) {
|
|
32
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
33
|
+
[weakSelf emitSindoorError:@{@"locator" : @(locator), @"source" : source, @"code" : code, @"message" : message}];
|
|
34
|
+
});
|
|
35
|
+
}];
|
|
36
|
+
}
|
|
37
|
+
return self;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
+ (BOOL)requiresMainQueueSetup
|
|
41
|
+
{
|
|
42
|
+
return NO;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
- (void)invalidate
|
|
46
|
+
{
|
|
47
|
+
[_bridge invalidate];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
- (NSString *)version
|
|
51
|
+
{
|
|
52
|
+
return [_bridge version];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
- (void)loadSite:(NSString *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
56
|
+
{
|
|
57
|
+
[_bridge loadSite:options done:^(NSString *_Nonnull result) { resolve(result); }];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
- (void)siteFromDownload:(NSString *)download resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
61
|
+
{
|
|
62
|
+
[_bridge siteFromDownload:download done:^(NSString *_Nonnull result) { resolve(result); }];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
- (NSString *)siteDownload:(double)site
|
|
66
|
+
{
|
|
67
|
+
return [_bridge siteDownload:site];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
- (void)siteFree:(double)site
|
|
71
|
+
{
|
|
72
|
+
[_bridge siteFree:site];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
- (NSString *)siteRoute:(double)site request:(NSString *)request
|
|
76
|
+
{
|
|
77
|
+
return [_bridge siteRoute:site request:request];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
- (NSString *)siteTour:(double)site request:(NSString *)request
|
|
81
|
+
{
|
|
82
|
+
return [_bridge siteTour:site request:request];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
- (NSString *)siteApproach:(double)site request:(NSString *)request
|
|
86
|
+
{
|
|
87
|
+
return [_bridge siteApproach:site request:request];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
- (NSString *)siteLevels:(double)site
|
|
91
|
+
{
|
|
92
|
+
return [_bridge siteLevels:site];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
- (NSString *)siteLayer:(double)site
|
|
96
|
+
{
|
|
97
|
+
return [_bridge siteLayer:site];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
- (NSString *)toLngLat:(double)site level:(NSString *)level x:(double)x y:(double)y
|
|
101
|
+
{
|
|
102
|
+
return [_bridge toLngLat:site level:level x:x y:y];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
- (NSString *)fromLngLat:(double)site level:(NSString *)level lng:(double)lng lat:(double)lat
|
|
106
|
+
{
|
|
107
|
+
return [_bridge fromLngLat:site level:level lng:lng lat:lat];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
- (NSString *)locatorNew:(double)site config:(NSString *)config
|
|
111
|
+
{
|
|
112
|
+
return [_bridge locatorNew:site config:config];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
- (NSString *)locatorObserve:(double)locator observations:(NSString *)observations
|
|
116
|
+
{
|
|
117
|
+
return [_bridge locatorObserve:locator observations:observations];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
- (NSString *)locatorState:(double)locator
|
|
121
|
+
{
|
|
122
|
+
return [_bridge locatorState:locator];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
- (NSString *)locatorSetCalibration:(double)locator calibration:(NSString *)calibration
|
|
126
|
+
{
|
|
127
|
+
return [_bridge locatorSetCalibration:locator calibration:calibration];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
- (void)locatorFree:(double)locator
|
|
131
|
+
{
|
|
132
|
+
[_bridge locatorFree:locator];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
- (NSString *)startSensors:(double)locator
|
|
136
|
+
{
|
|
137
|
+
return [_bridge startSensors:locator];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
- (void)stopSensors:(double)locator
|
|
141
|
+
{
|
|
142
|
+
[_bridge stopSensors:locator];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
- (NSString *)startBeacons:(double)locator uuids:(NSString *)uuids
|
|
146
|
+
{
|
|
147
|
+
return [_bridge startBeacons:locator uuids:uuids];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
- (void)stopBeacons:(double)locator
|
|
151
|
+
{
|
|
152
|
+
[_bridge stopBeacons:locator];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
156
|
+
{
|
|
157
|
+
return std::make_shared<facebook::react::NativeSindoorSpecJSI>(params);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@end
|