Main.hs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE DeriveGeneric #-}
  3. module Main where
  4. import Lib
  5. import Web.Scotty
  6. import qualified Text.Blaze.Html5 as BlazeHtml
  7. import qualified Text.Blaze.Html5.Attributes as BlazeAttributes
  8. import qualified Text.Blaze.Html.Renderer.Text as BlazeRenderer
  9. import Network.Wai.Middleware.RequestLogger -- Logging
  10. import Data.Monoid (mconcat)
  11. import System.Random(randomIO)
  12. import System.Process
  13. import System.IO
  14. import Control.Concurrent(threadDelay)
  15. import Network.Socket(connect, close, SocketType(Stream), socket)
  16. import Network.Socket.Internal(Family(AF_UNIX), SockAddr(SockAddrUnix))
  17. import Network.Socket.ByteString(send, recv)
  18. import Data.ByteString(breakSubstring)
  19. import Data.Aeson
  20. import GHC.Generics
  21. import Data.Text
  22. import qualified Data.Vector as V
  23. import qualified Data.ByteString.Lazy as DBSL
  24. import qualified Data.ByteString as DBS
  25. data Command = Command
  26. { command :: Array
  27. } deriving (Generic, Show)
  28. instance ToJSON Command where
  29. toJSON (Command command) =
  30. object ["command" .= command]
  31. -- this encodes directly to a bytestring Builder
  32. toEncoding (Command command) =
  33. pairs ("command" .= command)
  34. mpvSocket :: String
  35. mpvSocket = "/tmp/haskell-jukebox-mpv-socket"
  36. main :: IO ()
  37. main = do
  38. putStrLn ("MPV listening at " ++ mpvSocket)
  39. (_, _, _, p) <- createProcess (proc "mpv"
  40. [ "--input-ipc-server=" ++ mpvSocket
  41. , "/home/josh/shrimp.mp3"
  42. ]) { std_in = NoStream
  43. , std_out = NoStream
  44. , std_err = NoStream}
  45. threadDelay 2000000
  46. putStrLn "sending pause"
  47. togglePause
  48. threadDelay 2000000
  49. putStrLn "sending pause"
  50. togglePause
  51. waitForProcess p
  52. return ()
  53. togglePause :: IO ()
  54. togglePause = do
  55. received <- sendToMPV Command { command = V.fromList
  56. [ "get_property"
  57. , "pause"
  58. ]}
  59. sendToMPV Command { command = V.fromList
  60. [ "set_property"
  61. , "pause"
  62. , case snd $ breakSubstring "true" received of
  63. "" -> Bool True
  64. otherwise -> Bool False
  65. ]}
  66. return ()
  67. sendToMPV :: Command -> IO DBS.ByteString
  68. sendToMPV c = do
  69. putStrLn ("Connected to Socket: " ++ mpvSocket)
  70. soc <- socket AF_UNIX Stream 0
  71. connect soc (SockAddrUnix mpvSocket)
  72. send soc toSend
  73. putStrLn "Sending: "
  74. print toSend
  75. received <- recv soc 4096
  76. putStrLn ("Received from MPV: " ++ (show received))
  77. close soc
  78. return received
  79. where toSend = (DBSL.toStrict ((encode (c)) `DBSL.append` "\n"))
  80. oldmain :: IO ()
  81. oldmain = scotty 3000 $ do
  82. middleware logStdoutDev -- Logging
  83. get "/say/:word" $ do
  84. beam <- param "word"
  85. if beam == "" then
  86. html "<h1>Go to a URI, such as http://URL/say/beam.</h1>"
  87. else
  88. html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]
  89. get "/hello" $ do -- Access this with /hello?name=yee
  90. name <- param "name"
  91. text name
  92. get "/agent" $ do
  93. agent <- header "User-Agent"
  94. text $ maybe "Couldn't determine user agent." id agent
  95. get "/" $ do
  96. html . BlazeRenderer.renderHtml $ do
  97. BlazeHtml.h1 "This is blaze!"