-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontierSiliconAPI.pas
5161 lines (4674 loc) · 142 KB
/
FrontierSiliconAPI.pas
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit FrontierSiliconAPI;
{$mode objfpc}{$H+}
{$MACRO ON}
{____________________________________________________________
| _______________________________________________________ |
| | | |
| | Remote API for Frontier Silicon based devices | |
| | (c) 2019 Alexander Feuster (alexander.feuster@web.de) | |
| | https://www.github.com/feuster | |
| |_______________________________________________________| |
|___________________________________________________________}
//define API basics
{$DEFINE APIVERSION:='2.9'}
{$DEFINE INDY10}
//{$DEFINE FSAPI_DEBUG}
{___________________________________________________________}
interface
uses
Classes, SysUtils, StrUtils, DateUtils, LazUTF8, fphttpclient, XMLRead, DOM
{$IFDEF LCL}, Forms{$IFDEF FSAPI_DEBUG}, Dialogs{$ENDIF}{$ENDIF}
{$IFDEF INDY10},IdUDPClient, IdStack, IdHTTP{$ENDIF}
;
function fsapi_HexStrToStr(Value: String): String;
function fsapi_CheckStatus(XMLBuffer: String): String;
function fsapi_HTTP(COMMAND: String): String;
function fsapi_CreateSession(URL: String; PIN: String; const ReUseSessionID: Boolean = false): String;
function fsapi_DeleteSession(URL: String; PIN: String): Boolean;
function fsapi_RAW(URL: String; PIN: String; COMMAND: String; const MODE_SET: Boolean = false; const MODE_SET_VALUE: String = ''): String;
function fsapi_RAW_URL(URL: String): String;
function fsapi_LIST_GET_NEXT(URL: String; PIN: String; COMMAND: String; const MAXITEMS: Integer = 10): String;
function fsapi_FACTORY_RESET(URL: String; PIN: String): Boolean;
function fsapi_Info_ImageVersion(URL: String; PIN: String): String;
function fsapi_Info_GetNotifies(URL: String; PIN: String): TStringList;
function fsapi_Info_FriendlyName(URL: String; PIN: String): String;
function fsapi_Info_SetFriendlyName(URL: String; PIN: String; NewName: String): Boolean;
function fsapi_Info_RadioID(URL: String; PIN: String): String;
function fsapi_Info_Artist(URL: String; PIN: String): String;
function fsapi_Info_Name(URL: String; PIN: String): String;
function fsapi_Info_Text(URL: String; PIN: String): String;
function fsapi_Info_graphicUri(URL: String; PIN: String): String;
function fsapi_Info_Duration(URL: String; PIN: String): Integer;
function fsapi_Info_Position(URL: String; PIN: String): Integer;
function fsapi_Info_PlayInfo(URL: String; PIN: String): String;
function fsapi_Info_PlayStatus(URL: String; PIN: String): Byte;
function fsapi_Info_PlayError(URL: String; PIN: String): String;
function fsapi_Info_Time(URL: String; PIN: String): String;
function fsapi_Info_Date(URL: String; PIN: String): String;
function fsapi_Info_DateTime(URL: String; PIN: String): String;
function fsapi_Info_DeviceList(TimeoutMS: Integer; const Icon_URL: Boolean = false): TStringList;
function fsapi_Info_DeviceIcon(DescriptionURL: String; DescriptionXML: String): String;
function fsapi_Info_DeviceIconStreamDownload(IconURL: String): TMemoryStream;
function fsapi_Info_DeviceFileDownload(FileURL: String; LocalFile: String; const Overwrite: Boolean = true): Boolean;
function fsapi_Info_DeviceUpdateInfo(URL: String; PIN: String): String;
function fsapi_Info_DeviceID(URL: String; PIN: String): String;
function fsapi_Info_LAN_MAC(URL: String; PIN: String): String;
function fsapi_Info_WLAN_MAC(URL: String; PIN: String): String;
function fsapi_Info_WLAN_RSSI(URL: String; PIN: String): String;
function fsapi_Info_WLAN_ConnectedSSID(URL: String; PIN: String): String;
function fsapi_Info_LAN_Enabled(URL: String; PIN: String): Boolean;
function fsapi_Info_WLAN_Enabled(URL: String; PIN: String): Boolean;
function fsapi_Info_Network_Interfaces(URL: String; PIN: String): String;
function fsapi_FM_FreqRange_LowerCap(URL: String; PIN: String): Integer;
function fsapi_FM_FreqRange_UpperCap(URL: String; PIN: String): Integer;
function fsapi_FM_FreqRange_Steps(URL: String; PIN: String): Integer;
function fsapi_FM_SetFrequency(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_FM_GetFrequency(URL: String; PIN: String): Integer;
function fsapi_Modes_Get(URL: String; PIN: String): TStringList;
function fsapi_Modes_Set(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Modes_GetModeByIdLabel(URL: String; PIN: String; Value: String): Integer;
function fsapi_Modes_GetModeLabelById(URL: String; PIN: String; Mode: Integer): String;
function fsapi_Modes_SetModeByAlias(URL: String; PIN: String; Value: String): Boolean;
function fsapi_Modes_ActiveMode(URL: String; PIN: String): Integer;
function fsapi_Modes_ActiveModeLabel(URL: String; PIN: String): String;
function fsapi_Modes_CheckModeAlias(Mode: String): String;
function fsapi_Modes_NextMode(URL: String; PIN: String): Boolean;
function fsapi_Modes_PreviousMode(URL: String; PIN: String): Boolean;
function fsapi_PlayControl_PlayPause(URL: String; PIN: String): Boolean;
function fsapi_PlayControl_Play(URL: String; PIN: String): Boolean;
function fsapi_PlayControl_Pause(URL: String; PIN: String): Boolean;
function fsapi_PlayControl_Next(URL: String; PIN: String): Boolean;
function fsapi_PlayControl_Previous(URL: String; PIN: String): Boolean;
function fsapi_PlayControl_Value(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_PlayControl_SetPositionMS(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_PlayControl_SetPosition(URL: String; PIN: String; Value: String): Boolean;
function fsapi_Mute_State(URL: String; PIN: String): Boolean;
function fsapi_Mute(URL: String; PIN: String): Boolean;
function fsapi_Mute_Off(URL: String; PIN: String): Boolean;
function fsapi_Mute_On(URL: String; PIN: String): Boolean;
function fsapi_Mute_SetState(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Repeat_State(URL: String; PIN: String): Boolean;
function fsapi_Repeat(URL: String; PIN: String): Boolean;
function fsapi_Repeat_On(URL: String; PIN: String): Boolean;
function fsapi_Repeat_Off(URL: String; PIN: String): Boolean;
function fsapi_Repeat_SetState(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Shuffle_State(URL: String; PIN: String): Boolean;
function fsapi_Shuffle(URL: String; PIN: String): Boolean;
function fsapi_Shuffle_On(URL: String; PIN: String): Boolean;
function fsapi_Shuffle_Off(URL: String; PIN: String): Boolean;
function fsapi_Shuffle_SetState(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Scrobble_State(URL: String; PIN: String): Boolean;
function fsapi_Scrobble(URL: String; PIN: String): Boolean;
function fsapi_Scrobble_On(URL: String; PIN: String): Boolean;
function fsapi_Scrobble_Off(URL: String; PIN: String): Boolean;
function fsapi_Scrobble_SetState(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Standby_State(URL: String; PIN: String): Boolean;
function fsapi_Standby(URL: String; PIN: String): Boolean;
function fsapi_Standby_Off(URL: String; PIN: String): Boolean;
function fsapi_Standby_On(URL: String; PIN: String): Boolean;
function fsapi_Standby_SetState(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Volume_Get(URL: String; PIN: String): Byte;
function fsapi_Volume_GetSteps(URL: String; PIN: String): Byte;
function fsapi_Volume_Set(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Volume_Up(URL: String; PIN: String): Boolean;
function fsapi_Volume_Down(URL: String; PIN: String): Boolean;
function fsapi_DAB_Scan(URL: String; PIN: String): Boolean;
function fsapi_DAB_Prune(URL: String; PIN: String): Boolean;
function fsapi_DAB_FrequencyList(URL: String; PIN: String): TStringList;
function fsapi_Presets_List(URL: String; PIN: String): TStringList;
function fsapi_Presets_Set(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Presets_Add(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Presets_NextPreset(URL: String; PIN: String): Boolean;
function fsapi_Presets_PreviousPreset(URL: String; PIN: String): Boolean;
function fsapi_Nav_State(URL: String; PIN: String; STATE: Boolean): Boolean;
function fsapi_Nav_List(URL: String; PIN: String): TStringList;
function fsapi_Nav_Navigate(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Nav_SelectItem(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Nav_NavigateSelectItem(URL: String; PIN: String; Value: Integer; NavType: Integer): Boolean;
function fsapi_Nav_Caps(URL: String; PIN: String): Integer;
function fsapi_Nav_NumItems(URL: String; PIN: String): Integer;
function fsapi_Nav_Status(URL: String; PIN: String): Integer;
function fsapi_Sys_State(URL: String; PIN: String): Integer;
function fsapi_Sys_SetSleepTimer(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Sys_GetSleepTimer(URL: String; PIN: String): Integer;
function fsapi_Equalizer_Presets_List(URL: String; PIN: String): TStringList;
function fsapi_Equalizer_Bands_List(URL: String; PIN: String): TStringList;
function fsapi_Equalizer_Get(URL: String; PIN: String): Byte;
function fsapi_Equalizer_Set(URL: String; PIN: String; Value: Byte): Boolean;
function fsapi_Equalizer_CustomParam0_Get(URL: String; PIN: String): Integer;
function fsapi_Equalizer_CustomParam0_Set(URL: String; PIN: String; Value: Integer): Boolean;
function fsapi_Equalizer_CustomParam1_Get(URL: String; PIN: String): Integer;
function fsapi_Equalizer_CustomParam1_Set(URL: String; PIN: String; Value: Integer): Boolean;
var
fsapi_SessionID: String;
fsapi_ReUseSessionID: Boolean = false;
{$IFDEF FSAPI_DEBUG}
//storage variable for last debug message
fsapi_Debug_NoShowMessage: Boolean;
fsapi_Debug_Message: String;
{$ENDIF}
const
API_Version: String = APIVERSION;
{$IFDEF FSAPI_DEBUG}
//Debug message strings
STR_Error: String = 'Debug: Error: ';
STR_Info: String = 'Debug: Info: ';
STR_Space: String = ' ';
//Define DEBUG constant
FSAPI_DEBUG: Boolean = true;
{$ELSE}
FSAPI_DEBUG: Boolean = false;
{$ENDIF}
{$IFDEF INDY10}
API_License: String = '--------------------------------------------------------------------------------'+#13#10#13#10+
'Frontier Silicon API V'+APIVERSION+' (c) 2018 Alexander Feuster (alexander.feuster@web.de)'+#13#10+
'http://www.github.com/feuster'+#13#10+
'This API is provided "as-is" without any warranties for any data loss,'+#13#10+
'device defects etc. Use at own risk!'+#13#10+
'Free for personal use. Commercial use is prohibited without permission.'+#13#10#13#10+
'--------------------------------------------------------------------------------'+#13#10#13#10+
'Indy BSD License'+#13#10+
#13#10+
'Copyright'+#13#10+
#13#10+
'Portions of this software are Copyright (c) 1993 - 2003, Chad Z. Hower (Kudzu)'+#13#10+
'and the Indy Pit Crew - http://www.IndyProject.org/'+#13#10+
#13#10+
'License'+#13#10+
#13#10+
'Redistribution and use in source and binary forms, with or without modification,'+#13#10+
'are permitted provided that the following conditions are met: Redistributions'+#13#10+
'of source code must retain the above copyright notice, this list of conditions'+#13#10+
'and the following disclaimer.'+#13#10+
#13#10+
'Redistributions in binary form must reproduce the above copyright notice, this'+#13#10+
'list of conditions and the following disclaimer in the documentation, about box'+#13#10+
'and/or other materials provided with the distribution.'+#13#10+
#13#10+
'No personal names or organizations names associated with the Indy project may'+#13#10+
'be used to endorse or promote products derived from this software without'+#13#10+
'specific prior written permission of the specific individual or organization.'+#13#10+
#13#10+
'THIS SOFTWARE IS PROVIDED BY Chad Z. Hower (Kudzu) and the Indy Pit Crew "AS'+#13#10+
'IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE'+#13#10+
'IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE'+#13#10+
'DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY'+#13#10+
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES'+#13#10+
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;'+#13#10+
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON'+#13#10+
'ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT'+#13#10+
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS'+#13#10+
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.'+#13#10#13#10+
'-------------------------[scroll up for full License]--------------------------'+#13#10#13#10;
{$ELSE}
API_License: String = 'Frontier Silicon API V'+APIVERSION+' (c) 2018 Alexander Feuster (alexander.feuster@web.de)'+#13#10+
'http://www.github.com/feuster'+#13#10+
'This API is provided "as-is" without any warranties for any data loss, device defects etc.'+#13#10+
'Use at own risk!'+#13#10+
'Free for personal use. Commercial use is prohibited without permission.';
{$ENDIF}
implementation
{$IFDEF FSAPI_DEBUG}
procedure DebugPrint(DebugText: String);
//generate debug messages for Console or GUI application
begin
fsapi_Debug_Message:=fsapi_Debug_Message+#13#10+DebugText;
{$IFNDEF LCL}
WriteLn(fsapi_Debug_Message);
{$ELSE}
if fsapi_Debug_NoShowMessage=false then
ShowMessage(fsapi_Debug_Message);
{$ENDIF}
end;
{$ENDIF}
//------------------------------------------------------------------------------
// Helper functions
//------------------------------------------------------------------------------
function fsapi_HexStrToStr(Value: String): String;
//convert HEX coded string into regular ASCII string
var
Buffer: String;
Hex: String;
Index: Integer;
begin
try
Index:=1;
Buffer:='';
while Index<=Length(Value)-1 do
begin
Hex:=Value[Index]+Value[Index+1];
Buffer:=Buffer+Chr(Hex2Dec(Hex));
Index:=Index+2;
end;
Result:=Buffer;
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_HexStrToStr -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Check Status
//------------------------------------------------------------------------------
function fsapi_CheckStatus(XMLBuffer: String): String;
//Check Status from buffer
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: TStringStream;
begin
try
Buffer:=TStringStream.Create(XMLBuffer);
ReadXMLFile(XML, Buffer);
Node:=XML.DocumentElement.FindNode('status');
if Node<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_CheckStatus -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// HTTP Command
//------------------------------------------------------------------------------
{$IFDEF INDY10}
function fsapi_HTTP(COMMAND: String): String;
//Send an API command via HTTP to device
var
HTTPClient: TIdHTTP;
begin
try
if Command='' then
begin
Result:='';
exit;
end;
if LeftStr(LowerCase(Command),7)<>'http://' then
Command:='http://'+Command;
if LeftStr(LowerCase(Command),7)<>'http://' then
begin
{$IFDEF FSAPI_DEBUG}DebugPrint(STR_Error+'fsapi_HTTP -> not allowed command "'+Command+'"');{$ENDIF}
Result:='';
exit;
end;
HTTPClient:=TIdHTTP.Create(NIL);
{$IFDEF FSAPI_DEBUG}{$IFDEF LCL}DebugPrint(Trim(STR_Info+'fsapi_HTTP -> '+Command));{$ENDIF}{$ENDIF}
Result:=HTTPClient.Get(COMMAND);
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_HTTP -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
if Pos('404',E.Message)>0 then
begin
{$IFDEF FSAPI_DEBUG}DebugPrint(STR_Error+'fsapi_HTTP -> error 404: clearing stored session ID');{$ENDIF}
fsapi_SessionID:='';
end;
Result:='';
if HTTPClient<>NIL then HTTPClient.Free;
end;
end;
if HTTPClient<>NIL then HTTPClient.Free;
end;
{$ELSE}
function fsapi_HTTP(COMMAND: String): String;
//Send an API command via HTTP to device
var
HTTPClient: TFPCustomHTTPClient;
begin
try
if Command='' then
begin
Result:='';
exit;
end;
if LeftStr(LowerCase(Command),7)<>'http://' then
Command:='http://'+Command;
if LeftStr(LowerCase(Command),7)<>'http://' then
begin
{$IFDEF FSAPI_DEBUG}DebugPrint(STR_Error+'fsapi_HTTP -> not allowed command "'+Command+'"');{$ENDIF}
Result:='';
exit;
end;
HTTPClient:=TFPCustomHTTPClient.Create(NIL);
{$IFDEF FSAPI_DEBUG}{$IFDEF LCL}DebugPrint(Trim(STR_Info+'fsapi_HTTP -> '+Command));{$ENDIF}{$ENDIF}
Result:=HTTPClient.SimpleGet(COMMAND);
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_HTTP -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
if Pos('404',E.Message)>0 then
begin
{$IFDEF FSAPI_DEBUG}DebugPrint(STR_Error+'fsapi_HTTP -> error 404: clearing stored session ID');{$ENDIF}
fsapi_SessionID:='';
end;
Result:='';
if HTTPClient<>NIL then HTTPClient.Free;
end;
end;
if HTTPClient<>NIL then HTTPClient.Free;
end;
{$ENDIF}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Session
//------------------------------------------------------------------------------
function fsapi_CreateSession(URL: String; PIN: String; const ReUseSessionID: Boolean = false): String;
//Create a session ID
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//optional use last session ID again
if (fsapi_SessionID<>'') and ((fsapi_ReUseSessionID=true) or (ReUseSessionID=true)) then
begin
Result:=fsapi_SessionID;
exit;
end;
//create a new session ID
Buffer:=fsapi_HTTP(URL+'/fsapi/CREATE_SESSION?pin='+PIN);
if Buffer='' then
begin
{$IFDEF FSAPI_DEBUG}DebugPrint(STR_Error+'fsapi_CreateSession -> HTTP request failed');{$ENDIF}
fsapi_SessionID:='';
Result:='';
exit;
end;
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('status');
if Node<>NIL then
begin
if Node.FirstChild.NodeValue='FS_OK' then
begin
Node:=XML.DocumentElement.FindNode('sessionId');
Buffer:=AnsiString(Node.FirstChild.NodeValue);
end
else
begin
Buffer:='';
end;
end
else
begin
Buffer:='';
end;
fsapi_SessionID:=Buffer;
Result:=fsapi_SessionID;
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_CreateSession -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
fsapi_SessionID:='';
Result:='';
end;
end;
end;
function fsapi_DeleteSession(URL: String; PIN: String): Boolean;
//Delete a session ID
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
Buffer:=fsapi_HTTP(URL+'/fsapi/DELETE_SESSION?pin='+PIN);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('status');
if Node<>NIL then
begin
if Node.FirstChild.NodeValue='FS_OK' then
begin
fsapi_SessionID:='';
Result:=true;
end
else
begin
Result:=false;
end;
end
else
begin
Result:=false;
end;
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_DeleteSession -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:=false;
end;
end;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// RAW GET/SET commands
//------------------------------------------------------------------------------
function fsapi_RAW(URL: String; PIN: String; COMMAND: String; const MODE_SET: Boolean = false; const MODE_SET_VALUE: String = ''): String;
//Get or set a raw command
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get or set the raw command via fsapi
if MODE_SET then
Result:=fsapi_HTTP(URL+'/fsapi/SET/'+COMMAND+'?pin='+PIN+'&sid='+fsapi_SessionID+'&value='+StringReplace(MODE_SET_VALUE,' ','%20',[rfReplaceAll,rfIgnoreCase])) //spaces in MODE_SET_VALUE must be replaced with %20 to create a valid URL
else
Result:=fsapi_HTTP(URL+'/fsapi/GET/'+COMMAND+'?pin='+PIN+'&sid='+fsapi_SessionID);
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_RAW -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_RAW_URL(URL: String): String;
//Execute full raw command
begin
try
Result:=fsapi_HTTP(StringReplace(URL,' ','%20',[rfReplaceAll,rfIgnoreCase])); //spaces in MODE_SET_VALUE must be replaced with %20 to create a valid URL
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_RAW_URL -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// LIST_GET_NEXT commands
//------------------------------------------------------------------------------
function fsapi_LIST_GET_NEXT(URL: String; PIN: String; COMMAND: String; const MAXITEMS: Integer = 10): String;
//Execute LIST_GET_NEXT command
begin
try
Result:=fsapi_RAW_URL(URL+'/fsapi/LIST_GET_NEXT/'+StringReplace(COMMAND,' ','%20',[rfReplaceAll,rfIgnoreCase])+'?pin='+PIN+'&maxItems='+IntToStr(MAXITEMS)); //spaces in MODE_SET_VALUE must be replaced with %20 to create a valid URL
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_LIST_GET_NEXT -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Factory reset
//------------------------------------------------------------------------------
function fsapi_FACTORY_RESET(URL: String; PIN: String): Boolean;
//Execute LIST_GET_NEXT command
var
Buffer: String;
begin
try
///fsapi/SET/netRemote.sys.factoryReset?pin=1234&value=1
Buffer:=fsapi_RAW_URL(URL+'/fsapi/SET/netRemote.sys.factoryReset?pin='+PIN+'&value=1');
if Pos('FS_OK',Buffer)>0 then
begin
fsapi_SessionID:='';
Result:=true;
end
else
Result:=false;
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_FACTORY_RESET -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:=false;
end;
end;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Info
//------------------------------------------------------------------------------
function fsapi_Info_ImageVersion(URL: String; PIN: String): String;
//Get Image Version
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get version from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.sys.info.version?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');
if Node<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
end
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_ImageVersion -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_Info_GetNotifies(URL: String; PIN: String): TStringList;
//Get notifies
var
XML: TXMLDocument;
Node: TDOMNode;
Node2: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
Notifies: TStringList;
begin
try
Notifies:=TStringList.Create;
Notifies.Clear;
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:=Notifies;
exit;
end;
//Get notifies from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET_NOTIFIES?pin='+PIN+'&sid='+fsapi_SessionID);
if Buffer='' then
begin
Result:=Notifies;
exit;
end;
if Pos('notify',LowerCase(Buffer))=0 then
begin
Result:=Notifies;
exit;
end;
Buffer2:=TStringStream.Create(Buffer);
Buffer2.Seek(0,0);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('notify');
while Node<>NIL do
begin
Buffer:=String(Node.Attributes.Item[0].NodeValue);
Node2:=Node.FindNode('value');
if Node2<>NIL then
begin
Node2:=Node2.FirstChild;
if Node2.FirstChild<>NIL then
Notifies.Add(Buffer+'|'+String(Node2.FirstChild.NodeValue));
end;
Node:=Node.NextSibling;
if Node=NIL then
break;
end;
Result:=Notifies;
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_GetNotifies -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:=Notifies;
end;
end;
end;
function fsapi_Info_FriendlyName(URL: String; PIN: String): String;
//Get Friendly Name
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get friendly name from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.sys.info.friendlyName?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');
if Node<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
end
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_FriendlyName -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_Info_SetFriendlyName(URL: String; PIN: String; NewName: String): Boolean;
//Set new Friendly Name
var
Buffer: String;
begin
try
//Check new name
if NewName='' then
begin
Result:=false;
exit;
end;
NewName:=StringReplace(NewName,' ','%20',[rfReplaceAll,rfIgnoreCase]); //spaces in NewName must be replaced with %20 to create a valid URL
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:=false;
exit;
end;
//Set new friendly name for device
Buffer:=fsapi_HTTP(URL+'/fsapi/SET/netRemote.sys.info.friendlyName?pin='+PIN+'&sid='+fsapi_SessionID+'&value='+NewName);
if fsapi_CheckStatus(Buffer)='FS_OK' then
Result:=true
else
Result:=false;
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_SetFriendlyName -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:=false;
end;
end;
end;
function fsapi_Info_RadioID(URL: String; PIN: String): String;
//Get Radio ID
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get ID from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.sys.info.radioId?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');
if Node.FirstChild<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
end
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_RadioID -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_Info_Artist(URL: String; PIN: String): String;
//Get Artist
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get artist from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.play.info.artist?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');
if Node.FirstChild<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
end
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_Artist -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_Info_Name(URL: String; PIN: String): String;
//Get name/title
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get name from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.play.info.name?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');
if Node.FirstChild<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
end
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_Name -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_Info_Text(URL: String; PIN: String): String;
//Get text
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get text from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.play.info.text?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');
if Node.FirstChild<>NIL then
Result:=String(Node.FirstChild.NodeValue)
else
Result:='';
end
else
Result:='';
except
on E:Exception do
begin
{$IFDEF FSAPI_DEBUG}E.Message:=STR_Error+'fsapi_Info_Text -> '+E.Message; DebugPrint(E.Message);{$ENDIF}
Result:='';
end;
end;
end;
function fsapi_Info_graphicUri(URL: String; PIN: String): String;
//Get graphic URI
var
XML: TXMLDocument;
Node: TDOMNode;
Buffer: String;
Buffer2: TStringStream;
begin
try
//Get session ID
fsapi_SessionID:=fsapi_CreateSession(URL, PIN);
if fsapi_SessionID='' then
begin
Result:='';
exit;
end;
//Get graphic URI from device
Buffer:=fsapi_HTTP(URL+'/fsapi/GET/netRemote.play.info.graphicUri?pin='+PIN+'&sid='+fsapi_SessionID);
Buffer2:=TStringStream.Create(Buffer);
ReadXMLFile(XML, Buffer2);
Node:=XML.DocumentElement.FindNode('value');
if Node<>NIL then
begin
Node:=Node.FindNode('c8_array');