feat: device alias
This commit is contained in:
parent
3e8ae22455
commit
2deadba855
|
|
@ -58,6 +58,7 @@ func (a *Api) StartApi() {
|
||||||
authentication.HandleFunc("/admin/register", a.registerAdminUser).Methods("POST")
|
authentication.HandleFunc("/admin/register", a.registerAdminUser).Methods("POST")
|
||||||
authentication.HandleFunc("/admin/exists", a.adminUserExists).Methods("GET")
|
authentication.HandleFunc("/admin/exists", a.adminUserExists).Methods("GET")
|
||||||
iot := r.PathPrefix("/api/device").Subrouter()
|
iot := r.PathPrefix("/api/device").Subrouter()
|
||||||
|
iot.HandleFunc("/alias", a.setDeviceAlias).Methods("PUT")
|
||||||
iot.HandleFunc("/auth", a.deviceAuth).Methods("GET", "POST", "DELETE")
|
iot.HandleFunc("/auth", a.deviceAuth).Methods("GET", "POST", "DELETE")
|
||||||
iot.HandleFunc("/cwmp/{sn}/getParameterNames", a.cwmpGetParameterNamesMsg).Methods("PUT")
|
iot.HandleFunc("/cwmp/{sn}/getParameterNames", a.cwmpGetParameterNamesMsg).Methods("PUT")
|
||||||
iot.HandleFunc("/cwmp/{sn}/getParameterValues", a.cwmpGetParameterValuesMsg).Methods("PUT")
|
iot.HandleFunc("/cwmp/{sn}/getParameterValues", a.cwmpGetParameterValuesMsg).Methods("PUT")
|
||||||
|
|
@ -79,6 +80,8 @@ func (a *Api) StartApi() {
|
||||||
if a.enterpise {
|
if a.enterpise {
|
||||||
iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).Methods("GET")
|
iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).Methods("GET")
|
||||||
iot.HandleFunc("/{sn}/connecteddevices", a.deviceConnectedDevices).Methods("GET")
|
iot.HandleFunc("/{sn}/connecteddevices", a.deviceConnectedDevices).Methods("GET")
|
||||||
|
iot.HandleFunc("/{sn}/traceroute", a.deviceTraceRoute).Methods("GET", "PUT")
|
||||||
|
iot.HandleFunc("/{sn}/speedtest", a.deviceSpeedTest).Methods("PUT")
|
||||||
}
|
}
|
||||||
iot.HandleFunc("/{sn}/wifi", a.deviceWifi).Methods("PUT", "GET")
|
iot.HandleFunc("/{sn}/wifi", a.deviceWifi).Methods("PUT", "GET")
|
||||||
dash := r.PathPrefix("/api/info").Subrouter()
|
dash := r.PathPrefix("/api/info").Subrouter()
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,7 @@ func NatsReq[T entity.DataType](
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Error message received, msg: %s, code: %d", *errMsg.Msg, errMsg.Code)
|
log.Printf("message received, msg: %s, code: %d", *errMsg.Msg, errMsg.Code)
|
||||||
w.WriteHeader(errMsg.Code)
|
w.WriteHeader(errMsg.Code)
|
||||||
w.Write(utils.Marshall(*errMsg.Msg))
|
w.Write(utils.Marshall(*errMsg.Msg))
|
||||||
return nil, errNatsMsgReceivedWithErrorData
|
return nil, errNatsMsgReceivedWithErrorData
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ type Device struct {
|
||||||
Vendor string
|
Vendor string
|
||||||
Version string
|
Version string
|
||||||
ProductClass string
|
ProductClass string
|
||||||
|
Alias string
|
||||||
Status Status
|
Status Status
|
||||||
Mqtt Status
|
Mqtt Status
|
||||||
Stomp Status
|
Stomp Status
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ type Device struct {
|
||||||
Vendor string
|
Vendor string
|
||||||
Version string
|
Version string
|
||||||
ProductClass string
|
ProductClass string
|
||||||
|
Alias string
|
||||||
Status Status
|
Status Status
|
||||||
Mqtt Status
|
Mqtt Status
|
||||||
Stomp Status
|
Stomp Status
|
||||||
|
|
@ -145,6 +146,11 @@ func (d *Database) DeleteDevice() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Database) SetDeviceAlias(sn string, newAlias string) error {
|
||||||
|
err := d.devices.FindOneAndUpdate(d.ctx, bson.D{{"sn", sn}}, bson.D{{"$set", bson.D{{"alias", newAlias}}}}).Err()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Database) DeviceExists(sn string) (bool, error) {
|
func (d *Database) DeviceExists(sn string) (bool, error) {
|
||||||
_, err := d.RetrieveDevice(sn)
|
_, err := d.RetrieveDevice(sn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,22 @@ func StartRequestsListener(ctx context.Context, nc *nats.Conn, db db.Database) {
|
||||||
}
|
}
|
||||||
respondMsg(msg.Respond, 200, productClassCount)
|
respondMsg(msg.Respond, 200, productClassCount)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
nc.Subscribe(local.ADAPTER_SUBJECT+"*.device.alias", func(msg *nats.Msg) {
|
||||||
|
subject := strings.Split(msg.Subject, ".")
|
||||||
|
device := subject[len(subject)-3]
|
||||||
|
|
||||||
|
err := db.SetDeviceAlias(device, string(msg.Data))
|
||||||
|
if err != nil {
|
||||||
|
if err == mongo.ErrNoDocuments {
|
||||||
|
respondMsg(msg.Respond, 404, "Device not found")
|
||||||
|
} else {
|
||||||
|
respondMsg(msg.Respond, 500, err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondMsg(msg.Respond, 200, "Alias updated")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondMsg(respond func(data []byte) error, code int, msgData any) {
|
func respondMsg(respond func(data []byte) error, code int, msgData any) {
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,26 @@ import {
|
||||||
Card,
|
Card,
|
||||||
CardActions,
|
CardActions,
|
||||||
CardHeader,
|
CardHeader,
|
||||||
|
Dialog,
|
||||||
|
DialogActions,
|
||||||
|
DialogContent,
|
||||||
|
DialogTitle,
|
||||||
Divider,
|
Divider,
|
||||||
|
Input,
|
||||||
|
InputLabel,
|
||||||
SvgIcon,
|
SvgIcon,
|
||||||
Table,
|
Table,
|
||||||
TableBody,
|
TableBody,
|
||||||
TableCell,
|
TableCell,
|
||||||
TableHead,
|
TableHead,
|
||||||
TableRow
|
TableRow,
|
||||||
|
Tooltip
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
import { Scrollbar } from 'src/components/scrollbar';
|
import { Scrollbar } from 'src/components/scrollbar';
|
||||||
import { SeverityPill } from 'src/components/severity-pill';
|
import { SeverityPill } from 'src/components/severity-pill';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
|
import { useState } from 'react';
|
||||||
|
import PencilIcon from '@heroicons/react/24/outline/PencilIcon';
|
||||||
|
|
||||||
const statusMap = {
|
const statusMap = {
|
||||||
1: 'warning',
|
1: 'warning',
|
||||||
|
|
@ -51,7 +60,60 @@ export const OverviewLatestOrders = (props) => {
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
return (
|
const [showSetDeviceAlias, setShowSetDeviceAlias] = useState(false);
|
||||||
|
const [deviceAlias, setDeviceAlias] = useState(null);
|
||||||
|
const [deviceToBeChanged, setDeviceToBeChanged] = useState(null);
|
||||||
|
|
||||||
|
const setNewDeviceAlias = async (alias,sn) => {
|
||||||
|
var myHeaders = new Headers();
|
||||||
|
myHeaders.append("Content-Type", "application/json");
|
||||||
|
myHeaders.append("Authorization", localStorage.getItem("token"));
|
||||||
|
|
||||||
|
var requestOptions = {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: myHeaders,
|
||||||
|
body: alias,
|
||||||
|
redirect: 'follow'
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = await fetch(`${process.env.NEXT_PUBLIC_REST_ENDPOINT}/device/alias?id=${sn}`, requestOptions)
|
||||||
|
console.log("result:", result)
|
||||||
|
if (result.status === 401){
|
||||||
|
router.push("/auth/login")
|
||||||
|
}else if (result.status != 200){
|
||||||
|
console.log("Status:", result.status)
|
||||||
|
let content = await result.json()
|
||||||
|
console.log("Message:", content)
|
||||||
|
setShowSetDeviceAlias(false)
|
||||||
|
setDeviceAlias(null)
|
||||||
|
setDeviceToBeChanged(null)
|
||||||
|
}else{
|
||||||
|
let content = await result.json()
|
||||||
|
console.log("set alias result:", content)
|
||||||
|
setShowSetDeviceAlias(false)
|
||||||
|
setDeviceAlias(null)
|
||||||
|
orders[deviceToBeChanged].Alias = alias
|
||||||
|
setDeviceToBeChanged(null)
|
||||||
|
}
|
||||||
|
// .then(response => {
|
||||||
|
// if (response.status === 401) {
|
||||||
|
// router.push("/auth/login")
|
||||||
|
// }
|
||||||
|
// return response.json()
|
||||||
|
// })
|
||||||
|
// .then(result => {
|
||||||
|
// console.log("alias result:", result)
|
||||||
|
// setShowSetDeviceAlias(false)
|
||||||
|
// setDeviceAlias(null)
|
||||||
|
// })
|
||||||
|
// .catch(error => {
|
||||||
|
// console.log('error:', error)
|
||||||
|
// setShowSetDeviceAlias(false)
|
||||||
|
// setDeviceAlias(null)
|
||||||
|
// })
|
||||||
|
}
|
||||||
|
|
||||||
|
return (<div>
|
||||||
<Card sx={sx}>
|
<Card sx={sx}>
|
||||||
<CardHeader title="Devices" />
|
<CardHeader title="Devices" />
|
||||||
<Scrollbar sx={{ flexGrow: 1 }}>
|
<Scrollbar sx={{ flexGrow: 1 }}>
|
||||||
|
|
@ -62,6 +124,9 @@ export const OverviewLatestOrders = (props) => {
|
||||||
<TableCell align="center">
|
<TableCell align="center">
|
||||||
Serial Number
|
Serial Number
|
||||||
</TableCell>
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
Alias
|
||||||
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
Model
|
Model
|
||||||
</TableCell>
|
</TableCell>
|
||||||
|
|
@ -74,13 +139,13 @@ export const OverviewLatestOrders = (props) => {
|
||||||
<TableCell>
|
<TableCell>
|
||||||
Status
|
Status
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell align="center">
|
||||||
Access
|
Actions
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{orders && orders.map((order) => {
|
{orders && orders.map((order, index) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TableRow
|
<TableRow
|
||||||
|
|
@ -91,7 +156,10 @@ export const OverviewLatestOrders = (props) => {
|
||||||
{order.SN}
|
{order.SN}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
{order.Model}
|
{order.Alias}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{order.Model || order.ProductClass}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
{order.Vendor}
|
{order.Vendor}
|
||||||
|
|
@ -104,24 +172,49 @@ export const OverviewLatestOrders = (props) => {
|
||||||
{status(order.Status)}
|
{status(order.Status)}
|
||||||
</SeverityPill>
|
</SeverityPill>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell align="center">
|
||||||
{order.Status == 2 &&
|
{order.Status == 2 &&
|
||||||
<Button
|
<Tooltip title="Access the device">
|
||||||
onClick={()=>{
|
<Button
|
||||||
if (getDeviceProtocol(order) == "usp"){
|
onClick={()=>{
|
||||||
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/discovery")
|
if (getDeviceProtocol(order) == "usp"){
|
||||||
}else {
|
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/discovery")
|
||||||
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/wifi")
|
}else {
|
||||||
}
|
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/wifi")
|
||||||
}}
|
}
|
||||||
>
|
}}
|
||||||
<SvgIcon
|
|
||||||
fontSize="small"
|
|
||||||
sx={{cursor: 'pointer'}}
|
|
||||||
>
|
>
|
||||||
<ArrowTopRightOnSquareIcon />
|
<SvgIcon
|
||||||
</SvgIcon>
|
fontSize="small"
|
||||||
</Button>}
|
sx={{cursor: 'pointer'}}
|
||||||
|
>
|
||||||
|
<ArrowTopRightOnSquareIcon />
|
||||||
|
</SvgIcon>
|
||||||
|
</Button>
|
||||||
|
</Tooltip>}
|
||||||
|
<Tooltip title="Edit the device alias">
|
||||||
|
<Button
|
||||||
|
onClick={()=>{
|
||||||
|
setDeviceToBeChanged(index)
|
||||||
|
setDeviceAlias(order.Alias)
|
||||||
|
setShowSetDeviceAlias(true)
|
||||||
|
}}
|
||||||
|
onKeyDown={e => {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
setDeviceToBeChanged(index)
|
||||||
|
setDeviceAlias(order.Alias)
|
||||||
|
setShowSetDeviceAlias(true)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SvgIcon
|
||||||
|
fontSize="small"
|
||||||
|
sx={{cursor: 'pointer'}}
|
||||||
|
>
|
||||||
|
<PencilIcon />
|
||||||
|
</SvgIcon>
|
||||||
|
</Button>
|
||||||
|
</Tooltip>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
);
|
);
|
||||||
|
|
@ -146,6 +239,24 @@ export const OverviewLatestOrders = (props) => {
|
||||||
</Button>
|
</Button>
|
||||||
</CardActions>*/}
|
</CardActions>*/}
|
||||||
</Card>
|
</Card>
|
||||||
|
<Dialog open={showSetDeviceAlias}>
|
||||||
|
<DialogContent>
|
||||||
|
<InputLabel>Device Alias</InputLabel>
|
||||||
|
<Input value={deviceAlias} onChange={(e)=>{setDeviceAlias(e.target.value)}}>
|
||||||
|
</Input>
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={()=>{
|
||||||
|
setShowSetDeviceAlias(false)
|
||||||
|
setDeviceAlias(null)
|
||||||
|
setDeviceToBeChanged(null)
|
||||||
|
}}>Cancel</Button>
|
||||||
|
<Button onClick={()=>{
|
||||||
|
setNewDeviceAlias(deviceAlias, orders[deviceToBeChanged].SN)
|
||||||
|
}}>Save</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user