Skip to content

Commit

Permalink
Update add event
Browse files Browse the repository at this point in the history
Signed-off-by: rovast <rovast@163.com>
  • Loading branch information
rovast committed Sep 19, 2023
1 parent 7d1fdcb commit cc445ba
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dayjs": "^1.11.6",
"ethers": "^6.1.0",
"js-base64": "^3.7.5",
"js-yaml": "^4.1.0",
"leaflet": "^1.9.3",
"loadsh": "^0.0.4",
"lodash": "^4.17.21",
Expand Down
53 changes: 29 additions & 24 deletions pnpm-lock.yaml

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

43 changes: 41 additions & 2 deletions src/components/IgntCrudCreate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { reactive, computed } from "vue";
import { IgntModal } from "@ignt/vue-library";
import { useRoute } from "vue-router";
import { clone } from "lodash";
import { useIotEvents } from "@/hooks/useIotEvents";
const route = useRoute();
const { getIotDeviceInstructions, instructions } = useIotEvents();
if (route.name === "iot.device.events") {
getIotDeviceInstructions();
}
const props = defineProps({
storeName: {
Expand All @@ -31,6 +37,14 @@ const props = defineProps({
}
}
});
const payloadForm = reactive({
payload_type: "subscribe",
name: route.params.name,
path: "path",
data: "data"
});
const emit = defineEmits(["close"]);
const formData = reactive<any>(props.initalData);
const { address } = useAddress();
Expand Down Expand Up @@ -80,8 +94,14 @@ const creator = address.value;
const submitItem = async () => {
const cloneFormData = clone(formData);
if (route.name === "iot.device.events") {
const clonePayloadForm = clone(payloadForm);
clonePayloadForm["data"] = window.btoa(clonePayloadForm["data"]);
console.log(JSON.stringify(clonePayloadForm));
// wrap post data
cloneFormData["index"] = "";
cloneFormData["payload"] = window.btoa(cloneFormData["payload"]);
cloneFormData["payload"] = clonePayloadForm;
}
// auto fill device name value
Expand Down Expand Up @@ -125,7 +145,26 @@ const submitItem = async () => {
>
<template #body>
<div class="my-4 w-[500px]" />
<div v-for="field in itemFieldsFiltered" :key="'field_' + field">
<template v-if="route.name === 'iot.device.events'">
<div>
<label class="sp-label capitalize-first-letter">Type</label>
<select v-model="payloadForm.payload_type" class="sp-input">
<option value="subscribe">Subscribe</option>
<option value="publish">Publish</option>
</select>
</div>
<div>
<label class="sp-label capitalize-first-letter">Path</label>
<select v-model="payloadForm.path" class="sp-input">
<option :value="i" v-for="i in instructions">{{ i }}</option>
</select>
</div>
<div>
<label class="sp-label capitalize-first-letter">Data</label>
<input v-model="payloadForm.data" class="sp-input" />
</div>
</template>
<div v-else v-for="field in itemFieldsFiltered" :key="'field_' + field">
<label :for="`p${field.name}`" class="sp-label capitalize-first-letter">
{{ field.name }}
</label>
Expand Down
12 changes: 6 additions & 6 deletions src/components/PageLayout/vertical-with-site.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { t } = useLang();
const { nostrStore, relay } = useNostr();
const route = useRoute();
const router = useRouter();
const { data, loading, getIotDevices } = useIotDevices();
const { data, getIotDevices } = useIotDevices();
getIotDevices();
const children = computed(() => {
Expand Down Expand Up @@ -49,11 +49,14 @@ const accountStore = useAccountStore();
const currentDevice = ref(route.params.name);
const onDeviceChange = v => {
const mid = data.devices.find(i => i.name === v)?.mid;
router
.push({
name: route.name,
params: {
name: v
name: v,
mid
}
})
.then(() => {
Expand All @@ -72,10 +75,7 @@ const onDeviceChange = v => {
@change="onDeviceChange(currentDevice)"
>
<option disabled>Pick a device</option>
<option v-if="loading" :value="currentDevice">
{{ currentDevice }}
</option>
<option v-else v-for="r in data.devices" :value="r.name">
<option v-for="r in data.devices" :value="r.name">
{{ r.name }}
</option>
</select>
Expand Down
44 changes: 44 additions & 0 deletions src/hooks/useIotEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { reactive, ref, computed } from "vue";
import { useRoute } from "vue-router";
import { loadData } from "@/utils/shared";
import yaml from "js-yaml";

export function useIotEvents() {
const route = useRoute();

const data = reactive({
data: ""
});
const loading = ref(false);

const instructions = computed(() => {
try {
const res = data.data;

const json = yaml.load(res, "utf-8");

return Object.keys(json?.instructions);
} catch (e) {
return [];
}
});

const getIotDeviceInstructions = async () => {
loading.value = true;
loadData(
data,
"iot.device.instructions",
{
mid: route.params.mid
},
loading
);
};

return {
data,
loading,
getIotDeviceInstructions,
instructions
};
}
8 changes: 4 additions & 4 deletions src/router/modules/iot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
component: () => import("@/views/iot/device/detail/index.vue"),
children: [
{
path: "/iot/device/:name/metrics",
path: "/iot/device/:name/metrics/:mid",
name: "iot.device.metrics",
component: () => import("@/views/iot/device/detail/metrics.vue"),
meta: {
Expand All @@ -41,7 +41,7 @@ export default {
// }
// },
{
path: "/iot/device/:name/events",
path: "/iot/device/:name/events/:mid",
name: "iot.device.events",
component: () => import("@/views/iot/device/detail/events.vue"),
meta: {
Expand All @@ -50,7 +50,7 @@ export default {
}
},
{
path: "/iot/device/:name/data",
path: "/iot/device/:name/data/:mid",
name: "iot.device.data",
component: () => import("@/views/iot/device/detail/data.vue"),
meta: {
Expand All @@ -59,7 +59,7 @@ export default {
}
},
{
path: "/iot/device/:name/logs",
path: "/iot/device/:name/logs/:mid",
name: "iot.device.logs",
component: () => import("@/views/iot/device/detail/logs.vue"),
meta: {
Expand Down
5 changes: 4 additions & 1 deletion src/views/iot/device/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ onBeforeUnmount(() => {
<div class="card-body">
<router-link
class="card-title text-primary hover:cursor-pointer hover:text-primary-focus"
:to="{ name: 'iot.device.metrics', params: { name: d.name } }"
:to="{
name: 'iot.device.metrics',
params: { name: d.name, mid: d.mid }
}"
>
<IconifyIconOnline
icon="mingcute:device-line"
Expand Down

0 comments on commit cc445ba

Please sign in to comment.