Skip to content

Commit

Permalink
v0.6.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsRiprod committed Jul 29, 2024
1 parent a034f0f commit 9a1174a
Show file tree
Hide file tree
Showing 34 changed files with 603 additions and 283 deletions.
4 changes: 2 additions & 2 deletions DeskThing/src/helpers/WebSocketService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//const BASE_URL = 'ws://192.168.7.1:8891';
const BASE_URL = 'ws://localhost:8891';
const BASE_URL = 'ws://192.168.7.1:8891';
//const BASE_URL = 'ws://localhost:8891';

type SocketEventListener = (msg: socketData) => void;

Expand Down
2 changes: 1 addition & 1 deletion DeskThing/src/views/local/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const Local: React.FC = () => {
<p className="absolute bottom-24 font-semibold max-w-96 overflow-hidden">{`Listening On: ${songData?.device || 'Device name'}`}</p>
<div className={`info_container ${transitioning ? 'transition-all ease-in-out duration-700' : ''} `} style={{transform: `translateX(${offset}px)`, opacity: `${opacity}`}}>
<p className="m-3">{songData?.album || 'Album'}</p>
<h1 className="m-3">{songData?.track_name || 'Song Title'}</h1>
<h1 className="m-3 font-bold text-4xl">{songData?.track_name || 'Song Title'}</h1>
<h3 className="m-3">{songData?.artist || 'Artist'}</h3>
</div>

Expand Down
2 changes: 1 addition & 1 deletion DeskThing/src/views/utility/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const Utility: FC = (): JSX.Element => {
</div>
)) : (
<div className="flex justify-center items-center h-full">
<IconDevice iconSize={256} text={`v${version}-ADB`} fontSize={110} />
<IconDevice iconSize={256} text={`v${version}-RNDIS`} fontSize={110} />
</div>
)}
</div>
Expand Down
27 changes: 22 additions & 5 deletions DeskThingServer/src/main/handlers/adbHandler.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import path from 'path'
import { exec } from 'child_process'
import { execFile } from 'child_process'
import getPlatform from '../utils/get-platform'
import dataListener, { MESSAGE_TYPES } from '../utils/events'

const isProduction = process.env.NODE_ENV === 'development'
const execPath = isProduction
const isDevelopment = process.env.NODE_ENV === 'development'
const execPath = isDevelopment
? path.join(__dirname, '..', '..', 'adb_source', getPlatform())
: path.join(process.resourcesPath, getPlatform())

const adbExecutableName = process.platform === 'win32' ? 'adb.exe' : 'adb'
const adbPath = path.join(execPath, adbExecutableName)

export const handleAdbCommands = (command: string): Promise<string> => {
export const handleAdbCommands = (command: string, event?): Promise<string> => {
return new Promise((resolve, reject) => {
exec(`cd '${execPath}' && ${adbExecutableName} ${command}`, (error, stdout, stderr) => {
execFile(adbPath, command.split(' '), { cwd: execPath }, (error, stdout, stderr) => {
if (error) {
if (event) {
event.sender.send('logging', {
status: false,
data: 'Error Encountered!',
final: true,
error: stderr
})
}
dataListener.emit(MESSAGE_TYPES.ERROR, `ADB Error: ${stderr}, ${command}, ${adbPath}`)
reject(`ADB Error: ${stderr}, ${command}, ${adbPath}`)
} else {
if (event) {
event.sender.send('logging', {
status: true,
data: 'ADB Success!',
final: true
})
}
resolve(stdout)
}
})
Expand Down
40 changes: 25 additions & 15 deletions DeskThingServer/src/main/handlers/appHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ interface returnData {
platforms: string[]
requirements: string[]
}
async function handleZip(zipFilePath: string): Promise<returnData> {
async function handleZip(zipFilePath: string, event?): Promise<returnData> {
try {
const extractDir = join(app.getPath('userData'), 'apps') // Extract to user data folder
// Create the extraction directory if it doesn't exist
Expand All @@ -475,13 +475,18 @@ async function handleZip(zipFilePath: string): Promise<returnData> {
fs.mkdirSync(tempDir, { recursive: true })
}

event && event.reply('logging', { status: true, data: 'Extracting App', final: false })

// Extract the .zip file to a temporary location to get the manifest.json
await new Promise<void>((resolve, reject) => {
const extractStream = fs
.createReadStream(zipFilePath)
.pipe(unzipper.Extract({ path: tempDir }))

extractStream.on('error', reject)
extractStream.on('error', () => {
event && event.reply('logging', { status: false, data: 'Extraction failed!', final: true })
reject()
})
extractStream.on('close', () => {
console.log('Extraction finished')
resolve()
Expand All @@ -492,6 +497,7 @@ async function handleZip(zipFilePath: string): Promise<returnData> {
console.log(file)
})

event && event.reply('logging', { status: true, data: 'Getting Manifest', final: false })
let returnData = await getManifest(tempDir)

if (!returnData) {
Expand All @@ -509,6 +515,8 @@ async function handleZip(zipFilePath: string): Promise<returnData> {
}
}

event &&
event.reply('logging', { status: true, data: 'Disabling and purging app', final: false })
await disableApp(returnData.appId)

await purgeAppData(returnData.appId)
Expand All @@ -531,30 +539,33 @@ async function handleZip(zipFilePath: string): Promise<returnData> {
}
}

async function handleZipFromUrl(
zipUrlPath: string,
reply: (data: string, payload: any) => void
): Promise<void> {
async function handleZipFromUrl(zipUrlPath: string, event): Promise<void> {
const tempZipPath = getAppFilePath('apps', 'temp')
let returnData: returnData | undefined
try {
if (!fs.existsSync(getAppFilePath('apps', 'temp'))) {
fs.mkdirSync(getAppFilePath('apps', 'temp'), { recursive: true })
}

const writeStream = fs.createWriteStream(join(tempZipPath, 'temp.zip'))
writeStream.on('error', (error) => {
dataListener.emit(MESSAGE_TYPES.ERROR, `Error writing to temp file: ${error.message}`)
throw new Error(`Error writing to temp file: ${error.message}`)
})

event.reply('logging', { status: true, data: 'Making Request...', final: false })
const request = net.request(zipUrlPath)
request.on('response', (response) => {
if (response.statusCode !== 200) {
dataListener.emit(
MESSAGE_TYPES.ERROR,
`Failed to download zip file: ${response.statusCode}`
)
event.reply('logging', {
status: false,
data: 'Encountered an error',
final: true,
error: `Failed to download zip file: ${response.statusCode}`
})
return
}

Expand All @@ -565,10 +576,11 @@ async function handleZipFromUrl(
response.on('end', async () => {
writeStream.end()
try {
event.reply('logging', { status: true, data: 'Extracting zip file...', final: false })
// Run file like a normal zip file
returnData = await handleZip(join(tempZipPath, 'temp.zip'))
returnData = await handleZip(join(tempZipPath, 'temp.zip'), event)
console.log(`Successfully processed and deleted ${tempZipPath}`)
reply('zip-name', returnData)
event.reply('zip-name', { status: true, data: returnData, final: true })
} catch (error) {
dataListener.emit(MESSAGE_TYPES.ERROR, `Error processing zip file: ${error}`)
} finally {
Expand Down Expand Up @@ -629,12 +641,13 @@ async function loadAndRunEnabledApps(): Promise<void> {
* Adds an app to the program. This will check the config for if the app exists already or not, and update accordingly
* This will also enable the app and then run the app
*
* @param _event
* @param event
* @param appName
*/
async function addApp(_event, appName: string, appPath?: string): Promise<void> {
async function addApp(event, appName: string, appPath?: string): Promise<void> {
try {
// Load existing apps config
event.reply('logging', { status: true, data: 'Loading apps config', final: false })
if (appPath != undefined) {
console.log('Developer app detected: Purging old app...')
await purgeAppData(appName)
Expand All @@ -656,15 +669,12 @@ async function addApp(_event, appName: string, appPath?: string): Promise<void>
} else {
console.log(`App '${appName}' already exists and is enabled.`)
}

event.reply('logging', { status: true, data: 'Running App', final: false })
console.log('Running app...')
if (runningApps.has(appName)) {
await stopApp(appName)
}

await runApp(appName)

// Run the app if enabled
} catch (error) {
console.error('Error adding app:', error)
}
Expand Down
22 changes: 5 additions & 17 deletions DeskThingServer/src/main/handlers/configHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sendIpcData } from '..'
import dataListener, { MESSAGE_TYPES } from '../utils/events'
import { readFromFile, writeToFile } from '../utils/fileHandler'
import { ButtonMapping } from './keyMapHandler'
export interface Manifest {
isAudioSource: boolean
requires: Array<string>
Expand Down Expand Up @@ -44,36 +45,25 @@ const defaultData: AppData = {
const readData = (): AppData => {
const dataFilePath = 'apps.json'
try {
const data = readFromFile(dataFilePath)
const data = readFromFile<AppData>(dataFilePath)
if (!data) {
// File does not exist, create it with default data
writeToFile(defaultData, dataFilePath)
return defaultData
}

// If data is of type AppData, return it
if (isAppData(data)) {
return data as AppData
} else {
// Handle case where data is not of type AppData
console.error('Data format is incorrect')
return defaultData
}
return data as AppData
} catch (err) {
console.error('Error reading data:', err)
return defaultData
}
}

// Type guard to check if data is of type AppData
const isAppData = (data: any): data is AppData => {
return 'apps' in data && 'config' in data
}

// Helper function to write data
const writeData = (data: AppData): void => {
try {
const result = writeToFile(data, 'apps.json')
const result = writeToFile<AppData>(data, 'apps.json')
if (!result) {
dataListener.emit(MESSAGE_TYPES.ERROR, 'Error writing data')
}
Expand Down Expand Up @@ -212,7 +202,5 @@ export {
addAppManifest,
addConfig,
getConfig,
purgeAppConfig,
saveMappings,
loadMappings
purgeAppConfig
}
27 changes: 3 additions & 24 deletions DeskThingServer/src/main/handlers/dataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,26 @@ const defaultData: Data = {}
const readData = (): Data => {
const dataFilePath = 'data.json'
try {
const data = readFromFile(dataFilePath)
const data = readFromFile<Data>(dataFilePath)
if (!data) {
// File does not exist, create it with default data
writeToFile(defaultData, dataFilePath)
return defaultData
}

// If data is of type Data, return it
if (isData(data)) {
return data as Data
} else {
// Handle case where data is not of type Data
console.error('Data format is incorrect')
return defaultData
}
return data as Data
} catch (err) {
console.error('Error reading data:', err)
return defaultData
}
}

// Type guard to check if data is of type Data
const isData = (data: any): data is Data => {
// Simple check to verify if data conforms to the Data interface
return (
typeof data === 'object' &&
data !== null &&
Object.values(data).every(
(value) =>
typeof value === 'object' &&
value !== null &&
Object.values(value).every((val) => typeof val === 'string')
)
)
}

// Updated function to write Data using the new fileHandler
const writeData = (data: Data): void => {
try {
const dataFilePath = 'data.json'
writeToFile(data, dataFilePath)
writeToFile<Data>(data, dataFilePath)
} catch (err) {
console.error('Error writing data:', err)
}
Expand Down
Loading

0 comments on commit 9a1174a

Please sign in to comment.