import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit"; import { RootState } from "./store"; import ReactNativeBlobUtil from "react-native-blob-util"; import { setActionData } from "./AOPSlice"; import RNFS from "react-native-fs"; import { addToCaptureSettings, CameraSetting } from "./UtilSlice"; const digestFetch = require("react-native-digest-fetch"); interface AppState { ricohAuth?: { ip: string; username: string; password: string; }; ricohSettingsLoaded: boolean; ricohCaptureState: | "CAPTURING" | "FINISHED" | "CONNECTED" | "NOT_CONNECTED" | "ERROR"; } const initialState: AppState = { ricohCaptureState: "NOT_CONNECTED", ricohSettingsLoaded: false, }; export const initializeRicoh = createAsyncThunk( "appState/initializeRicoh", async (_, ThunkAPI) => { const appState = (ThunkAPI.getState() as RootState).AppState; if (appState.ricohAuth) { const { username, password, ip } = appState.ricohAuth; await digestFetch.default( `https://${username}:${password}@${ip}` + "/osc/state", { method: "POST", headers: { "content-type": "application/json", }, username, password, } ); } ThunkAPI.dispatch(loadRicohSettings()); } ); export const loadRicohSettings = createAsyncThunk( "appState/loadRicohSettings", async (_, ThunkAPI) => { const appState = (ThunkAPI.getState() as RootState).AppState; if (appState.ricohAuth) { const { username, password, ip } = appState.ricohAuth; const optionNames = [ "iso", "exposureCompensation", "whiteBalance", "aperture", ]; const optionSupportNames = [ "isoSupport", "exposureCompensationSupport", "whiteBalanceSupport", "apertureSupport", ]; const response = await digestFetch.default( `https://${username}:${password}@${ip}` + "/osc/commands/execute", { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify({ name: "camera.getOptions", parameters: { optionNames: [...optionNames, ...optionSupportNames], }, }), username, password, } ); const data = await response.json(); const { options } = data.results; const settings = optionNames.map((name) => { return { value: options[name] as string | number, available: options[`${name}Support`] as string[] | number[], key: name, camera: "ricoh", platform: "all", type: "text", } as CameraSetting; }); console.log(settings); ThunkAPI.dispatch(addToCaptureSettings(settings)); } } ); export const setRicohSetting = createAsyncThunk( "appState/setRicohSetting", async (data: Object, ThunkAPI) => { const appState = (ThunkAPI.getState() as RootState).AppState; if (appState.ricohAuth) { console.log(data); const { username, password, ip } = appState.ricohAuth; const response = await digestFetch.default( `https://${username}:${password}@${ip}` + "/osc/commands/execute", { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify({ name: "camera.setOptions", parameters: { options: { ...data, }, }, }), username, password, } ); const json = await response.json(); console.log(json); ThunkAPI.dispatch(loadRicohSettings()); } } ); export const captureRicoh = createAsyncThunk( "appState/captureRicoh", async (_, ThunkAPI) => { const appState = (ThunkAPI.getState() as RootState).AppState; if (appState.ricohAuth) { const { username, password, ip } = appState.ricohAuth; await digestFetch.default( `https://${username}:${password}@${ip}` + "/osc/commands/execute", { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify({ name: "camera.takePicture" }), username, password, } ); setTimeout(() => { ThunkAPI.dispatch(fetchImageRicoh()); }, 5000); } } ); export const fetchImageRicoh = createAsyncThunk( "appState/fetchImageRicoh", async (_, ThunkAPI) => { const appState = (ThunkAPI.getState() as RootState).AppState; if (appState.ricohAuth) { const { username, password, ip } = appState.ricohAuth; const response = await digestFetch.default( `https://${username}:${password}@${ip}` + "/osc/state", { method: "POST", headers: { "content-type": "application/json", }, username, password, } ); const data = await response.json(); console.log(data); const imageData: string = data.state._latestFileUrl.replace( /^https?:\/\//, "" ); console.log(imageData); console.log(`https://${username}:${password}@${imageData}`); const image = await digestFetch.default( `https://${username}:${password}@${imageData}`, { method: "GET", headers: { "content-type": "image/jpeg", }, username, password, } ); image.blob().then((blobData: any) => { const reader = new FileReader(); reader.readAsDataURL(blobData); reader.onloadend = async () => { const content = (reader.result as string).split(",")[1]; const { activeStepIndex, activeSlotIndex, steps } = ( ThunkAPI.getState() as RootState ).AOP; const { productID } = (ThunkAPI.getState() as RootState).App; const fileSavePath = `${RNFS.DocumentDirectoryPath}/${ productID + "_" + steps[activeStepIndex].title + "_" + steps[activeStepIndex].slots[activeSlotIndex].title + "_" }.jpeg`; const streams = await ReactNativeBlobUtil.fs.writeStream( fileSavePath, "base64" ); await streams.write(content as string); const actionDataConstructor = Object.assign( {}, { path: fileSavePath, } ); ThunkAPI.dispatch(setActionData(actionDataConstructor)); }; }); } } ); export const appSlice = createSlice({ name: "appState", initialState, reducers: { setRicohCaptureState: ( state, action: PayloadAction< "CAPTURING" | "FINISHED" | "CONNECTED" | "NOT_CONNECTED" | "ERROR" > ) => { state.ricohCaptureState = action.payload; }, setRicohAuthData: ( state, action: PayloadAction<{ ip: string; username: string; password: string; }> ) => { state.ricohAuth = action.payload; }, }, extraReducers: (builder) => { builder.addCase(initializeRicoh.rejected, (state, __) => { state.ricohCaptureState = "ERROR"; }); builder.addCase(initializeRicoh.fulfilled, (state, __) => { state.ricohCaptureState = "CONNECTED"; }); builder.addCase(captureRicoh.fulfilled, (state, __) => { state.ricohCaptureState = "CAPTURING"; }); builder.addCase(fetchImageRicoh.fulfilled, (state, __) => { state.ricohCaptureState = "FINISHED"; }); }, }); export const { setRicohAuthData, setRicohCaptureState } = appSlice.actions; export default appSlice.reducer;