Skip to content

Commit

Permalink
added ability to update intel msn data from control
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseflorig committed Mar 21, 2023
1 parent 3553734 commit 1478984
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 118 deletions.
91 changes: 0 additions & 91 deletions dashboard/package-lock.json

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

1 change: 0 additions & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@tanstack/react-router": "^0.0.1-beta.52",
"axios": "^1.2.3",
"framer-motion": "^6.5.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
Expand Down
19 changes: 15 additions & 4 deletions dashboard/src/Control.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { InfoIcon } from "@chakra-ui/icons"
import msnActions from './msnActions'
import mxActions from './mxActions'
import useActionLog from './useActionLog'
import useLocalStorage from './useLocalStorage'
import usePersistentSocket from './usePersistentSocket'

import ConnectionStatus from './ConnectionStatus'
Expand All @@ -34,6 +35,7 @@ const {

function ActionButton({label, desc, handler, connectionCode}){
const isDisabled = connectionCode !== 1

return (
<Tooltip label={desc} placement="right" openDelay={1000} aria-label={`${label}-tooltip`}>
<Button onClick={handler} disabled={isDisabled}>{label}</Button>
Expand All @@ -52,15 +54,22 @@ ActionButton.propTypes = {
connectionCode: PropTypes.number
}

function ActionInput({label, desc, connectionCode}){
function ActionInput({label, desc, localStorageKey, defaultValue, connectionCode}){
const [localVal, setLocalVal] = useLocalStorage(localStorageKey, defaultValue)
const isDisabled = connectionCode !== 1

// Hacky way to force the default value to be written to localStorage before the user edits the default value
// This is needed since the unmodified value is not initially written - there is an action that relies on this being in localstorage
if(!window.localStorage.getItem(localStorageKey)) setLocalVal(defaultValue)

const handleChange = (e) => setLocalVal(e.target.value)
return (
<HStack>
<Text fontWeight="bold" flexGrow={1} noOfLines={1}>{label}</Text>
<Tooltip label={desc} placement="top-start" openDelay={1000} aria-label={`${label}-tooltip`}>
<InfoIcon />
</Tooltip>
<Input flexBasis="60%" placeholder={label} disabled={isDisabled} />
<Input value={localVal} onChange={handleChange} flexBasis="60%" placeholder={label} disabled={isDisabled} />
</HStack>
)
}
Expand All @@ -72,6 +81,8 @@ ActionInput.defaultProps = {
ActionInput.propTypes = {
label: PropTypes.string.isRequired,
desc: PropTypes.string.isRequired,
localStorageKey: PropTypes.string.isRequired,
defaultValue: PropTypes.string.isRequired,
connectionCode: PropTypes.number
}

Expand Down Expand Up @@ -140,15 +151,15 @@ function ActionsCard({title, actions, wsUrl, logKey}){
</UnorderedList>
</Stack>
</Stack>
{ actions.map(({type, label, desc, handler, options}) => {
{ actions.map(({type, label, desc, handler, localStorageKey, defaultValue, options}) => {
let component
if(type === "button"){
component = (
<ActionButton key={label} label={label} desc={desc} handler={() => handler(sendMessage)} connectionCode={statusCode} />
)
}
else if(type === "input"){
component = (<ActionInput key={label} label={label} desc={desc} connectionCode={statusCode} />)
component = (<ActionInput key={label} label={label} desc={desc} localStorageKey={localStorageKey} defaultValue={defaultValue} connectionCode={statusCode} />)
}
else if(type === "select"){
component = (<ActionSelect key={label} label={label} desc={desc} options={options} handler={() => handler(sendMessage)} connectionCode={statusCode} />)
Expand Down
45 changes: 39 additions & 6 deletions dashboard/src/msnActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

function handleBuildATO(send){
send('build')
}
Expand All @@ -7,31 +6,65 @@ function handlePublishATO(send){
send('publish')
}

function handleWriteMsnData(send){
function getLocalStorageItem(key){
return JSON.parse(window.localStorage.getItem(key))
}

const msnId = getLocalStorageItem('tv-msn-id')
const msnTakeoff = getLocalStorageItem('tv-msn-takeoff')
const msnReturn = getLocalStorageItem('tv-msn-return')
const msnPlatform = getLocalStorageItem('tv-msn-platform')
const msnTarget = getLocalStorageItem('tv-msn-target')
const postData = {msn_id: msnId, msn_takeoff: msnTakeoff, msn_return: msnReturn, msn_platform: msnPlatform, msn_target: msnTarget}
const encodedData = btoa(JSON.stringify(postData))

console.log(postData)

send(`updateIntel ${encodedData}`)
}

export default [
{
type: "input",
label: "Mission ID",
desc: "Mission ID on the dashboard"
desc: "Mission ID on the dashboard",
localStorageKey: 'tv-msn-id',
defaultValue: 'UNK-TV-XX'
},
{
type: "input",
label: "Takeoff Time",
desc: "Mission takeoff on the dashboard"
desc: "Mission takeoff on the dashboard",
localStorageKey: 'tv-msn-takeoff',
defaultValue: '1000'
},
{
type: "input",
label: "Return Time",
desc: "Mission return on the dashboard"
desc: "Mission return on the dashboard",
localStorageKey: 'tv-msn-return',
defaultValue: '1100'
},
{
type: "input",
label: "Mission Platform",
desc: "Mission platform on the dashboard"
desc: "Mission platform on the dashboard",
localStorageKey: 'tv-msn-platform',
defaultValue: 'F-35'
},
{
type: "input",
label: "Mission Target",
desc: "Mission target on the dashboard"
desc: "Mission target on the dashboard",
localStorageKey: 'tv-msn-target',
defaultValue: 'SAM Sites'
},
{
type: "button",
label: "Push Intel Mission Data",
desc: "Control will send mission data to API Server and write to Data Server",
handler: handleWriteMsnData
},
{
type: "button",
Expand Down
17 changes: 13 additions & 4 deletions msn-api-server/msn-api-server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sys import argv
from urllib.request import urlopen
from urllib import request, parse
import asyncio
import json
import socket
Expand All @@ -10,7 +10,7 @@
"http://localhost:8012",
"http://localhost:8013",
"http://localhost:8014",
"http://localhost:8015",
"http://localhost:8015", #Intel
"http://localhost:8016",
"http://localhost:8017"
]
Expand All @@ -25,7 +25,7 @@ def build_msn_data(path='/'):
with open(MSN_DATA_FILE, 'w') as msn_data_file:
data = {}
for idx, data_server in enumerate(DATA_SERVERS):
content = urlopen(f"{data_server}{path}").read().decode()
content = request.urlopen(f"{data_server}{path}").read().decode()
json_data = json.loads(content)

key = list(json_data.keys())[0] # Grab the first key (shop name)
Expand All @@ -37,7 +37,7 @@ def build_msn_data(path='/'):

def restore_msn_data():
for idx, data_server in enumerate(DATA_SERVERS):
urlopen(f"{data_server}/restore")
request.urlopen(f"{data_server}/restore")
websockets.broadcast(connections['/msn-controller'], "Restored MSN Data on MSN Data Servers")

async def socket_handler(websocket, path):
Expand Down Expand Up @@ -65,6 +65,15 @@ async def socket_handler(websocket, path):
msn_data_file.write(f"[]")
msn_data_file.close()
websockets.broadcast(connections['/msn-controller'], "Reset mission data on MSN API Server")
elif message.startswith("updateIntel"):
data = { 'msn_data': message.split(' ')[1] }

post_url = DATA_SERVERS[3] + '/updateMsn'
post_data = parse.urlencode(data).encode()
req = request.Request(post_url, data=post_data)
resp = request.urlopen(req)

websockets.broadcast(connections['/msn-controller'], "Updated Intel mission data")

finally:
# Unregister connection
Expand Down
2 changes: 1 addition & 1 deletion msn-api-server/msn-data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"bcd": {"status": "good"}, "gccs": {"status": "good"}, "iamd": {"status": "good"}, "intel": {"status": "good", "msn_id": "01-JAN-23-TV-01", "msn_takeoff": "1000", "msn_return": "1100", "msn_platform": "f-35", "msn_target": "staging area"}, "freq": {"status": "good"}, "wx": {"status": "good"}}
{"bcd": {"status": "good"}, "gccs": {"status": "good"}, "iamd": {"status": "good"}, "intel": {"status": "good", "msn_id": "UNK-TV-XXX", "msn_takeoff": "1000", "msn_return": "1100", "msn_platform": "F-35", "msn_target": "Staging Area", "msnId": "\"UNK-TV-XX\"", "msnTakeoff": "\"1000\"", "msnReturn": "\"1100\"", "msnPlatform": "\"F-35\"", "msnTarget": "\"No Way\""}, "freq": {"status": "good"}, "wx": {"status": "good"}}
4 changes: 0 additions & 4 deletions msn-data-server/log
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@

test
test2
test
something else
Loading

0 comments on commit 1478984

Please sign in to comment.