"use strict";(self.webpackChunkdocs_openc3_com=self.webpackChunkdocs_openc3_com||[]).push([[9366],{7338:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>o,default:()=>h,frontMatter:()=>s,metadata:()=>r,toc:()=>d});var a=n(2322),i=n(5392);const s={title:"Streaming API"},o=void 0,r={id:"development/streaming-api",title:"Streaming API",description:"This information is just generally used behind the scenes in COSMOS tools",source:"@site/docs/development/streaming-api.md",sourceDirName:"development",slug:"/development/streaming-api",permalink:"/tools/staticdocs/docs/development/streaming-api",draft:!1,unlisted:!1,editUrl:"https://github.com/OpenC3/cosmos/tree/main/docs.openc3.com/docs/development/streaming-api.md",tags:[],version:"current",frontMatter:{title:"Streaming API"},sidebar:"defaultSidebar",previous:{title:"Roadmap",permalink:"/tools/staticdocs/docs/development/roadmap"},next:{title:"Testing COSMOS",permalink:"/tools/staticdocs/docs/development/testing"}},c={},d=[];function l(e){const t={admonition:"admonition",code:"code",p:"p",pre:"pre",...(0,i.a)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(t.admonition,{title:"This documentation is for COSMOS Developers",type:"note",children:(0,a.jsx)(t.p,{children:"This information is just generally used behind the scenes in COSMOS tools"})}),"\n",(0,a.jsx)(t.p,{children:"The COSMOS 5 Streaming Api is the primary interface to receive a stream of the telemetry packets and/or command packets that have passed through the COSMOS system, both logged and continuously in realtime. Either raw binary packets or decommutated JSON packets can be requested."}),"\n",(0,a.jsx)(t.p,{children:"This API is implemented over Websockets using the Rails ActionCable framework. Actioncable client libraries are known to exist for at least Javascript, Ruby, and Python. Other languages may exist or could be created. Websockets allow for easy interaction with the new COSMOS 5 Javascript based frontend."}),"\n",(0,a.jsx)(t.p,{children:"The following interactions are all shown in Javascript, but would be very similar in any language.\nConnecting to this API begins by initiating an ActionCable connection."}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{children:"cable = ActionCable.createConsumer('/openc3-api/cable')\n"})}),"\n",(0,a.jsx)(t.p,{children:"This call opens the HTTP connection to the given URL and upgrades it to a websocket connection. This connection can then be shared with multiple \u201csubscriptions\u201d."}),"\n",(0,a.jsx)(t.p,{children:"A subscription describes a set of data that you want the API to stream to you. Creating a subscription looks like this:"}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-javascript",children:'subscription = cable.subscriptions.create(\n {\n channel: "StreamingChannel",\n scope: "DEFAULT",\n token: token,\n },\n {\n received: (data) => {\n // Handle received data\n },\n connected: () => {\n // First chance to add what you want to stream here\n },\n disconnected: () => {\n // Handle the subscription being disconnected\n },\n rejected: () => {\n // Handle the subscription being rejected\n },\n }\n);\n'})}),"\n",(0,a.jsxs)(t.p,{children:["Subscribing to the StreamingApi requires passing a channel name set to \u201cStreamingChannel\u201d, a scope which is typically \u201cDEFAULT\u201d, and an access token (a password in OpenSource COSMOS). In Javascript you also pass a set of callback functions that run at various lifecycle points in the subscription. The most important of these are ",(0,a.jsx)(t.code,{children:"connected"})," and ",(0,a.jsx)(t.code,{children:"received"}),"."]}),"\n",(0,a.jsxs)(t.p,{children:[(0,a.jsx)(t.code,{children:"connected"})," runs when the subscription is accepted by the StreamApi. This callback is the first opportunity to request specific data that you would like streamed. Data can also be added or removed at any time while the subscription is open."]}),"\n",(0,a.jsx)(t.p,{children:"Data can be added to the stream by requesting individual items from a packet or by requesting the entire packet."}),"\n",(0,a.jsx)(t.p,{children:"Adding items to stream is done as follows:"}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-javascript",children:'var items = [\n ["DECOM__TLM__INST__ADCS__Q1__RAW", "0"],\n ["DECOM__CMD__INST__COLLECT__DURATION__WITH_UNITS", "1"],\n];\nOpenC3Auth.updateToken(OpenC3Auth.defaultMinValidity).then(() => {\n this.subscription.perform("add", {\n scope: window.openc3Scope,\n token: localStorage.openc3Token,\n items: items,\n start_time: this.startDateTime,\n end_time: this.endDateTime,\n });\n});\n'})}),"\n",(0,a.jsxs)(t.p,{children:["The values in the item name are separated by double underscores, e.g. ",(0,a.jsx)(t.code,{children:"____________"}),". Mode is either RAW, DECOM, REDUCED_MINUTE, REDUCED_HOUR, or REDUCED_DAY. The next parameter is CMD or TLM followed by the target, packet and item names. The Value Type is one of RAW, CONVERTED, FORMATTED, or WITH_UNITS. The last parameter is optional if you want to use the reduced data types. Reduced Type is one of SAMPLE, MIN, MAX, AVG, or STDDEV."]}),"\n",(0,a.jsx)(t.p,{children:"Adding packets to stream is done as follows:"}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-javascript",children:'var packets = [\n ["RAW__TLM__INST__ADCS", "0"],\n ["DECOM__TLM__INST__HEALTH_STATUS__FORMATTED", "1"],\n];\nOpenC3Auth.updateToken(OpenC3Auth.defaultMinValidity).then(() => {\n this.subscription.perform("add", {\n scope: window.openc3Scope,\n token: localStorage.openc3Token,\n packets: packets,\n start_time: this.startDateTime,\n end_time: this.endDateTime,\n });\n});\n'})}),"\n",(0,a.jsxs)(t.p,{children:["The values in the packet name are separated by double underscores, e.g. ",(0,a.jsx)(t.code,{children:"________"}),". Mode is either RAW or DECOM. The next parameter is CMD or TLM followed by the target and packet names. The Value Type is one of RAW, CONVERTED, FORMATTED, or WITH_UNITS."]}),"\n",(0,a.jsx)(t.p,{children:"For Raw mode, VALUE TYPE should be set to RAW or omitted (e.g. TLM__INST__ADCS__RAW or TLM__INST__ADCS).\nstart_time and end_time are standard COSMOS 64-bit integer timestamps in nanoseconds since the Unix Epoch (midnight January 1st, 1970). If start_time is null, that indicates to start streaming from the current time in realtime, indefinitely until items are removed, or the subscription is unsubscribed. end_time is ignored if start_time is null. If start_time is given and end_time is null, that indicates to playback from the given starttime and then continue indefinitely in realtime. If both start_time and end_time are given, then that indicates a temporary playback of historical data."}),"\n",(0,a.jsx)(t.p,{children:"Data returned by the streaming API is handled by the received callback in Javascript. Data is returned as a JSON Array, with a JSON object in the array for each packet returned. Results are batched, and the current implementation will return up to 100 packets in each batch (the array will have 100 entries). 100 packets per batch is not guaranteed, and batches may take on varying sizes based on the size of the data returned, or other factors. An empty array indicates that all data has been sent for a purely historical query and can be used as an end of data indicator."}),"\n",(0,a.jsx)(t.p,{children:"For decommutated items, each packet is represented as a JSON object with a 'time' field holding the COSMOS nanosecond timestamp of the packet, and then each of the requested item keys with their corresponding value from the packet."}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-json",children:'[\n {\n "time": 1234657585858,\n "TLM__INST__ADCS__Q1__RAW": 50.0,\n "TLM__INST__ADCS__Q2__RAW": 100.0\n },\n {\n "time": 1234657585859,\n "TLM__INST__ADCS__Q1__RAW": 60.0,\n "TLM__INST__ADCS__Q2__RAW": 110.0\n }\n]\n'})}),"\n",(0,a.jsx)(t.p,{children:"For raw packets, each packet is represented as a JSON object with a time field holding the COSMOS nanosecond timestamp of the packet, a packet field holding the topic the packet was read from in the form of SCOPE__TELEMETRY__TARGETNAME__PACKETNAME, and a buffer field holding a BASE64 encoded copy of the packet data."}),"\n",(0,a.jsx)(t.pre,{children:(0,a.jsx)(t.code,{className:"language-json",children:'[\n {\n "time": 1234657585858,\n "packet": "DEFAULT__TELEMETRY__INST__ADCS",\n "buffer": "SkdfjGodkdfjdfoekfsg"\n },\n {\n "time": 1234657585859,\n "packet": "DEFAULT__TELEMETRY__INST__ADCS",\n "buffer": "3i5n49dmnfg9fl32k3"\n }\n]\n'})})]})}function h(e={}){const{wrapper:t}={...(0,i.a)(),...e.components};return t?(0,a.jsx)(t,{...e,children:(0,a.jsx)(l,{...e})}):l(e)}},5392:(e,t,n)=>{n.d(t,{Z:()=>r,a:()=>o});var a=n(2784);const i={},s=a.createContext(i);function o(e){const t=a.useContext(s);return a.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function r(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:o(e.components),a.createElement(s.Provider,{value:t},e.children)}}}]);