Created
July 16, 2014 14:44
-
-
Save smurphy8/0d732189e715f2479e6f to your computer and use it in GitHub Desktop.
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
| {-# LANGUAGE NoImplicitPrelude, OverloadedStrings, RankNTypes, RecordWildCards, TemplateHaskell #-} | |
| module SingleWellSpec (main, spec) where | |
| import Test.Hspec | |
| import Data.Conduit.Cereal | |
| import Data.Conduit.Network | |
| import Data.Serialize | |
| import Conduit | |
| import Data.Modbus | |
| import qualified Data.ByteString.Lazy as BL | |
| import qualified Data.Text.Encoding as TE | |
| import Data.Text | |
| import Data.Time | |
| import BasicPrelude | |
| import Well.Poll | |
| import Well.Types | |
| main :: IO () | |
| main = hspec spec | |
| spec :: Spec | |
| spec = do | |
| describe "someFunction" $ do | |
| it "should work fine" $ do | |
| True `shouldBe` False | |
| testReadTCPClient = runTCPClient (clientSettings 20090 (TE.encodeUtf8 $ url )) $ testRequest | |
| where | |
| testRequest server = do | |
| sourceModRequestRead $$ appSink server | |
| appSource server $$ sinkGet modReqGetter | |
| -- sourceModRequestWrite $$ appSink server | |
| -- appSource server $$ sinkGet modReqGetter | |
| testSlaveId :: SlaveId | |
| testSlaveId = 1 | |
| readTarget = 1 | |
| testWellPoll = pollWell $ testReadSW | |
| {- | | |
| data SingleWell = SingleWell { | |
| locationId :: Int | |
| , wellUrl :: Text | |
| , wellPort :: Maybe Int | |
| , slaveId :: Word8 | |
| , pollTime :: Int -- Seconds | |
| , wellParams :: [SingleWellParam] | |
| , lastUpdate :: Int | |
| , wellProt :: Protocol | |
| } deriving (Eq,Show,Generic, Typeable) | |
| data SingleWellParam = SingleWellParam { | |
| paramType :: ParamType | |
| , writeable :: Bool | |
| , val :: Text | |
| , time :: Int | |
| , pid :: Int | |
| , slaveParamId :: Word16 | |
| |-} | |
| testReadSW = SingleWell 5 url Nothing 1 60 testSWPList 17 ModBusRTU | |
| --testReadSWP1 = SingleWellParam Coils True (pack $ "5") 6 11 1 | |
| --testReadSWP2 = SingleWellParam InputRegister False (pack $ "6") 5 11 1 | |
| --testReadSWP3 = SingleWellParam HoldingRegister True (pack $ "7") 6 11 3 | |
| --testReadSWP4 = SingleWellParam DiscreteInputs False (pack $ "25") 5 11 14 | |
| testReadSWP0 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 0 | |
| testReadSWP1 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 1 | |
| testReadSWP2 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 2 | |
| testReadSWP3 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 3 | |
| testReadSWP4 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 4 | |
| testReadSWP5 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 5 | |
| testReadSWP6 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 6 | |
| testReadSWP7 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 7 | |
| testReadSWP8 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 8 | |
| testReadSWP9 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 9 | |
| testReadSWP10 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 10 | |
| testReadSWP11 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 11 | |
| testReadSWP12 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 12 | |
| testReadSWP13 = SingleWellParam DiscreteInputs True (pack $ "7") 6 11 13 | |
| testSWPList = [testReadSWP0,testReadSWP1,testReadSWP2,testReadSWP3,testReadSWP4,testReadSWP5,testReadSWP6,testReadSWP7,testReadSWP8,testReadSWP9,testReadSWP10,testReadSWP11,testReadSWP12,testReadSWP13] | |
| -- writeTarget = 0 | |
| -- writeThing :: Word16 | |
| -- writeThing = 7569 | |
| sourceModRequestRead = sourceLazy $ BL.fromStrict.runPut.put $ ModRequestFrame testSlaveId (ReadHoldingRegisters readTarget 2) | |
| -- --sourceModRequestWrite = sourceLazy $ BL.fromStrict.runPut.put $ ModRequestFrame testSlaveId (WriteSingleRegister writeTarget writeThing) | |
| -- converter :: Get [Word8] | |
| -- converter = do | |
| -- i <- remaining | |
| -- b <- getBytes i | |
| -- return . B.unpack $ b | |
| modReqGetter :: Get ByteString -- ModResponseFrame | |
| modReqGetter = getByteString 1 | |
| url :: Text | |
| url = pack $ "166.131.38.10" | |
| -- printAsNumbers bs = showHex <$> B.unpack bs <*> [""] | |
| -- testGetModResponseFcn :: Get ModResponse | |
| -- testGetModResponseFcn = get | |
| -- testResponseRawSimple = B.pack [0x83,0x02] | |
| -- testResponseRaw = B.pack [0x0b,0x83,0x02,0xe0,0xf3] | |
| -- expectedRequestCrc = [0x85,0xde] | |
| -- n :: Int16 | |
| -- n = 47 | |
| -- t :: B.ByteString | |
| -- t = BL.toStrict $ BB.toLazyByteString (BB.int16BE n) | |
| testSingleWellOne :: SingleWell | |
| testSingleWellOne = SingleWell {locationId = 968, wellUrl = "166.131.37.197", wellPort = Just 20090, slaveId = 1, pollTime = 60, wellParams = [SingleWellParam {paramType = InputRegister, writeable = True, val = "N/A", time = 1398190426, slaveParamId = 0}], lastUpdate = 0} | |
| tgt = [SingleWellParam {paramType = HoldingRegister, writeable = True, val = "160", time = 1398190426, slaveParamId = 15 ,pid=0}] | |
| testWriteToWellOne = testSingleWellOne{wellParams=tgt} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment