Skip to content

Commit

Permalink
Handle OpenAPI configurations and add share command
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed Oct 9, 2023
1 parent a43158b commit 5838411
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 115 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMMONERS_AUTH_STRING = 'adsfsafasdfasdfasdfsafsadfdsafasdfsda'
PORT = 8080
44 changes: 27 additions & 17 deletions commoners.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// ------------- PRODUCTION -------------
import * as bluetoothPlugin from '@commoners/bluetooth'
import * as serialPlugin from '@commoners/serial'
// import { defineConfig } from 'commoners' // NOTE: COMMONERS dependencies are missing in local development...
// // ------------- PRODUCTION -------------
// import * as bluetoothPlugin from '@commoners/bluetooth'
// import * as serialPlugin from '@commoners/serial'
// import localServicesPlugin from '@commoners/local-services'
// // import { defineConfig } from 'commoners' // NOTE: COMMONERS dependencies are missing in local development...

// // ------------- DEVELOPMENT -------------
// import * as bluetoothPlugin from '../commoners/packages/plugins/devices/ble/index.js'
// import * as serialPlugin from '../commoners/packages/plugins/devices/serial/index.js'
// // import { defineConfig } from '../commoners/packages/core/index' // NOTE: COMMONERS dependencies are missing in local development...
// ------------- DEVELOPMENT -------------
import * as bluetoothPlugin from '../commoners/packages/plugins/devices/ble/index.js'
import * as serialPlugin from '../commoners/packages/plugins/devices/serial/index.js'
import localServicesPlugin from '../commoners/packages/plugins/local-services/index.js'
// import { defineConfig } from '../commoners/packages/core/index' // NOTE: COMMONERS dependencies are missing in local development...

// // ----------- Package.json Dependencies -----------
// // "@commoners/autoupdate": "file:../commoners/packages/plugins/autoupdate",
// // "@commoners/bluetooth": "file:../commoners/packages/plugins/devices/ble",
// // "@commoners/serial": "file:../commoners/packages/plugins/devices/serial",
// // "commoners": "file:../commoners"
// ----------- Package.json Dependencies -----------
// "@commoners/autoupdate": "file:../commoners/packages/plugins/autoupdate",
// "@commoners/bluetooth": "file:../commoners/packages/plugins/devices/ble",
// "@commoners/serial": "file:../commoners/packages/plugins/devices/serial",
// "@commoners/local-services": "file:../commoners/packages/plugins/local-services",
// "commoners": "file:../commoners"


const defineConfig = (o) => o
Expand All @@ -26,6 +29,11 @@ export default defineConfig({
// autoUpdatePlugin,
bluetoothPlugin,
serialPlugin,
localServicesPlugin(null, (ip, env) => {
const isLocalIP = process.env.LOCAL_IP === ip
const hasAuthString = process.env.COMMONERS_AUTH_STRING === env.COMMONERS_AUTH_STRING
return hasAuthString || isLocalIP
}),

{
name: 'selective-builds',
Expand Down Expand Up @@ -56,16 +64,18 @@ export default defineConfig({

services: {

// Example Node server (using pkg)
// Packaged with pkg
node: {
description: 'A simple Node.js server',
src: './src/services/node/index.js',
publish: 'https://node-production-aa81.up.railway.app/'
},

// Example Python server (using pyinstaller)
// Packaged with pyinstaller
python: {
description: 'A simple Python server',
src: './src/services/python/main.py',
port: 3768,
port: 1234,
publish: {
build: 'python -m PyInstaller --name solidarity --onedir --clean ./src/services/python/main.py --distpath ./dist/services/python',
remote: 'https://python-production-4f11.up.railway.app',
Expand All @@ -74,7 +84,7 @@ export default defineConfig({
},
remote: 'https://jsonplaceholder.typicode.com',
dynamic: {
src: 'http://localhost:3768', // Call the python server in development
src: 'http://localhost:1234', // Call the python server in development
publish: 'https://jsonplaceholder.typicode.com'
}
}
Expand Down
12 changes: 7 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
<div class="container">
<h1>Solidarity ✊</h1>
<main>
<ul id="commands">
<li>Connect to Serial Device <button id="testSerialConnection">Run</button></li>
<li>Connect to Bluetooth Device <button id="testBluetoothConnection">Run</button></li>
</ul>

<div id="console">
<ul id="messages"></ul>
</div>
<div id="sidebar">
<h3>Available Commands</h3>
<ul id="commands">
<li>Connect to Serial Device <button id="testSerialConnection">Run</button></li>
<li>Connect to Bluetooth Device <button id="testBluetoothConnection">Run</button></li>
</ul>
</div>
</main>
</div>
</body>
Expand Down
65 changes: 47 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build:frontend": "commoners build --frontend",
"build:services": "commoners build --services",
"build:node": "cd src/services/node && npm run build",
"share": "commoners share",
"launch": "commoners launch",
"launch:desktop": "commoners launch --desktop",
"launch:mobile": "commoners launch --mobile",
Expand All @@ -34,6 +35,7 @@
"@capacitor/ios": "^5.3.0",
"@commoners/bluetooth": "0.0.0",
"@commoners/serial": "0.0.0",
"commoners": "0.0.15"
"@commoners/local-services": "0.0.0",
"commoners": "0.0.16"
}
}
49 changes: 40 additions & 9 deletions src/frontend/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { BleClient } from '@capacitor-community/bluetooth-le';

const list = document.querySelector('ul')

async function createClient(url: string | URL) {
const list = document.querySelector('#commands') as HTMLUListElement

async function createClient(url: string | URL, id = '') {

url = new URL('.commoners', url)

const client = await new SwaggerClient(typeof url === 'string' ? url : url.href).catch((e: any) => {
throw new Error(`Failed to create client for ${url}: ${e.message}`)
})

const nestedList = document.createElement('ul')
nestedList.id = id
list.appendChild(nestedList)


const { title, description } = client.spec.info
const section = document.createElement('section')
section.innerHTML = `<h2>${title}</h2><small>${description}</small>`
list.appendChild(section)
section.innerHTML = `<h3>${title}</h3><small>${description}</small>`
nestedList.append(section)

// Populate list of available methods
Object.keys(client.spec.paths).forEach((path: any) => {
Expand All @@ -32,7 +40,7 @@ async function createClient(url: string | URL) {
}

li.append(container, button)
list.appendChild(li)
nestedList.appendChild(li)
})
})

Expand Down Expand Up @@ -104,7 +112,7 @@ if (COMMONERS.services.python) {
const pythonUrl = new URL(COMMONERS.services.python.url) // Equivalent to commoners://python

setTimeout(async () => {
const client = await createClient(new URL('.commoners', pythonUrl))
const client = await createClient(pythonUrl)
client.apis.version.getPythonVersion().then(res => {
onData({ source: 'Python', command: 'version', payload: res.body })
});
Expand All @@ -129,10 +137,33 @@ async function requestSerialPort () {
}


COMMONERS.ready.then(() => {
COMMONERS.ready.then(plugins => {

if ('local-services' in plugins) {

const localServices = plugins['local-services']
const ids: { [x:string]: string } = {}

localServices.onFound((url) => {
if (ids[url]) return
const id = ids[url] = Math.random().toString(36).substring(7)
createClient(url, id)
})

localServices.onClosed((url) => {
const el = document.getElementById(ids[url])
if (el) el.remove()
delete ids[url]
})

localServices.get()

}


const testSerialConnection = document.getElementById('testSerialConnection')
if (testSerialConnection) {
if ('serial' in COMMONERS.plugins.loaded) testSerialConnection.addEventListener('click', requestSerialPort)
if ('serial' in plugins) testSerialConnection.addEventListener('click', requestSerialPort)
else testSerialConnection.setAttribute('disabled', '')
}
// --------- Web Bluetooth Test ---------
Expand All @@ -150,7 +181,7 @@ COMMONERS.ready.then(() => {
const testBluetoothConnection = document.getElementById('testBluetoothConnection')

if (testBluetoothConnection) {
if ('bluetooth' in COMMONERS.plugins.loaded) testBluetoothConnection.addEventListener('click', requestBluetoothDevice)
if ('bluetooth' in plugins) testBluetoothConnection.addEventListener('click', requestBluetoothDevice)
else testBluetoothConnection.setAttribute('disabled', '')
}

Expand Down
7 changes: 6 additions & 1 deletion src/frontend/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ main {
justify-content: center;
gap: 50px;
flex-wrap: wrap;
flex-direction: row-reverse;
}

#sidebar {
flex-grow: 1
}

#console {
Expand Down Expand Up @@ -120,7 +125,7 @@ ul {
overflow-y: auto;
}

#commands h2 {
#commands h3 {
margin-bottom: 0;
}

Expand Down
Loading

0 comments on commit 5838411

Please sign in to comment.