File size: 6,074 Bytes
343e5a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import flash.text.*;
    import com.adobe.images.PNGEncoder;
    import com.adobe.crypto.MD5;
    import com.sociodox.utils.Base64;
    import flash.filesystem.File;
    import flash.filesystem.FileStream;
    import flash.filesystem.FileMode;
    
    public class FullScreenImageLoader extends Sprite {
        private var apiUrl:String;
        private var imageWidth:int;
        private var imageHeight:int;
        private var imageX:int;
        private var imageY:int;
        private var refreshInterval:int = 2000; // Refresh every 2 seconds
        private var timer:Timer;
        private var bitmap:Bitmap;
        private var currentChecksum:String = ""; // Store the checksum of the currently displayed image
        private var errorMessage:TextField;

        public function FullScreenImageLoader() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.displayState = StageDisplayState.FULL_SCREEN;

            // Load configuration from JSON file
            loadConfig();

            bitmap = new Bitmap();
            addChild(bitmap);

            // Set up the timer to refresh the image
            timer = new Timer(refreshInterval);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();

            // Create an error message text field for display
            errorMessage = new TextField();
            errorMessage.defaultTextFormat = new TextFormat("Arial", 20, 0xFF0000);
            errorMessage.width = stage.stageWidth;
            errorMessage.height = 50;
            errorMessage.y = stage.stageHeight - 50;
            errorMessage.selectable = false;
            addChild(errorMessage);

            // Initial load
            loadImage();
        }

        private function loadConfig():void {
            var configFile:File = File.applicationDirectory.resolvePath("config.json");
            if (configFile.exists) {
                var fileStream:FileStream = new FileStream();
                fileStream.open(configFile, FileMode.READ);
                var configData:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
                fileStream.close();

                var config:Object = JSON.parse(configData);
                apiUrl = config.api_url;
                imageWidth = config.size.width;
                imageHeight = config.size.height;
                imageX = config.position.x;
                imageY = config.position.y;

                trace("Configuration loaded: ", apiUrl, imageWidth, imageHeight, imageX, imageY);
            } else {
                trace("Config file not found: config.json");
                errorMessage.text = "Config file not found: config.json";
            }
        }

        private function onTimer(event:TimerEvent):void {
            loadImage();
        }

        private function loadImage():void {
            var request:URLRequest = new URLRequest(apiUrl + "/get_status");
            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            loader.addEventListener(Event.COMPLETE, onImageLoadComplete);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            loader.load(request);
        }

        private function onImageLoadComplete(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            try {
                var response:Object = JSON.parse(loader.data);

                if (response.hasOwnProperty("checksum")) {
                    var newChecksum:String = response.checksum;

                    // Compare checksums, if different, update the image
                    if (newChecksum != currentChecksum) {
                        currentChecksum = newChecksum;

                        if (response.hasOwnProperty("image_base64") && response.image_base64 != "") {
                            displayNewImage(response.image_base64);
                        } else {
                            trace("No image data found.");
                            errorMessage.text = "No image data found.";
                        }
                    }
                } else {
                    trace("Invalid response from server.");
                    errorMessage.text = "Invalid response from server.";
                }
            } catch (error:Error) {
                trace("Error parsing response: " + error.message);
                errorMessage.text = "Error parsing response: " + error.message;
            }
        }

        private function displayNewImage(base64Image:String):void {
            try {
                var byteArray:ByteArray = Base64.decode(base64Image);
                var loader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageDecoded);
                loader.loadBytes(byteArray);
            } catch (error:Error) {
                trace("Error decoding image: " + error.message);
                errorMessage.text = "Error decoding image: " + error.message;
            }
        }

         private function onImageDecoded(event:Event):void {
            var loader:Loader = Loader(event.target.loader);
            var bitmapData:BitmapData = Bitmap(loader.content).bitmapData;

            // Update the display
            bitmap.bitmapData = bitmapData;
            bitmap.width = imageWidth;
            bitmap.height = imageHeight;
            bitmap.x = imageX;
            bitmap.y = imageY;
            bitmap.smoothing = true;
        }

        private function onIOError(event:IOErrorEvent):void {
            trace("Error loading image: " + event.text);
            errorMessage.text = "Error loading image: " + event.text;
        }
    }
}