-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintersagent.fs
271 lines (242 loc) · 14.1 KB
/
printersagent.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
module PrintersAgent
open System
open System.IO
open System.Runtime.Serialization
open JsonHelper
open MessageLogAgent
open Suave.ZebraWebSocket
type ChannelFrame = Opcode*byte[]*bool
type ChannelAgent = MailboxProcessor<ChannelFrame*bool>
[<DataContract>]
type PrinterApp =
{
[<field: DataMember(Name = "printerSN")>]
printerSN : string;
[<field: DataMember(Name = "realm")>]
realm : string;
[<field: DataMember(Name = "appCode")>]
appCode : string;
}
[<DataContract>]
type Printer =
{
[<field: DataMember(Name = "uniqueID")>]
uniqueID : string;
[<field: DataMember(Name = "productName")>]
productName : string;
[<field: DataMember(Name = "realm")>]
realm : string;
[<field: DataMember(Name = "appVersion")>]
appVersion : string;
[<field: DataMember(Name = "friendlyName")>]
friendlyName : string;
[<field: DataMember(Name = "printerLocation")>]
printerLocation : string;
[<field: DataMember(Name = "sgdSetAlertFeedback")>]
sgdSetAlertProcessor : string; // "none" | "priceTag" | "ifadLabelConversion" | "dhlRFID" | "labelToGo"
[<field: DataMember(Name = "connectedSince")>]
connectedSince : string;
[<field: DataMember(Name = "wlanCertExpDate")>]
wlanCertExpDate : string;
mainChannelAgent : ChannelAgent;
rawChannelAgent : ChannelAgent option;
configChannelAgent : ChannelAgent option;
}
let rec tryFindPrinterData id list =
match list with
| [] -> None
| { printerSN = p; realm = u; appCode = a} :: xs -> if p = id then Some (u, a) else (tryFindPrinterData id xs)
let rec addPrinter id agent list appList =
let someData = tryFindPrinterData id appList
let knownPrinterData =
match someData with
| None -> "public", "none"
| Some x -> x
match list with
| [] -> [{uniqueID = id;
mainChannelAgent = agent;
productName = "";
realm = fst knownPrinterData;
appVersion = "";
connectedSince = DateTime.Now.ToString();
friendlyName = "";
printerLocation = "";
wlanCertExpDate = "";
sgdSetAlertProcessor = snd knownPrinterData;
rawChannelAgent = None;
configChannelAgent = None}]
| printHead :: xs -> if (printHead.uniqueID = id)
then {printHead with mainChannelAgent = agent;
rawChannelAgent = None;
configChannelAgent = None;
realm = fst knownPrinterData
productName = "";
appVersion = "";
wlanCertExpDate = "";
friendlyName = "";
sgdSetAlertProcessor = snd knownPrinterData} :: xs
else (printHead :: addPrinter id agent xs appList)
let rec removePrinter id channel list =
match list with
| [] -> []
| printer :: xs -> if (printer.uniqueID = id && printer.mainChannelAgent = channel)
then xs
else (printer :: removePrinter id channel xs)
let rec updatePartNumber id pn list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with productName = pn} :: xs
else (printer :: updatePartNumber id pn xs)
let rec updateCertExpDate id ce list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with wlanCertExpDate = ce} :: xs
else (printer :: updateCertExpDate id ce xs)
let rec updateAppVersion id ver list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with appVersion = ver} :: xs
else (printer :: updateAppVersion id ver xs)
let rec updateFriendlyName id friendlyName list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with friendlyName = friendlyName} :: xs
else (printer :: updateFriendlyName id friendlyName xs)
let rec updateApp id appname list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with sgdSetAlertProcessor = appname} :: xs
else (printer :: updateApp id appname xs)
let rec updateRawChannel id agent list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with rawChannelAgent = agent} :: xs
else (printer :: updateRawChannel id agent xs)
let rec clearRawChannel id agent list =
match list with
| [] -> []
| printer :: xs -> if (printer.uniqueID = id && printer.rawChannelAgent = agent)
then {printer with rawChannelAgent = None} :: xs
else (printer :: clearRawChannel id agent xs)
let rec updateConfigChannel id agent list =
match list with
| [] -> []
| printer :: xs -> if printer.uniqueID = id
then {printer with configChannelAgent = agent} :: xs
else (printer :: updateConfigChannel id agent xs)
let rec clearConfigChannel id agent list =
match list with
| [] -> []
| printer :: xs -> if (printer.uniqueID = id && printer.configChannelAgent = agent)
then {printer with configChannelAgent = None} :: xs
else (printer :: clearConfigChannel id agent xs)
let isKnownID id list = List.exists (fun printer -> printer.uniqueID = id) list
let tryFindPrinter id list = List.tryFind (fun (prt:Printer) -> prt.uniqueID = id) list
type PrintersAgentMsg =
| Exit
| Clear
| AddPrinter of string * ChannelAgent
| UpdateRawChannel of string * (ChannelAgent option)
| UpdateConfigChannel of string * (ChannelAgent option)
| RemovePrinter of string * ChannelAgent
| ClearRawChannel of string * (ChannelAgent option)
| ClearConfigChannel of string * (ChannelAgent option)
| UpdatePartNumber of string * string
| UpdateCertDate of string * string
| UpdateAppVersion of string * string
| UpdateFriendlyName of string * string
| IsKnownID of string * AsyncReplyChannel<Boolean>
| PrintersInventory of Boolean * AsyncReplyChannel<String>
| PrintersDefault of AsyncReplyChannel<String>
| FetchPrinterInfo of string * AsyncReplyChannel<Printer Option>
| SendMsgOverMainChannel of string * ChannelFrame * bool
| SendMsgOverRawChannel of string * ChannelFrame * bool
| SendMsgOverConfigChannel of string * ChannelFrame * bool
| UpdateApp of string * string
[<DataContract>]
type ConnectedPrinters =
{ [<field: DataMember(Name = "connectedPrinters")>] PrinterList : Printer list }
static member Empty = {PrinterList = [] }
type PrintersAgent(logAgent:LogAgent) =
let storeAgentMailboxProcessor =
MailboxProcessor.Start(fun inbox ->
let rec printersAgentLoop connPrts printAppList =
async {
let! msg = inbox.Receive()
// logAgent.AppendToLog (sprintf "Printersagent: message received %A" msg )
match msg with
| Exit -> return ()
| Clear -> return! printersAgentLoop ConnectedPrinters.Empty printAppList
| AddPrinter (id,chan) -> return! printersAgentLoop ({ PrinterList = addPrinter id chan connPrts.PrinterList printAppList}) printAppList
| UpdateRawChannel (id,chan) -> return! printersAgentLoop ({ PrinterList = updateRawChannel id chan connPrts.PrinterList}) printAppList
| UpdateConfigChannel (id,chan) -> return! printersAgentLoop ({ PrinterList = updateConfigChannel id chan connPrts.PrinterList}) printAppList
| RemovePrinter (id,chan) -> return! printersAgentLoop ({ PrinterList = removePrinter id chan connPrts.PrinterList}) printAppList
| ClearRawChannel (id,chan) -> return! printersAgentLoop ({ PrinterList = clearRawChannel id chan connPrts.PrinterList}) printAppList
| ClearConfigChannel (id,chan) -> return! printersAgentLoop ({ PrinterList = clearConfigChannel id chan connPrts.PrinterList}) printAppList
| UpdatePartNumber (id,pn) -> return! printersAgentLoop ({ PrinterList = updatePartNumber id pn connPrts.PrinterList}) printAppList
| UpdateCertDate (id,ce) -> return! printersAgentLoop ({ PrinterList = updateCertExpDate id ce connPrts.PrinterList}) printAppList
| UpdateAppVersion (id,ver) -> return! printersAgentLoop ({ PrinterList = updateAppVersion id ver connPrts.PrinterList}) printAppList
| UpdateFriendlyName (id,fName) -> return! printersAgentLoop ({ PrinterList = updateFriendlyName id fName connPrts.PrinterList}) printAppList
| UpdateApp (id,appname) -> return! printersAgentLoop ({ PrinterList = updateApp id appname connPrts.PrinterList}) printAppList
| PrintersInventory (includePrivatePrinters, replyChannel) ->
// logAgent.AppendToLog (sprintf "Printersagent: inside PrintersInventory printerList: %A" connPrts.PrinterList )
replyChannel.Reply (json<Printer array> (List.toArray (List.filter (fun dp -> includePrivatePrinters || dp.realm = "public") connPrts.PrinterList) ))
return! printersAgentLoop connPrts printAppList
| PrintersDefault replyChannel ->
replyChannel.Reply (json<PrinterApp array> (List.toArray printAppList))
return! printersAgentLoop connPrts printAppList
| IsKnownID (id, replyChannel) ->
replyChannel.Reply (isKnownID id connPrts.PrinterList)
return! printersAgentLoop connPrts printAppList
| FetchPrinterInfo (id, replyChannel) ->
replyChannel.Reply (tryFindPrinter id connPrts.PrinterList)
return! printersAgentLoop connPrts printAppList
| SendMsgOverMainChannel (id, frame, toLog) ->
match tryFindPrinter id connPrts.PrinterList with
| None -> ()
| Some prt -> prt.mainChannelAgent.Post (frame ,toLog)
return! printersAgentLoop connPrts printAppList
| SendMsgOverRawChannel (id, frame, toLog) ->
match tryFindPrinter id connPrts.PrinterList with
| None -> ()
| Some prt -> match prt.rawChannelAgent with
| None -> ()
| Some chanAgent -> chanAgent.Post (frame ,toLog)
return! printersAgentLoop connPrts printAppList
| SendMsgOverConfigChannel (id, frame, toLog) ->
match tryFindPrinter id connPrts.PrinterList with
| None -> ()
| Some prt -> match prt.configChannelAgent with
| None -> ()
| Some chanAgent -> chanAgent.Post (frame ,toLog)
return! printersAgentLoop connPrts printAppList
}
let jsonStr =File.ReadAllText(Path.GetFullPath "./json/knownprinters.json")
printersAgentLoop ConnectedPrinters.Empty (Array.toList (unjson<PrinterApp array> jsonStr))
)
member this.Exit() = storeAgentMailboxProcessor.Post(Exit)
member this.Empty() = storeAgentMailboxProcessor.Post(Clear)
member this.AddPrinter id chan = storeAgentMailboxProcessor.Post(AddPrinter (id, chan))
member this.UpdateRawChannel id chan = storeAgentMailboxProcessor.Post(UpdateRawChannel (id, chan))
member this.UpdateConfigChannel id chan = storeAgentMailboxProcessor.Post(UpdateConfigChannel (id, chan))
member this.RemovePrinter id chan = storeAgentMailboxProcessor.Post(RemovePrinter (id, chan))
member this.ClearRawChannel id chan = storeAgentMailboxProcessor.Post(ClearRawChannel (id, chan))
member this.ClearConfigChannel id chan = storeAgentMailboxProcessor.Post(ClearConfigChannel (id, chan))
member this.UpdatePartNumber id pn = storeAgentMailboxProcessor.Post(UpdatePartNumber (id,pn))
member this.UpdateCertExpDate id ce = storeAgentMailboxProcessor.Post(UpdateCertDate (id,ce))
member this.UpdateAppVersion id ver = storeAgentMailboxProcessor.Post(UpdateAppVersion (id,ver))
member this.UpdateFriendlyName id ver = storeAgentMailboxProcessor.Post(UpdateFriendlyName (id,ver))
member this.PrintersInventory includePrivatePrinters = storeAgentMailboxProcessor.PostAndReply((fun reply -> PrintersInventory (includePrivatePrinters, reply) ), timeout = 2000)
member this.PrintersDefault() = storeAgentMailboxProcessor.PostAndReply((fun reply -> PrintersDefault reply), timeout = 2000)
member this.IsKnownID sku = storeAgentMailboxProcessor.PostAndReply((fun reply -> IsKnownID(sku,reply)), timeout = 2000)
member this.FetchPrinterInfo id = storeAgentMailboxProcessor.PostAndReply((fun reply -> FetchPrinterInfo(id,reply)), timeout = 2000)
member this.SendMsgOverMainChannel id frame toLog = storeAgentMailboxProcessor.Post(SendMsgOverMainChannel (id,frame,toLog))
member this.SendMsgOverRawChannel id frame toLog = storeAgentMailboxProcessor.Post(SendMsgOverRawChannel (id,frame,toLog))
member this.SendMsgOverConfigChannel id frame toLog = storeAgentMailboxProcessor.Post(SendMsgOverConfigChannel (id,frame,toLog))
member this.UpdateApp id appname = storeAgentMailboxProcessor.Post(UpdateApp (id,appname))