Created
January 27, 2020 13:36
-
-
Save fvilante/655b95a832222ad8caac35a912c405c1 to your computer and use it in GitHub Desktop.
Datalink generic protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ZIO, ZIO_ } from "../zio/zio" | |
// STREAM | |
// async steam | |
type Stream<E,A> = { | |
readonly kind: 'Stream' | |
readonly identity: () => Stream<E,A> | |
readonly take: (n: number) => Stream<E,readonly A[]> | |
readonly map: <B>(f: (_:A) => B) => Stream<E,B> | |
readonly mapError: <E1>(f: (_:E) => E1) => Stream<E1,A> | |
} | |
const Stream = <R,E,A>(zio: ZIO<R,E,A>):Stream<E,A> => { | |
type H = Stream<E,A> | |
const identity: H['identity'] = () => Stream(zio) | |
const take: H['take'] = n => { | |
const z = ZIO_.fromSync( env => { | |
}) | |
} | |
return { | |
kind: 'Stream', | |
identity, | |
take, | |
map, | |
} | |
} | |
// GENERIC PROTOCOL ANALYSER MODEL | |
type GenericCommand<A> = { | |
readonly shortName: string //you can introduce more commands here | |
readonly symbols: readonly A[] //CAUTION: Do not repeat symbols (unsafe) | |
readonly parameterLength: number //NOTE: 8 bits number which represents number of symbols 'A' to represent the parameter //Todo: Use type 'Byte' instead of 'number' | |
} | |
type ProtocolDefinition<A> = { | |
readonly cmdMarker: readonly A[] | |
readonly cmds: readonly (GenericCommand<A>)[] | |
} | |
// Posijet1 in terms of bytes | |
const Posijet1_ProtocolDefinition: ProtocolDefinition<number> = { | |
cmdMarker: [27], | |
cmds: [ | |
{ shortName: 'STX', symbols: [2] , parameterLength: 0}, | |
{ shortName: 'ACK', symbols: [6] , parameterLength: 0}, | |
{ shortName: 'NACK', symbols: [21] , parameterLength: 0}, | |
{ shortName: 'ETX', symbols: [3] , parameterLength: 1}, | |
] | |
} | |
type UnknownData<A> = { | |
readonly kind: 'UnknownData', | |
readonly data: readonly A[] | |
} | |
type UnrecognizedCommand<A> = { | |
readonly kind: 'UnrecognizedCommand' | |
readonly data: readonly A[] | |
} | |
type ComandWithoutParameter<A> = { | |
readonly kind: 'ComandWithoutParameter' | |
readonly data: readonly A[] | |
} | |
type CommandWithFixedLengthParameter<A> = { | |
readonly kind: 'CommandWithFixedLengthParameter' | |
readonly data: readonly A[] | |
} | |
type Output<A> = | |
| UnknownData<A> | |
| UnrecognizedCommand<A> | |
| ComandWithoutParameter<A> | |
| CommandWithFixedLengthParameter<A> | |
const Output = <A>(kind: Output<A>['kind'], data: readonly A[]): Output<A> => ({kind, data}) | |
type ProtocolState = { | |
readonly state: 'waiting command' | 'waiting parameter' | |
} | |
// Algorithm | |
type StateProcessor = (state: ProtocolState, stream: Stream<A>) => readonly [ProtocolState, Output<A>] | |
const Main: StateProcessor = (state, stream) => { | |
const currentState = state.state | |
switch (currentState) { | |
case 'waiting command': | |
return Watiting_Command(state,stream) | |
case 'waiting parameter': | |
return Waiting_Parameter(state, stream) | |
} | |
} | |
const Watiting_Command: StateProcessor = (state, stream) => { | |
} | |
const Waiting_Parameter: StateProcessor = (state, stream) => { } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment