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/exists", a.adminUserExists).Methods("GET")
|
||||
iot := r.PathPrefix("/api/device").Subrouter()
|
||||
iot.HandleFunc("/alias", a.setDeviceAlias).Methods("PUT")
|
||||
iot.HandleFunc("/auth", a.deviceAuth).Methods("GET", "POST", "DELETE")
|
||||
iot.HandleFunc("/cwmp/{sn}/getParameterNames", a.cwmpGetParameterNamesMsg).Methods("PUT")
|
||||
iot.HandleFunc("/cwmp/{sn}/getParameterValues", a.cwmpGetParameterValuesMsg).Methods("PUT")
|
||||
|
|
@ -79,6 +80,8 @@ func (a *Api) StartApi() {
|
|||
if a.enterpise {
|
||||
iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).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")
|
||||
dash := r.PathPrefix("/api/info").Subrouter()
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ func NatsReq[T entity.DataType](
|
|||
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.Write(utils.Marshall(*errMsg.Msg))
|
||||
return nil, errNatsMsgReceivedWithErrorData
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ type Device struct {
|
|||
Vendor string
|
||||
Version string
|
||||
ProductClass string
|
||||
Alias string
|
||||
Status Status
|
||||
Mqtt Status
|
||||
Stomp Status
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ type Device struct {
|
|||
Vendor string
|
||||
Version string
|
||||
ProductClass string
|
||||
Alias string
|
||||
Status Status
|
||||
Mqtt 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) {
|
||||
_, err := d.RetrieveDevice(sn)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -89,6 +89,22 @@ func StartRequestsListener(ctx context.Context, nc *nats.Conn, db db.Database) {
|
|||
}
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -8,17 +8,26 @@ import {
|
|||
Card,
|
||||
CardActions,
|
||||
CardHeader,
|
||||
Dialog,
|
||||
DialogActions,
|
||||
DialogContent,
|
||||
DialogTitle,
|
||||
Divider,
|
||||
Input,
|
||||
InputLabel,
|
||||
SvgIcon,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableRow
|
||||
TableRow,
|
||||
Tooltip
|
||||
} from '@mui/material';
|
||||
import { Scrollbar } from 'src/components/scrollbar';
|
||||
import { SeverityPill } from 'src/components/severity-pill';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useState } from 'react';
|
||||
import PencilIcon from '@heroicons/react/24/outline/PencilIcon';
|
||||
|
||||
const statusMap = {
|
||||
1: 'warning',
|
||||
|
|
@ -51,7 +60,60 @@ export const OverviewLatestOrders = (props) => {
|
|||
|
||||
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}>
|
||||
<CardHeader title="Devices" />
|
||||
<Scrollbar sx={{ flexGrow: 1 }}>
|
||||
|
|
@ -62,6 +124,9 @@ export const OverviewLatestOrders = (props) => {
|
|||
<TableCell align="center">
|
||||
Serial Number
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
Alias
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
Model
|
||||
</TableCell>
|
||||
|
|
@ -74,13 +139,13 @@ export const OverviewLatestOrders = (props) => {
|
|||
<TableCell>
|
||||
Status
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
Access
|
||||
<TableCell align="center">
|
||||
Actions
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{orders && orders.map((order) => {
|
||||
{orders && orders.map((order, index) => {
|
||||
|
||||
return (
|
||||
<TableRow
|
||||
|
|
@ -91,7 +156,10 @@ export const OverviewLatestOrders = (props) => {
|
|||
{order.SN}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{order.Model}
|
||||
{order.Alias}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{order.Model || order.ProductClass}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{order.Vendor}
|
||||
|
|
@ -104,24 +172,49 @@ export const OverviewLatestOrders = (props) => {
|
|||
{status(order.Status)}
|
||||
</SeverityPill>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<TableCell align="center">
|
||||
{order.Status == 2 &&
|
||||
<Button
|
||||
onClick={()=>{
|
||||
if (getDeviceProtocol(order) == "usp"){
|
||||
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/discovery")
|
||||
}else {
|
||||
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/wifi")
|
||||
}
|
||||
}}
|
||||
>
|
||||
<SvgIcon
|
||||
fontSize="small"
|
||||
sx={{cursor: 'pointer'}}
|
||||
<Tooltip title="Access the device">
|
||||
<Button
|
||||
onClick={()=>{
|
||||
if (getDeviceProtocol(order) == "usp"){
|
||||
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/discovery")
|
||||
}else {
|
||||
router.push("devices/"+ getDeviceProtocol(order) +"/"+order.SN+"/wifi")
|
||||
}
|
||||
}}
|
||||
>
|
||||
<ArrowTopRightOnSquareIcon />
|
||||
</SvgIcon>
|
||||
</Button>}
|
||||
<SvgIcon
|
||||
fontSize="small"
|
||||
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>
|
||||
</TableRow>
|
||||
);
|
||||
|
|
@ -146,6 +239,24 @@ export const OverviewLatestOrders = (props) => {
|
|||
</Button>
|
||||
</CardActions>*/}
|
||||
</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